/* eslint-disable no-unused-vars */ import moment from 'moment' import '../../config' import { Project, PostGetPayload, NotificationGetPayload, User } from 'nexus-plugin-prisma/client' import GeneralUtils from '../../utils/general' import { send as sendNotifications } from '../../utils/notification' import { prismaClient as prisma } from '../../prismaClient' type PostWithRelations = PostGetPayload<{ include: { attachments: true segments: true audience: true } }> type PostNotificationWithRelations = NotificationGetPayload<{ include: { user: true } }> prisma.project.findMany().then(async (projects: Project[]) => { for await (const project of projects) { console.log('Processing project:', project.name) const projectUsers: User[] = await prisma.user.findMany({ where: { projects: { some: { id: project.id } } }, }) const lastPosts: PostWithRelations[] = await prisma.post.findMany({ where: { project: { id: project.id, }, }, take: 2, orderBy: { createdAt: 'desc' }, include: { attachments: true, segments: true, audience: true }, }) for await (const post of lastPosts) { const postSentToday = moment().isSame(moment(post.createdAt), 'day') const postSentYesterday = moment().isSame(moment(post.createdAt).subtract(1, 'days'), 'day') if (!postSentToday && !postSentYesterday) { return } // Get the notifications of last post const postNotifications: PostNotificationWithRelations[] = await prisma.post .findOne({ where: { id: post.id } }) .notifications({ include: { user: true } }) const usersThatWillReceiveThePost = // No audience WITH a segment means the user WANTS to send it to 0 users post.audience.length === 0 && post.segments.length > 0 ? [] : // No audience means all residents post.audience.length === 0 ? projectUsers : // Else we take the audience post.audience console.log('Number of users in audience:', usersThatWillReceiveThePost.length) await prisma.post.update({ where: { id: post.id }, data: { audience: { connect: usersThatWillReceiveThePost.map(({ id }) => ({ id })), }, }, }) const usersThatDontHaveNotification = usersThatWillReceiveThePost.filter((u) => { const existingNotification = postNotifications.find((n) => n.user.id === u.id) return !existingNotification }) const usersToSendTo = GeneralUtils.uniq([...usersThatDontHaveNotification], 'id') console.log('Number of notifications for last post', postNotifications.length) console.log('Users that dont have notification created:', usersToSendTo.length) // Don't add this because we might send notification to users and it's been a long time // const notificationThatCouldntBeSent = postNotifications.filter(n => !n.sent && n.user) // console.log('notificationThatCouldntBeSent', notificationThatCouldntBeSent) if (usersToSendTo.length) { await sendNotifications({ forceSend: true, // Memo NEEDS to be sent no mather what! users: usersToSendTo, title: post.title, message: GeneralUtils.removeRichTextareaLineBreak(post.description), type: post.type === 'PROXY' ? 'NEW_PROXY' : 'NEW_POST', data: { id: post.id, }, forceSMS: post.sendAsSMS || post.type === 'PROXY', projectId: project.id, getEmailDataForUser: (user) => Promise.resolve({ template: post.type === 'PROXY' ? user.preferedLanguage === 'fr' ? 'NEW_PROXY_FROM_MANAGER_FR' : 'NEW_PROXY_FROM_MANAGER' : user.preferedLanguage === 'fr' ? 'RESIDENT_NEW_POST_FR' : 'RESIDENT_NEW_POST', attachments: post.attachments.map(({ name, url }) => ({ url, filename: name })), customArgs: { postId: post.id, }, customVariables: { postId: post.id, userId: user.id, }, }), }) } console.log('Finish processing project:', project.name) } } console.log('JOB DONE') })