function uniq(values) { return Array.from(new Set((values ?? []).filter(Boolean))); } async function resolveRecipientsForAssignee(tx, params) { if (params.assigneeId) { return [params.assigneeId]; } if (params.assigneeRole) { const rows = await tx.employeeRole.findMany({ where: { role_code: params.assigneeRole, employee: { status: "active", }, }, select: { employee_id: true, }, }); return rows.map((row) => row.employee_id); } return []; } async function createNotifications(tx, input) { const recipientIds = uniq(input.recipientEmployeeIds); if (recipientIds.length === 0) { return { count: 0 }; } const title = String(input.title ?? "").slice(0, 200); const body = input.body != null ? String(input.body).slice(0, 5000) : null; const type = String(input.type ?? "").slice(0, 64); const targetType = input.targetType != null ? String(input.targetType).slice(0, 64) : null; return tx.notification.createMany({ data: recipientIds.map((recipientId) => ({ recipient_employee_id: recipientId, type, title, body, target_type: targetType, target_id: input.targetId ?? null, })), }); } async function createNotificationsWithRecords(tx, input) { const recipientIds = uniq(input.recipientEmployeeIds); if (recipientIds.length === 0) { return []; } const title = String(input.title ?? '').slice(0, 200); const body = input.body != null ? String(input.body).slice(0, 5000) : null; const type = String(input.type ?? '').slice(0, 64); const targetType = input.targetType != null ? String(input.targetType).slice(0, 64) : null; const created = []; for (const recipientId of recipientIds) { created.push( await tx.notification.create({ data: { recipient_employee_id: recipientId, type, title, body, target_type: targetType, target_id: input.targetId ?? null, }, }), ); } return created; } module.exports = { createNotifications, createNotificationsWithRecords, resolveRecipientsForAssignee, };