Files
portal-api/app/Http/Controllers/Api/NotificationController.php
T
baodev0 520f21cb11 feat: add GeneratesUniqueSlug class for unique slug generation
feat: create PostCategoryFactory for generating post category data

feat: create PostFactory for generating post data

feat: add retry_requested_at column to services table

feat: add vcenter provisioning fields to products table

feat: add module_host_id to os table

feat: create email_notification_templates table for email templates

feat: add scenario and format fields to template table

feat: add admin fields to transactions table

fix: update ticket table columns and types

feat: add uuid to ticket table and backfill existing records

fix: make ticket_reply user_id nullable

feat: create notifications table for user notifications

fix: make ticket uuid nullable

feat: create post_categories table for blog categories

feat: create posts table for blog posts

feat: add invoice email template for PDF generation
2026-05-25 15:39:42 +07:00

79 lines
2.3 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);
$notifications = Notification::query()
->where('recipient_type', $recipientType)
->where('recipient_id', $recipient->id)
->orderByDesc('created_at')
->limit(20)
->get();
return response()->json([
'error' => false,
'data' => [
'items' => $notifications,
'unread_count' => $notifications->where('is_read', false)->count(),
],
]);
}
public function markRead(Request $request, string $recipientType, int $id): JsonResponse
{
$recipient = $this->resolveRecipient($recipientType);
$notification = Notification::query()
->where('recipient_type', $recipientType)
->where('recipient_id', $recipient->id)
->whereKey($id)
->first();
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 resolveRecipient(string $recipientType)
{
return match ($recipientType) {
'admin' => auth('admin')->user(),
'client' => auth('client')->user(),
default => abort(404),
};
}
}