@@ -91,6 +91,15 @@ getReview.schema = {
91
91
* @return {Object } Data fetched from ES
92
92
*/
93
93
function * listReviews ( query ) {
94
+ if ( query . scoreCardId ) {
95
+ // Always use legacy scorecard id since v5 isn't stored in db
96
+ query . scoreCardId = helper . getLegacyScoreCardId ( query . scoreCardId )
97
+
98
+ if ( ! query . scoreCardId ) {
99
+ throw new errors . HttpStatusError ( 400 , 'Legacy scorecard id not found for the provided v5 scorecard id' )
100
+ }
101
+ }
102
+
94
103
return yield helper . fetchFromES ( query , helper . camelize ( table ) )
95
104
}
96
105
@@ -134,6 +143,20 @@ function * createReview (authUser, entity) {
134
143
135
144
const currDate = new Date ( ) . toISOString ( )
136
145
146
+ const possibleV5ScoreCardId = entity . scoreCardId
147
+
148
+ // Always use legacy id instead of v5 legacy id
149
+ entity . scoreCardId = helper . getLegacyScoreCardId ( entity . scoreCardId )
150
+
151
+ if ( ! entity . scoreCardId ) {
152
+ throw new errors . HttpStatusError ( 400 , 'Legacy scorecard id not found for the provided v5 scorecard id' )
153
+ }
154
+
155
+ if ( entity . scoreCardId !== possibleV5ScoreCardId ) {
156
+ // Remember the v5 score card id too that was passed
157
+ entity . v5ScoreCardId = possibleV5ScoreCardId
158
+ }
159
+
137
160
const item = _ . extend (
138
161
{
139
162
id : uuid ( ) ,
@@ -240,6 +263,28 @@ function * _updateReview (authUser, reviewId, entity) {
240
263
241
264
const currDate = new Date ( ) . toISOString ( )
242
265
266
+ let scoreCardId = exist . scoreCardId
267
+ let v5ScoreCardId = exist . v5ScoreCardId
268
+
269
+ if ( entity . scoreCardId ) {
270
+ scoreCardId = helper . getLegacyScoreCardId ( entity . scoreCardId )
271
+
272
+ if ( ! scoreCardId ) {
273
+ throw new errors . HttpStatusError ( 400 , 'Legacy scorecard id not found for the provided v5 scorecard id' )
274
+ }
275
+
276
+ if ( entity . scoreCardId !== scoreCardId ) {
277
+ v5ScoreCardId = entity . scoreCardId
278
+ } else if ( v5ScoreCardId ) {
279
+ // Since we currently have a static mapping b/w legacy and v5 scorecard ids,
280
+ // there is no way for us to fetch the v5 scorecard id for a legacy scorecard id
281
+ // In this scenario, the review already had a v5 scorecard id, so if it's legacy
282
+ // scorecard id gets updated, we would also need to update the v5 scorecard id
283
+ // which we cannot - hence the error. Ideally, in this case, a new review needs to be created
284
+ throw new errors . HttpStatusError ( 400 , `Cannot update legacy scorecard id since review with id ${ reviewId } already has a v5 scorecard id` )
285
+ }
286
+ }
287
+
243
288
// Record used for updating in Database
244
289
const record = {
245
290
TableName : table ,
@@ -248,17 +293,19 @@ function * _updateReview (authUser, reviewId, entity) {
248
293
} ,
249
294
UpdateExpression : `set score = :s, scoreCardId = :sc, submissionId = :su,
250
295
typeId = :t, reviewerId = :r, #st = :st,
251
- updated = :ua, updatedBy = :ub, reviewedDate = :rd` ,
296
+ updated = :ua, updatedBy = :ub, reviewedDate = :rd
297
+ ${ v5ScoreCardId ? ', v5ScoreCardId = :v5s' : '' } ` ,
252
298
ExpressionAttributeValues : {
253
299
':s' : entity . score || exist . score ,
254
- ':sc' : entity . scoreCardId || exist . scoreCardId ,
300
+ ':sc' : scoreCardId ,
255
301
':su' : entity . submissionId || exist . submissionId ,
256
302
':t' : entity . typeId || exist . typeId ,
257
303
':r' : entity . reviewerId || exist . reviewerId ,
258
304
':st' : entity . status || exist . status || 'completed' ,
259
305
':ua' : currDate ,
260
306
':ub' : authUser . handle || authUser . sub ,
261
- ':rd' : entity . reviewedDate || exist . reviewedDate || exist . created
307
+ ':rd' : entity . reviewedDate || exist . reviewedDate || exist . created ,
308
+ ...( v5ScoreCardId ? { ':v5s' : v5ScoreCardId } : { } )
262
309
} ,
263
310
ExpressionAttributeNames : {
264
311
'#st' : 'status'
@@ -293,7 +340,11 @@ function * _updateReview (authUser, reviewId, entity) {
293
340
updatedBy : authUser . handle || authUser . sub ,
294
341
reviewedDate : entity . reviewedDate || exist . reviewedDate || exist . created
295
342
} ,
296
- entity
343
+ entity ,
344
+ {
345
+ scoreCardId,
346
+ v5ScoreCardId
347
+ }
297
348
)
298
349
}
299
350
@@ -305,7 +356,9 @@ function * _updateReview (authUser, reviewId, entity) {
305
356
return _ . extend ( exist , entity , {
306
357
updated : currDate ,
307
358
updatedBy : authUser . handle || authUser . sub ,
308
- reviewedDate : entity . reviewedDate || exist . reviewedDate || exist . created
359
+ reviewedDate : entity . reviewedDate || exist . reviewedDate || exist . created ,
360
+ scoreCardId,
361
+ v5ScoreCardId
309
362
} )
310
363
}
311
364
0 commit comments