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.
This commit is contained in:
Daeng Deni Mardaeni
2025-04-23 09:26:08 +07:00
parent 2506b6115c
commit 10aa59d65d

View File

@@ -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));
}
}
}
}
}