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:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user