diff --git a/app/Services/PermohonanHistoryService.php b/app/Services/PermohonanHistoryService.php index 05bf609..723eb42 100644 --- a/app/Services/PermohonanHistoryService.php +++ b/app/Services/PermohonanHistoryService.php @@ -4,6 +4,8 @@ namespace Modules\Lpj\Services; use Modules\Lpj\Models\Permohonan; use Modules\Lpj\Models\PermohonanHistory; +use Modules\Lpj\Notifications\PermohonanNotif; +use Modules\Usermanagement\Models\User; class PermohonanHistoryService { @@ -21,6 +23,8 @@ class PermohonanHistoryService 'user_id' => auth()->id(), ]); + $this->createNotification($permohonan, $status, $beforeRequest, $afterRequest); + } catch (\Exception $e) { // Log the error \Log::error('Error creating PermohonanHistory: ' . $e->getMessage()); @@ -34,4 +38,25 @@ class PermohonanHistoryService throw new \Exception('Failed to create PermohonanHistory: ' . $e->getMessage()); } } + + private function createNotification(Permohonan $permohonan, string $status, array $beforeRequest, array $afterRequest) + { + $beforeStatus = ''; + if(!empty($beforeRequest)){ + $beforeStatus = $beforeRequest['status'] ?? ''; + } + + if($beforeStatus !== $status){ + if($status === 'order'){ + $users = User::where(['branch_id' => $permohonan->branch_id])->whereHas('roles',function($q){ + $q->where('name', 'pemohon-eo'); + })->get(); + + foreach ($users as $user) { + $message = "telah diorder oleh {$permohonan->creator->name}, Mohon Lakukan konfirmasi"; + $user->notify(new PermohonanNotif($permohonan,$message)); + } + } + } + } }