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.',
]);
}
}

View File

@@ -0,0 +1,22 @@
<?php
namespace App\Http\Requests\Tenant;
use Illuminate\Foundation\Http\FormRequest;
class NotificationMarkRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
public function rules(): array
{
return [
'ids' => ['required', 'array'],
'ids.*' => ['integer', 'min:1'],
'status' => ['required', 'in:read,dismissed'],
];
}
}

View File

@@ -2,9 +2,12 @@
namespace App\Jobs\Concerns;
use App\Models\TenantNotificationLog;
use App\Models\TenantNotificationReceipt;
use App\Models\Tenant;
use App\Services\Packages\TenantNotificationLogger;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Carbon;
trait LogsTenantNotifications
{
@@ -18,6 +21,21 @@ trait LogsTenantNotifications
$this->notificationLogger()->log($tenant, $attributes);
}
protected function createNotificationReceipt(
Tenant $tenant,
TenantNotificationLog $log,
?int $userId,
?string $recipient
): TenantNotificationReceipt {
return TenantNotificationReceipt::query()->create([
'tenant_id' => $tenant->id,
'notification_log_id' => $log->id,
'user_id' => $userId,
'recipient' => $recipient,
'status' => 'delivered',
]);
}
protected function dispatchToRecipients(
Tenant $tenant,
iterable $recipients,
@@ -29,7 +47,7 @@ trait LogsTenantNotifications
try {
$callback($recipient);
$this->logNotification($tenant, [
$log = $this->notificationLogger()->log($tenant, [
'type' => $type,
'channel' => 'mail',
'recipient' => $recipient,
@@ -37,6 +55,13 @@ trait LogsTenantNotifications
'context' => $context,
'sent_at' => now(),
]);
$this->createNotificationReceipt(
$tenant,
$log,
$tenant->user?->id,
$recipient
);
} catch (\Throwable $e) {
Log::error('Tenant notification failed', [
'tenant_id' => $tenant->id,
@@ -57,4 +82,51 @@ trait LogsTenantNotifications
}
}
}
/**
* Simple idempotency guard to avoid duplicate notifications within a cooldown window.
*
* @param string[] $dedupeKeys
*/
protected function isDuplicateNotification(
Tenant $tenant,
string $type,
array $context,
array $dedupeKeys,
int $cooldownMinutes = 1440
): bool {
$window = Carbon::now()->subMinutes($cooldownMinutes);
$logs = TenantNotificationLog::query()
->where('tenant_id', $tenant->id)
->where('type', $type)
->whereIn('status', ['sent', 'queued'])
->where(function ($query) use ($window) {
$query->whereNull('created_at')
->orWhere('created_at', '>=', $window)
->orWhere('sent_at', '>=', $window);
})
->get();
foreach ($logs as $log) {
$existing = is_array($log->context) ? $log->context : [];
$matches = true;
foreach ($dedupeKeys as $key) {
$currentValue = $context[$key] ?? null;
$existingValue = $existing[$key] ?? null;
if ($currentValue != $existingValue) {
$matches = false;
break;
}
}
if ($matches) {
return true;
}
}
return false;
}
}

View File

@@ -41,11 +41,24 @@ class SendEventPackageGalleryExpired implements ShouldQueue
}
$preferences = app(\App\Services\Packages\TenantNotificationPreferences::class);
$context = $this->context($eventPackage);
if ($this->isDuplicateNotification($tenant, 'gallery_expired', $context, ['event_package_id'])) {
$this->logNotification($tenant, [
'type' => 'gallery_expired',
'status' => 'skipped',
'context' => array_merge($context, ['reason' => 'duplicate']),
]);
return;
}
if (! $preferences->shouldNotify($tenant, 'gallery_expired')) {
$this->logNotification($tenant, [
'type' => 'gallery_expired',
'status' => 'skipped',
'context' => $this->context($eventPackage),
'context' => $context,
]);
return;
@@ -65,14 +78,12 @@ class SendEventPackageGalleryExpired implements ShouldQueue
$this->logNotification($tenant, [
'type' => 'gallery_expired',
'status' => 'skipped',
'context' => array_merge($this->context($eventPackage), ['reason' => 'no_recipient']),
'context' => array_merge($context, ['reason' => 'no_recipient']),
]);
return;
}
$context = $this->context($eventPackage);
$this->dispatchToRecipients(
$tenant,
$emails,

View File

@@ -44,11 +44,24 @@ class SendEventPackageGalleryWarning implements ShouldQueue
}
$preferences = app(\App\Services\Packages\TenantNotificationPreferences::class);
$context = $this->context($eventPackage);
if ($this->isDuplicateNotification($tenant, 'gallery_warning', $context, ['event_package_id', 'days_remaining'])) {
$this->logNotification($tenant, [
'type' => 'gallery_warning',
'status' => 'skipped',
'context' => array_merge($context, ['reason' => 'duplicate']),
]);
return;
}
if (! $preferences->shouldNotify($tenant, 'gallery_warnings')) {
$this->logNotification($tenant, [
'type' => 'gallery_warning',
'status' => 'skipped',
'context' => $this->context($eventPackage),
'context' => $context,
]);
return;
@@ -69,14 +82,12 @@ class SendEventPackageGalleryWarning implements ShouldQueue
$this->logNotification($tenant, [
'type' => 'gallery_warning',
'status' => 'skipped',
'context' => array_merge($this->context($eventPackage), ['reason' => 'no_recipient']),
'context' => array_merge($context, ['reason' => 'no_recipient']),
]);
return;
}
$context = $this->context($eventPackage);
$this->dispatchToRecipients(
$tenant,
$emails,

View File

@@ -43,12 +43,24 @@ class SendEventPackageGuestLimitNotification implements ShouldQueue
return;
}
$context = $this->context($eventPackage);
if ($this->isDuplicateNotification($tenant, 'guest_limit', $context, ['event_package_id', 'limit'])) {
$this->logNotification($tenant, [
'type' => 'guest_limit',
'status' => 'skipped',
'context' => array_merge($context, ['reason' => 'duplicate']),
]);
return;
}
$preferences = app(\App\Services\Packages\TenantNotificationPreferences::class);
if (! $preferences->shouldNotify($tenant, 'guest_limits')) {
$this->logNotification($tenant, [
'type' => 'guest_limit',
'status' => 'skipped',
'context' => $this->context($eventPackage),
'context' => $context,
]);
return;
@@ -68,14 +80,12 @@ class SendEventPackageGuestLimitNotification implements ShouldQueue
$this->logNotification($tenant, [
'type' => 'guest_limit',
'status' => 'skipped',
'context' => array_merge($this->context($eventPackage), ['reason' => 'no_recipient']),
'context' => array_merge($context, ['reason' => 'no_recipient']),
]);
return;
}
$context = $this->context($eventPackage);
$this->dispatchToRecipients(
$tenant,
$emails,

View File

@@ -45,12 +45,24 @@ class SendEventPackageGuestThresholdWarning implements ShouldQueue
return;
}
$context = $this->context($eventPackage);
if ($this->isDuplicateNotification($tenant, 'guest_threshold', $context, ['event_package_id', 'threshold', 'limit'])) {
$this->logNotification($tenant, [
'type' => 'guest_threshold',
'status' => 'skipped',
'context' => array_merge($context, ['reason' => 'duplicate']),
]);
return;
}
$preferences = app(\App\Services\Packages\TenantNotificationPreferences::class);
if (! $preferences->shouldNotify($tenant, 'guest_thresholds')) {
$this->logNotification($tenant, [
'type' => 'guest_threshold',
'status' => 'skipped',
'context' => $this->context($eventPackage),
'context' => $context,
]);
return;
@@ -71,14 +83,12 @@ class SendEventPackageGuestThresholdWarning implements ShouldQueue
$this->logNotification($tenant, [
'type' => 'guest_threshold',
'status' => 'skipped',
'context' => array_merge($this->context($eventPackage), ['reason' => 'no_recipient']),
'context' => array_merge($context, ['reason' => 'no_recipient']),
]);
return;
}
$context = $this->context($eventPackage);
$this->dispatchToRecipients(
$tenant,
$emails,

View File

@@ -43,12 +43,24 @@ class SendEventPackagePhotoLimitNotification implements ShouldQueue
return;
}
$context = $this->context($eventPackage);
if ($this->isDuplicateNotification($tenant, 'photo_limit', $context, ['event_package_id', 'limit'])) {
$this->logNotification($tenant, [
'type' => 'photo_limit',
'status' => 'skipped',
'context' => array_merge($context, ['reason' => 'duplicate']),
]);
return;
}
$preferences = app(\App\Services\Packages\TenantNotificationPreferences::class);
if (! $preferences->shouldNotify($tenant, 'photo_limits')) {
$this->logNotification($tenant, [
'type' => 'photo_limit',
'status' => 'skipped',
'context' => $this->context($eventPackage),
'context' => $context,
]);
return;
@@ -69,14 +81,12 @@ class SendEventPackagePhotoLimitNotification implements ShouldQueue
$this->logNotification($tenant, [
'type' => 'photo_limit',
'status' => 'skipped',
'context' => array_merge($this->context($eventPackage), ['reason' => 'no_recipient']),
'context' => array_merge($context, ['reason' => 'no_recipient']),
]);
return;
}
$context = $this->context($eventPackage);
$this->dispatchToRecipients(
$tenant,
$emails,

View File

@@ -45,12 +45,24 @@ class SendEventPackagePhotoThresholdWarning implements ShouldQueue
return;
}
$context = $this->context($eventPackage);
if ($this->isDuplicateNotification($tenant, 'photo_threshold', $context, ['event_package_id', 'threshold', 'limit'])) {
$this->logNotification($tenant, [
'type' => 'photo_threshold',
'status' => 'skipped',
'context' => array_merge($context, ['reason' => 'duplicate']),
]);
return;
}
$preferences = app(\App\Services\Packages\TenantNotificationPreferences::class);
if (! $preferences->shouldNotify($tenant, 'photo_thresholds')) {
$this->logNotification($tenant, [
'type' => 'photo_threshold',
'status' => 'skipped',
'context' => $this->context($eventPackage),
'context' => $context,
]);
return;
@@ -71,14 +83,12 @@ class SendEventPackagePhotoThresholdWarning implements ShouldQueue
$this->logNotification($tenant, [
'type' => 'photo_threshold',
'status' => 'skipped',
'context' => array_merge($this->context($eventPackage), ['reason' => 'no_recipient']),
'context' => array_merge($context, ['reason' => 'no_recipient']),
]);
return;
}
$context = $this->context($eventPackage);
$this->dispatchToRecipients(
$tenant,
$emails,

View File

@@ -40,12 +40,24 @@ class SendTenantPackageEventLimitNotification implements ShouldQueue
$tenant = $tenantPackage->tenant;
$context = $this->context($tenantPackage);
if ($this->isDuplicateNotification($tenant, 'event_limit', $context, ['tenant_package_id', 'limit'])) {
$this->logNotification($tenant, [
'type' => 'event_limit',
'status' => 'skipped',
'context' => array_merge($context, ['reason' => 'duplicate']),
]);
return;
}
$preferences = app(\App\Services\Packages\TenantNotificationPreferences::class);
if (! $preferences->shouldNotify($tenant, 'event_limits')) {
$this->logNotification($tenant, [
'type' => 'event_limit',
'status' => 'skipped',
'context' => $this->context($tenantPackage),
'context' => $context,
]);
return;
@@ -65,14 +77,12 @@ class SendTenantPackageEventLimitNotification implements ShouldQueue
$this->logNotification($tenant, [
'type' => 'event_limit',
'status' => 'skipped',
'context' => array_merge($this->context($tenantPackage), ['reason' => 'no_recipient']),
'context' => array_merge($context, ['reason' => 'no_recipient']),
]);
return;
}
$context = $this->context($tenantPackage);
$this->dispatchToRecipients(
$tenant,
$emails,

View File

@@ -42,12 +42,24 @@ class SendTenantPackageEventThresholdWarning implements ShouldQueue
$tenant = $tenantPackage->tenant;
$context = $this->context($tenantPackage);
if ($this->isDuplicateNotification($tenant, 'event_threshold', $context, ['tenant_package_id', 'threshold', 'limit'])) {
$this->logNotification($tenant, [
'type' => 'event_threshold',
'status' => 'skipped',
'context' => array_merge($context, ['reason' => 'duplicate']),
]);
return;
}
$preferences = app(\App\Services\Packages\TenantNotificationPreferences::class);
if (! $preferences->shouldNotify($tenant, 'event_thresholds')) {
$this->logNotification($tenant, [
'type' => 'event_threshold',
'status' => 'skipped',
'context' => $this->context($tenantPackage),
'context' => $context,
]);
return;
@@ -68,14 +80,12 @@ class SendTenantPackageEventThresholdWarning implements ShouldQueue
$this->logNotification($tenant, [
'type' => 'event_threshold',
'status' => 'skipped',
'context' => array_merge($this->context($tenantPackage), ['reason' => 'no_recipient']),
'context' => array_merge($context, ['reason' => 'no_recipient']),
]);
return;
}
$context = $this->context($tenantPackage);
$this->dispatchToRecipients(
$tenant,
$emails,

View File

@@ -37,15 +37,26 @@ class SendTenantPackageExpiredNotification implements ShouldQueue
$tenant = $tenantPackage->tenant;
$context = [
'tenant_package_id' => $tenantPackage->id,
];
if ($this->isDuplicateNotification($tenant, 'package_expired', $context, ['tenant_package_id'])) {
$this->logNotification($tenant, [
'type' => 'package_expired',
'status' => 'skipped',
'context' => array_merge($context, ['reason' => 'duplicate']),
]);
return;
}
$preferences = app(\App\Services\Packages\TenantNotificationPreferences::class);
if (! $preferences->shouldNotify($tenant, 'package_expired')) {
$this->logNotification($tenant, [
'type' => 'package_expired',
'status' => 'skipped',
'context' => [
'tenant_package_id' => $tenantPackage->id,
'reason' => 'opt_out',
],
'context' => array_merge($context, ['reason' => 'opt_out']),
]);
return;
@@ -65,19 +76,12 @@ class SendTenantPackageExpiredNotification implements ShouldQueue
$this->logNotification($tenant, [
'type' => 'package_expired',
'status' => 'skipped',
'context' => [
'tenant_package_id' => $tenantPackage->id,
'reason' => 'no_recipient',
],
'context' => array_merge($context, ['reason' => 'no_recipient']),
]);
return;
}
$context = [
'tenant_package_id' => $tenantPackage->id,
];
$this->dispatchToRecipients(
$tenant,
$emails,

View File

@@ -40,16 +40,27 @@ class SendTenantPackageExpiringNotification implements ShouldQueue
$tenant = $tenantPackage->tenant;
$context = [
'tenant_package_id' => $tenantPackage->id,
'days_remaining' => $this->daysRemaining,
];
if ($this->isDuplicateNotification($tenant, 'package_expiring', $context, ['tenant_package_id', 'days_remaining'])) {
$this->logNotification($tenant, [
'type' => 'package_expiring',
'status' => 'skipped',
'context' => array_merge($context, ['reason' => 'duplicate']),
]);
return;
}
$preferences = app(\App\Services\Packages\TenantNotificationPreferences::class);
if (! $preferences->shouldNotify($tenant, 'package_expiring')) {
$this->logNotification($tenant, [
'type' => 'package_expiring',
'status' => 'skipped',
'context' => [
'tenant_package_id' => $tenantPackage->id,
'days_remaining' => $this->daysRemaining,
'reason' => 'opt_out',
],
'context' => array_merge($context, ['reason' => 'opt_out']),
]);
return;
@@ -70,21 +81,12 @@ class SendTenantPackageExpiringNotification implements ShouldQueue
$this->logNotification($tenant, [
'type' => 'package_expiring',
'status' => 'skipped',
'context' => [
'tenant_package_id' => $tenantPackage->id,
'days_remaining' => $this->daysRemaining,
'reason' => 'no_recipient',
],
'context' => array_merge($context, ['reason' => 'no_recipient']),
]);
return;
}
$context = [
'tenant_package_id' => $tenantPackage->id,
'days_remaining' => $this->daysRemaining,
];
$this->dispatchToRecipients(
$tenant,
$emails,

View File

@@ -28,4 +28,9 @@ class TenantNotificationLog extends Model
{
return $this->belongsTo(Tenant::class);
}
public function receipts()
{
return $this->hasMany(TenantNotificationReceipt::class, 'notification_log_id');
}
}

View File

@@ -0,0 +1,36 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class TenantNotificationReceipt extends Model
{
protected $fillable = [
'tenant_id',
'notification_log_id',
'user_id',
'recipient',
'status',
];
protected $casts = [
'created_at' => 'datetime',
'updated_at' => 'datetime',
];
public function tenant()
{
return $this->belongsTo(Tenant::class);
}
public function notificationLog()
{
return $this->belongsTo(TenantNotificationLog::class, 'notification_log_id');
}
public function user()
{
return $this->belongsTo(User::class);
}
}