PM_Console/backend/app/Http/Controllers/Api/NotificationController.php

169 خطوط
6.1 KiB
PHP

<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Http\Resources\NotificationResource;
use App\Models\Notification;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class NotificationController extends Controller
{
public function index(Request $request): JsonResponse
{
try {
$query = $request->user()->notifications();
$archived = $request->boolean('archived');
$query->when($archived, fn ($q) => $q->whereNotNull('archived_at'))
->when(! $archived, fn ($q) => $q->whereNull('archived_at'));
if ($request->boolean('unread')) {
$query->whereNull('read_at');
}
$perPage = $request->input('per_page', 15);
$notifications = $query->orderBy('created_at', 'desc')->paginate($perPage);
return response()->json([
'success' => true,
'data' => NotificationResource::collection($notifications),
'meta' => [
'current_page' => $notifications->currentPage(),
'last_page' => $notifications->lastPage(),
'per_page' => $notifications->perPage(),
'total' => $notifications->total(),
'unread_count' => $request->user()->notifications()->whereNull('archived_at')->whereNull('read_at')->count(),
'archived_count' => $request->user()->notifications()->whereNotNull('archived_at')->count(),
],
'message' => 'لیست اعلان‌ها',
]);
} catch (\Exception $e) {
return response()->json([
'success' => false,
'message' => 'خطا در دریافت اعلان‌ها',
], 500);
}
}
public function markAsRead(Notification $notification): JsonResponse
{
try {
if ($notification->user_id !== request()->user()->id) {
return response()->json([
'success' => false,
'message' => 'شما اجازه دسترسی به این اعلان را ندارید',
], 403);
}
$notification->update(['read_at' => Carbon::now(), 'is_read' => true]);
return response()->json([
'success' => true,
'data' => new NotificationResource($notification->fresh()),
'message' => 'اعلان به عنوان خوانده شده علامت خورد',
]);
} catch (\Exception $e) {
return response()->json([
'success' => false,
'message' => 'خطا در به‌روزرسانی اعلان',
], 500);
}
}
public function markAllAsRead(Request $request): JsonResponse
{
try {
$request->user()->notifications()->whereNull('read_at')->update([
'read_at' => Carbon::now(),
'is_read' => true,
]);
return response()->json([
'success' => true,
'message' => 'همه اعلان‌ها به عنوان خوانده شده علامت خوردند',
]);
} catch (\Exception $e) {
return response()->json([
'success' => false,
'message' => 'خطا در به‌روزرسانی اعلان‌ها',
], 500);
}
}
public function destroy(Request $request, Notification $notification): JsonResponse
{
try {
if ($notification->user_id !== $request->user()->id) {
return response()->json([
'success' => false,
'message' => 'شما اجازه حذف این اعلان را ندارید',
], 403);
}
$notification->delete();
return response()->json([
'success' => true,
'message' => 'اعلان حذف شد',
]);
} catch (\Exception $e) {
return response()->json([
'success' => false,
'message' => 'خطا در حذف اعلان',
], 500);
}
}
public function archive(Request $request, Notification $notification): JsonResponse
{
if ($notification->user_id !== $request->user()->id) {
return response()->json(['success' => false, 'message' => 'شما اجازه آرشیو این اعلان را ندارید'], 403);
}
$notification->update([
'archived_at' => now(),
'read_at' => $notification->read_at ?? now(),
'is_read' => true,
]);
return response()->json([
'success' => true,
'data' => new NotificationResource($notification->fresh()),
'message' => 'اعلان آرشیو شد',
]);
}
public function restore(Request $request, Notification $notification): JsonResponse
{
if ($notification->user_id !== $request->user()->id) {
return response()->json(['success' => false, 'message' => 'شما اجازه بازیابی این اعلان را ندارید'], 403);
}
$notification->update(['archived_at' => null]);
return response()->json([
'success' => true,
'data' => new NotificationResource($notification->fresh()),
'message' => 'اعلان از آرشیو خارج شد',
]);
}
public function preview(Request $request, Notification $notification): JsonResponse
{
if ($notification->user_id !== $request->user()->id) {
return response()->json(['success' => false, 'message' => 'شما اجازه مشاهده این اعلان را ندارید'], 403);
}
if (! $notification->read_at) {
$notification->update(['read_at' => now(), 'is_read' => true]);
}
return response()->json([
'success' => true,
'data' => new NotificationResource($notification->fresh()),
'message' => 'جزئیات اعلان',
]);
}
}