fixed notification system and added a new tenant notifications receipt table to track read status and filter messages by scope.

This commit is contained in:
Codex Agent
2025-12-17 10:57:19 +01:00
parent 0aae494945
commit d64839ba2f
31 changed files with 1089 additions and 127 deletions

View File

@@ -2018,6 +2018,8 @@ class EventPublicController extends BaseController
[$event] = $result;
$guestIdentifier = $this->resolveNotificationIdentifier($request);
$limit = max(1, min(50, (int) $request->integer('limit', 35)));
$statusFilter = $request->string('status')->lower()->value();
$scopeFilter = $request->string('scope')->lower()->value();
if (! Schema::hasTable('guest_notifications')) {
return $this->emptyNotificationsResponse($request, $event->id, 'disabled');
@@ -2029,6 +2031,31 @@ class EventPublicController extends BaseController
->notExpired()
->visibleToGuest($guestIdentifier);
if ($statusFilter === 'unread') {
$baseQuery->where(function ($query) use ($guestIdentifier) {
$query->whereDoesntHave('receipts', fn ($receipt) => $receipt->where('guest_identifier', $guestIdentifier))
->orWhereHas('receipts', fn ($receipt) => $receipt
->where('guest_identifier', $guestIdentifier)
->where('status', GuestNotificationDeliveryStatus::NEW->value));
});
} elseif ($statusFilter === 'read') {
$baseQuery->whereHas('receipts', fn ($receipt) => $receipt
->where('guest_identifier', $guestIdentifier)
->where('status', GuestNotificationDeliveryStatus::READ->value));
} elseif ($statusFilter === 'dismissed') {
$baseQuery->whereHas('receipts', fn ($receipt) => $receipt
->where('guest_identifier', $guestIdentifier)
->where('status', GuestNotificationDeliveryStatus::DISMISSED->value));
}
if ($scopeFilter === 'uploads') {
$baseQuery->whereIn('type', [GuestNotificationType::UPLOAD_ALERT->value, GuestNotificationType::PHOTO_ACTIVITY->value]);
} elseif ($scopeFilter === 'tips') {
$baseQuery->whereIn('type', [GuestNotificationType::SUPPORT_TIP->value, GuestNotificationType::ACHIEVEMENT_MAJOR->value]);
} elseif ($scopeFilter === 'general') {
$baseQuery->whereIn('type', [GuestNotificationType::BROADCAST->value, GuestNotificationType::FEEDBACK_REQUEST->value]);
}
$notifications = (clone $baseQuery)
->with(['receipts' => fn ($query) => $query->where('guest_identifier', $guestIdentifier)])
->orderByDesc('priority')

View File

@@ -3,7 +3,10 @@
namespace App\Http\Controllers\Api\Tenant;
use App\Http\Controllers\Controller;
use App\Http\Requests\Tenant\NotificationMarkRequest;
use App\Models\TenantNotificationLog;
use App\Models\TenantNotificationReceipt;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
@@ -23,8 +26,11 @@ class NotificationLogController extends Controller
], 403);
}
$user = $request->user();
$query = TenantNotificationLog::query()
->where('tenant_id', $tenant->id)
->with('receipts')
->latest();
if ($type = $request->query('type')) {
@@ -35,19 +41,85 @@ class NotificationLogController extends Controller
$query->where('status', $status);
}
if ($scope = $request->query('scope')) {
$query->where(function (Builder $inner) use ($scope) {
$inner->where('type', $scope)
->orWhere(function (Builder $ctx) use ($scope) {
$ctx->whereJsonContains('context->scope', $scope);
});
});
}
if ($eventId = $request->query('event_id')) {
$query->where(function (Builder $inner) use ($eventId) {
$inner->where('context->event_id', (int) $eventId)
->orWhere('context->eventId', (int) $eventId);
});
}
$perPage = (int) $request->query('per_page', 20);
$perPage = max(1, min($perPage, 100));
$logs = $query->paginate($perPage);
$receipts = collect($logs->items())
->map(fn ($log) => $log->receipts ?? collect())
->flatten();
$unreadCount = $receipts
->filter(fn ($receipt) => $user && $receipt->user_id === $user->id && $receipt->status !== 'read')
->count();
$data = collect($logs->items())->map(function (TenantNotificationLog $log) use ($user) {
$receipt = $user
? $log->receipts->firstWhere('user_id', $user->id)
: null;
return array_merge($log->toArray(), [
'is_read' => $receipt ? $receipt->status === 'read' : false,
]);
})->all();
return response()->json([
'data' => $logs->items(),
'data' => $data,
'meta' => [
'current_page' => $logs->currentPage(),
'last_page' => $logs->lastPage(),
'per_page' => $logs->perPage(),
'total' => $logs->total(),
'unread_count' => $unreadCount,
],
]);
}
public function mark(NotificationMarkRequest $request): JsonResponse
{
$tenant = $request->attributes->get('tenant') ?? $request->user()?->tenant;
if (! $tenant) {
return response()->json([
'error' => [
'code' => 'tenant_context_missing',
'title' => 'Tenant context missing',
'message' => 'Unable to resolve tenant for notification logs.',
],
], 403);
}
$userId = $request->user()?->id;
$status = $request->validated('status');
$ids = $request->validated('ids');
TenantNotificationReceipt::query()
->where('tenant_id', $tenant->id)
->whereIn('notification_log_id', $ids)
->when($userId, fn ($q) => $q->where(function ($inner) use ($userId) {
$inner->whereNull('user_id')->orWhere('user_id', $userId);
}))
->update(['status' => $status]);
return response()->json([
'message' => 'Notifications updated.',
]);
}
}