first
This commit is contained in:
65
app/Services/Admin/PpmAssignmentService.php
Normal file
65
app/Services/Admin/PpmAssignmentService.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Admin;
|
||||
|
||||
use App\Models\Position;
|
||||
use App\Models\PusatMengundi;
|
||||
use App\Models\StaffAssignment;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class PpmAssignmentService
|
||||
{
|
||||
public function assign(PusatMengundi $pusatMengundi, User $user): StaffAssignment
|
||||
{
|
||||
return DB::transaction(function () use ($pusatMengundi, $user): StaffAssignment {
|
||||
$position = Position::query()->where('code', 'PPM')->firstOrFail();
|
||||
|
||||
StaffAssignment::query()
|
||||
->where('election_id', $pusatMengundi->election_id)
|
||||
->where('pusat_mengundi_id', $pusatMengundi->id)
|
||||
->where('position_id', $position->id)
|
||||
->whereNull('saluran_mengundi_id')
|
||||
->where('status', 'active')
|
||||
->where('user_id', '!=', $user->id)
|
||||
->update(['status' => 'replaced']);
|
||||
|
||||
$assignment = StaffAssignment::query()->updateOrCreate(
|
||||
[
|
||||
'election_id' => $pusatMengundi->election_id,
|
||||
'pusat_mengundi_id' => $pusatMengundi->id,
|
||||
'position_id' => $position->id,
|
||||
'saluran_mengundi_id' => null,
|
||||
'user_id' => $user->id,
|
||||
],
|
||||
[
|
||||
'status' => 'active',
|
||||
'assigned_by_user_id' => auth()->id(),
|
||||
'assigned_at' => now(),
|
||||
'source' => 'admin_setup',
|
||||
],
|
||||
);
|
||||
|
||||
$assignment->histories()->create([
|
||||
'to_position_id' => $position->id,
|
||||
'to_pusat_mengundi_id' => $pusatMengundi->id,
|
||||
'changed_by_user_id' => auth()->id(),
|
||||
'note' => 'PPM assigned from Admin setup module.',
|
||||
]);
|
||||
|
||||
$user->assignRole('PPM');
|
||||
|
||||
activity('admin_setup')
|
||||
->performedOn($assignment)
|
||||
->causedBy(auth()->user())
|
||||
->withProperties([
|
||||
'pusat_mengundi_id' => $pusatMengundi->id,
|
||||
'user_id' => $user->id,
|
||||
'position_code' => 'PPM',
|
||||
])
|
||||
->log('ppm_assigned');
|
||||
|
||||
return $assignment;
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user