feat: endpoint riwayat poin wali (events + monthly 6 bulan)

This commit is contained in:
Novy San87
2026-08-06 12:41:27 +07:00
parent 75bafa4997
commit e7c146a6bb
2 changed files with 55 additions and 0 deletions
@@ -4,6 +4,7 @@ namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Models\ParentStudent; use App\Models\ParentStudent;
use App\Models\PointAuditLog;
use App\Models\SpLetter; use App\Models\SpLetter;
use App\Models\Student; use App\Models\Student;
use App\Models\Violation; 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. * Tautkan anak tambahan (kakak/adik) ke akun wali.
*/ */
+1
View File
@@ -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}/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}/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}/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 // Notifikasi & perangkat
Route::get('/notifications', [ParentNotificationController::class, 'index'])->name('api.parent.notifications'); Route::get('/notifications', [ParentNotificationController::class, 'index'])->name('api.parent.notifications');