chore: snapshot notifications hardening and css/docs alignment

This commit is contained in:
2026-03-20 00:05:03 +01:00
parent fb6e30a062
commit 60c27e50cb
41 changed files with 1862 additions and 254 deletions

View File

@@ -0,0 +1,90 @@
<?php
namespace MintyPHP\Module\Notifications\Listeners;
use MintyPHP\App\Module\Contracts\EventListener;
use MintyPHP\Module\Notifications\Service\NotificationService;
use MintyPHP\Repository\Tenant\UserTenantRepositoryInterface;
use MintyPHP\Repository\User\UserReadRepositoryInterface;
use MintyPHP\Service\Access\PermissionService;
final class UserActivatedNotificationListener implements EventListener
{
public function __construct(
private readonly NotificationService $notificationService,
private readonly UserReadRepositoryInterface $userReadRepository,
private readonly UserTenantRepositoryInterface $userTenantRepository
) {
}
public function handle(string $event, array $payload): void
{
$userId = (int) ($payload['user_id'] ?? 0);
if ($userId <= 0) {
return;
}
$user = $this->userReadRepository->find($userId);
$displayName = trim((string) ($payload['display_name'] ?? ''));
if ($displayName === '' && is_array($user)) {
$displayName = trim((string) ($user['display_name'] ?? ''));
if ($displayName === '') {
$displayName = trim((string) (($user['first_name'] ?? '') . ' ' . ($user['last_name'] ?? '')));
}
}
if ($displayName === '') {
$displayName = '#' . $userId;
}
$uuid = trim((string) ($payload['uuid'] ?? ''));
if ($uuid === '' && is_array($user)) {
$uuid = trim((string) ($user['uuid'] ?? ''));
}
$tenantIds = $this->sanitizeTenantIds($payload['tenant_ids'] ?? []);
if ($tenantIds === []) {
$tenantIds = $this->userTenantRepository->listTenantIdsByUserId($userId);
}
if ($tenantIds === []) {
return;
}
$adminUserIds = $this->userReadRepository->listPrivilegedUserIdsByPermissionKeys(
[PermissionService::USERS_UPDATE]
);
if ($adminUserIds === []) {
return;
}
$adminSet = array_fill_keys(array_map('intval', $adminUserIds), true);
$actorUserId = isset($payload['actor_user_id']) ? (int) $payload['actor_user_id'] : null;
$title = sprintf(t('User activated: %s'), $displayName);
$link = $uuid !== '' ? 'admin/users/edit/' . $uuid : null;
foreach ($tenantIds as $tenantId) {
$this->notificationService->createForTenantAdminUsers(
$tenantId,
'user.activated',
$title,
null,
$link,
['user_id' => $userId, 'uuid' => $uuid],
$actorUserId,
$adminSet
);
}
}
/**
* @return list<int>
*/
private function sanitizeTenantIds(mixed $tenantIds): array
{
if (!is_array($tenantIds)) {
return [];
}
$ids = array_values(array_unique(array_map('intval', $tenantIds)));
return array_values(array_filter($ids, static fn (int $tenantId): bool => $tenantId > 0));
}
}

View File

