Files
portal-api/app/Http/Controllers/Api/NotificationController.php
baodev0andClaude Opus 5.5 bf78f3b810 feat: hồ sơ khách (ngày sinh, tỉnh/phường theo mã), gộp menu + nút tắt, thông báo theo uuid
- PUT v1/client/update: gender theo enum ClientGender (0 Nam/1 Nữ), thêm
  birthday (cột mới), province_code/ward_code validate theo bảng provinces/wards
  (phường phải thuộc tỉnh). Migration nạp dữ liệu hành chính 2025 từ
  province_ward_import.sql (chỉ bảng trống), API GET v1/provinces[/{code}/wards].
- CustomerObserver/OrderObserver log giá trị cũ bằng getRawOriginal: getOriginal
  trả enum object, nối chuỗi gây 500 khi khách đổi giới tính.
- ClientResource: các cờ trả boolean, CCCD chỉ trả cccd_updated (không lộ ảnh/số).
- GET v1/setting/menu trả cả items + quick_links, bỏ v1/setting/quick-links.
- Thông báo admin/khách tra theo uuid thay vì id tự tăng.
- Chặn gửi template marketing cho khách đã tắt nhận email marketing.
- Xoá ProvinceWardSeeder và Customer::$updatable không dùng.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-13 16:03:18 +07:00

122 lines
3.8 KiB
PHP

<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\Notification;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class NotificationController extends Controller
{
public function index(Request $request, string $recipientType): JsonResponse
{
$recipient = $this->resolveRecipient($recipientType);
$query = Notification::query()
->where('recipient_type', $recipientType)
->where('recipient_id', $recipient->id);
$unreadCount = (clone $query)->where('is_read', false)->count();
if ($request->filled('is_read')) {
$query->where('is_read', $request->boolean('is_read'));
}
if ($request->filled('search')) {
$search = $request->string('search');
$query->where(function ($q) use ($search) {
$q->where('title', 'like', "%{$search}%")
->orWhere('body', 'like', "%{$search}%");
});
}
$query->orderByDesc('created_at');
if ($request->filled('page') || $request->filled('per_page')) {
$perPage = (int) $request->integer('per_page', 15);
$paginator = $query->paginate($perPage, ['*'], 'page', (int) $request->integer('page', 1));
return response()->json([
'error' => false,
'data' => [
'items' => $paginator->items(),
'total' => $paginator->total(),
'unread_count' => $unreadCount,
],
]);
}
$notifications = $query->limit(20)->get();
return response()->json([
'error' => false,
'data' => [
'items' => $notifications,
'unread_count' => $unreadCount,
],
]);
}
public function show(Request $request, string $recipientType, string $uuid): JsonResponse
{
$notification = $this->findOwned($recipientType, $uuid);
if (!$notification) {
return response()->json(['error' => true, 'message' => 'Notification not found'], 404);
}
return response()->json(['error' => false, 'data' => $notification]);
}
public function markRead(Request $request, string $recipientType, string $uuid): JsonResponse
{
$notification = $this->findOwned($recipientType, $uuid);
if (!$notification) {
return response()->json(['error' => true, 'message' => 'Notification not found'], 404);
}
$notification->update([
'is_read' => true,
'read_at' => now(),
]);
return response()->json(['error' => false, 'data' => $notification]);
}
public function markAllRead(Request $request, string $recipientType): JsonResponse
{
$recipient = $this->resolveRecipient($recipientType);
Notification::query()
->where('recipient_type', $recipientType)
->where('recipient_id', $recipient->id)
->where('is_read', false)
->update([
'is_read' => true,
'read_at' => now(),
]);
return response()->json(['error' => false, 'message' => 'Marked as read']);
}
private function findOwned(string $recipientType, string $uuid): ?Notification
{
return Notification::query()
->where('recipient_type', $recipientType)
->where('recipient_id', $this->resolveRecipient($recipientType)->id)
->where('uuid', $uuid)
->first();
}
private function resolveRecipient(string $recipientType)
{
return match ($recipientType) {
'admin' => auth('admin')->user(),
'client' => auth('client')->user(),
default => abort(404),
};
}
}