import orderBy from 'lodash/orderBy' import config from '../../../config' import GeneralUtils from '../../general' import { condoManagerLogger } from '../../logger' import processResident from './processResident' export default async function processComptes({ projectId, comptes, condoManagerProperties, condoManagerService, }: { projectId: string comptes: any[] condoManagerProperties: any[] condoManagerService: any }) { if (config.thirdPartySync.logProcessAction) { condoManagerLogger.info('♻️ Processing comptes') } // Do the inactive comptes first so that if a user is put inactive but he's not because he has another compte that is active, // the system will first put it inactive but then reput it active. // It's temporary since it shouldn't put the user inactive at all if we know he has another active compte... // Does are really rare cases. const comptesSorted = orderBy(comptes, [(compte) => (compte.Inactif === '1' ? 0 : 1)]) await comptesSorted.reduce(async (accumulatorPromise, compte) => { await accumulatorPromise if (config.thirdPartySync.logProcessAction) { condoManagerLogger.info( `♻️ Processing compte: ${compte.NomCompte} - Unit: ${compte.NoCondo}` ) } // Uncomment this if we want to process only 1 compte to see what's happening // if (compte.NomCompte !== '3091-9609 Québec Inc') { // return // } // if (![compte.Courriel, compte.Courriel2].includes('mainguymarie@hotmail.com')) { // return // } try { const condoManagerUnitForCompte = condoManagerProperties.find( (copropriete) => copropriete.NoCondo === compte.NoCondo ) // Mostly mean that we've created a random unit in Walter for our test users if (!condoManagerUnitForCompte) { condoManagerLogger.info(`No unit in Condo Manager for compte ${compte.NoCompteReel}`) return Promise.resolve() } // const condoManagerOtherActiveComptesForUnit = allValidComptes.filter( // compte => compte.NoCondo === condoManagerUnitForCompte.NoCondo && compte.Inactif === '0' // ) // condoManagerLogger.info(`Adding parkings data for property`) // This is a compte owner if (compte.TypeCompte === '7') { const [firstName, lastName] = GeneralUtils.getFirstNameAndLastNameFromFullName(compte.Nom) await processResident({ compte, firstName, lastName, thirdPartyService: condoManagerService, condoManagerCompteUserNumber: 1, projectId: projectId, email: compte.Courriel, phoneNumber: compte.TelCellulaire, homePhoneNumber: compte.TelResidence, officePhoneNumber: compte.TelBureau, officePhoneNumberExt: compte.ExtTelBureau, isTenant: false, }) // Only create if there's a second user. In Condo Manager, the prenom is the second owner if (compte.Prenom) { const [firstName, lastName] = GeneralUtils.getFirstNameAndLastNameFromFullName( compte.Prenom ) await processResident({ compte, firstName, lastName, thirdPartyService: condoManagerService, condoManagerCompteUserNumber: 2, projectId: projectId, email: compte.Courriel2, phoneNumber: compte.TelCellulaireNom2, homePhoneNumber: compte.TelResidenceNom2, officePhoneNumber: compte.TelBureauNom2, officePhoneNumberExt: compte.ExtTelBureauNom2, isTenant: false, }) } } // OCCUPANT if (Object.prototype.hasOwnProperty.call(compte, 'DateChangementPresentDansUnite')) { await processResident({ compte, thirdPartyService: condoManagerService, condoManagerCompteUserNumber: 1, firstName: compte.Prenom, lastName: compte.Nom, projectId: projectId, email: compte.Courriel, phoneNumber: compte.TelCellulaire, homePhoneNumber: compte.TelResidence, officePhoneNumber: compte.TelBureau, officePhoneNumberExt: compte.ExtTelBureau, isTenant: true, }) } if (compte.TypeCompte === '2' && compte.TypeCompteARecevoir === '1') { await processResident({ compte, thirdPartyService: condoManagerService, condoManagerCompteUserNumber: 1, firstName: compte.Prenom, lastName: compte.Nom, projectId: projectId, email: compte.Courriel, phoneNumber: compte.TelCellulaire, homePhoneNumber: compte.TelResidence, officePhoneNumber: compte.TelBureau, officePhoneNumberExt: compte.ExtTelBureau, isTenant: true, }) } if (config.thirdPartySync.logProcessAction) { condoManagerLogger.info( `✅ Finish processing compte: ${compte.NomCompte} - Unit: ${compte.NoCondo}` ) } } catch (error) { condoManagerLogger.error(error) } }, Promise.resolve) if (config.thirdPartySync.logProcessAction) { condoManagerLogger.info('✅ Finish processing comptes') } }