@@ -0,0 +1,104 @@
<?php
namespace MintyPHP\Module\Notifications\Listeners;
use MintyPHP\App\Module\Contracts\EventListener;
use MintyPHP\Module\Notifications\Service\NotificationService;
use MintyPHP\Repository\User\UserReadRepositoryInterface;
use MintyPHP\Service\Access\PermissionService;
final class UserAssignmentChangedNotificationListener implements EventListener
{
public function __construct(
private readonly NotificationService $notificationService,
private readonly UserReadRepositoryInterface $userReadRepository
) {
}
public function handle(string $event, array $payload): void
{
$userId = (int) ($payload['user_id'] ?? 0);
if ($userId <= 0) {
return;
}
$actorUserId = isset($payload['actor_user_id']) ? (int) $payload['actor_user_id'] : null;
$user = $this->userReadRepository->find($userId);
$displayName = trim((string) ($payload['display_name'] ?? ''));
if ($displayName === '' && is_array($user)) {
$displayName = trim((string) ($user['display_name'] ?? ''));
if ($displayName === '') {
$displayName = trim((string) (($user['first_name'] ?? '') . ' ' . ($user['last_name'] ?? '')));
}
}
if ($displayName === '') {
$displayName = '#' . $userId;
}
$uuid = trim((string) ($payload['uuid'] ?? ''));
if ($uuid === '' && is_array($user)) {
$uuid = trim((string) ($user['uuid'] ?? ''));
}
$tenantIds = $this->sanitizeTenantIds($payload['tenant_ids'] ?? []);
$changes = is_array($payload['changes'] ?? null) ? $payload['changes'] : [];
$title = sprintf(t('User assignments updated: %s'), $displayName);
$body = t('Tenant, role, or department assignments were changed.');
$link = $uuid !== '' ? 'admin/users/edit/' . $uuid : null;
$data = [
'user_id' => $userId,
'uuid' => $uuid,
'changes' => $changes,
];
if ($actorUserId === null || $actorUserId !== $userId) {
$this->notificationService->createForUser(
$userId,
null,
'user.assignment_changed',
$title,
$body,
$link,
$data
);
}
if ($tenantIds === []) {
return;
}
$adminUserIds = $this->userReadRepository->listPrivilegedUserIdsByPermissionKeys(
[PermissionService::USERS_UPDATE_ASSIGNMENTS]
);
if ($adminUserIds === []) {
return;
}
$adminSet = array_fill_keys(array_map('intval', $adminUserIds), true);
foreach ($tenantIds as $tenantId) {
$this->notificationService->createForTenantAdminUsers(
$tenantId,
'user.assignment_changed',
$title,
$body,
$link,
$data,
$actorUserId,
$adminSet
);
}
}
/**
* @return list<int>
*/
private function sanitizeTenantIds(mixed $tenantIds): array
{
if (!is_array($tenantIds)) {
return [];
}
$ids = array_values(array_unique(array_map('intval', $tenantIds)));
return array_values(array_filter($ids, static fn (int $tenantId): bool => $tenantId > 0));
}
}

View File

@@ -0,0 +1,90 @@
<?php
namespace MintyPHP\Module\Notifications\Listeners;
use MintyPHP\App\Module\Contracts\EventListener;
use MintyPHP\Module\Notifications\Service\NotificationService;
use MintyPHP\Repository\Tenant\UserTenantRepositoryInterface;
use MintyPHP\Repository\User\UserReadRepositoryInterface;
use MintyPHP\Service\Access\PermissionService;
final class UserDeactivatedNotificationListener implements EventListener
{
public function __construct(
private readonly NotificationService $notificationService,
private readonly UserReadRepositoryInterface $userReadRepository,
private readonly UserTenantRepositoryInterface $userTenantRepository
) {
}
public function handle(string $event, array $payload): void
{
$userId = (int) ($payload['user_id'] ?? 0);
if ($userId <= 0) {
return;
}
$user = $this->userReadRepository->find($userId);
$displayName = trim((string) ($payload['display_name'] ?? ''));
if ($displayName === '' && is_array($user)) {
$displayName = trim((string) ($user['display_name'] ?? ''));
if ($displayName === '') {
$displayName = trim((string) (($user['first_name'] ?? '') . ' ' . ($user['last_name'] ?? '')));
}
}
if ($displayName === '') {
$displayName = '#' . $userId;
}
$uuid = trim((string) ($payload['uuid'] ?? ''));
if ($uuid === '' && is_array($user)) {
$uuid = trim((string) ($user['uuid'] ?? ''));
}
$tenantIds = $this->sanitizeTenantIds($payload['tenant_ids'] ?? []);
if ($tenantIds === []) {
$tenantIds = $this->userTenantRepository->listTenantIdsByUserId($userId);
}
if ($tenantIds === []) {
return;
}
$adminUserIds = $this->userReadRepository->listPrivilegedUserIdsByPermissionKeys(
[PermissionService::USERS_UPDATE]
);
if ($adminUserIds === []) {
return;
}
$adminSet = array_fill_keys(array_map('intval', $adminUserIds), true);
$actorUserId = isset($payload['actor_user_id']) ? (int) $payload['actor_user_id'] : null;
$title = sprintf(t('User deactivated: %s'), $displayName);
$link = $uuid !== '' ? 'admin/users/edit/' . $uuid : null;
foreach ($tenantIds as $tenantId) {
$this->notificationService->createForTenantAdminUsers(
$tenantId,
'user.deactivated',
$title,
null,
$link,
['user_id' => $userId, 'uuid' => $uuid],
$actorUserId,
$adminSet
);
}
}
/**
* @return list<int>
*/
private function sanitizeTenantIds(mixed $tenantIds): array
{
if (!is_array($tenantIds)) {
return [];
}
$ids = array_values(array_unique(array_map('intval', $tenantIds)));
return array_values(array_filter($ids, static fn (int $tenantId): bool => $tenantId > 0));
}
}

