added notifications

This commit is contained in:
Jagad R R
2025-01-17 16:17:53 +07:00
parent 47eb2950ef
commit f4a8972a1d
13 changed files with 344 additions and 9 deletions
@@ -14,6 +14,7 @@ use App\Models\FormVehicleRunningCost;
use App\Models\FormMeetingSeminar;
use App\Models\FormOthers;
use App\Models\UserHasCabang;
use App\Models\Notifications;
class DashboardController extends Controller
@@ -99,4 +100,43 @@ class DashboardController extends Controller
]
);
}
public function notifications()
{
$this->checkAuthorization(auth()->user(), ['notification.view']);
$role = auth()->user()->getRoleNames()[0];
// Mulai dengan query dasar
$notifications = Notifications::orderBy('created_at', 'desc')
->where('receiver_id', auth()->user()->id)
->where('is_read', 0);
// Tambahkan filter berdasarkan role
if ($role == 'Admin Region') {
$notifications->where('type', 'new_expense');
} elseif ($role == 'Marketing Operational Manager Region') {
$notifications->where('type', 'approve2_expense');
} elseif ($role == 'Area Manager Cabang' || $role == 'Marketing Information System') {
$notifications->where('type', 'approve_expense');
} elseif ($role == 'Head of Sales Marketing') {
$notifications->where('type', 'approve2_expense');
} else {
// Jika tidak ada role yang sesuai, kembalikan koleksi kosong
return response()->json([]);
}
// Ambil data dan kembalikan sebagai JSON
return response()->json($notifications->limit(6)->get());
}
public function mark_read()
{
$this->checkAuthorization(auth()->user(), ['notification.delete']);
$receiver_id = auth()->user()->id;
Notifications::where('receiver_id', $receiver_id)->update(['is_read' => 1]);
session()->flash('success', 'Notifications marked as read.');
return redirect()->back();
}
}