import htmlToText from 'html-to-text'
import moment from 'moment'
import GeneralUtils from '../../utils/general'
import * as Notification from '../../utils/notification'
import { prismaClient as prisma } from '../../prismaClient'
import { logger } from '../../utils/logger'
export default async function sendPost({ data: postId }: { data: string }): Promise {
try {
logger.info(`Sending post ${postId}`)
const post = await prisma.post.findOne({
where: {
id: postId,
},
select: {
id: true,
title: true,
description: true,
sendAsSMS: true,
type: true,
assemblyDate: true,
assemblyLocation: true,
assemblyTime: true,
segments: {
select: {
id: true,
},
},
attachments: {
select: {
url: true,
name: true,
format: true,
size: true,
},
},
coverImage: {
select: {
url: true,
},
},
audience: {
select: {
id: true,
firstName: true,
lastName: true,
},
},
project: {
select: {
id: true,
name: true,
users: {
select: {
id: true,
},
},
},
},
},
})
// Notify users
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
? post.project.users
: // Else we take the audience
post.audience
await Notification.send({
forceSend: true, // Memo NEEDS to be sent no mather what!
users: usersThatWillReceiveThePost,
title: post.title,
...(post.type === 'PROXY' && {
// prettier-ignore
title: `Procuration pour l'assemblée le ${moment(post.assemblyDate).locale('fr').format('LL')}`,
titleFr: `Proxy for assembly on ${moment(post.assemblyDate).format('LL')}`,
}),
message: GeneralUtils.removeRichTextareaLineBreak(post.description),
type: post.type === 'PROXY' ? 'NEW_PROXY' : 'NEW_POST',
data: {
id: post.id,
},
forceEmail: post.type === 'PROXY', // Force by email because right now proxy are only by email and they must be delivered
forceSMS: post.sendAsSMS || post.type === 'PROXY',
// CAN'T SEND PDF
// smsData: {
// mediaUrl: post.attachments.map(attachment => attachment.url),
// },
projectId: post.project.id,
...(post.type === 'PROXY' && {
noAppNotification: true,
getSmsDataForUser: async (user) => {
const userNameAndUnit = await GeneralUtils.getUserNameAndUnits(user, post.project)
return Promise.resolve({
body:
user.preferedLanguage === 'fr'
? // prettier-ignore
`**IMPORTANT**\n\n${post.project.name}, ${userNameAndUnit}.
\n\nProcuration pour l'assemblée le ${moment(post.assemblyDate).locale('fr').format('LL')}
\n\nLieux: ${post.assemblyLocation || ''}
\n\nHeure: ${post.assemblyTime || ''}
\n\n${htmlToText.fromString(post.description || '')}
\n\nresident.usewalter.com/Utils/ProxySelectMember?user=${user.id}&post=${post.id}`
: user.preferedLanguage === 'en'
? // prettier-ignore
`**IMPORTANT**\n\n${post.project.name}, ${userNameAndUnit}.
\n\nProxy for the assembly on ${moment(post.assemblyDate).format('LL')}
\n\nLocation: ${post.assemblyLocation || ''}
\n\nTime: ${post.assemblyTime || ''}
\n\n${htmlToText.fromString(post.description || '')}
\n\nresident.usewalter.com/Utils/ProxySelectMember?user=${user.id}&post=${post.id}`
: // prettier-ignore
`**IMPORTANT**\n\n${post.project.name}, ${userNameAndUnit}.
\n\nProcuration pour l'assemblée le ${moment(post.assemblyDate).locale('fr').format('LL')} / Proxy assembly on ${moment(post.assemblyDate).format('LL')}
\n\nLieux / Location: ${post.assemblyLocation || ''}
\n\nHeure / Time: ${post.assemblyTime || ''}
\n\n${htmlToText.fromString(post.description ||'')}\n\nresident.usewalter.com/Utils/ProxySelectMember?user=${user.id}&post=${post.id}`,
})
},
}),
getEmailDataForUser: async (user) => {
return 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((attachment) => ({
filename: attachment.name,
url: attachment.url,
})),
customArgs: {
postId: post.id,
},
customVariables: {
postId: post.id,
userId: user.id,
coverImageUrl: post.coverImage?.url,
...(post.type === 'PROXY' && {
subject: `${post.project.name} - ${await GeneralUtils.getUserNameAndUnits(
user,
post.project
)}`,
assemblyDate:
user.preferedLanguage === 'fr'
? moment(post.assemblyDate).locale('fr').format('LL')
: moment(post.assemblyDate).format('LL'),
assemblyLocation: post.assemblyLocation || '',
assemblyTime: post.assemblyTime || '',
}),
},
})
},
})
await prisma.post.update({ where: { id: postId }, data: { postSentAt: new Date() } })
logger.info(`Post ${postId} sent`)
} catch (error) {
logger.error(error)
throw error
}
}