View File

@@ -4,12 +4,18 @@ namespace MintyPHP\Module\Notifications;
use MintyPHP\Service\Access\AuthorizationDecision;
use MintyPHP\Service\Access\AuthorizationPolicyInterface;
use MintyPHP\Service\Access\PermissionService;
final class NotificationsAuthorizationPolicy implements AuthorizationPolicyInterface
{
public const ABILITY_VIEW = 'notifications.view';
public const PERMISSION_KEY = 'notifications.view';
public function __construct(
private readonly PermissionService $permissionService
) {
}
public function supports(string $ability): bool
{
return $ability === self::ABILITY_VIEW;
@@ -22,6 +28,10 @@ final class NotificationsAuthorizationPolicy implements AuthorizationPolicyInter
return AuthorizationDecision::deny(403, 'forbidden');
}
if (!$this->permissionService->userHas($actorUserId, self::PERMISSION_KEY)) {
return AuthorizationDecision::deny(403, 'forbidden');
}
return AuthorizationDecision::allow();
}
}

View File

@@ -5,13 +5,16 @@ namespace MintyPHP\Module\Notifications;
use MintyPHP\App\AppContainer;
use MintyPHP\App\Container\ContainerRegistrar;
use MintyPHP\Module\Notifications\Handler\NotificationCleanupJobHandler;
use MintyPHP\Module\Notifications\Listeners\UserActivatedNotificationListener;
use MintyPHP\Module\Notifications\Listeners\UserAssignmentChangedNotificationListener;
use MintyPHP\Module\Notifications\Listeners\UserCreatedNotificationListener;
use MintyPHP\Module\Notifications\Listeners\UserDeactivatedNotificationListener;
use MintyPHP\Module\Notifications\Listeners\UserDeletedNotificationListener;
use MintyPHP\Module\Notifications\Service\NotificationRepositoryFactory;
use MintyPHP\Module\Notifications\Service\NotificationService;
use MintyPHP\Module\Notifications\Service\NotificationServicesFactory;
use MintyPHP\Repository\Tenant\UserTenantRepositoryInterface;
use MintyPHP\Repository\User\UserReadRepositoryInterface;
use MintyPHP\Repository\Tenant\UserTenantRepository;
use MintyPHP\Repository\User\UserReadRepository;
final class NotificationsContainerRegistrar implements ContainerRegistrar
{
@@ -23,7 +26,9 @@ final class NotificationsContainerRegistrar implements ContainerRegistrar
$c->get(NotificationRepositoryFactory::class)
));
$container->set(NotificationService::class, static fn (AppContainer $c): NotificationService => $c->get(NotificationServicesFactory::class)->createNotificationService());
$container->set(NotificationService::class, static fn (AppContainer $c): NotificationService => $c->get(NotificationServicesFactory::class)->createNotificationService(
$c->get(UserTenantRepository::class)
));
$container->set(NotificationCleanupJobHandler::class, static fn (AppContainer $c): NotificationCleanupJobHandler => new NotificationCleanupJobHandler(
$c->get(NotificationService::class)
@@ -31,13 +36,30 @@ final class NotificationsContainerRegistrar implements ContainerRegistrar
$container->set(UserCreatedNotificationListener::class, static fn (AppContainer $c): UserCreatedNotificationListener => new UserCreatedNotificationListener(
$c->get(NotificationService::class),
$c->get(UserReadRepositoryInterface::class),
$c->get(UserTenantRepositoryInterface::class)
$c->get(UserReadRepository::class),
$c->get(UserTenantRepository::class)
));
$container->set(UserDeletedNotificationListener::class, static fn (AppContainer $c): UserDeletedNotificationListener => new UserDeletedNotificationListener(
$c->get(NotificationService::class),
$c->get(UserReadRepositoryInterface::class)
$c->get(UserReadRepository::class)
));
$container->set(UserActivatedNotificationListener::class, static fn (AppContainer $c): UserActivatedNotificationListener => new UserActivatedNotificationListener(
$c->get(NotificationService::class),
$c->get(UserReadRepository::class),
$c->get(UserTenantRepository::class)
));
$container->set(UserDeactivatedNotificationListener::class, static fn (AppContainer $c): UserDeactivatedNotificationListener => new UserDeactivatedNotificationListener(
$c->get(NotificationService::class),
$c->get(UserReadRepository::class),
$c->get(UserTenantRepository::class)
));
$container->set(UserAssignmentChangedNotificationListener::class, static fn (AppContainer $c): UserAssignmentChangedNotificationListener => new UserAssignmentChangedNotificationListener(
$c->get(NotificationService::class),
$c->get(UserReadRepository::class)
));
}
}

