@@ -20,6 +20,7 @@ const { createOrSetNumberOfReviewers } = require('./selfServiceReviewerService')
20
20
const { disableTimelineNotifications } = require ( './selfServiceNotificationService' )
21
21
const legacyChallengeService = require ( './legacyChallengeService' )
22
22
const legacyChallengeReviewService = require ( './legacyChallengeReviewService' )
23
+ const phaseCriteriaService = require ( './phaseCriteriaService' ) ;
23
24
24
25
/**
25
26
* Drop and recreate phases in ifx
@@ -92,6 +93,79 @@ async function recreatePhases (legacyId, v5Phases, createdBy) {
92
93
logger . info ( 'recreatePhases :: end' )
93
94
}
94
95
96
+ async function addPhaseConstraints ( legacyId , v5Phases , createdBy ) {
97
+ logger . info ( `addPhaseConstraints :: start: ${ legacyId } , ${ JSON . stringify ( v5Phases ) } ` )
98
+ const allPhaseCriteria = await phaseCriteriaService . getPhaseCriteria ( ) ;
99
+ logger . info ( `addPhaseConstraints :: allPhaseCriteria: ${ JSON . stringify ( allPhaseCriteria ) } ` )
100
+
101
+ const phaseTypes = await timelineService . getPhaseTypes ( )
102
+ logger . info ( `addPhaseConstraints :: phaseTypes: ${ JSON . stringify ( phaseTypes ) } ` )
103
+
104
+ const phasesFromIFx = await timelineService . getChallengePhases ( legacyId )
105
+
106
+ for ( const phase of v5Phases ) {
107
+ logger . info ( `addPhaseConstraints :: phase: ${ legacyId } -> ${ JSON . stringify ( phase ) } ` )
108
+ if ( phase . constraints == null || phase . constraints . length === 0 ) continue ;
109
+
110
+ const phaseLegacyId = _ . get ( _ . find ( phaseTypes , pt => pt . name === phase . name ) , 'phase_type_id' )
111
+ const existingLegacyPhase = _ . find ( phasesFromIFx , p => p . phase_type_id === phaseLegacyId )
112
+
113
+ const projectPhaseId = _ . get ( existingLegacyPhase , 'project_phase_id' )
114
+ if ( ! projectPhaseId ) {
115
+ logger . warn ( `Could not find phase ${ phase . name } on legacy!` )
116
+ continue
117
+ }
118
+
119
+ let constraintName = null ;
120
+ let constraintValue = null
121
+
122
+ if ( phase . name === 'Submission' ) {
123
+ const numSubmissionsConstraint = phase . constraints . find ( c => c . name === 'Number of Submissions' )
124
+ if ( numSubmissionsConstraint ) {
125
+ constraintName = 'Submission Number'
126
+ constraintValue = numSubmissionsConstraint . value
127
+ }
128
+ }
129
+
130
+ if ( phase . name === 'Registration' ) {
131
+ const numRegistrantsConstraint = phase . constraints . find ( c => c . name === 'Number of Registrants' )
132
+ if ( numRegistrantsConstraint ) {
133
+ constraintName = 'Registration Number'
134
+ constraintValue = numRegistrantsConstraint . value
135
+ }
136
+ }
137
+
138
+ if ( phase . name === 'Review' ) {
139
+ const numReviewersConstraint = phase . constraints . find ( c => c . name === 'Number of Reviewers' )
140
+ if ( numReviewersConstraint ) {
141
+ constraintName = 'Reviewer Number'
142
+ constraintValue = numReviewersConstraint . value
143
+ }
144
+ }
145
+
146
+ // We have an interesting situation if a submission phase constraint was added but
147
+ // no registgration phase constraint was added. This ends up opening Post-Mortem
148
+ // phase if registration closes with 0 submissions.
149
+ // For now I'll leave it as is and handle this better in the new Autopilot implementation
150
+ // A quick solution would have been adding a registration constraint with value 1 if none is provided when there is a submission phase constraint
151
+
152
+ if ( constraintName && constraintValue ) {
153
+ const phaseCriteriaTypeId = _ . get ( _ . find ( allPhaseCriteria , pc => pc . name === constraintName ) , 'phase_criteria_type_id' )
154
+ if ( phaseCriteriaTypeId ) {
155
+ logger . debug ( `Will create phase constraint ${ constraintName } with value ${ constraintValue } ` )
156
+ // Ideally we should update the existing phase criteria, but this processor will go away in weeks
157
+ // and it's a backend processor, so we can just drop and recreate without slowing down anything
158
+ await phaseCriteriaService . dropPhaseCriteria ( projectPhaseId , phaseCriteriaTypeId )
159
+ await phaseCriteriaService . createPhaseCriteria ( projectPhaseId , phaseCriteriaTypeId , constraintValue , createdBy )
160
+ } else {
161
+ logger . warn ( `Could not find phase criteria type for ${ constraintName } ` )
162
+ }
163
+ }
164
+
165
+ }
166
+ logger . info ( 'addPhaseConstraints :: end' )
167
+ }
168
+
95
169
/**
96
170
* Sync the information from the v5 phases into legacy
97
171
* @param {Number } legacyId the legacy challenge ID
@@ -806,6 +880,7 @@ async function processMessage (message) {
806
880
if ( ! _ . get ( message . payload , 'task.isTask' ) ) {
807
881
const numOfReviewers = 2
808
882
await syncChallengePhases ( legacyId , message . payload . phases , createdByUserId , _ . get ( message , 'payload.legacy.selfService' ) , numOfReviewers , isBeingActivated )
883
+ await addPhaseConstraints ( legacyId , message . payload . phases , createdByUserId ) ;
809
884
needSyncV4ES = true
810
885
} else {
811
886
logger . info ( 'Will skip syncing phases as the challenge is a task...' )
0 commit comments