fix(KAN-14): preserve template inheritance and project data
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Actions\ProjectBoard;
|
||||
|
||||
use App\Actions\Projects\RefreshProjectBaseTemplate;
|
||||
use App\Models\Project;
|
||||
use App\Models\TicketStatus;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
@@ -23,6 +24,7 @@ trait ManagesTicketStatusPositions
|
||||
'position' => $index + 1,
|
||||
])->saveQuietly();
|
||||
});
|
||||
app(RefreshProjectBaseTemplate::class)->handle($project, 'ticket_statuses');
|
||||
}
|
||||
|
||||
private function statusContextLabel(TicketStatus $ticketStatus): string
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Actions\ProjectBoard;
|
||||
|
||||
use App\Actions\Projects\RefreshProjectBaseTemplate;
|
||||
use App\Models\BoardColumn;
|
||||
use App\Models\Project;
|
||||
use Illuminate\Support\Collection;
|
||||
@@ -50,5 +51,9 @@ trait ReordersBoardColumns
|
||||
'position' => $index + 1,
|
||||
])->saveQuietly();
|
||||
});
|
||||
$project = $columns->first()?->project;
|
||||
if ($project !== null) {
|
||||
app(RefreshProjectBaseTemplate::class)->handle($project, 'board_columns');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,8 +5,10 @@ namespace App\Actions\ProjectBoard;
|
||||
use App\Actions\Projects\DuplicateProjectConfiguration;
|
||||
use App\Actions\Projects\ProjectTemplateOverrideManager;
|
||||
use App\Actions\Projects\RefreshProjectBaseTemplate;
|
||||
use App\Models\Project;
|
||||
use App\Models\TicketStatus;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class UpdateTicketStatusTransitions
|
||||
{
|
||||
@@ -20,22 +22,25 @@ class UpdateTicketStatusTransitions
|
||||
*/
|
||||
public function handle(TicketStatus $ticketStatus, array $validated): TicketStatus
|
||||
{
|
||||
$allowedTicketStatusIds = collect(Arr::get($validated, 'allowed_ticket_status_ids', []))
|
||||
->map(fn (int|string $ticketStatusId): int => (int) $ticketStatusId)
|
||||
->unique()
|
||||
->values()
|
||||
->all();
|
||||
return DB::transaction(function () use ($ticketStatus, $validated): TicketStatus {
|
||||
Project::query()->whereKey($ticketStatus->project_id)->lockForUpdate()->firstOrFail();
|
||||
$allowedTicketStatusIds = collect(Arr::get($validated, 'allowed_ticket_status_ids', []))
|
||||
->map(fn (int|string $ticketStatusId): int => (int) $ticketStatusId)
|
||||
->unique()
|
||||
->values()
|
||||
->all();
|
||||
|
||||
$ticketStatus->allowedTargetStatuses()->sync($allowedTicketStatusIds);
|
||||
$this->projectTemplateOverrideManager->mark(
|
||||
$ticketStatus->project,
|
||||
DuplicateProjectConfiguration::COPY_STATUS_TRANSITIONS,
|
||||
);
|
||||
$this->refreshProjectBaseTemplate->handle(
|
||||
$ticketStatus->project,
|
||||
DuplicateProjectConfiguration::COPY_STATUS_TRANSITIONS,
|
||||
);
|
||||
$ticketStatus->allowedTargetStatuses()->sync($allowedTicketStatusIds);
|
||||
$this->projectTemplateOverrideManager->mark(
|
||||
$ticketStatus->project,
|
||||
DuplicateProjectConfiguration::COPY_STATUS_TRANSITIONS,
|
||||
);
|
||||
$this->refreshProjectBaseTemplate->handle(
|
||||
$ticketStatus->project,
|
||||
DuplicateProjectConfiguration::COPY_STATUS_TRANSITIONS,
|
||||
);
|
||||
|
||||
return $ticketStatus->fresh();
|
||||
return $ticketStatus->fresh();
|
||||
}, 3);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ use App\Actions\Projects\ProjectTemplateOverrideManager;
|
||||
use App\Actions\Projects\RefreshProjectBaseTemplate;
|
||||
use App\Models\Project;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class UpdateProjectTicketMetadataSettings
|
||||
{
|
||||
@@ -20,19 +21,22 @@ class UpdateProjectTicketMetadataSettings
|
||||
*/
|
||||
public function handle(Project $project, array $validated): Project
|
||||
{
|
||||
$project->update([
|
||||
'show_author' => Arr::get($validated, 'show_author', false),
|
||||
'show_story_points' => Arr::get($validated, 'show_story_points', false),
|
||||
]);
|
||||
$this->projectTemplateOverrideManager->mark(
|
||||
$project,
|
||||
DuplicateProjectConfiguration::COPY_PROJECT_SETTINGS,
|
||||
);
|
||||
$this->refreshProjectBaseTemplate->handle(
|
||||
$project,
|
||||
DuplicateProjectConfiguration::COPY_PROJECT_SETTINGS,
|
||||
);
|
||||
return DB::transaction(function () use ($project, $validated): Project {
|
||||
Project::query()->whereKey($project->id)->lockForUpdate()->firstOrFail();
|
||||
$project->update([
|
||||
'show_author' => Arr::get($validated, 'show_author', false),
|
||||
'show_story_points' => Arr::get($validated, 'show_story_points', false),
|
||||
]);
|
||||
$this->projectTemplateOverrideManager->mark(
|
||||
$project,
|
||||
DuplicateProjectConfiguration::COPY_PROJECT_SETTINGS,
|
||||
);
|
||||
$this->refreshProjectBaseTemplate->handle(
|
||||
$project,
|
||||
DuplicateProjectConfiguration::COPY_PROJECT_SETTINGS,
|
||||
);
|
||||
|
||||
return $project->fresh();
|
||||
return $project->fresh();
|
||||
}, 3);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,6 +36,7 @@ class CreateProject
|
||||
if ($creationMode === 'template' && $sourceTemplateId !== null) {
|
||||
$sourceTemplate = ProjectTemplate::query()
|
||||
->published()
|
||||
->lockForUpdate()
|
||||
->findOrFail((int) $sourceTemplateId);
|
||||
|
||||
$project->forceFill([
|
||||
@@ -72,6 +73,7 @@ class CreateProject
|
||||
$this->duplicateProjectConfiguration->apply(
|
||||
$project->fresh() ?? $project,
|
||||
$sourceTemplate->configuration_snapshot ?? [],
|
||||
adoptExisting: true,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -15,11 +15,18 @@ class CreateProjectBaseTemplate
|
||||
{
|
||||
$copyAreas = DuplicateProjectConfiguration::copyableAreas();
|
||||
|
||||
$slug = strtolower($project->key).'-base-template';
|
||||
$suffix = 1;
|
||||
$candidate = $slug;
|
||||
while (ProjectTemplate::query()->where('slug', $candidate)->exists()) {
|
||||
$candidate = $slug.'-'.++$suffix;
|
||||
}
|
||||
|
||||
return ProjectTemplate::query()->create([
|
||||
'project_id' => $project->id,
|
||||
'source_project_id' => $project->id,
|
||||
'name' => "{$project->name} base template",
|
||||
'slug' => strtolower($project->key).'-base-template',
|
||||
'slug' => $candidate,
|
||||
'is_published' => false,
|
||||
'copy_areas' => $copyAreas,
|
||||
'configuration_snapshot' => $this->duplicateProjectConfiguration->export($project, $copyAreas),
|
||||
|
||||
@@ -5,28 +5,61 @@ namespace App\Actions\Projects;
|
||||
use App\Models\Project;
|
||||
use App\Models\ProjectTemplate;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class CreateProjectTemplate
|
||||
{
|
||||
public function __construct(
|
||||
private readonly DuplicateProjectConfiguration $duplicateProjectConfiguration,
|
||||
private readonly ProjectConfigurationIdentity $projectConfigurationIdentity,
|
||||
) {}
|
||||
|
||||
/** @param array<string, mixed> $attributes */
|
||||
public function empty(array $attributes): ProjectTemplate
|
||||
{
|
||||
$areas = $this->duplicateProjectConfiguration->normalizeCopyAreas(Arr::get($attributes, 'copy_areas', []));
|
||||
|
||||
return ProjectTemplate::query()->create([
|
||||
'name' => Arr::get($attributes, 'name'),
|
||||
'slug' => Arr::get($attributes, 'slug'),
|
||||
'is_published' => true,
|
||||
'copy_areas' => $areas,
|
||||
'configuration_snapshot' => collect($this->duplicateProjectConfiguration->normalizeConfiguration(
|
||||
Arr::get($attributes, 'configuration_snapshot', $this->projectConfigurationIdentity->defaults()),
|
||||
))->only($areas)->all(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array{name: string, slug: string, copy_areas?: array<int, string>} $attributes
|
||||
*/
|
||||
public function handle(Project $sourceProject, array $attributes): ProjectTemplate
|
||||
{
|
||||
$copyAreas = array_values(Arr::get($attributes, 'copy_areas', []));
|
||||
return DB::transaction(function () use ($sourceProject, $attributes): ProjectTemplate {
|
||||
$sourceProject = Project::query()->whereKey($sourceProject->id)->lockForUpdate()->firstOrFail();
|
||||
$copyAreas = $this->duplicateProjectConfiguration->normalizeCopyAreas(Arr::get($attributes, 'copy_areas', []));
|
||||
$template = $sourceProject->projectTemplate;
|
||||
if ($template === null || $template->is_published || $template->project_id !== $sourceProject->id) {
|
||||
$template = new ProjectTemplate;
|
||||
$template->slug = Arr::get($attributes, 'slug');
|
||||
}
|
||||
$template->forceFill([
|
||||
'project_id' => null,
|
||||
'source_project_id' => $sourceProject->id,
|
||||
'name' => Arr::get($attributes, 'name'),
|
||||
'is_published' => true,
|
||||
'copy_areas' => $copyAreas,
|
||||
'configuration_snapshot' => $this->duplicateProjectConfiguration->export($sourceProject, $copyAreas),
|
||||
])->save();
|
||||
$sourceProject->forceFill([
|
||||
'project_template_id' => $template->id,
|
||||
'template_overrides' => array_values(array_diff(DuplicateProjectConfiguration::copyableAreas(), $copyAreas)),
|
||||
])->saveQuietly();
|
||||
foreach ($sourceProject->configurationRelations() as $relation) {
|
||||
$sourceProject->{$relation}()->update(['project_template_id' => $template->id]);
|
||||
}
|
||||
|
||||
return ProjectTemplate::query()->create([
|
||||
'project_id' => null,
|
||||
'source_project_id' => $sourceProject->id,
|
||||
'name' => $attributes['name'],
|
||||
'slug' => $attributes['slug'],
|
||||
'is_published' => true,
|
||||
'copy_areas' => $this->duplicateProjectConfiguration->normalizeCopyAreas($copyAreas),
|
||||
'configuration_snapshot' => $this->duplicateProjectConfiguration->export($sourceProject, $copyAreas),
|
||||
]);
|
||||
return $template;
|
||||
}, 3);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
@@ -2,9 +2,13 @@
|
||||
|
||||
namespace App\Actions\Projects;
|
||||
|
||||
use App\Actions\Notifications\NormalizeNotificationFilters;
|
||||
use App\Models\Project;
|
||||
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
|
||||
use App\Models\TicketStatus;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class DuplicateProjectConfiguration
|
||||
{
|
||||
@@ -32,6 +36,7 @@ class DuplicateProjectConfiguration
|
||||
|
||||
public function __construct(
|
||||
private readonly ProjectTemplateOverrideManager $projectTemplateOverrideManager,
|
||||
private readonly ProjectConfigurationIdentity $projectConfigurationIdentity,
|
||||
) {}
|
||||
|
||||
/** @return list<string> */
|
||||
@@ -108,7 +113,7 @@ class DuplicateProjectConfiguration
|
||||
unset($configuration[self::COPY_BOARD_WORKFLOW]);
|
||||
}
|
||||
|
||||
return $configuration;
|
||||
return $this->projectConfigurationIdentity->normalize($configuration);
|
||||
}
|
||||
|
||||
/** @param list<string> $copyAreas @return array<string, mixed> */
|
||||
@@ -133,23 +138,24 @@ class DuplicateProjectConfiguration
|
||||
];
|
||||
}
|
||||
|
||||
$columnIndexes = $sourceProject->boardColumns->values()->mapWithKeys(
|
||||
static fn ($column, int $index): array => [$column->id => $index],
|
||||
$columnKeys = $sourceProject->boardColumns->values()->mapWithKeys(
|
||||
static fn ($column): array => [$column->id => $column->configuration_key],
|
||||
);
|
||||
$statusIndexes = $sourceProject->ticketStatuses->values()->mapWithKeys(
|
||||
static fn ($status, int $index): array => [$status->id => $index],
|
||||
$statusKeys = $sourceProject->ticketStatuses->values()->mapWithKeys(
|
||||
static fn ($status): array => [$status->id => $status->configuration_key],
|
||||
);
|
||||
|
||||
if (in_array(self::COPY_BOARD_COLUMNS, $selectedAreas, true)) {
|
||||
$configuration[self::COPY_BOARD_COLUMNS] = $sourceProject->boardColumns->values()->map(
|
||||
static fn ($column): array => ['name' => $column->name, 'position' => $column->position],
|
||||
static fn ($column): array => ['configuration_key' => $column->configuration_key, 'name' => $column->name, 'position' => $column->position],
|
||||
)->all();
|
||||
}
|
||||
|
||||
if (in_array(self::COPY_TICKET_STATUSES, $selectedAreas, true)) {
|
||||
$configuration[self::COPY_TICKET_STATUSES] = $sourceProject->ticketStatuses->values()->map(
|
||||
static fn ($status): array => [
|
||||
'board_column_index' => $status->board_column_id === null ? null : $columnIndexes->get($status->board_column_id),
|
||||
'configuration_key' => $status->configuration_key,
|
||||
'board_column_key' => $status->board_column_id === null ? null : $columnKeys->get($status->board_column_id),
|
||||
'name' => $status->name,
|
||||
'position' => $status->position,
|
||||
],
|
||||
@@ -158,9 +164,12 @@ class DuplicateProjectConfiguration
|
||||
|
||||
if (in_array(self::COPY_STATUS_TRANSITIONS, $selectedAreas, true)) {
|
||||
$configuration[self::COPY_STATUS_TRANSITIONS] = $sourceProject->ticketStatuses->values()->map(
|
||||
static fn ($status): array => $status->allowedTargetStatuses->pluck('id')->map(
|
||||
static fn (int $id): ?int => $statusIndexes->get($id),
|
||||
)->filter(static fn (?int $index): bool => $index !== null)->values()->all(),
|
||||
static fn ($status): array => [
|
||||
'status_key' => $status->configuration_key,
|
||||
'target_keys' => $status->allowedTargetStatuses->pluck('id')->map(
|
||||
static fn (int $id): ?string => $statusKeys->get($id),
|
||||
)->filter()->sort()->values()->all(),
|
||||
],
|
||||
)->all();
|
||||
}
|
||||
|
||||
@@ -179,7 +188,7 @@ class DuplicateProjectConfiguration
|
||||
'name' => $item->name, 'enabled' => $item->enabled, 'event_type' => $item->event_type->value,
|
||||
'channel' => $item->channel->value, 'channel_configuration' => $item->channel_configuration ?? [],
|
||||
'message_template' => $item->message_template, 'delivery_url' => $item->delivery_url,
|
||||
'filters' => $item->filters ?? [],
|
||||
'filters' => $this->mapNotificationFilters($item->filters ?? [], $statusKeys->all()),
|
||||
],
|
||||
];
|
||||
$relations = [
|
||||
@@ -190,7 +199,9 @@ class DuplicateProjectConfiguration
|
||||
|
||||
foreach ($simpleAreas as $area => $mapper) {
|
||||
if (in_array($area, $selectedAreas, true)) {
|
||||
$configuration[$area] = $sourceProject->{$relations[$area]}->values()->map($mapper)->all();
|
||||
$configuration[$area] = $sourceProject->{$relations[$area]}->values()->map(
|
||||
static fn ($item): array => ['configuration_key' => $item->configuration_key, ...$mapper($item)],
|
||||
)->all();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -200,95 +211,124 @@ class DuplicateProjectConfiguration
|
||||
/**
|
||||
* @param array<string, mixed> $configuration
|
||||
* @param array<int|string, int|string> $statusReplacements
|
||||
* @return list<string> areas which retained referenced local values
|
||||
* @return list<string> Areas which retain referenced project configuration.
|
||||
*/
|
||||
public function apply(Project $targetProject, array $configuration, array $statusReplacements = []): array
|
||||
public function apply(Project $targetProject, array $configuration, array $statusReplacements = [], bool $adoptExisting = false): array
|
||||
{
|
||||
return DB::transaction(function () use ($targetProject, $configuration, $statusReplacements, $adoptExisting): array {
|
||||
$targetProject = Project::query()->whereKey($targetProject->id)->lockForUpdate()->firstOrFail();
|
||||
|
||||
return $this->applyLocked($targetProject, $configuration, $statusReplacements, $adoptExisting);
|
||||
}, 3);
|
||||
}
|
||||
|
||||
/** @param array<string, mixed> $configuration @param array<int|string, int|string> $statusReplacements @return list<string> */
|
||||
private function applyLocked(Project $targetProject, array $configuration, array $statusReplacements, bool $adoptExisting): array
|
||||
{
|
||||
$configuration = $this->normalizeConfiguration($configuration);
|
||||
|
||||
$blockedAreas = $this->projectTemplateOverrideManager->withoutRecording(function () use ($targetProject, $configuration, $statusReplacements): array {
|
||||
$blockedAreas = $this->projectTemplateOverrideManager->withoutRecording(function () use ($targetProject, $configuration, $statusReplacements, $adoptExisting): array {
|
||||
$blockedAreas = [];
|
||||
|
||||
if (isset($configuration[self::COPY_PROJECT_SETTINGS])) {
|
||||
$targetProject->forceFill($configuration[self::COPY_PROJECT_SETTINGS])->saveQuietly();
|
||||
}
|
||||
|
||||
if (isset($configuration[self::COPY_BOARD_COLUMNS])) {
|
||||
$blocked = $this->syncModels(
|
||||
$targetProject->boardColumns()->get(),
|
||||
$configuration[self::COPY_BOARD_COLUMNS],
|
||||
fn (array $item) => $targetProject->boardColumns()->create($item),
|
||||
static fn (Model $model, array $item) => $model->forceFill($item)->saveQuietly(),
|
||||
static fn (Model $column): bool => ! $column->ticketStatuses()->exists(),
|
||||
$currentStatuses = $targetProject->ticketStatuses()->withCount('tickets')->get();
|
||||
$nextStatuses = collect($configuration[self::COPY_TICKET_STATUSES] ?? [])->keyBy('configuration_key');
|
||||
$columnKeys = array_column($configuration[self::COPY_BOARD_COLUMNS], 'configuration_key');
|
||||
$statusesCanChange = isset($configuration[self::COPY_TICKET_STATUSES]) && $currentStatuses->every(
|
||||
static fn ($status): bool => $nextStatuses->has($status->configuration_key)
|
||||
|| $status->tickets_count === 0 || array_key_exists($status->id, $statusReplacements),
|
||||
);
|
||||
if ($blocked) {
|
||||
$canDeleteColumn = static fn (Model $column): bool => $currentStatuses->where('board_column_id', $column->id)->every(
|
||||
static function ($status) use ($column, $statusesCanChange, $nextStatuses, $columnKeys): bool {
|
||||
if (! $statusesCanChange) {
|
||||
return false;
|
||||
}
|
||||
$next = $nextStatuses->get($status->configuration_key);
|
||||
$nextColumn = $next['board_column_key'] ?? null;
|
||||
|
||||
return $next === null || ($nextColumn !== $column->configuration_key
|
||||
&& ($nextColumn === null || in_array($nextColumn, $columnKeys, true)));
|
||||
},
|
||||
);
|
||||
if ($this->syncModels($targetProject, self::COPY_BOARD_COLUMNS, $configuration[self::COPY_BOARD_COLUMNS],
|
||||
$canDeleteColumn, $adoptExisting)) {
|
||||
$blockedAreas[] = self::COPY_BOARD_COLUMNS;
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($configuration[self::COPY_TICKET_STATUSES])) {
|
||||
$columns = $targetProject->boardColumns()->get()->values();
|
||||
$items = collect($configuration[self::COPY_TICKET_STATUSES])->map(static function (array $item) use ($columns): array {
|
||||
$columnIndex = $item['board_column_index'] ?? null;
|
||||
$item['board_column_id'] = $columnIndex === null ? null : $columns->get($columnIndex)?->id;
|
||||
unset($item['board_column_index']);
|
||||
$columns = $targetProject->boardColumns()->get()->keyBy('configuration_key');
|
||||
$existingStatuses = $targetProject->ticketStatuses()->get()->keyBy('configuration_key');
|
||||
$items = collect($configuration[self::COPY_TICKET_STATUSES])->map(static function (array $item) use ($columns, $existingStatuses): array {
|
||||
$columnKey = $item['board_column_key'] ?? null;
|
||||
$item['board_column_id'] = $columnKey === null
|
||||
? null
|
||||
: ($columns->get($columnKey)?->id ?? $existingStatuses->get($item['configuration_key'])?->board_column_id);
|
||||
$item['slug'] = Str::slug($item['name']);
|
||||
unset($item['board_column_key']);
|
||||
|
||||
return $item;
|
||||
})->all();
|
||||
$blocked = $this->syncModels(
|
||||
$targetProject->ticketStatuses()->get(),
|
||||
$items,
|
||||
fn (array $item) => $targetProject->ticketStatuses()->create($item),
|
||||
static fn (Model $model, array $item) => $model->forceFill($item)->saveQuietly(),
|
||||
function (Model $status) use ($statusReplacements): bool {
|
||||
if (! $status->tickets()->exists()) {
|
||||
return true;
|
||||
}
|
||||
$replacementId = $statusReplacements[$status->id] ?? null;
|
||||
if ($replacementId === null) {
|
||||
return false;
|
||||
}
|
||||
$status->tickets()->update(['ticket_status_id' => $replacementId]);
|
||||
|
||||
return true;
|
||||
},
|
||||
);
|
||||
if ($blocked) {
|
||||
$templateKeys = array_column($items, 'configuration_key');
|
||||
$canDelete = static fn (Model $status): bool => ! $status->tickets()->exists()
|
||||
|| array_key_exists($status->id, $statusReplacements);
|
||||
$beforeDelete = function (Model $status) use ($statusReplacements, $targetProject, $templateKeys): void {
|
||||
$replacement = $statusReplacements[$status->id] ?? null;
|
||||
if ($replacement === null) {
|
||||
return;
|
||||
}
|
||||
$replacementStatus = $targetProject->ticketStatuses()->whereIn('configuration_key', $templateKeys)
|
||||
->where('configuration_key', (string) $replacement)->first();
|
||||
if ($replacementStatus === null && is_numeric($replacement)) {
|
||||
$replacementStatus = $targetProject->ticketStatuses()->whereIn('configuration_key', $templateKeys)->find($replacement);
|
||||
}
|
||||
if ($replacementStatus === null) {
|
||||
throw ValidationException::withMessages([
|
||||
'status_replacements.'.$status->id => 'Choose a replacement from the current template.',
|
||||
]);
|
||||
}
|
||||
$status->tickets()->update(['ticket_status_id' => $replacementStatus->id]);
|
||||
$this->transferStatusTransitions($targetProject, $status, $replacementStatus, $statusReplacements, $templateKeys);
|
||||
};
|
||||
if ($this->syncModels($targetProject, self::COPY_TICKET_STATUSES, $items, $canDelete, $adoptExisting, $beforeDelete)) {
|
||||
$blockedAreas[] = self::COPY_TICKET_STATUSES;
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($configuration[self::COPY_STATUS_TRANSITIONS])) {
|
||||
$statuses = $targetProject->ticketStatuses()->get()->values();
|
||||
foreach ($configuration[self::COPY_STATUS_TRANSITIONS] as $index => $targets) {
|
||||
$status = $statuses->get($index);
|
||||
$statuses = $targetProject->ticketStatuses()->get()->keyBy('configuration_key');
|
||||
foreach ($configuration[self::COPY_STATUS_TRANSITIONS] as $transition) {
|
||||
$status = $statuses->get($transition['status_key']);
|
||||
if ($status !== null) {
|
||||
$status->allowedTargetStatuses()->sync(collect($targets)->map(fn (int $target): ?int => $statuses->get($target)?->id)->filter()->all());
|
||||
$status->allowedTargetStatuses()->sync(collect($transition['target_keys'])->map(
|
||||
static fn (string $key): ?int => $statuses->get($key)?->id,
|
||||
)->filter()->all());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$relations = [
|
||||
self::COPY_TICKET_TYPES => ['ticketTypes', fn (Model $item): bool => ! $item->tickets()->exists()],
|
||||
self::COPY_TICKET_PRIORITIES => ['ticketPriorities', fn (Model $item): bool => ! $item->tickets()->exists()],
|
||||
self::COPY_TICKET_FIELDS => ['ticketFieldDefinitions', fn (Model $item): bool => ! $item->ticketFieldValues()->exists()],
|
||||
self::COPY_TICKET_LINK_TYPES => ['ticketLinkTypes', fn (Model $item): bool => ! $item->ticketLinks()->exists()],
|
||||
self::COPY_REPOSITORY_CONNECTIONS => ['repositoryConnections', null],
|
||||
self::COPY_NOTIFICATION_ACTIONS => ['notificationActions', null],
|
||||
$guards = [
|
||||
self::COPY_TICKET_TYPES => static fn (Model $item): bool => ! $item->tickets()->exists(),
|
||||
self::COPY_TICKET_PRIORITIES => static fn (Model $item): bool => ! $item->tickets()->exists(),
|
||||
self::COPY_TICKET_FIELDS => static fn (Model $item): bool => ! $item->ticketFieldValues()->exists(),
|
||||
self::COPY_TICKET_LINK_TYPES => static fn (Model $item): bool => ! $item->ticketLinks()->exists(),
|
||||
self::COPY_REPOSITORY_CONNECTIONS => null,
|
||||
self::COPY_NOTIFICATION_ACTIONS => null,
|
||||
];
|
||||
|
||||
foreach ($relations as $area => [$relation, $canDelete]) {
|
||||
foreach ($guards as $area => $canDelete) {
|
||||
if (! isset($configuration[$area])) {
|
||||
continue;
|
||||
}
|
||||
$blocked = $this->syncModels(
|
||||
$targetProject->{$relation}()->get(),
|
||||
$configuration[$area],
|
||||
fn (array $item) => $targetProject->{$relation}()->create($item),
|
||||
static fn (Model $model, array $item) => $model->forceFill($item)->saveQuietly(),
|
||||
$canDelete,
|
||||
);
|
||||
if ($blocked) {
|
||||
$items = $configuration[$area];
|
||||
if ($area === self::COPY_NOTIFICATION_ACTIONS) {
|
||||
$statusIds = $targetProject->ticketStatuses()->pluck('id', 'configuration_key')->all();
|
||||
$items = array_map(fn (array $item): array => [
|
||||
...$item, 'filters' => $this->mapNotificationFilters($item['filters'] ?? [], $statusIds),
|
||||
], $items);
|
||||
}
|
||||
if ($this->syncModels($targetProject, $area, $items, $canDelete, $adoptExisting)) {
|
||||
$blockedAreas[] = $area;
|
||||
}
|
||||
}
|
||||
@@ -297,9 +337,8 @@ class DuplicateProjectConfiguration
|
||||
$targetProject->{$relation}()->update(['project_template_id' => $targetProject->project_template_id]);
|
||||
}
|
||||
|
||||
return array_values(array_unique($blockedAreas));
|
||||
return $blockedAreas;
|
||||
});
|
||||
|
||||
foreach ($blockedAreas as $area) {
|
||||
$this->projectTemplateOverrideManager->mark($targetProject->fresh(), $area);
|
||||
}
|
||||
@@ -310,30 +349,113 @@ class DuplicateProjectConfiguration
|
||||
/** @param list<string> $copyAreas */
|
||||
public function handle(Project $sourceProject, Project $targetProject, array $copyAreas): void
|
||||
{
|
||||
$this->apply($targetProject, $this->export($sourceProject, $copyAreas));
|
||||
$this->apply($targetProject, $this->export($sourceProject, $copyAreas), adoptExisting: true);
|
||||
}
|
||||
|
||||
/** @param list<array<string, mixed>> $filters @param array<int|string, int|string> $mapping @return list<array<string, mixed>> */
|
||||
public function mapNotificationFilters(array $filters, array $mapping): array
|
||||
{
|
||||
return array_map(static function (array $filter) use ($mapping): array {
|
||||
if (! in_array($filter['attribute'], ['status.from.id', 'status.to.id'], true)) {
|
||||
return $filter;
|
||||
}
|
||||
$map = static fn (string $value): string => (string) ($mapping[$value] ?? $value);
|
||||
$filter['value'] = is_array($filter['value'])
|
||||
? array_map($map, $filter['value'])
|
||||
: ($filter['value'] === null ? null : $map($filter['value']));
|
||||
|
||||
return $filter;
|
||||
}, app(NormalizeNotificationFilters::class)->handle($filters));
|
||||
}
|
||||
|
||||
/** @param array<int|string, int|string> $replacements @param list<string> $templateKeys */
|
||||
private function transferStatusTransitions(Project $project, TicketStatus $removed, TicketStatus $replacement, array $replacements, array $templateKeys): void
|
||||
{
|
||||
$statuses = $project->ticketStatuses()->get();
|
||||
$targets = $statuses->whereIn('configuration_key', $templateKeys);
|
||||
$mapping = [];
|
||||
foreach ($statuses as $status) {
|
||||
if (in_array($status->configuration_key, $templateKeys, true)) {
|
||||
$mapping[$status->id] = $status->id;
|
||||
|
||||
continue;
|
||||
}
|
||||
$target = $replacements[$status->id] ?? null;
|
||||
if (is_scalar($target)) {
|
||||
$mapping[$status->id] = $targets->firstWhere('configuration_key', (string) $target)?->id
|
||||
?? (is_numeric($target) ? $targets->firstWhere('id', (int) $target)?->id : null);
|
||||
}
|
||||
}
|
||||
$mapping[$removed->id] = $replacement->id;
|
||||
$edges = DB::table('ticket_status_transitions')->where('from_ticket_status_id', $removed->id)
|
||||
->orWhere('to_ticket_status_id', $removed->id)->get();
|
||||
foreach ($edges as $edge) {
|
||||
$from = $mapping[$edge->from_ticket_status_id] ?? null;
|
||||
$to = $mapping[$edge->to_ticket_status_id] ?? null;
|
||||
if ($from !== null && $to !== null && $from !== $to) {
|
||||
$statuses->firstWhere('id', $from)->allowedTargetStatuses()->syncWithoutDetaching([$to]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param EloquentCollection<int, Model> $models
|
||||
* @param array<int, array<string, mixed>> $items
|
||||
* @param list<array<string, mixed>> $items
|
||||
*/
|
||||
private function syncModels(EloquentCollection $models, array $items, callable $create, callable $update, ?callable $canDelete = null): bool
|
||||
private function syncModels(Project $project, string $area, array $items, ?callable $canDelete, bool $adoptExisting, ?callable $beforeDelete = null): bool
|
||||
{
|
||||
foreach ($items as $index => $item) {
|
||||
$model = $models->get($index);
|
||||
$model === null ? $create($item) : $update($model, $item);
|
||||
$relation = ProjectConfigurationIdentity::relations()[$area];
|
||||
$models = $project->{$relation}()->get();
|
||||
$byKey = $models->keyBy('configuration_key');
|
||||
$matched = [];
|
||||
$assignments = [];
|
||||
foreach ($items as $item) {
|
||||
$model = $byKey->get($item['configuration_key']);
|
||||
if ($model === null && $adoptExisting) {
|
||||
$candidates = $models->filter(fn (Model $candidate): bool => ! in_array($candidate->id, $matched, true)
|
||||
&& $this->projectConfigurationIdentity->signature($area, $candidate->attributesToArray()) === $this->projectConfigurationIdentity->signature($area, $item));
|
||||
$model = $candidates->count() === 1 ? $candidates->first() : null;
|
||||
}
|
||||
if ($model !== null) {
|
||||
$matched[] = $model->id;
|
||||
if ($area === self::COPY_TICKET_FIELDS && $model->field_type !== $item['field_type'] && $model->ticketFieldValues()->exists()) {
|
||||
return true;
|
||||
}
|
||||
if ($area === self::COPY_TICKET_TYPES && $model->is_parent && ! ($item['is_parent'] ?? false)
|
||||
&& $model->tickets()->whereHas('childTickets')->exists()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
$assignments[] = [$model, $item];
|
||||
}
|
||||
$removed = $models->reject(static fn (Model $model): bool => in_array($model->id, $matched, true));
|
||||
foreach ($removed as $model) {
|
||||
if ($canDelete !== null && ! $canDelete($model)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
$blocked = false;
|
||||
foreach ($models->slice(count($items)) as $model) {
|
||||
if ($canDelete !== null && ! $canDelete($model)) {
|
||||
$blocked = true;
|
||||
if ($area === self::COPY_REPOSITORY_CONNECTIONS) {
|
||||
foreach ($models as $model) {
|
||||
$model->forceFill(['provider_repository_id' => 'template-sync-'.Str::uuid()])->saveQuietly();
|
||||
}
|
||||
}
|
||||
|
||||
continue;
|
||||
if ($models->isNotEmpty() && $models->first()->getAttribute('position') !== null) {
|
||||
$offset = max((int) $models->max('position'), (int) collect($items)->max('position')) + $models->count() + count($items) + 1;
|
||||
foreach ($models->values() as $index => $model) {
|
||||
$model->forceFill(['position' => $offset + $index])->saveQuietly();
|
||||
}
|
||||
}
|
||||
foreach ($assignments as [$model, $item]) {
|
||||
$model === null ? $project->{$relation}()->create($item) : $model->forceFill($item)->saveQuietly();
|
||||
}
|
||||
foreach ($removed as $model) {
|
||||
if ($beforeDelete !== null) {
|
||||
$beforeDelete($model);
|
||||
}
|
||||
$model->deleteQuietly();
|
||||
}
|
||||
|
||||
return $blocked;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Projects;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
|
||||
class ProjectConfigurationIdentity
|
||||
{
|
||||
/** @return array<string, string> */
|
||||
public static function relations(): array
|
||||
{
|
||||
return [
|
||||
'board_columns' => 'boardColumns', 'ticket_statuses' => 'ticketStatuses',
|
||||
'ticket_types' => 'ticketTypes', 'ticket_priorities' => 'ticketPriorities',
|
||||
'ticket_fields' => 'ticketFieldDefinitions', 'ticket_link_types' => 'ticketLinkTypes',
|
||||
'repository_connections' => 'repositoryConnections', 'notification_actions' => 'notificationActions',
|
||||
];
|
||||
}
|
||||
|
||||
/** @param array<string, mixed> $configuration @return array<string, mixed> */
|
||||
public function normalize(array $configuration): array
|
||||
{
|
||||
foreach (self::relations() as $area => $relation) {
|
||||
if (! array_key_exists($area, $configuration)) {
|
||||
continue;
|
||||
}
|
||||
foreach ($configuration[$area] as $index => &$item) {
|
||||
$item['configuration_key'] ??= (string) Uuid::uuid5(
|
||||
Uuid::NAMESPACE_URL, 'kanbangit:'.$area.':'.$index.':'.$this->signature($area, $item),
|
||||
);
|
||||
}
|
||||
unset($item);
|
||||
}
|
||||
|
||||
$columns = array_column($configuration['board_columns'] ?? [], 'configuration_key');
|
||||
foreach ($configuration['ticket_statuses'] ?? [] as $index => $status) {
|
||||
if (! array_key_exists('board_column_key', $status)) {
|
||||
$columnIndex = $status['board_column_index'] ?? null;
|
||||
$status['board_column_key'] = $columnIndex === null ? null : ($columns[$columnIndex] ?? null);
|
||||
}
|
||||
unset($status['board_column_index']);
|
||||
$configuration['ticket_statuses'][$index] = $status;
|
||||
}
|
||||
|
||||
$statuses = array_column($configuration['ticket_statuses'] ?? [], 'configuration_key');
|
||||
foreach ($configuration['status_transitions'] ?? [] as $index => $transition) {
|
||||
if (array_key_exists('status_key', $transition)) {
|
||||
continue;
|
||||
}
|
||||
if (! isset($statuses[$index])) {
|
||||
continue;
|
||||
}
|
||||
$configuration['status_transitions'][$index] = [
|
||||
'status_key' => $statuses[$index],
|
||||
'target_keys' => array_values(array_filter(array_map(
|
||||
static fn (int $target): ?string => $statuses[$target] ?? null, $transition,
|
||||
))),
|
||||
];
|
||||
}
|
||||
|
||||
return $configuration;
|
||||
}
|
||||
|
||||
/** @param array<string, mixed> $item */
|
||||
public function signature(string $area, array $item): string
|
||||
{
|
||||
return $area === 'repository_connections'
|
||||
? ($item['provider'] ?? '').':'.($item['base_url'] ?? '').':'.($item['repository_full_name'] ?? '')
|
||||
: (string) ($item['name'] ?? $item['label'] ?? '');
|
||||
}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
public function defaults(): array
|
||||
{
|
||||
$configuration = array_fill_keys(DuplicateProjectConfiguration::copyableAreas(), []);
|
||||
$configuration['project_settings'] = ['show_author' => true, 'show_story_points' => true];
|
||||
$configuration['ticket_statuses'] = [[
|
||||
'configuration_key' => (string) Str::uuid(), 'name' => 'Backlog',
|
||||
'position' => 1, 'board_column_key' => null,
|
||||
]];
|
||||
$configuration['ticket_types'] = [
|
||||
['configuration_key' => (string) Str::uuid(), 'name' => 'Task', 'position' => 1, 'is_parent' => false],
|
||||
['configuration_key' => (string) Str::uuid(), 'name' => 'Release', 'position' => 2, 'is_parent' => true],
|
||||
];
|
||||
$configuration['ticket_priorities'] = [[
|
||||
'configuration_key' => (string) Str::uuid(), 'name' => 'Medium', 'position' => 1,
|
||||
]];
|
||||
$configuration['status_transitions'] = [[
|
||||
'status_key' => $configuration['ticket_statuses'][0]['configuration_key'], 'target_keys' => [],
|
||||
]];
|
||||
|
||||
return $configuration;
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Actions\Projects;
|
||||
|
||||
use App\Models\Project;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class ProjectTemplateOverrideManager
|
||||
{
|
||||
@@ -14,18 +15,16 @@ class ProjectTemplateOverrideManager
|
||||
return;
|
||||
}
|
||||
|
||||
$project->loadMissing('projectTemplate');
|
||||
|
||||
if ($project->projectTemplate === null || ! $project->projectTemplate->is_published) {
|
||||
return;
|
||||
}
|
||||
|
||||
$overrides = array_values(array_unique([
|
||||
...($project->template_overrides ?? []),
|
||||
$area,
|
||||
]));
|
||||
|
||||
$project->forceFill(['template_overrides' => $overrides])->saveQuietly();
|
||||
DB::transaction(function () use ($project, $area): void {
|
||||
$project = Project::query()->whereKey($project->id)->lockForUpdate()->firstOrFail();
|
||||
$project->load('projectTemplate');
|
||||
if ($project->projectTemplate === null || ! $project->projectTemplate->is_published) {
|
||||
return;
|
||||
}
|
||||
$project->forceFill(['template_overrides' => array_values(array_unique([
|
||||
...($project->template_overrides ?? []), $area,
|
||||
]))])->saveQuietly();
|
||||
}, 3);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -33,8 +32,10 @@ class ProjectTemplateOverrideManager
|
||||
*/
|
||||
public function clear(Project $project, array $areas): void
|
||||
{
|
||||
$remaining = array_values(array_diff($project->template_overrides ?? [], $areas));
|
||||
$project->forceFill(['template_overrides' => $remaining])->saveQuietly();
|
||||
DB::transaction(function () use ($project, $areas): void {
|
||||
$project = Project::query()->whereKey($project->id)->lockForUpdate()->firstOrFail();
|
||||
$project->forceFill(['template_overrides' => array_values(array_diff($project->template_overrides ?? [], $areas))])->saveQuietly();
|
||||
}, 3);
|
||||
}
|
||||
|
||||
public function withoutRecording(callable $callback): mixed
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
namespace App\Actions\Projects;
|
||||
|
||||
use App\Models\Project;
|
||||
use App\Models\ProjectTemplate;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class RefreshProjectBaseTemplate
|
||||
{
|
||||
@@ -12,21 +14,17 @@ class RefreshProjectBaseTemplate
|
||||
|
||||
public function handle(Project $project, string $area): void
|
||||
{
|
||||
$project->loadMissing('projectTemplate');
|
||||
|
||||
if ($project->projectTemplate === null || $project->projectTemplate->is_published) {
|
||||
return;
|
||||
}
|
||||
|
||||
$snapshot = $this->duplicateProjectConfiguration->normalizeConfiguration(
|
||||
$project->projectTemplate->configuration_snapshot ?? [],
|
||||
);
|
||||
|
||||
$project->projectTemplate->forceFill([
|
||||
'configuration_snapshot' => array_replace(
|
||||
$snapshot,
|
||||
$this->duplicateProjectConfiguration->export($project->fresh(), [$area]),
|
||||
),
|
||||
])->saveQuietly();
|
||||
DB::transaction(function () use ($project, $area): void {
|
||||
$project = Project::query()->whereKey($project->id)->lockForUpdate()->firstOrFail();
|
||||
$template = ProjectTemplate::query()->find($project->project_template_id);
|
||||
if ($template === null || $template->is_published) {
|
||||
return;
|
||||
}
|
||||
$template = ProjectTemplate::query()->whereKey($template->id)->lockForUpdate()->firstOrFail();
|
||||
$snapshot = $this->duplicateProjectConfiguration->normalizeConfiguration($template->configuration_snapshot ?? []);
|
||||
$template->forceFill(['configuration_snapshot' => array_replace(
|
||||
$snapshot, $this->duplicateProjectConfiguration->export($project, [$area]),
|
||||
)])->saveQuietly();
|
||||
}, 3);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,7 +71,9 @@ class ResolveProjectTemplateState
|
||||
$inheritedAreaValues = array_flip(array_column($inheritedAreas, 'value'));
|
||||
|
||||
return [
|
||||
'effectiveConfiguration' => array_replace($templateConfiguration, array_intersect_key($currentConfiguration, $overriddenAreaValues)),
|
||||
'effectiveConfiguration' => $currentConfiguration,
|
||||
'localConfiguration' => array_diff_key($currentConfiguration, $templateConfiguration),
|
||||
'localAreas' => collect(array_diff(array_keys($currentConfiguration), array_keys($templateConfiguration)))->map(fn (string $area): array => ['label' => $this->labelForArea($area), 'value' => $area])->values()->all(),
|
||||
'inheritedAreas' => $inheritedAreas,
|
||||
'inheritedConfiguration' => array_intersect_key($templateConfiguration, $inheritedAreaValues),
|
||||
'inheritsFromTemplate' => $projectTemplate->is_published,
|
||||
@@ -96,24 +98,20 @@ class ResolveProjectTemplateState
|
||||
return [];
|
||||
}
|
||||
|
||||
$templateStatuses = $templateConfiguration[DuplicateProjectConfiguration::COPY_TICKET_STATUSES] ?? [];
|
||||
$templateStatuses = $templateConfiguration[DuplicateProjectConfiguration::COPY_TICKET_STATUSES] ?? null;
|
||||
if (! is_array($templateStatuses)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$statuses = $project->ticketStatuses()->withCount('tickets')->get()->values();
|
||||
$replacementOptions = $statuses->take(count($templateStatuses))->map(
|
||||
static fn ($status): array => ['id' => $status->id, 'name' => $status->name],
|
||||
$statuses = $project->ticketStatuses()->withCount('tickets')->get();
|
||||
$templateKeys = array_column($templateStatuses, 'configuration_key');
|
||||
$replacementOptions = collect($templateStatuses)->map(
|
||||
static fn (array $status): array => ['id' => $status['configuration_key'], 'name' => $status['name']],
|
||||
)->all();
|
||||
$removedStatuses = $statuses->slice(count($templateStatuses))
|
||||
->filter(static fn ($status): bool => $status->tickets_count > 0)
|
||||
->map(static fn ($status): array => [
|
||||
'id' => $status->id,
|
||||
'name' => $status->name,
|
||||
'ticketCount' => $status->tickets_count,
|
||||
])
|
||||
->values()
|
||||
->all();
|
||||
$removedStatuses = $statuses
|
||||
->filter(static fn ($status): bool => ! in_array($status->configuration_key, $templateKeys, true) && $status->tickets_count > 0)
|
||||
->map(static fn ($status): array => ['id' => $status->id, 'name' => $status->name, 'ticketCount' => $status->tickets_count])
|
||||
->values()->all();
|
||||
|
||||
return $removedStatuses === [] ? [] : [
|
||||
DuplicateProjectConfiguration::COPY_TICKET_STATUSES => [
|
||||
|
||||
@@ -19,37 +19,34 @@ class UpdateProjectTemplate
|
||||
*/
|
||||
public function handle(ProjectTemplate $projectTemplate, array $attributes): ProjectTemplate
|
||||
{
|
||||
$projectTemplate->loadMissing('sourceProject');
|
||||
|
||||
$sourceProject = $projectTemplate->sourceProject;
|
||||
abort_if($sourceProject === null, 422, 'This template cannot be edited because the source project is missing.');
|
||||
|
||||
$copyAreas = array_values(Arr::get($attributes, 'copy_areas', []));
|
||||
$normalizedCopyAreas = $this->duplicateProjectConfiguration->normalizeCopyAreas($copyAreas);
|
||||
$previousConfiguration = $this->duplicateProjectConfiguration->normalizeConfiguration(
|
||||
$projectTemplate->configuration_snapshot ?? [],
|
||||
);
|
||||
$nextConfiguration = $this->duplicateProjectConfiguration->export($sourceProject, $normalizedCopyAreas);
|
||||
$affectedAreas = collect(array_unique([...array_keys($previousConfiguration), ...array_keys($nextConfiguration)]))
|
||||
->filter(fn (string $area): bool => ($previousConfiguration[$area] ?? null) !== ($nextConfiguration[$area] ?? null))
|
||||
->values()
|
||||
->all();
|
||||
|
||||
DB::transaction(function () use ($projectTemplate, $attributes, $normalizedCopyAreas, $nextConfiguration, $previousConfiguration, $affectedAreas): void {
|
||||
ProjectTemplate::query()->whereKey($projectTemplate->id)->lockForUpdate()->firstOrFail();
|
||||
|
||||
return DB::transaction(function () use ($projectTemplate, $attributes): ProjectTemplate {
|
||||
$projectTemplate = ProjectTemplate::query()->whereKey($projectTemplate->id)->lockForUpdate()->firstOrFail();
|
||||
$previousConfiguration = $this->duplicateProjectConfiguration->normalizeConfiguration($projectTemplate->configuration_snapshot ?? []);
|
||||
$copyAreas = $this->duplicateProjectConfiguration->normalizeCopyAreas(Arr::get($attributes, 'copy_areas', $projectTemplate->copy_areas ?? []));
|
||||
$nextConfiguration = collect($this->duplicateProjectConfiguration->normalizeConfiguration(
|
||||
Arr::get($attributes, 'configuration_snapshot', $previousConfiguration),
|
||||
))->only($copyAreas)->all();
|
||||
$affectedAreas = collect(array_unique([...array_keys($previousConfiguration), ...array_keys($nextConfiguration)]))
|
||||
->filter(fn (string $area): bool => ($previousConfiguration[$area] ?? null) !== ($nextConfiguration[$area] ?? null))
|
||||
->values()->all();
|
||||
$projectTemplate->forceFill([
|
||||
'name' => $attributes['name'],
|
||||
'copy_areas' => $normalizedCopyAreas,
|
||||
'name' => Arr::get($attributes, 'name'),
|
||||
'copy_areas' => $copyAreas,
|
||||
'configuration_snapshot' => $nextConfiguration,
|
||||
])->save();
|
||||
|
||||
Project::query()
|
||||
->where('project_template_id', $projectTemplate->id)
|
||||
->orderBy('id')
|
||||
->chunkById(100, function ($linkedProjects) use ($affectedAreas, $nextConfiguration, $previousConfiguration): void {
|
||||
->chunkById(100, function ($linkedProjects) use ($projectTemplate, $affectedAreas, $nextConfiguration, $previousConfiguration): void {
|
||||
foreach ($linkedProjects as $linkedProject) {
|
||||
$linkedProject = Project::query()->whereKey($linkedProject->id)->lockForUpdate()->firstOrFail();
|
||||
$linkedProject = Project::query()
|
||||
->whereKey($linkedProject->id)
|
||||
->where('project_template_id', $projectTemplate->id)
|
||||
->lockForUpdate()->first();
|
||||
if ($linkedProject === null) {
|
||||
continue;
|
||||
}
|
||||
$currentConfiguration = $this->duplicateProjectConfiguration->export($linkedProject, $affectedAreas);
|
||||
$overrides = $linkedProject->template_overrides ?? [];
|
||||
$replacementConfiguration = [];
|
||||
@@ -81,8 +78,8 @@ class UpdateProjectTemplate
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
return $projectTemplate->fresh(['sourceProject'])->loadCount('linkedProjects');
|
||||
return $projectTemplate->fresh(['sourceProject'])->loadCount('linkedProjects');
|
||||
}, 3);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace App\Actions\Projects;
|
||||
use App\Models\Project;
|
||||
use App\Models\ProjectTemplate;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class UpdateProjectTemplateLink
|
||||
{
|
||||
@@ -16,6 +17,7 @@ class UpdateProjectTemplateLink
|
||||
public function switch(Project $project, ProjectTemplate $template): void
|
||||
{
|
||||
DB::transaction(function () use ($project, $template): void {
|
||||
$template = ProjectTemplate::query()->whereKey($template->id)->lockForUpdate()->firstOrFail();
|
||||
$project = Project::query()->whereKey($project->id)->lockForUpdate()->firstOrFail();
|
||||
$configuration = $this->duplicateProjectConfiguration->normalizeConfiguration(
|
||||
$template->configuration_snapshot ?? [],
|
||||
@@ -42,19 +44,27 @@ class UpdateProjectTemplateLink
|
||||
* @param list<string> $areas
|
||||
* @param array<int|string, int|string> $statusReplacements
|
||||
*/
|
||||
public function reset(Project $project, array $areas, array $statusReplacements = []): void
|
||||
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): void {
|
||||
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();
|
||||
$configuration = collect($this->duplicateProjectConfiguration->normalizeConfiguration(
|
||||
$template->configuration_snapshot ?? [],
|
||||
))->only($areas)->all();
|
||||
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,
|
||||
@@ -64,6 +74,8 @@ class UpdateProjectTemplateLink
|
||||
$project->fresh(),
|
||||
array_values(array_diff(array_keys($configuration), $blockedAreas)),
|
||||
);
|
||||
});
|
||||
|
||||
return $blockedAreas;
|
||||
}, 3);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user