import { buildNotificationFromType } from '@open-mercato/core/modules/notifications/lib/notificationBuilder' import { resolveNotificationService } from '@open-mercato/core/modules/notifications/lib/notificationService' import { resolveRecordLockNotificationType, resolveRecordResourceLink } from '../lib/notificationHelpers' export const metadata = { event: 'record_locks.participant.left', persistent: true, id: 'record_locks:participant-left-notification', } type Payload = { lockId: string resourceKind: string resourceId: string tenantId: string organizationId?: string | null leftUserId: string recipientUserIds?: string[] } type ResolverContext = { resolve: (name: string) => T } export default async function handle(payload: Payload, ctx: ResolverContext) { const recipientUserIds = Array.isArray(payload.recipientUserIds) ? Array.from(new Set(payload.recipientUserIds.filter((id): id is string => typeof id === 'string' && id.trim().length > 0))) : [] if (!recipientUserIds.length) return const notificationService = resolveNotificationService(ctx) const typeDef = resolveRecordLockNotificationType('record_locks.participant.left') if (!typeDef) return const linkHref = resolveRecordResourceLink(payload.resourceKind, payload.resourceId) for (const recipientUserId of recipientUserIds) { if (recipientUserId === payload.leftUserId) continue const notificationInput = buildNotificationFromType(typeDef, { recipientUserId, bodyVariables: { resourceKind: payload.resourceKind, }, sourceEntityType: 'record_locks:lock', sourceEntityId: payload.lockId, linkHref, groupKey: `record_locks.participant.left:${payload.lockId}:${payload.leftUserId}`, }) await notificationService.create(notificationInput, { tenantId: payload.tenantId, organizationId: payload.organizationId ?? null, }) } }