import moment from 'moment' import config from '../../../config' import GeneralUtils from '../../general' import { condoManagerLogger } from '../../logger' import * as Slack from '../../slack' import { Project } from 'nexus-plugin-prisma/client' const PROJECT_TO_SYNC = null // const PROJECT_TO_SYNC = 12321i3122u1d1dd122d11 export default function getValidProjects(projects: Project[]) { projects.forEach((project) => GeneralUtils.throwIfObjHasNotProperties(project, [ 'isLaunched', 'hasSyncedWithCondoManager', 'condoManagerId', 'id', ]) ) return projects.filter((project) => { let reasonNotToProcess = null if (!project.condoManagerId) { reasonNotToProcess = `⚠️ Project ${project.name} don't have condo manager id` // TODO: Put back on on migration to master Slack.send({ channel: 'newProjectCondoManager', subject: '⚠️ WARNING', text: `Project ${project.name} don't have condo manager id. The project won't get synced with Condo Manager`, }).catch(condoManagerLogger.error) } // If we want to sync specific project if (PROJECT_TO_SYNC && PROJECT_TO_SYNC !== project.id) { reasonNotToProcess = `Project ${project.name} id is not the one we want to sync` } // NO need that anymore because PM can decide to sync whenever they want const numberOfHoursSinceWeProcessedProject = moment .duration(moment().diff(moment(project.lastDataSyncDate))) .asHours() if (numberOfHoursSinceWeProcessedProject <= config.thirdPartySync.processProjectMinHours) { reasonNotToProcess = `Not processing project "${ project.name }" because we synced data with this project ${Math.ceil( numberOfHoursSinceWeProcessedProject )} hours ago` } // If we want to only sync the launched projects if ( config.thirdPartySync.onlyLaunchedProject && (project.isLaunched || project.name.startsWith('Ass')) ) { reasonNotToProcess = `We only want to sync launched project and ${project.name} is not launched` } // If we want to only sync the unsynced if ( config.thirdPartySync.syncUnsyncedProjectsOnly && (project.hasSyncedWithCondoManager || !project.name.startsWith('Ass')) ) { reasonNotToProcess = `We only want to process unsync project and ${project.name} is already synched` } if (reasonNotToProcess) { condoManagerLogger.info(reasonNotToProcess) return false } return true }) }