import { NOTIFICATION_TYPE, User } from 'nexus-plugin-prisma/client' import { addNotification } from '../queues' import * as EmailUtils from './email' import GeneralUtils from './general' import { notificationLogger } from './logger' import { TwilioSendInputOptionalParams } from './twilio' export const MANAGER_NOTIFICATION_TYPES = [ 'NEW_RESIDENT', 'NEW_CHAT_MESSAGE', 'NEW_CHAT_PRIVATE_MENTIONED', 'NEW_PROXY_COMPLETED', 'NEW_RESERVATION', 'UPDATE_RESERVATION', 'NEW_AD', 'UPDATE_AD', 'TASK_ASSIGNEE', 'NEW_TASK', 'NEW_COMMENT_SUBSCRIBED', 'NEW_COMMENT_ON_ASSIGNEE_TASK', 'NEW_COMMENT_WITH_MENTION', ] type SenderType = { firstName: string lastName?: string email?: string title?: string phoneNumber?: string address?: string } export type NotificationType = { title: string type: NOTIFICATION_TYPE titleFr?: string subtitle?: string subtitleFr?: string message?: string messageFr?: string data?: any smsData?: any emailData?: EmailUtils.EmailDataType noAppNotification?: boolean noEmail?: boolean noSMS?: boolean forceApp?: boolean forceEmail?: boolean forceSMS?: boolean forceSend?: boolean sender?: SenderType isFromWalter?: boolean skipValidating?: boolean projectId?: string serviceProjectId?: string } type SendNotificationType = NotificationType & { users: Partial[] getSmsDataForUser?: (user: Partial) => Promise getEmailDataForUser?: (user: Partial) => Promise } export const send = async ({ title, users, data, titleFr, subtitle, subtitleFr, message, messageFr, type, projectId, serviceProjectId, smsData, getSmsDataForUser, emailData, getEmailDataForUser, forceApp, noAppNotification, forceSMS, noSMS, forceEmail, noEmail, forceSend, sender, isFromWalter, skipValidating, }: SendNotificationType) => { if (!users || users.length == 0 || !title || !data) { notificationLogger.error(new Error('missing required parameter for notification')) } else { notificationLogger.info(`Will send notifications to ${users.length} users`) const uniqueUsers: Partial[] = GeneralUtils.uniq(users, 'id').filter(Boolean) if (uniqueUsers?.length === 0) { return Promise.resolve() } for await (const user of users) { if (getEmailDataForUser) { emailData = { ...emailData, ...(await getEmailDataForUser(user)) } } if (getSmsDataForUser) { smsData = { ...smsData, ...(await getSmsDataForUser(user)) } } await addNotification({ userId: user.id, title, titleFr, subtitle, subtitleFr, message, messageFr, type, projectId, serviceProjectId, smsData, emailData, data, forceApp, noAppNotification, forceSMS, noSMS, forceEmail, noEmail, forceSend, sender, isFromWalter, skipValidating, }) } } }