import moment from 'moment' import GeneralConstants from '../../constants/general' import GeneralUtils from '../../utils/general' import { logger } from '../../utils/logger' import * as NotificationsUtils from '../../utils/notification' import * as PDFGenerator from '../../utils/pdfGenerator' import { prismaClient as prisma } from '../../prismaClient' // import { getMomentDateForLanguage } from '../../utils/translation' export const getSettingsForResidentApp = (req, res) => { return res.status(200).send({ firstOrderPromoMessageFr: 'Obtenez 30% de rabais sur votre première commande avec le code promo WELCOME30', firstOrderPromoMessageEn: 'Get 30% off your first order with the promo code WELCOME30', firstOrderPromoMessageZh: '使用促销代码获得第一笔订单30%的折扣 WELCOME30', ios: { minimumAppVersion: '1.20.0', upToDateAppVersion: '1.42.0', }, android: { minimumAppVersion: '5', upToDateAppVersion: '9', }, updates: [ { iosVersion: '1.40.0', androidVersion: '9', news: [ { titleEn: 'Direct chat in announcement', descriptionEn: "It's now faster than ever to comment regarding a new announcement. Simply click on the chat icon at the footer of each new post.", }, ], }, ], }) } export const getSettingsForManagerApp = (req, res) => { return res.status(200).send({ ios: { minimumAppVersion: '1.20.0', upToDateAppVersion: '1.42.0', }, android: { minimumAppVersion: '5', upToDateAppVersion: '9', }, updates: [ { iosVersion: '1.40.0', androidVersion: '9', news: [ { titleEn: 'Direct chat in announcement', descriptionEn: "It's now faster than ever to comment regarding a new announcement. Simply click on the chat icon at the footer of each new post.", }, ], }, ], }) } export const generateProxyPDF = async (req, res) => { try { const { userLanguage, postId, userId, signDate, projectName, currentUserName, selectedMemberName, assemblyDate, todayDayOfMonth, todayMonthName, signeeSignatureImage, } = req.body if (!userId || !postId) { throw new Error('Missing info') } const [post, user] = await Promise.all([ prisma.post.findOne({ where: { id: postId, }, select: { id: true, assemblyLocation: true, assemblyDate: true, assemblyTime: true, project: { select: { id: true, name: true, buildingId: true, managingCompany: { select: { shortName: true, longName: true, email: true, phone: { select: { extension: true, nationalFormat: true, number: true, }, }, logo: { select: { url: true, }, }, address: { select: { address1: true, apartmentNumber: true, city: true, zip: true, state: true, country: true, }, }, users: { select: { id: true, }, }, }, }, }, }, }, }), prisma.user.findOne({ where: { id: userId, }, include: { properties: { include: { address: true, }, }, }, }), ]) // Why not! if (userLanguage && (!user.preferedLanguage || user.preferedLanguage !== userLanguage)) { await prisma.user.update({ where: { id: user.id }, data: { preferedLanguage: userLanguage } }) user.preferedLanguage = userLanguage } // Don't show properties of other buildings.... const userPropertiesForProject = user.properties.filter( (p) => p.buildingId === post.project.buildingId ) if (!userPropertiesForProject.length) { throw new Error('No units associated with resident. Cannot create proxy') } const userNameAndUnits = await GeneralUtils.getUserNameAndUnits(user, post.project) // Only the PDF of the proxy const fileName = GeneralUtils.cleanString( `${projectName}-proxy-${assemblyDate}-${currentUserName}-${moment().unix()}` ) const pdfUrl = await PDFGenerator.generate({ fileName, templateName: user.preferedLanguage === 'fr' ? 'proxy-fr.html' : 'proxy.html', data: { selectedMemberName, todayDayOfMonth, todayMonthName, signeeSignatureImage, signDate, assemblyDate, managingCompanyName: post.project.managingCompany.longName || post.project.managingCompany.shortName, managingCompanyAddress: GeneralUtils.getFormattedAddress( post.project.managingCompany.address ), managingCompanyEmail: post.project.managingCompany.email, managingCompanyPhoneNumber: post.project.managingCompany.phone ? post.project.managingCompany.phone.extension ? `${ post.project.managingCompany.phone.nationalFormat || post.project.managingCompany.phone.number } #${post.project.managingCompany.phone.extension}` : post.project.managingCompany.phone.nationalFormat || post.project.managingCompany.phone.number : '', walterLogoUrl: 'https://res.cloudinary.com/din7dm84v/image/upload/v1538091823/janitor/logo/Icon-256.png', managingCompanyLogoUrl: post.project.managingCompany.logo ? post.project.managingCompany.logo.url : '', currentUserNameAndUnit: userNameAndUnits, assemblyTime: post.assemblyTime, assemblyLocation: post.assemblyLocation, projectName: post.project.name, currentUserName: GeneralUtils.getUserName(user), currentUserAddress: userPropertiesForProject[0].address.address1, currentUserApartmentNumber: userPropertiesForProject .map((property) => property.address.apartmentNumber) .join(', ') .toString(), currentUserCity: userPropertiesForProject[0].address.city, }, }) // Create the proxy for real now await prisma.post.update({ where: { id: postId, }, data: { signedProxies: { create: [ { representativeName: selectedMemberName, pdf: { create: { name: fileName, url: pdfUrl, }, }, property: { connect: { id: userPropertiesForProject[0].id, }, }, signee: { connect: { id: user.id, }, }, }, ], }, }, }) // Notifications await Promise.all([ // RESIDENT NotificationsUtils.send({ type: 'NEW_PROXY_COMPLETED', users: [user], //withEmail: true, // Because we want the person to receive it by email since it's kinda important noAppNotification: true, // Because we don't have the feature in the app yet title: user.preferedLanguage === 'fr' ? `Procuration pour assemblée le ${moment(post.assemblyDate).locale('fr').format('LL')}` : `Proxy for assembly on ${moment(post.assemblyDate).format('LL')}`, message: user.preferedLanguage === 'fr' ? `Votre procuration a bien été transmise à votre gestionnaire pour l'assemblée qui se déroulera le ${moment( post.assemblyDate ) .locale('fr') .format('LL')}.` : `Your proxy was sent to your property manager for the assembly on ${moment( post.assemblyDate ).format('LL')}.`, data: { id: post.id, }, projectId: post.project.id, emailData: { template: user.preferedLanguage === 'fr' ? 'NEW_PROXY_COMPLETED_TO_RESIDENT_FR' : 'NEW_PROXY_COMPLETED_TO_RESIDENT', from: { email: GeneralConstants.EMAIL_COMMUNICATION_WALTER_FROM_EMAIL, name: post.project.managingCompany.shortName || post.project.managingCompany.longName, }, customVariables: { assemblyDate, proxyPDFUrl: pdfUrl, userNameAndUnits: userNameAndUnits, }, }, }), // MANAGER NotificationsUtils.send({ type: 'NEW_PROXY_COMPLETED', users: post.project.managingCompany.users, noAppNotification: true, title: `${userNameAndUnits} - Proxy for assembly on ${moment(post.assemblyDate).format( 'LL' )}`, message: `You've received a new proxy for your assembly on ${moment( post.assemblyDate ).format('LL')}`, data: { id: post.id, }, projectId: post.project.id, emailData: { template: 'NEW_PROXY_COMPLETED_TO_MANAGER', from: { email: GeneralConstants.EMAIL_COMMUNICATION_WALTER_FROM_EMAIL, name: post.project.managingCompany.shortName || post.project.managingCompany.longName, }, customVariables: { assemblyDate, proxyPDFUrl: pdfUrl, userNameAndUnits: userNameAndUnits, }, }, }), ]) return res.status(200).end() } catch (error) { logger.error(error) return res.status(400).send(error?.message || "Can't create proxy") } }