View File

@@ -25,7 +25,9 @@ final class NotificationsSessionProvider implements SessionProvider
return;
}
$sessionStore->set(self::SESSION_KEY, $service->unreadCount($userId));
$currentTenant = $sessionStore->get('current_tenant', []);
$tenantId = is_array($currentTenant) ? (int) ($currentTenant['id'] ?? 0) : 0;
$sessionStore->set(self::SESSION_KEY, $service->unreadCount($userId, $tenantId > 0 ? $tenantId : null));
}
public function clear(): void

View File

@@ -7,6 +7,8 @@ use MintyPHP\Repository\Support\RepositoryArrayHelper;
class NotificationRepository implements NotificationRepositoryInterface
{
private const DEDUPE_WINDOW_SECONDS = 1800;
private function unwrapList(mixed $rows): array
{
return RepositoryArrayHelper::unwrapList($rows, 'user_notifications');
@@ -14,21 +16,29 @@ class NotificationRepository implements NotificationRepositoryInterface
public function create(array $data): int|false
{
$dedupeFingerprint = trim((string) ($data['dedupe_fingerprint'] ?? ''));
$dedupeBucket = (int) ($data['dedupe_bucket'] ?? 0);
$hasDedupe = $dedupeFingerprint !== '' && $dedupeBucket > 0;
$result = DB::insert(
'insert into user_notifications (recipient_user_id, tenant_id, type, title, body, link, data, created) values (?, ?, ?, ?, ?, ?, ?, NOW())',
($hasDedupe ? 'insert ignore' : 'insert') .
' into user_notifications (recipient_user_id, tenant_id, type, title, body, link, data, dedupe_fingerprint, dedupe_bucket, dedupe_until, created) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW())',
(string) ($data['recipient_user_id'] ?? 0),
$data['tenant_id'] !== null ? (string) $data['tenant_id'] : null,
(string) ($data['type'] ?? ''),
(string) ($data['title'] ?? ''),
$data['body'] ?? null,
$data['link'] ?? null,
isset($data['data']) ? json_encode($data['data'], JSON_THROW_ON_ERROR) : null
isset($data['data']) ? json_encode($data['data'], JSON_THROW_ON_ERROR) : null,
$hasDedupe ? $dedupeFingerprint : null,
$hasDedupe ? (string) $dedupeBucket : null,
$hasDedupe ? gmdate('Y-m-d H:i:s', time() + self::DEDUPE_WINDOW_SECONDS) : null
);
return is_int($result) && $result > 0 ? $result : false;
}
public function listByUser(int $userId, int $limit, int $offset): array
public function listByUser(int $userId, ?int $tenantId, int $limit, int $offset): array
{
if ($userId <= 0) {
return [];
@@ -37,69 +47,83 @@ class NotificationRepository implements NotificationRepositoryInterface
$limit = max(1, min($limit, 100));
$offset = max(0, $offset);
$tenantParams = [];
$rows = DB::select(
'select id, type, title, body, link, is_read, created from user_notifications where recipient_user_id = ? order by created desc limit ? offset ?',
'select id, type, title, body, link, is_read, created from user_notifications where recipient_user_id = ?' .
$this->tenantScopeClause($tenantId, true, $tenantParams) .
' order by created desc limit ' . $limit . ' offset ' . $offset,
(string) $userId,
(string) $limit,
(string) $offset
...$tenantParams
);
return $this->unwrapList($rows);
}
public function countUnreadByUser(int $userId): int
public function countUnreadByUser(int $userId, ?int $tenantId): int
{
if ($userId <= 0) {
return 0;
}
$tenantParams = [];
$count = DB::selectValue(
'select count(*) from user_notifications where recipient_user_id = ? and is_read = 0',
(string) $userId
'select count(*) from user_notifications where recipient_user_id = ? and is_read = 0' .
$this->tenantScopeClause($tenantId, true, $tenantParams),
(string) $userId,
...$tenantParams
);
return (int) $count;
}
public function markRead(int $id, int $userId): bool
public function markRead(int $id, int $userId, ?int $tenantId): bool
{
if ($id <= 0 || $userId <= 0) {
return false;
}
$tenantParams = [];
$affected = DB::update(
'update user_notifications set is_read = 1, read_at = NOW() where id = ? and recipient_user_id = ? and is_read = 0',
'update user_notifications set is_read = 1, read_at = NOW() where id = ? and recipient_user_id = ? and is_read = 0' .
$this->tenantScopeClause($tenantId, true, $tenantParams),
(string) $id,
(string) $userId
(string) $userId,
...$tenantParams
);
return is_int($affected) && $affected > 0;
}
public function markAllReadByUser(int $userId): int
public function markAllReadByUser(int $userId, ?int $tenantId): int
{
if ($userId <= 0) {
return 0;
}
$tenantParams = [];
$affected = DB::update(
'update user_notifications set is_read = 1, read_at = NOW() where recipient_user_id = ? and is_read = 0',
(string) $userId
'update user_notifications set is_read = 1, read_at = NOW() where recipient_user_id = ? and is_read = 0' .
$this->tenantScopeClause($tenantId, true, $tenantParams),
(string) $userId,
...$tenantParams
);
return is_int($affected) ? $affected : 0;
}
public function delete(int $id, int $userId): bool
public function delete(int $id, int $userId, ?int $tenantId): bool
{
if ($id <= 0 || $userId <= 0) {
return false;
}
$tenantParams = [];
$affected = DB::delete(
'delete from user_notifications where id = ? and recipient_user_id = ?',
'delete from user_notifications where id = ? and recipient_user_id = ?' .
$this->tenantScopeClause($tenantId, true, $tenantParams),
(string) $id,
(string) $userId
(string) $userId,
...$tenantParams
);
return is_int($affected) && $affected > 0;
@@ -132,4 +156,25 @@ class NotificationRepository implements NotificationRepositoryInterface
return is_int($affected) ? $affected : 0;
}
/**
* Builds a tenant scope SQL condition for a notification recipient query.
* Scope always includes global notifications (`tenant_id is null`).
*
* @param array<int, string> $params
*/
private function tenantScopeClause(?int $tenantId, bool $includeGlobal, array &$params): string
{
$scopedTenantId = (int) ($tenantId ?? 0);
if ($scopedTenantId > 0) {
if ($includeGlobal) {
$params[] = (string) $scopedTenantId;
return ' and (tenant_id is null or tenant_id = ?)';
}
$params[] = (string) $scopedTenantId;
return ' and tenant_id = ?';
}
return $includeGlobal ? ' and tenant_id is null' : '';
}
}

View File

@@ -10,15 +10,15 @@ interface NotificationRepositoryInterface
/**
* @return list<array<string, mixed>>
*/
public function listByUser(int $userId, int $limit, int $offset): array;
public function listByUser(int $userId, ?int $tenantId, int $limit, int $offset): array;
public function countUnreadByUser(int $userId): int;
public function countUnreadByUser(int $userId, ?int $tenantId): int;
public function markRead(int $id, int $userId): bool;
public function markRead(int $id, int $userId, ?int $tenantId): bool;
public function markAllReadByUser(int $userId): int;
public function markAllReadByUser(int $userId, ?int $tenantId): int;
public function delete(int $id, int $userId): bool;
public function delete(int $id, int $userId, ?int $tenantId): bool;
public function deleteAllByUser(int $userId): int;

View File

@@ -3,58 +3,69 @@
namespace MintyPHP\Module\Notifications\Service;
use MintyPHP\Module\Notifications\Repository\NotificationRepositoryInterface;
use MintyPHP\Repository\Tenant\UserTenantRepositoryInterface;
class NotificationService
{
private const DEDUPE_WINDOW_MINUTES = 30;
private const DEDUPE_TYPES = [
'user.created' => true,
'user.deleted' => true,
'user.activated' => true,
'user.deactivated' => true,
'user.assignment_changed' => true,
];
public function __construct(
private readonly NotificationRepositoryInterface $notificationRepository
private readonly NotificationRepositoryInterface $notificationRepository,
private readonly UserTenantRepositoryInterface $userTenantRepository
) {
}
/** @return list<array<string, mixed>> */
public function listForUser(int $userId, int $limit = 50): array
public function listForUser(int $userId, ?int $tenantId = null, int $limit = 50): array
{
if ($userId <= 0) {
return [];
}
return $this->notificationRepository->listByUser($userId, $limit, 0);
return $this->notificationRepository->listByUser($userId, $tenantId, $limit, 0);
}
public function unreadCount(int $userId): int
public function unreadCount(int $userId, ?int $tenantId = null): int
{
if ($userId <= 0) {
return 0;
}
return $this->notificationRepository->countUnreadByUser($userId);
return $this->notificationRepository->countUnreadByUser($userId, $tenantId);
}
public function markAsRead(int $userId, int $notificationId): bool
public function markAsRead(int $userId, int $notificationId, ?int $tenantId = null): bool
{
if ($userId <= 0 || $notificationId <= 0) {
return false;
}
return $this->notificationRepository->markRead($notificationId, $userId);
return $this->notificationRepository->markRead($notificationId, $userId, $tenantId);
}
public function markAllAsRead(int $userId): int
public function markAllAsRead(int $userId, ?int $tenantId = null): int
{
if ($userId <= 0) {
return 0;
}
return $this->notificationRepository->markAllReadByUser($userId);
return $this->notificationRepository->markAllReadByUser($userId, $tenantId);
}
public function dismiss(int $userId, int $notificationId): bool
public function dismiss(int $userId, int $notificationId, ?int $tenantId = null): bool
{
if ($userId <= 0 || $notificationId <= 0) {
return false;
}
return $this->notificationRepository->delete($notificationId, $userId);
return $this->notificationRepository->delete($notificationId, $userId, $tenantId);
}
public function createForUser(
@@ -70,6 +81,8 @@ class NotificationService
return false;
}
$dedupeMeta = $this->buildDedupeMeta($recipientUserId, $tenantId, $type, $data);
return $this->notificationRepository->create([
'recipient_user_id' => $recipientUserId,
'tenant_id' => $tenantId,
@@ -78,6 +91,8 @@ class NotificationService
'body' => $body,
'link' => $link,
'data' => $data ?: null,
'dedupe_fingerprint' => $dedupeMeta['fingerprint'] ?? null,
'dedupe_bucket' => $dedupeMeta['bucket'] ?? null,
]);
}
@@ -99,7 +114,7 @@ class NotificationService
return 0;
}
$userIds = $this->listUserIdsForTenant($tenantId);
$userIds = $this->userTenantRepository->listActiveUserIdsByTenantId($tenantId);
$created = 0;
foreach ($userIds as $userId) {
@@ -136,7 +151,7 @@ class NotificationService
return 0;
}
$userIds = $this->listUserIdsForTenant($tenantId);
$userIds = $this->userTenantRepository->listActiveUserIdsByTenantId($tenantId);
$created = 0;
foreach ($userIds as $userId) {
@@ -171,33 +186,55 @@ class NotificationService
}
/**
* Look up active user IDs belonging to a tenant.
*
* @return list<int>
* @param array<string, mixed> $data
* @return array{fingerprint: string, bucket: int}|array{}
*/
private function listUserIdsForTenant(int $tenantId): array
private function buildDedupeMeta(int $recipientUserId, ?int $tenantId, string $type, array $data): array
{
if ($tenantId <= 0) {
if (!isset(self::DEDUPE_TYPES[$type])) {
return [];
}
$rows = \MintyPHP\DB::select(
'select ut.user_id from user_tenants ut join users u on u.id = ut.user_id and u.active = 1 where ut.tenant_id = ?',
(string) $tenantId
);
if (!is_array($rows)) {
$target = $this->dedupeTargetFromData($data);
if ($target === '') {
return [];
}
$ids = [];
foreach ($rows as $row) {
$data = $row['user_tenants'] ?? $row;
if (is_array($data) && isset($data['user_id'])) {
$ids[] = (int) $data['user_id'];
}
}
$tenantScope = $tenantId !== null && $tenantId > 0 ? $tenantId : 0;
$raw = implode('|', [
'recipient:' . $recipientUserId,
'tenant:' . $tenantScope,
'type:' . $type,
'target:' . $target,
]);
return array_values(array_unique($ids));
return [
'fingerprint' => hash('sha256', $raw),
'bucket' => intdiv(time(), self::DEDUPE_WINDOW_MINUTES * 60),
];
}
/**
* @param array<string, mixed> $data
*/
private function dedupeTargetFromData(array $data): string
{
$explicit = trim((string) ($data['dedupe_target'] ?? ''));
if ($explicit !== '') {
return $explicit;
}
$targetUserId = (int) ($data['user_id'] ?? 0);
if ($targetUserId > 0) {
return 'user:' . $targetUserId;
}
$targetUuid = trim((string) ($data['uuid'] ?? ''));
if ($targetUuid !== '') {
return 'uuid:' . $targetUuid;
}
return '';
}
}

View File

@@ -2,6 +2,8 @@
namespace MintyPHP\Module\Notifications\Service;
use MintyPHP\Repository\Tenant\UserTenantRepositoryInterface;
class NotificationServicesFactory
{
private ?NotificationService $notificationService = null;
@@ -11,10 +13,11 @@ class NotificationServicesFactory
) {
}
public function createNotificationService(): NotificationService
public function createNotificationService(UserTenantRepositoryInterface $userTenantRepository): NotificationService
{
return $this->notificationService ??= new NotificationService(
$this->notificationRepositoryFactory->createNotificationRepository()
$this->notificationRepositoryFactory->createNotificationRepository(),
$userTenantRepository
);
}
}