fix: set assigned_by when syncing shift staff pivot records
Some checks failed
CI / tests (push) Has been cancelled
CI / deploy-staging (push) Has been cancelled
CI / deploy-production (push) Has been cancelled

This commit is contained in:
2026-03-12 17:51:32 +02:00
parent 5a93cc76fd
commit 2475daca4b
6 changed files with 50 additions and 2 deletions

View File

@@ -56,7 +56,7 @@ public static function form(Form $form): Form
Forms\Components\Select::make('staffMembers')
->label('Assign Staff')
->multiple()
->relationship('staff', 'name')
->options(User::query()->pluck('name', 'id'))
->preload()
->searchable(),
])

View File

@@ -17,4 +17,17 @@ protected function mutateFormDataBeforeCreate(array $data): array
return $data;
}
protected function afterCreate(): void
{
$staffIds = $this->data['staffMembers'] ?? [];
if (empty($staffIds)) {
return;
}
$this->record->staff()->syncWithPivotValues(
$staffIds,
['assigned_by' => auth()->id()]
);
}
}

View File

@@ -54,4 +54,14 @@ protected function getHeaderActions(): array
->visible(fn () => $this->record->isPlanned()),
];
}
protected function afterSave(): void
{
$staffIds = $this->data['staffMembers'] ?? [];
$this->record->staff()->syncWithPivotValues(
$staffIds,
['assigned_by' => auth()->id()]
);
}
}

View File

@@ -31,7 +31,13 @@ public function table(Table $table): Table
Tables\Actions\AttachAction::make()
->label('Add Staff')
->preloadRecordSelect()
->visible(fn () => $this->getOwnerRecord()->isPlanned()),
->visible(fn () => $this->getOwnerRecord()->isPlanned())
->action(function (array $data, $ownerRecord) {
$staffId = $data['record'] ?? null;
if ($staffId) {
app(\App\Services\ShiftService::class)->assignStaff($ownerRecord, [$staffId]);
}
}),
])
->actions([
Tables\Actions\DetachAction::make()

View File

@@ -29,6 +29,7 @@ public function createShift(array $data, ?array $staffIds = null): Shift
]);
if ($staffIds) {
logger()->info('Staff IDs:', $staffIds);
$this->assignStaff($shift, $staffIds);
}
@@ -102,6 +103,11 @@ public function assignStaff(Shift $shift, array $staffIds): void
}
$managerId = Auth::id();
if(!$managerId) {
throw new InvalidArgumentException('Authenticated user is required to assign staff.');
}
$syncData = [];
foreach ($staffIds as $staffId) {

View File

@@ -13,11 +13,24 @@ class DatabaseSeeder extends Seeder
*/
public function run(): void
{
// Create the admin user
User::factory()->create([
'name' => 'Admin User',
'email' => 'admin@example.com',
]);
// Create 10 random staff users
User::factory()->create(['name' => 'Staff One', 'email' => 'staff1@example.com']);
User::factory()->create(['name' => 'Staff Two', 'email' => 'staff2@example.com']);
User::factory()->create(['name' => 'Staff Three', 'email' => 'staff3@example.com']);
User::factory()->create(['name' => 'Staff Four', 'email' => 'staff4@example.com']);
User::factory()->create(['name' => 'Staff Five', 'email' => 'staff5@example.com']);
User::factory()->create(['name' => 'Staff Six', 'email' => 'staff6@example.com']);
User::factory()->create(['name' => 'Staff Seven', 'email' => 'staff7@example.com']);
User::factory()->create(['name' => 'Staff Eight', 'email' => 'staff8@example.com']);
User::factory()->create(['name' => 'Staff Nine', 'email' => 'staff9@example.com']);
User::factory()->create(['name' => 'Staff Ten', 'email' => 'staff10@example.com']);
$this->call(RolePermissionSeeder::class);
}
}