From 10aa59d65d4c847b18e207566082d3c4eba53541 Mon Sep 17 00:00:00 2001 From: Daeng Deni Mardaeni Date: Wed, 23 Apr 2025 09:26:08 +0700 Subject: [PATCH] feat(permohonan): tambahkan fungsi notifikasi saat membuat riwayat permohonan - Menambahkan pemanggilan fungsi createNotification setelah membuat riwayat permohonan. - Mengimplementasikan logika untuk mengirim notifikasi kepada pengguna terkait status 'order'. - Menggunakan kelas PermohonanNotif untuk mengirim pesan notifikasi. --- app/Services/PermohonanHistoryService.php | 25 +++++++++++++++++++++++ 1 file changed, 25 insertions(+) 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)); + } + } + } + } }