32 lines
1.4 KiB
PHP
32 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Actions\Projects;
|
|
|
|
use App\Models\ProjectTemplate;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class DeleteProjectTemplate
|
|
{
|
|
public function __construct(
|
|
private readonly CreateProjectBaseTemplate $createProjectBaseTemplate,
|
|
private readonly DuplicateProjectConfiguration $duplicateProjectConfiguration,
|
|
) {}
|
|
|
|
public function handle(ProjectTemplate $template): void
|
|
{
|
|
DB::transaction(function () use ($template): void {
|
|
$template = ProjectTemplate::query()->whereKey($template->id)->lockForUpdate()->firstOrFail();
|
|
$projects = $template->linkedProjects()->orderBy('id')->lockForUpdate()->get();
|
|
$template->delete();
|
|
foreach ($projects as $project) {
|
|
$base = $project->baseTemplate()->first() ?? $this->createProjectBaseTemplate->handle($project);
|
|
$base->forceFill(['configuration_snapshot' => $this->duplicateProjectConfiguration->export($project, DuplicateProjectConfiguration::copyableAreas())])->saveQuietly();
|
|
$project->forceFill(['project_template_id' => $base->id, 'template_overrides' => []])->saveQuietly();
|
|
foreach ($project->configurationRelations() as $relation) {
|
|
$project->{$relation}()->update(['project_template_id' => $base->id]);
|
|
}
|
|
}
|
|
}, 3);
|
|
}
|
|
}
|