Allow task attach search across global tasks
This commit is contained in:
@@ -56,16 +56,7 @@ class TasksRelationManager extends RelationManager
|
||||
->headerActions([
|
||||
AttachAction::make()
|
||||
->recordTitle(fn (Task $record) => $this->formatTaskTitle($record->title))
|
||||
->recordSelectOptionsQuery(function (Builder $query): Builder {
|
||||
$collectionId = $this->getOwnerRecord()->getKey();
|
||||
|
||||
return $query
|
||||
->whereNull('tenant_id')
|
||||
->where(function (Builder $inner) use ($collectionId): void {
|
||||
$inner->whereNull('collection_id')
|
||||
->orWhere('collection_id', $collectionId);
|
||||
});
|
||||
})
|
||||
->recordSelectOptionsQuery(fn (Builder $query): Builder => $query->whereNull('tenant_id'))
|
||||
->multiple()
|
||||
->after(function (array $data): void {
|
||||
$collection = $this->getOwnerRecord();
|
||||
@@ -75,9 +66,7 @@ class TasksRelationManager extends RelationManager
|
||||
return;
|
||||
}
|
||||
|
||||
Task::query()
|
||||
->whereIn('id', $recordIds)
|
||||
->update(['collection_id' => $collection->getKey()]);
|
||||
$collection->reassignTasks($recordIds);
|
||||
}),
|
||||
])
|
||||
->recordActions([
|
||||
|
||||
@@ -8,6 +8,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class TaskCollection extends Model
|
||||
{
|
||||
@@ -99,6 +100,27 @@ class TaskCollection extends Model
|
||||
return $value ?: null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, int|string> $taskIds
|
||||
*/
|
||||
public function reassignTasks(array $taskIds): void
|
||||
{
|
||||
$ids = array_values(array_unique(array_map('intval', $taskIds)));
|
||||
|
||||
if ($ids === []) {
|
||||
return;
|
||||
}
|
||||
|
||||
DB::table('task_collection_task')
|
||||
->whereIn('task_id', $ids)
|
||||
->where('task_collection_id', '!=', $this->getKey())
|
||||
->delete();
|
||||
|
||||
Task::query()
|
||||
->whereIn('id', $ids)
|
||||
->update(['collection_id' => $this->getKey()]);
|
||||
}
|
||||
|
||||
protected function resolveTranslation(string $attribute, ?string $locale = null): string
|
||||
{
|
||||
$translations = $this->{$attribute} ?? [];
|
||||
|
||||
@@ -3,11 +3,15 @@
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Models\Task;
|
||||
use App\Models\TaskCollection;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class TaskCollectionsRelationTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_task_has_task_collections_relation(): void
|
||||
{
|
||||
$task = new Task;
|
||||
@@ -16,4 +20,31 @@ class TaskCollectionsRelationTest extends TestCase
|
||||
$this->assertInstanceOf(BelongsToMany::class, $relation);
|
||||
$this->assertSame(['sort_order'], $relation->getPivotColumns());
|
||||
}
|
||||
|
||||
public function test_reassign_tasks_moves_pivot_and_collection_id(): void
|
||||
{
|
||||
$collectionA = TaskCollection::factory()->create([
|
||||
'tenant_id' => null,
|
||||
'event_type_id' => null,
|
||||
]);
|
||||
$collectionB = TaskCollection::factory()->create([
|
||||
'tenant_id' => null,
|
||||
'event_type_id' => null,
|
||||
]);
|
||||
|
||||
$task = Task::factory()->create([
|
||||
'tenant_id' => null,
|
||||
'event_type_id' => null,
|
||||
'collection_id' => $collectionA->id,
|
||||
]);
|
||||
|
||||
$collectionA->tasks()->attach($task->id);
|
||||
$collectionB->tasks()->attach($task->id);
|
||||
|
||||
$collectionB->reassignTasks([$task->id]);
|
||||
|
||||
$this->assertFalse($collectionA->tasks()->whereKey($task->id)->exists());
|
||||
$this->assertTrue($collectionB->tasks()->whereKey($task->id)->exists());
|
||||
$this->assertSame($collectionB->id, $task->fresh()->collection_id);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user