@@ -2,23 +2,7 @@ import { actionLibrary } from '../../pluginsConnect'
2
2
import { getApplicationData } from '.././getApplicationData'
3
3
import { combineRequestParams } from '../../utilityFunctions'
4
4
import DBConnect from '../../databaseConnect'
5
- import db from './databaseMethods'
6
- import { processTrigger } from '.././processTrigger'
7
- import {
8
- ActionQueueStatus ,
9
- ApplicationOutcome ,
10
- ApplicationStatus ,
11
- Decision ,
12
- Trigger ,
13
- } from '../../../generated/graphql'
14
- import {
15
- selectRandomReviewAssignment ,
16
- getRandomReviewId ,
17
- getApplicationBasics ,
18
- mapTriggerShortcut ,
19
- triggerMap ,
20
- } from './helpers'
21
- import { ActionApplicationData , ActionResult , TriggerPayload } from '../../../types'
5
+ import { testTrigger , RequestProps } from './testTrigger'
22
6
23
7
// These routes should only be used for testing in development. They should
24
8
// NEVER be used in the app.
@@ -34,242 +18,10 @@ export const routeRunAction = async (request: any, reply: any) => {
34
18
return reply . send ( actionResult )
35
19
}
36
20
37
- export interface RequestProps {
38
- templateCode : string
39
- trigger : Trigger | 'RESET' | keyof typeof triggerMap
40
- assignmentId ?: number
41
- sections ?: string [ ]
42
- reviewId ?: number
43
- decision ?: Decision
44
- comment ?: string
45
- eventCode ?: string
46
- applicationId ?: number
47
- serial ?: string
48
- stageNumber ?: number
49
- status ?: ApplicationStatus
50
- outcome ?: ApplicationOutcome
51
- applicationDataOverride ?: Partial < ActionApplicationData >
52
- suppressEmail ?: boolean
53
- }
54
-
55
21
// Wrapper for "testTrigger". Use routeTestTrigger provides the REST endpoint,
56
22
// "testTrigger" is the actual function.
57
23
export const routeTestTrigger = async ( request : any , reply : any ) => {
58
24
const params : RequestProps = combineRequestParams ( request , 'camel' )
59
25
60
26
reply . send ( await testTrigger ( params ) )
61
27
}
62
-
63
- export const testTrigger = async ( params : RequestProps ) => {
64
- let { applicationId, serial } = params
65
- const {
66
- templateCode,
67
- trigger,
68
- assignmentId,
69
- sections,
70
- reviewId,
71
- decision,
72
- comment,
73
- eventCode,
74
- stageNumber,
75
- status,
76
- outcome,
77
- // Prevents actual email from being sent, safer when testing
78
- suppressEmail = true ,
79
- applicationDataOverride = { } ,
80
- } = params
81
-
82
- const triggerFull = mapTriggerShortcut ( trigger as string )
83
-
84
- const { configId, configSerial, templateId, sectionCodes } = await db . getConfigApplicationInfo (
85
- templateCode
86
- )
87
-
88
- if ( ! configId ) return 'Invalid template code, or no config application available'
89
-
90
- const { appId, appSerial } = await getApplicationBasics (
91
- applicationId ,
92
- serial ,
93
- configId ,
94
- configSerial
95
- )
96
-
97
- applicationId = appId
98
- serial = appSerial
99
-
100
- let actionsOutput : ActionResult [ ] = [ ]
101
- let finalApplicationData
102
-
103
- if ( stageNumber ) applicationDataOverride . stageNumber = stageNumber
104
- if ( status ) applicationDataOverride . status = status
105
- if ( outcome ) applicationDataOverride . outcome = outcome
106
- applicationDataOverride . other = { suppressEmail }
107
-
108
- // A dummy triggerPayload object, as though it was retrieved from the
109
- // trigger_queue table
110
- const triggerPayload : TriggerPayload = {
111
- trigger_id : null ,
112
- trigger : Trigger . DevTest ,
113
- table : 'application' ,
114
- record_id : appId ,
115
- applicationDataOverride,
116
- }
117
-
118
- switch ( triggerFull ) {
119
- case 'DEV_TEST' :
120
- actionsOutput = await processTrigger ( triggerPayload )
121
- finalApplicationData = await getApplicationData ( { applicationId } )
122
- break
123
-
124
- case 'ON_APPLICATION_CREATE' :
125
- // To-do: actually create an "is_config" application -- currently relies
126
- // on one already existing
127
- triggerPayload . trigger = Trigger . OnApplicationCreate
128
- actionsOutput = await processTrigger ( triggerPayload )
129
- finalApplicationData = await getApplicationData ( { applicationId } )
130
- break
131
-
132
- case 'ON_APPLICATION_RESTART' :
133
- triggerPayload . trigger = Trigger . OnApplicationRestart
134
- actionsOutput = await processTrigger ( triggerPayload )
135
- finalApplicationData = await getApplicationData ( { applicationId } )
136
- break
137
-
138
- case 'ON_APPLICATION_SUBMIT' :
139
- triggerPayload . trigger = Trigger . OnApplicationSubmit
140
- actionsOutput = await processTrigger ( triggerPayload )
141
- finalApplicationData = await getApplicationData ( { applicationId } )
142
- break
143
-
144
- case 'ON_EXTEND' :
145
- triggerPayload . trigger = Trigger . OnExtend
146
- actionsOutput = await processTrigger ( triggerPayload )
147
- finalApplicationData = await getApplicationData ( { applicationId } )
148
- break
149
-
150
- case 'ON_SCHEDULE' :
151
- if ( ! eventCode ) return 'eventCode required'
152
- const { id, data } = await db . getScheduledEvent ( applicationId , eventCode )
153
- triggerPayload . trigger = Trigger . OnSchedule
154
- triggerPayload . table = 'trigger_schedule'
155
- triggerPayload . record_id = id
156
- triggerPayload . data = data
157
- actionsOutput = await processTrigger ( triggerPayload )
158
- finalApplicationData = await getApplicationData ( { applicationId } )
159
- break
160
-
161
- case 'ON_PREVIEW' :
162
- const revId = reviewId ?? ( await getRandomReviewId ( applicationId ) )
163
- triggerPayload . trigger = Trigger . OnPreview
164
- triggerPayload . table = 'review'
165
- triggerPayload . record_id = revId
166
- triggerPayload . applicationDataOverride = {
167
- ...applicationDataOverride ,
168
- reviewData : {
169
- latestDecision : {
170
- decision : decision ?? Decision . Conform ,
171
- comment : comment ?? 'Test comment' ,
172
- } ,
173
- } ,
174
- }
175
-
176
- actionsOutput = await processTrigger ( triggerPayload )
177
- finalApplicationData = await getApplicationData ( { applicationId, reviewId : revId } )
178
- break
179
-
180
- case 'ON_REVIEW_ASSIGN' :
181
- // If no assignment id, select one at random (using latest stage and level) and assign all possible sections to it
182
- {
183
- const assignment = assignmentId
184
- ? await db . getSingleReviewAssignment ( assignmentId )
185
- : await selectRandomReviewAssignment ( applicationId , sectionCodes )
186
- if ( ! assignment . allowedSections ) assignment . allowedSections = sectionCodes
187
- if ( sections )
188
- assignment . allowedSections = assignment . allowedSections . filter ( ( section : string ) =>
189
- sections . includes ( section )
190
- )
191
- await db . assignReview ( assignment )
192
-
193
- triggerPayload . trigger = Trigger . OnReviewAssign
194
- triggerPayload . table = 'review_assignment'
195
- triggerPayload . record_id = assignment . id
196
-
197
- actionsOutput = await processTrigger ( triggerPayload )
198
- finalApplicationData = await getApplicationData ( {
199
- applicationId,
200
- reviewAssignmentId : assignmentId ,
201
- } )
202
- }
203
- break
204
-
205
- case 'ON_REVIEW_CREATE' :
206
- // Get assignment id -- either passed in, or pick one at random (assigned and highest stage/level)
207
- {
208
- const assignment = assignmentId
209
- ? await db . getSingleReviewAssignment ( assignmentId )
210
- : await selectRandomReviewAssignment ( applicationId , sectionCodes , true )
211
- // Create a review record and review_decision record
212
- const { id : revId } = await db . createReview ( assignment . id )
213
- await db . createReviewDecision ( revId )
214
- // Create one review response
215
-
216
- // TO-DO: Make some review responses and give them dummy values?
217
-
218
- triggerPayload . trigger = Trigger . OnReviewCreate
219
- triggerPayload . table = 'review'
220
- triggerPayload . record_id = revId
221
-
222
- actionsOutput = await processTrigger ( triggerPayload )
223
- finalApplicationData = await getApplicationData ( { applicationId, reviewId : revId } )
224
- }
225
- break
226
-
227
- case 'ON_REVIEW_RESTART' :
228
- {
229
- const revId = reviewId ?? ( await getRandomReviewId ( applicationId ) )
230
-
231
- triggerPayload . trigger = Trigger . OnReviewRestart
232
- triggerPayload . table = 'review'
233
- triggerPayload . record_id = revId
234
-
235
- actionsOutput = await processTrigger ( triggerPayload )
236
- finalApplicationData = await getApplicationData ( { applicationId, reviewId : revId } )
237
- }
238
- break
239
-
240
- case 'ON_REVIEW_SUBMIT' :
241
- {
242
- const revId = reviewId ?? ( await getRandomReviewId ( applicationId ) )
243
- await db . updateReviewDecision ( revId , decision ?? Decision . Conform , comment ?? 'No comment' )
244
-
245
- triggerPayload . trigger = Trigger . OnReviewSubmit
246
- triggerPayload . table = 'review'
247
- triggerPayload . record_id = revId
248
-
249
- actionsOutput = await processTrigger ( triggerPayload )
250
- finalApplicationData = await getApplicationData ( { applicationId, reviewId : revId } )
251
- }
252
- break
253
-
254
- case 'RESET' :
255
- await db . resetApplication ( applicationId )
256
- finalApplicationData = await getApplicationData ( { applicationId } )
257
- break
258
-
259
- default :
260
- return 'Trigger not recognised'
261
- }
262
-
263
- const failedActions = actionsOutput . filter (
264
- ( action ) => action . status !== ActionQueueStatus . Success
265
- )
266
-
267
- return {
268
- applicationId,
269
- serial,
270
- trigger : triggerFull ,
271
- failedActions : failedActions . length > 0 ? failedActions : undefined ,
272
- actionResult : actionsOutput ,
273
- finalApplicationData,
274
- }
275
- }
0 commit comments