|
| 1 | +const _ = require('lodash') |
| 2 | +const uuid = require('uuid/v4') |
| 3 | +const moment = require('moment') |
| 4 | + |
| 5 | +const errors = require('./errors') |
| 6 | +const helper = require('./helper') |
| 7 | + |
| 8 | +class ChallengePhaseHelper { |
| 9 | + /** |
| 10 | + * Populate challenge phases. |
| 11 | + * @param {Array} phases the phases to populate |
| 12 | + * @param {Date} startDate the challenge start date |
| 13 | + * @param {String} timelineTemplateId the timeline template id |
| 14 | + */ |
| 15 | + async populatePhases (phases, startDate, timelineTemplateId) { |
| 16 | + if (_.isUndefined(timelineTemplateId)) { |
| 17 | + throw new errors.BadRequestError(`Invalid timeline template ID: ${timelineTemplateId}`) |
| 18 | + } |
| 19 | + |
| 20 | + const { timelineTempate, timelineTemplateMap } = await this.getTemplateAndTemplateMap(timelineTemplateId) |
| 21 | + const { phaseDefinitionMap } = await this.getPhaseDefinitionsAndMap() |
| 22 | + |
| 23 | + if (!phases || phases.length === 0) { |
| 24 | + // auto populate phases |
| 25 | + for (const p of timelineTempate) { |
| 26 | + phases.push({ ...p }) |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + for (const p of phases) { |
| 31 | + const phaseDefinition = phaseDefinitionMap.get(p.phaseId) |
| 32 | + |
| 33 | + p.id = uuid() |
| 34 | + p.name = phaseDefinition.name |
| 35 | + p.description = phaseDefinition.description |
| 36 | + |
| 37 | + // set p.open based on current phase |
| 38 | + const phaseTemplate = timelineTemplateMap.get(p.phaseId) |
| 39 | + if (phaseTemplate) { |
| 40 | + if (!p.duration) { |
| 41 | + p.duration = phaseTemplate.defaultDuration |
| 42 | + } |
| 43 | + |
| 44 | + if (phaseTemplate.predecessor) { |
| 45 | + const predecessor = _.find(phases, { phaseId: phaseTemplate.predecessor }) |
| 46 | + if (!predecessor) { |
| 47 | + throw new errors.BadRequestError(`Predecessor ${phaseTemplate.predecessor} not found in given phases.`) |
| 48 | + } |
| 49 | + p.predecessor = phaseTemplate.predecessor |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + // calculate dates |
| 55 | + if (!startDate) { |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + // sort phases by predecessor |
| 60 | + phases.sort((a, b) => { |
| 61 | + if (a.predecessor === b.phaseId) { |
| 62 | + return 1 |
| 63 | + } |
| 64 | + if (b.predecessor === a.phaseId) { |
| 65 | + return -1 |
| 66 | + } |
| 67 | + return 0 |
| 68 | + }) |
| 69 | + |
| 70 | + let isSubmissionPhaseOpen = false |
| 71 | + |
| 72 | + for (let p of phases) { |
| 73 | + const predecessor = timelineTemplateMap.get(p.predecessor) |
| 74 | + |
| 75 | + if (predecessor == null) { |
| 76 | + if (p.name === 'Registration') { |
| 77 | + p.scheduledStartDate = moment(startDate).toDate() |
| 78 | + } |
| 79 | + if (p.name === 'Submission') { |
| 80 | + if (p.scheduledStartDate != null) { |
| 81 | + p.scheduledStartDate = moment(p.scheduledStartDate).toDate() |
| 82 | + } else { |
| 83 | + p.scheduledStartDate = moment(startDate).add(5, 'minutes').toDate() |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + if (moment(p.scheduledStartDate).isSameOrBefore(moment())) { |
| 88 | + p.actualStartDate = p.scheduledStartDate |
| 89 | + } else { |
| 90 | + delete p.actualStartDate |
| 91 | + } |
| 92 | + |
| 93 | + p.scheduledEndDate = moment(p.scheduledStartDate).add(p.duration, 'seconds').toDate() |
| 94 | + if (moment(p.scheduledEndDate).isBefore(moment())) { |
| 95 | + delete p.actualEndDate |
| 96 | + } else { |
| 97 | + p.actualEndDate = p.scheduledEndDate |
| 98 | + } |
| 99 | + } else { |
| 100 | + const precedecessorPhase = _.find(phases, { phaseId: predecessor.phaseId }) |
| 101 | + if (precedecessorPhase == null) { |
| 102 | + throw new errors.BadRequestError(`Predecessor ${predecessor.phaseId} not found in given phases.`) |
| 103 | + } |
| 104 | + let phaseEndDate = moment(precedecessorPhase.scheduledEndDate) |
| 105 | + if (precedecessorPhase.actualEndDate != null && moment(precedecessorPhase.actualEndDate).isAfter(phaseEndDate)) { |
| 106 | + phaseEndDate = moment(precedecessorPhase.actualEndDate) |
| 107 | + } else { |
| 108 | + phaseEndDate = moment(precedecessorPhase.scheduledEndDate) |
| 109 | + } |
| 110 | + |
| 111 | + p.scheduledStartDate = phaseEndDate.toDate() |
| 112 | + p.scheduledEndDate = moment(p.scheduledStartDate).add(p.duration, 'seconds').toDate() |
| 113 | + } |
| 114 | + |
| 115 | + p.isOpen = moment().isBetween(p.scheduledStartDate, p.scheduledEndDate) |
| 116 | + |
| 117 | + if (p.isOpen) { |
| 118 | + if (p.name === 'Submission') { |
| 119 | + isSubmissionPhaseOpen = true |
| 120 | + } |
| 121 | + delete p.actualEndDate |
| 122 | + } |
| 123 | + |
| 124 | + if (moment(p.scheduledStartDate).isAfter(moment())) { |
| 125 | + delete p.actualStartDate |
| 126 | + delete p.actualEndDate |
| 127 | + } |
| 128 | + |
| 129 | + if (p.name === 'Post-Mortem' && isSubmissionPhaseOpen) { |
| 130 | + delete p.actualStartDate |
| 131 | + delete p.actualEndDate |
| 132 | + p.isOpen = false |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + // phases.sort((a, b) => moment(a.scheduledStartDate).isAfter(b.scheduledStartDate)) |
| 137 | + } |
| 138 | + |
| 139 | + async validatePhases (phases) { |
| 140 | + if (!phases || phases.length === 0) { |
| 141 | + return |
| 142 | + } |
| 143 | + const records = await helper.scan('Phase') |
| 144 | + const map = new Map() |
| 145 | + _.each(records, (r) => { |
| 146 | + map.set(r.id, r) |
| 147 | + }) |
| 148 | + const invalidPhases = _.filter(phases, (p) => !map.has(p.phaseId)) |
| 149 | + if (invalidPhases.length > 0) { |
| 150 | + throw new errors.BadRequestError( |
| 151 | + `The following phases are invalid: ${toString(invalidPhases)}` |
| 152 | + ) |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + async getPhaseDefinitionsAndMap () { |
| 157 | + const records = await helper.scan('Phase') |
| 158 | + const map = new Map() |
| 159 | + _.each(records, (r) => { |
| 160 | + map.set(r.id, r) |
| 161 | + }) |
| 162 | + return { phaseDefinitions: records, phaseDefinitionMap: map } |
| 163 | + } |
| 164 | + |
| 165 | + async getTemplateAndTemplateMap (timelineTemplateId) { |
| 166 | + const records = await helper.getById('TimelineTemplate', timelineTemplateId) |
| 167 | + const map = new Map() |
| 168 | + _.each(records.phases, (r) => { |
| 169 | + map.set(r.phaseId, r) |
| 170 | + }) |
| 171 | + |
| 172 | + return { |
| 173 | + timelineTempate: records.phases, |
| 174 | + timelineTemplateMap: map |
| 175 | + } |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +module.exports = new ChallengePhaseHelper() |
0 commit comments