import moment from 'moment' import { logger } from '../../utils/logger' import { prismaClient as prisma } from '../../prismaClient' const notificationMinDateToCheck = moment().subtract(1, 'months').toDate() export default async function sendFirstReminderForNewChatMessageStillUnseen() { try { logger.info( 'Start sending first notification reminder for new chat message that are still not seen' ) const usersThatHaveAtLeastOneChatNotificationThatIsUnseen = await prisma.user.findMany({ where: { AND: [ { OR: [{ hasReceivedInvitation: true }, { hasInstalledApp: true }], }, { role: 'RESIDENT', }, { notifications: { some: { AND: [ { type: 'NEW_CHAT_MESSAGE', }, { hasBeenSeen: false, }, { createdAt: { gte: notificationMinDateToCheck, }, }, { OR: [{ firstReminderSent: null }, { firstReminderSent: false }], }, ], }, }, }, ], }, select: { notifications: { where: { AND: [ { type: 'NEW_CHAT_MESSAGE', }, { hasBeenSeen: false, }, { createdAt: { gte: `${notificationMinDateToCheck}`, }, }, ], }, select: { id: true, type: true, hasBeenSeen: true, title: true, message: true, }, }, }, }) // await usersThatHaveAtLeastOneChatNotificationThatIsUnseen.reduce(async (promise, _) => { // await promise // // const message = `You have ${user.notifications.length || // // 'some'} unread messages on Walter.\n\nMESSAGE:${user.notifications // // .map(n => GeneralUtils.truncate({ str: n.message, maxChars: 50 })) // // .join('\n\nMESSAGE:')}` // // console.log(message) // // const createdNotifications = await NotificationUtils.send({ // // type: 'CHAT_NOTIFICATION_REMINDER', // // users: [user], // // forceSMS: true, // // noAppNotification: true, // // noEmail: true, // // title: `Reminder`, // // titleFr: `Rappel`, // // message: `You have ${user.notifications.length || // // 'some'} unread messages on Walter.\n\nMESSAGE:${user.notifications // // .map(n => GeneralUtils.truncate({ str: n.message, maxChars: 50 })) // // .join('\n\nMESSAGE:')}\n\nPlease ignore this message if you already answered or noticed those messages.`, // // messageFr: `Vous avez ${user.notifications.length || // // 'des'} messages non lus sur Walter.\n\nMESSAGE:${user.notifications // // .map(n => GeneralUtils.truncate({ str: n.message, maxChars: 50 })) // // .join('\n\nMESSAGE:')}\n\nVeuillez ignorer ce message si vous avez déjà répondu ou vu à ces messages.` // // }) // // UPDATE NOTIFICATION BECAUSE FIRST REMINDER WAS SENT // // const createdNotification = createdNotifications[0] // // await user.notifications.map(notification => // // prisma.updateNotification({ // // where: { id: notification.id }, // // data: { // // firstReminderSent: true, // // firstReminderSentAt: moment().toDate(), // // ...(createdNotification.sentByApp && { // // firstReminderSentByApp: true, // // firstReminderSentByAppAt: createdNotification.sentByAppAt // // }), // // ...(createdNotification.sentByEmail && { // // firstReminderSentByEmail: true, // // firstReminderSentByEmailAt: createdNotification.sentByEmailAt // // }), // // ...(createdNotification.sentBySMS && { // // firstReminderSentBySMS: true, // // firstReminderSentBySMSAt: createdNotification.sentBySMSAt // // }) // // } // // }) // // ) // }, Promise.resolve) // logger.info( // `Finish sending first notification reminder for new chat message that are still not seen` // ) } catch (e) { logger.error(e) } }