@@ -91,6 +91,15 @@ getReview.schema = {
9191 * @return {Object } Data fetched from ES
9292 */
9393function * 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+
94103 return yield helper . fetchFromES ( query , helper . camelize ( table ) )
95104}
96105
@@ -134,6 +143,20 @@ function * createReview (authUser, entity) {
134143
135144 const currDate = new Date ( ) . toISOString ( )
136145
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+
137160 const item = _ . extend (
138161 {
139162 id : uuid ( ) ,
@@ -240,6 +263,28 @@ function * _updateReview (authUser, reviewId, entity) {
240263
241264 const currDate = new Date ( ) . toISOString ( )
242265
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+
243288 // Record used for updating in Database
244289 const record = {
245290 TableName : table ,
@@ -248,17 +293,19 @@ function * _updateReview (authUser, reviewId, entity) {
248293 } ,
249294 UpdateExpression : `set score = :s, scoreCardId = :sc, submissionId = :su,
250295 typeId = :t, reviewerId = :r, #st = :st,
251- updated = :ua, updatedBy = :ub, reviewedDate = :rd` ,
296+ updated = :ua, updatedBy = :ub, reviewedDate = :rd
297+ ${ v5ScoreCardId ? ', v5ScoreCardId = :v5s' : '' } ` ,
252298 ExpressionAttributeValues : {
253299 ':s' : entity . score || exist . score ,
254- ':sc' : entity . scoreCardId || exist . scoreCardId ,
300+ ':sc' : scoreCardId ,
255301 ':su' : entity . submissionId || exist . submissionId ,
256302 ':t' : entity . typeId || exist . typeId ,
257303 ':r' : entity . reviewerId || exist . reviewerId ,
258304 ':st' : entity . status || exist . status || 'completed' ,
259305 ':ua' : currDate ,
260306 ':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 } : { } )
262309 } ,
263310 ExpressionAttributeNames : {
264311 '#st' : 'status'
@@ -293,7 +340,11 @@ function * _updateReview (authUser, reviewId, entity) {
293340 updatedBy : authUser . handle || authUser . sub ,
294341 reviewedDate : entity . reviewedDate || exist . reviewedDate || exist . created
295342 } ,
296- entity
343+ entity ,
344+ {
345+ scoreCardId,
346+ v5ScoreCardId
347+ }
297348 )
298349 }
299350
@@ -305,7 +356,9 @@ function * _updateReview (authUser, reviewId, entity) {
305356 return _ . extend ( exist , entity , {
306357 updated : currDate ,
307358 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
309362 } )
310363}
311364
0 commit comments