@@ -22,7 +22,7 @@ const getExperimentWorkflowByCallId = (callId: number) => {
2222
2323export const getWorkflowConnectionByStatusId = (
2424 workflowId : number ,
25- statusId : number ,
25+ statusId ? : number ,
2626 prevStatusId ?: number
2727) => {
2828 const workflowDataSource = container . resolve < WorkflowDataSource > (
@@ -45,7 +45,6 @@ const shouldMoveToNextStatus = (
4545 experimentSafetyEventsKey as keyof ExperimentSafetyEventsRecord
4646 ]
4747 ) ;
48-
4948 const allNextStatusRulesFulfilled = ! statusChangingEvents . some (
5049 ( statusChangingEvent ) =>
5150 allExperimentIncompleteEvents . indexOf (
@@ -203,78 +202,78 @@ export const workflowEngine = async (
203202 * We store one record of FEASIBILITY_REVIEW with nextStatusId = FAP_SELECTION and another one with nextStatusId = NOT_FEASIBLE.
204203 * We go through each record and based on the currentEvent we move the proposal into the right direction
205204 */
206- return Promise . all (
205+ const response = await Promise . all (
207206 currentWorkflowConnections . map ( async ( currentWorkflowConnection ) => {
208- if ( ! currentWorkflowConnection . nextStatusId ) {
209- return ;
210- }
211-
212- if ( ! experimentSafetyWithEvents . experimentSafetyEvents ) {
213- return ;
214- }
215-
216207 const nextWorkflowConnections =
217208 await getWorkflowConnectionByStatusId (
218209 experimentWorkflow . id ,
219- currentWorkflowConnection . nextStatusId ,
210+ undefined ,
220211 currentWorkflowConnection . statusId
221212 ) ;
222- if ( ! nextWorkflowConnections ?. length ) {
223- return ;
224- }
225213
226- const workflowDataSource = container . resolve < WorkflowDataSource > (
227- Tokens . WorkflowDataSource
214+ return Promise . all (
215+ nextWorkflowConnections . map ( async ( nextWorkflowConnection ) => {
216+ if ( ! experimentSafetyWithEvents . experimentSafetyEvents ) {
217+ return ;
218+ }
219+ const workflowDataSource =
220+ container . resolve < WorkflowDataSource > (
221+ Tokens . WorkflowDataSource
222+ ) ;
223+
224+ const statusChangingEvents =
225+ await workflowDataSource . getStatusChangingEventsByConnectionIds (
226+ [ nextWorkflowConnection . id ]
227+ ) ;
228+ if ( ! statusChangingEvents ) {
229+ return ;
230+ }
231+
232+ const eventThatTriggeredStatusChangeIsStatusChangingEvent =
233+ statusChangingEvents . find (
234+ ( statusChangingEvent ) =>
235+ experimentSafetyWithEvents . currentEvent ===
236+ statusChangingEvent . statusChangingEvent
237+ ) ;
238+
239+ if ( ! eventThatTriggeredStatusChangeIsStatusChangingEvent ) {
240+ return ;
241+ }
242+
243+ if (
244+ shouldMoveToNextStatus (
245+ statusChangingEvents ,
246+ experimentSafetyWithEvents . experimentSafetyEvents
247+ )
248+ ) {
249+ const updatedExperimentSafety =
250+ await experimentDataSource . updateExperimentSafetyStatus (
251+ experimentSafety . experimentSafetyPk ,
252+ nextWorkflowConnection . statusId
253+ ) ;
254+
255+ if ( updatedExperimentSafety ) {
256+ await checkIfConditionsForNextStatusAreMet ( {
257+ nextWorkflowConnections,
258+ experimentWorkflow,
259+ workflowDataSource,
260+ experimentSafetyWithEvents,
261+ } ) ;
262+
263+ return {
264+ ...updatedExperimentSafety ,
265+ workflowId : experimentWorkflow . id ,
266+ prevStatusId : currentWorkflowConnection . statusId ,
267+ callShortCode : call . shortCode ,
268+ } ;
269+ }
270+ }
271+ } )
228272 ) ;
229-
230- const statusChangingEvents =
231- await workflowDataSource . getStatusChangingEventsByConnectionIds (
232- nextWorkflowConnections . map ( ( connection ) => connection . id )
233- ) ;
234- if ( ! statusChangingEvents ) {
235- return ;
236- }
237-
238- const eventThatTriggeredStatusChangeIsStatusChangingEvent =
239- statusChangingEvents . find (
240- ( statusChangingEvent ) =>
241- experimentSafetyWithEvents . currentEvent ===
242- statusChangingEvent . statusChangingEvent
243- ) ;
244- if ( ! eventThatTriggeredStatusChangeIsStatusChangingEvent ) {
245- return ;
246- }
247-
248- if (
249- shouldMoveToNextStatus (
250- statusChangingEvents ,
251- experimentSafetyWithEvents . experimentSafetyEvents
252- )
253- ) {
254- const updatedExperimentSafety =
255- await experimentDataSource . updateExperimentSafetyStatus (
256- experimentSafety . experimentSafetyPk ,
257- currentWorkflowConnection . nextStatusId
258- ) ;
259-
260- if ( updatedExperimentSafety ) {
261- await checkIfConditionsForNextStatusAreMet ( {
262- nextWorkflowConnections,
263- experimentWorkflow,
264- workflowDataSource,
265- experimentSafetyWithEvents,
266- } ) ;
267-
268- return {
269- ...updatedExperimentSafety ,
270- workflowId : experimentWorkflow . id ,
271- prevStatusId : currentWorkflowConnection . statusId ,
272- callShortCode : call . shortCode ,
273- } ;
274- }
275- }
276273 } )
277- ) ;
274+ ) . then ( ( results ) => results . flat ( ) ) ;
275+
276+ return response ;
278277 } )
279278 )
280279 ) . flat ( ) ;
0 commit comments