fix(KAN-14): preserve template inheritance and project data
This commit is contained in:
@@ -5,11 +5,44 @@ namespace App\Models\Concerns;
|
||||
use App\Models\Project;
|
||||
use App\Models\ProjectTemplate;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
trait SyncsProjectTemplateOwnership
|
||||
{
|
||||
/** @param array<string, mixed> $options */
|
||||
public function save(array $options = []): bool
|
||||
{
|
||||
return DB::transaction(function () use ($options): bool {
|
||||
$this->syncProjectTemplateOwnership();
|
||||
if ($this->getAttribute('project_id') !== null) {
|
||||
$project = Project::query()->whereKey($this->getAttribute('project_id'))->lockForUpdate()->first();
|
||||
if ($project?->project_template_id !== null) {
|
||||
$this->setAttribute('project_template_id', $project->project_template_id);
|
||||
}
|
||||
}
|
||||
|
||||
return parent::save($options);
|
||||
}, 3);
|
||||
}
|
||||
|
||||
public function delete(): ?bool
|
||||
{
|
||||
return DB::transaction(function (): ?bool {
|
||||
if ($this->getAttribute('project_id') !== null) {
|
||||
Project::query()->whereKey($this->getAttribute('project_id'))->lockForUpdate()->first();
|
||||
}
|
||||
|
||||
return parent::delete();
|
||||
}, 3);
|
||||
}
|
||||
|
||||
protected function syncProjectTemplateOwnership(): void
|
||||
{
|
||||
if ($this->getAttribute('configuration_key') === null) {
|
||||
$this->setAttribute('configuration_key', (string) Str::uuid());
|
||||
}
|
||||
|
||||
$projectTemplateId = $this->getAttribute('project_template_id');
|
||||
$projectId = $this->getAttribute('project_id');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user