From 8f866737a0f79b73013adc5a33a289d1c3c289ae Mon Sep 17 00:00:00 2001 From: Novy San87 Date: Thu, 6 Aug 2026 18:34:26 +0700 Subject: [PATCH] feat: endpoint notifikasi unread count (7 hari) --- .../Api/ParentNotificationController.php | 24 +++++++++++++++++++ routes/api.php | 1 + 2 files changed, 25 insertions(+) diff --git a/app/Http/Controllers/Api/ParentNotificationController.php b/app/Http/Controllers/Api/ParentNotificationController.php index 12d57d8..85ab1d0 100644 --- a/app/Http/Controllers/Api/ParentNotificationController.php +++ b/app/Http/Controllers/Api/ParentNotificationController.php @@ -54,6 +54,30 @@ class ParentNotificationController extends Controller ]); } + /** + * Jumlah notifikasi penting (pelanggaran/SP) 7 hari terakhir — untuk badge. + */ + public function unreadCount(Request $request): JsonResponse + { + $user = $request->user(); + + $studentIds = ParentStudent::where('user_id', $user->id) + ->where('status', 'active') + ->pluck('student_id') + ->all(); + + if (empty($studentIds)) { + return response()->json(['count' => 0]); + } + + $count = ViolationNotification::whereIn('student_id', $studentIds) + ->where('channel', 'push') // pelanggaran & SP (bukan kehadiran) + ->where('created_at', '>=', now()->subDays(7)) + ->count(); + + return response()->json(['count' => $count]); + } + /** * Daftarkan perangkat (token FCM) untuk push notification. */ diff --git a/routes/api.php b/routes/api.php index 27a9184..0627bc9 100644 --- a/routes/api.php +++ b/routes/api.php @@ -43,6 +43,7 @@ Route::middleware('auth:sanctum')->prefix('parent')->group(function () { // Notifikasi & perangkat Route::get('/notifications', [ParentNotificationController::class, 'index'])->name('api.parent.notifications'); + Route::get('/notifications/unread', [ParentNotificationController::class, 'unreadCount'])->name('api.parent.notifications.unread'); Route::get('/school-informations', [ParentSchoolInformationController::class, 'index'])->name('api.parent.school-informations'); Route::get('/academic-calendar', [ParentSchoolInformationController::class, 'calendar'])->name('api.parent.academic-calendar'); Route::post('/devices', [ParentNotificationController::class, 'registerDevice'])->name('api.parent.devices');