whereKey($template->id)->lockForUpdate()->firstOrFail(); $project = Project::query()->whereKey($project->id)->lockForUpdate()->firstOrFail(); $configuration = $this->duplicateProjectConfiguration->normalizeConfiguration( $template->configuration_snapshot ?? [], ); $localAreas = array_values(array_diff( DuplicateProjectConfiguration::copyableAreas(), array_keys($configuration), )); $project->forceFill([ 'project_template_id' => $template->id, 'template_overrides' => $localAreas, ])->saveQuietly(); $blockedAreas = $this->duplicateProjectConfiguration->apply($project, $configuration); $this->projectTemplateOverrideManager->clear( $project->fresh(), array_values(array_diff(array_keys($configuration), $blockedAreas)), ); }); } /** * @param list $areas * @param array $statusReplacements */ public function reset(Project $project, array $areas, array $statusReplacements = []): array { $template = $project->projectTemplate; if ($template === null) { return []; } return DB::transaction(function () use ($project, $template, $areas, $statusReplacements): array { $template = ProjectTemplate::query()->whereKey($template->id)->lockForUpdate()->firstOrFail(); $project = Project::query()->whereKey($project->id)->lockForUpdate()->firstOrFail(); if ($project->project_template_id !== $template->id) { throw ValidationException::withMessages(['areas' => 'The linked template changed. Reload this project before resetting.']); } $baseline = $this->duplicateProjectConfiguration->normalizeConfiguration($template->configuration_snapshot ?? []); $configuration = collect($baseline)->only($areas)->all(); if (in_array('ticket_statuses', $areas, true) && ! in_array('status_transitions', $project->template_overrides ?? [], true) && isset($baseline['status_transitions'])) { $configuration['status_transitions'] = $baseline['status_transitions']; } $blockedAreas = $this->duplicateProjectConfiguration->apply( $project, $configuration, $statusReplacements, ); $this->projectTemplateOverrideManager->clear( $project->fresh(), array_values(array_diff(array_keys($configuration), $blockedAreas)), ); return $blockedAreas; }, 3); } }