Skip to content

Commit a8fd596

Browse files
authored
Merge pull request topcoder-platform#247 from topcoder-platform/Issue_246
Issue 246 - support v5 scorecard id
2 parents 339b583 + ecf373c commit a8fd596

File tree

5 files changed

+105
-11
lines changed

5 files changed

+105
-11
lines changed

config/default.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,15 @@ module.exports = {
4242
AUTH0_PROXY_SERVER_URL: process.env.AUTH0_PROXY_SERVER_URL,
4343
FETCH_CREATED_DATE_START: process.env.FETCH_CREATED_DATE_START || '2021-01-01',
4444
FETCH_PAGE_SIZE: process.env.FETCH_PAGE_SIZE || 500,
45-
MIGRATE_CHALLENGES: process.env.MIGRATE_CHALLENGES || []
45+
MIGRATE_CHALLENGES: process.env.MIGRATE_CHALLENGES || [],
46+
47+
V5TOLEGACYSCORECARDMAPPING: {
48+
'c56a4180-65aa-42ec-a945-5fd21dec0501': 30001363,
49+
'c56a4180-65aa-42ec-a945-5fd21dec0502': 123456789,
50+
'c56a4180-65aa-42ec-a945-5fd21dec0503': 30001031,
51+
'c56a4180-65aa-42ec-a945-5fd21dec0504': 987654321,
52+
'c56a4180-65aa-42ec-a945-5fd21dec0505': 987123456,
53+
'9ecc88e5-a4ee-44a4-8ec1-70bd98022510': 123789456,
54+
'd6d31f34-8ee5-4589-ae65-45652fcc01a6': 30000720
55+
}
4656
}

docs/swagger.yaml

+9-3
Original file line numberDiff line numberDiff line change
@@ -1757,7 +1757,9 @@ components:
17571757
description: The scoreCardId filter of the reviews associated with the submission.
17581758
required: false
17591759
schema:
1760-
type: integer
1760+
oneOf:
1761+
- type: integer
1762+
- type: string
17611763
filterSubmissionReviewSubmissionId:
17621764
in: query
17631765
name: review.submissionId
@@ -1871,7 +1873,9 @@ components:
18711873
description: The score card id filter for reviews.
18721874
required: false
18731875
schema:
1874-
type: integer
1876+
oneOf:
1877+
- type: integer
1878+
- type: string
18751879
filterReviewSubmissionId:
18761880
in: query
18771881
name: submissionId
@@ -2147,7 +2151,9 @@ components:
21472151
example: a12bc280-65ab-42ec-a945-5fd21dec1567
21482152
description: The review reviewer id.
21492153
scoreCardId:
2150-
type: integer
2154+
oneOf:
2155+
- type: integer
2156+
- type: string
21512157
description: The review score card id.
21522158
example: 123456789
21532159
submissionId:

src/common/helper.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,21 @@ function * getLatestChallenges (page) {
838838
}
839839
}
840840

841+
/**
842+
* Get legacy scorecard id if the scorecard id is uuid form
843+
* @param {String} scoreCardId Scorecard ID
844+
* @returns {String} Legacy scorecard ID of the given challengeId
845+
*/
846+
function getLegacyScoreCardId (scoreCardId) {
847+
if (/^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(scoreCardId)) {
848+
logger.debug(`${scoreCardId} detected as uuid. Converting to legacy scorecard id`)
849+
850+
return config.get('V5TOLEGACYSCORECARDMAPPING')[scoreCardId]
851+
}
852+
853+
return scoreCardId
854+
}
855+
841856
module.exports = {
842857
wrapExpress,
843858
autoWrapExpress,
@@ -856,5 +871,6 @@ module.exports = {
856871
getRoleIdToRoleNameMap,
857872
getV5ChallengeId,
858873
adjustSubmissionChallengeId,
859-
getLatestChallenges
874+
getLatestChallenges,
875+
getLegacyScoreCardId
860876
}

src/services/ReviewService.js

+58-5
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,15 @@ getReview.schema = {
9191
* @return {Object} Data fetched from ES
9292
*/
9393
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+
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

src/services/SubmissionService.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,15 @@ function * listSubmissions (authUser, query) {
188188
query.challengeId = yield helper.getV5ChallengeId(query.challengeId)
189189
}
190190

191+
// TODO - support v5 for review scorecardid
192+
if (query['review.scoreCardId']) {
193+
query['review.scoreCardId'] = helper.getLegacyScoreCardId(query['review.scoreCardId'])
194+
195+
if (!query['review.scoreCardId']) {
196+
throw new errors.HttpStatusError(400, 'Legacy scorecard id not found for the provided v5 scorecard id')
197+
}
198+
}
199+
191200
const data = yield helper.fetchFromES(query, helper.camelize(table))
192201
logger.info(`Data returned from ES: ${JSON.stringify(data, null, 4)}`)
193202
logger.info(`listSubmissions: returning ${data.length} submissions for query: ${JSON.stringify(query)}`)
@@ -216,7 +225,7 @@ const listSubmissionsQuerySchema = {
216225
'review.score': joi.score(),
217226
'review.typeId': joi.string().uuid(),
218227
'review.reviewerId': joi.string().uuid(),
219-
'review.scoreCardId': joi.id(),
228+
'review.scoreCardId': joi.alternatives().try(joi.id(), joi.string().uuid()),
220229
'review.submissionId': joi.string().uuid(),
221230
'review.status': joi.reviewStatus(),
222231
'reviewSummation.scoreCardId': joi.id(),

0 commit comments

Comments
 (0)