Enforce task limits and update event form
This commit is contained in:
@@ -11,12 +11,10 @@ use RuntimeException;
|
||||
|
||||
class TaskCollectionImportService
|
||||
{
|
||||
public function __construct(private readonly DatabaseManager $db)
|
||||
{
|
||||
}
|
||||
public function __construct(private readonly DatabaseManager $db) {}
|
||||
|
||||
/**
|
||||
* @return array{collection: TaskCollection, created_task_ids: array<int>, attached_task_ids: array<int>}
|
||||
* @return array{collection: TaskCollection, created_task_ids: array<int>, attached_task_ids: array<int>, skipped_task_ids: array<int>}
|
||||
*/
|
||||
public function import(TaskCollection $collection, Event $event): array
|
||||
{
|
||||
@@ -33,8 +31,28 @@ class TaskCollectionImportService
|
||||
|
||||
$createdTaskIds = [];
|
||||
$attachedTaskIds = [];
|
||||
$skippedTaskIds = [];
|
||||
|
||||
$event->loadMissing(['eventPackage.package', 'eventPackages.package']);
|
||||
$eventPackage = $event->eventPackage;
|
||||
if (! $eventPackage && method_exists($event, 'eventPackages')) {
|
||||
$eventPackage = $event->eventPackages()
|
||||
->with('package')
|
||||
->orderByDesc('purchased_at')
|
||||
->orderByDesc('created_at')
|
||||
->first();
|
||||
}
|
||||
|
||||
$taskLimit = $eventPackage?->effectiveLimits()['max_tasks'] ?? null;
|
||||
$remaining = $taskLimit === null ? null : max(0, (int) $taskLimit - $event->tasks()->count());
|
||||
|
||||
foreach ($collection->tasks as $task) {
|
||||
if ($remaining !== null && $remaining <= 0) {
|
||||
$skippedTaskIds[] = $task->id;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$tenantTask = $this->resolveTenantTask($task, $targetCollection, $tenantId);
|
||||
|
||||
if ($tenantTask->wasRecentlyCreated) {
|
||||
@@ -44,6 +62,9 @@ class TaskCollectionImportService
|
||||
if (! $tenantTask->assignedEvents()->where('event_id', $event->id)->exists()) {
|
||||
$tenantTask->assignedEvents()->attach($event->id);
|
||||
$attachedTaskIds[] = $tenantTask->id;
|
||||
if ($remaining !== null) {
|
||||
$remaining = max(0, $remaining - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,6 +76,7 @@ class TaskCollectionImportService
|
||||
'collection' => $targetCollection->fresh(),
|
||||
'created_task_ids' => $createdTaskIds,
|
||||
'attached_task_ids' => $attachedTaskIds,
|
||||
'skipped_task_ids' => $skippedTaskIds,
|
||||
];
|
||||
});
|
||||
}
|
||||
@@ -139,10 +161,10 @@ class TaskCollectionImportService
|
||||
|
||||
protected function buildCollectionSlug(?string $slug, int $tenantId): string
|
||||
{
|
||||
$base = Str::slug(($slug ?: 'collection') . '-' . $tenantId);
|
||||
$base = Str::slug(($slug ?: 'collection').'-'.$tenantId);
|
||||
|
||||
do {
|
||||
$candidate = $base . '-' . Str::random(4);
|
||||
$candidate = $base.'-'.Str::random(4);
|
||||
} while (TaskCollection::where('slug', $candidate)->exists());
|
||||
|
||||
return $candidate;
|
||||
@@ -153,7 +175,7 @@ class TaskCollectionImportService
|
||||
$slugBase = Str::slug($base) ?: 'task';
|
||||
|
||||
do {
|
||||
$candidate = $slugBase . '-' . Str::random(6);
|
||||
$candidate = $slugBase.'-'.Str::random(6);
|
||||
} while (Task::where('slug', $candidate)->exists());
|
||||
|
||||
return $candidate;
|
||||
|
||||
Reference in New Issue
Block a user