feat: audit log poin — catat pencatatan/penghapusan pelanggaran (sebelum/sesudah/delta, metadata snapshot, actor, IP) + halaman Riwayat Poin (filter, permission view-point-audit) + 4 feature test

This commit is contained in:
Sari
2026-08-03 09:12:46 +07:00
parent 6f430f35d7
commit 2dda08bb8d
11 changed files with 576 additions and 0 deletions
@@ -0,0 +1,39 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\PointAuditLog;
use Illuminate\Http\Request;
use Illuminate\View\View;
class PointAuditController extends Controller
{
public function index(Request $request): View
{
$query = PointAuditLog::with(['student', 'actor']);
// Wali kelas murni: hanya siswa di kelasnya
if (auth()->user()->isScopedWaliKelas()) {
$query->whereHas('student', fn ($q) => $q->whereIn('class_id', auth()->user()->homeroomClassIds()));
}
$query->when($request->filled('search'), fn ($q) => $q->whereHas('student', fn ($qq) => $qq
->where('full_name', 'like', "%{$request->search}%")
->orWhere('nisn', 'like', "%{$request->search}%")));
$query->when($request->filled('action'), fn ($q) => $q->where('action', $request->action));
$query->when($request->filled('date_from'), fn ($q) => $q->whereDate('created_at', '>=', $request->date_from));
$query->when($request->filled('date_to'), fn ($q) => $q->whereDate('created_at', '<=', $request->date_to));
$logs = $query->orderByDesc('created_at')->paginate(25)->withQueryString();
$summary = [
'total' => PointAuditLog::count(),
'penambahan' => PointAuditLog::where('points_delta', '>', 0)->count(),
'pengurangan' => PointAuditLog::where('points_delta', '<', 0)->count(),
];
return view('point-audit.index', compact('logs', 'summary'));
}
}
@@ -272,6 +272,31 @@ class ViolationController extends Controller
public function destroy(Violation $violation): RedirectResponse
{
abort_unless(auth()->user()->canViewStudent($violation->student_id), 403);
// Audit log perubahan poin (penghapusan pelanggaran)
$student = $violation->student;
$before = $student->total_points; // termasuk pelanggaran yang akan dihapus
$after = max(0, $before - $violation->points);
\App\Models\PointAuditLog::log(
studentId: $violation->student_id,
action: \App\Models\PointAuditLog::ACTION_DELETED,
pointsBefore: $before,
pointsAfter: $after,
description: ($violation->violationType?->name ?? 'Pelanggaran').' (dihapus)',
metadata: [
'violation_date' => $violation->violation_date?->format('Y-m-d'),
'description' => $violation->description,
'sanction' => $violation->sanction,
'location' => $violation->location,
'points' => $violation->points,
],
violationId: $violation->id,
actorId: auth()->id(),
ipAddress: request()->ip(),
);
$violation->delete();
return redirect()->route('violations.index')
+88
View File
@@ -0,0 +1,88 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class PointAuditLog extends Model
{
/** @use HasFactory<\Database\Factories\PointAuditLogFactory> */
use HasFactory;
public const ACTION_CREATED = 'created';
public const ACTION_DELETED = 'deleted';
public const ACTION_ADJUSTED = 'adjusted';
protected $fillable = [
'student_id', 'violation_id', 'action',
'points_before', 'points_after', 'points_delta',
'description', 'metadata', 'actor_id', 'ip_address',
];
protected function casts(): array
{
return [
'points_before' => 'integer',
'points_after' => 'integer',
'points_delta' => 'integer',
'metadata' => 'array',
];
}
public function student(): BelongsTo
{
return $this->belongsTo(Student::class);
}
public function violation(): BelongsTo
{
return $this->belongsTo(Violation::class);
}
public function actor(): BelongsTo
{
return $this->belongsTo(User::class, 'actor_id');
}
public function getActionLabelAttribute(): string
{
return match ($this->action) {
self::ACTION_CREATED => 'Pencatatan',
self::ACTION_DELETED => 'Penghapusan',
self::ACTION_ADJUSTED => 'Penyesuaian',
default => ucfirst($this->action),
};
}
/**
* Catat log audit perubahan poin.
*
* @param array<string, mixed> $metadata
*/
public static function log(
int $studentId,
string $action,
int $pointsBefore,
int $pointsAfter,
?string $description = null,
?array $metadata = null,
?int $violationId = null,
?int $actorId = null,
?string $ipAddress = null,
): self {
return static::create([
'student_id' => $studentId,
'violation_id' => $violationId,
'action' => $action,
'points_before' => $pointsBefore,
'points_after' => $pointsAfter,
'points_delta' => $pointsAfter - $pointsBefore,
'description' => $description,
'metadata' => $metadata,
'actor_id' => $actorId,
'ip_address' => $ipAddress,
]);
}
}
+20
View File
@@ -3,6 +3,7 @@
namespace App\Services;
use App\Events\ViolationRecorded;
use App\Models\PointAuditLog;
use App\Models\SpLetter;
use App\Models\SpThreshold;
use App\Models\Student;
@@ -45,6 +46,25 @@ class ViolationService
}
}
// Audit log perubahan poin (pencatatan pelanggaran)
$after = $violation->student->total_points;
PointAuditLog::log(
studentId: $violation->student_id,
action: PointAuditLog::ACTION_CREATED,
pointsBefore: $after - $violation->points,
pointsAfter: $after,
description: $violation->violationType?->name ?? 'Pelanggaran',
metadata: [
'violation_date' => $violation->violation_date?->format('Y-m-d'),
'description' => $violation->description,
'sanction' => $violation->sanction,
'location' => $violation->location,
],
violationId: $violation->id,
actorId: $userId,
ipAddress: request()->ip(),
);
return $violation;
});