diff --git a/app/Http/Controllers/Api/ParentStudentController.php b/app/Http/Controllers/Api/ParentStudentController.php index f8759c7..c9b229c 100644 --- a/app/Http/Controllers/Api/ParentStudentController.php +++ b/app/Http/Controllers/Api/ParentStudentController.php @@ -4,6 +4,7 @@ namespace App\Http\Controllers\Api; use App\Http\Controllers\Controller; use App\Models\ParentStudent; +use App\Models\PointAuditLog; use App\Models\SpLetter; use App\Models\Student; use App\Models\Violation; @@ -212,6 +213,59 @@ class ParentStudentController extends Controller ]); } + /** + * Riwayat poin putra/putri (peristiwa + grafik bulanan). + */ + public function pointsHistory(Request $request, Student $student): JsonResponse + { + $hasLink = ParentStudent::where('user_id', $request->user()->id) + ->where('student_id', $student->id) + ->where('status', 'active') + ->exists(); + + abort_unless($hasLink, 403, 'Putra/putri belum tertaut aktif.'); + + $logs = PointAuditLog::where('student_id', $student->id) + ->orderByDesc('created_at') + ->limit(200) + ->get(); + + $events = $logs->map(function (PointAuditLog $l) { + $meta = $l->metadata ?? []; + + return [ + 'id' => $l->id, + 'date' => $l->created_at?->toDateString(), + 'description' => $l->description, + 'detail' => $meta['description'] ?? null, + 'delta' => (int) $l->points_delta, + 'total_after' => (int) $l->points_after, + 'action' => $l->action, + ]; + })->values(); + + // Grafik 6 bulan terakhir (net poin per bulan) + $monthly = []; + for ($i = 5; $i >= 0; $i--) { + $m = now()->subMonths($i); + $key = $m->format('Y-m'); + $monthNumber = (int) $m->format('n'); + $sum = $logs->filter(fn (PointAuditLog $l) => $l->created_at?->format('Y-m') === $key) + ->sum('points_delta'); + $monthly[] = [ + 'month' => $key, + 'month_number' => $monthNumber, + 'points' => (int) $sum, + ]; + } + + return response()->json([ + 'total_points' => (int) $student->total_points, + 'events' => $events, + 'monthly' => $monthly, + ]); + } + /** * Tautkan anak tambahan (kakak/adik) ke akun wali. */ diff --git a/routes/api.php b/routes/api.php index 91b565e..e0f0211 100644 --- a/routes/api.php +++ b/routes/api.php @@ -37,6 +37,7 @@ Route::middleware('auth:sanctum')->prefix('parent')->group(function () { Route::get('/students/{student}/sp-letters', [ParentStudentController::class, 'spLetters'])->name('api.parent.students.sp-letters'); Route::get('/students/{student}/schedule', [ParentStudentController::class, 'schedule'])->name('api.parent.students.schedule'); Route::get('/students/{student}/attendance', [ParentStudentController::class, 'attendance'])->name('api.parent.students.attendance'); + Route::get('/students/{student}/points-history', [ParentStudentController::class, 'pointsHistory'])->name('api.parent.students.points-history'); // Notifikasi & perangkat Route::get('/notifications', [ParentNotificationController::class, 'index'])->name('api.parent.notifications');