From 08ca508c70ff6eaef21a9d78b261d5617b9c7d53 Mon Sep 17 00:00:00 2001 From: Mithun Kamath Date: Fri, 29 Oct 2021 17:35:56 +0530 Subject: [PATCH] #253 - do not store legacy challenge id if there isnt one --- src/common/helper.js | 2 ++ src/services/SubmissionService.js | 21 ++++++++++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/common/helper.js b/src/common/helper.js index c6f91ed5..e1159432 100755 --- a/src/common/helper.js +++ b/src/common/helper.js @@ -766,6 +766,8 @@ function adjustSubmissionChallengeId (submission) { if (submission.challengeId && submission.legacyChallengeId) { submission.v5ChallengeId = submission.challengeId submission.challengeId = submission.legacyChallengeId + } else if (submission.challengeId && !submission.legacyChallengeId) { + submission.v5ChallengeId = submission.challengeId } } diff --git a/src/services/SubmissionService.js b/src/services/SubmissionService.js index 06281ccd..336049e3 100755 --- a/src/services/SubmissionService.js +++ b/src/services/SubmissionService.js @@ -282,13 +282,17 @@ function * createSubmission (authUser, files, entity) { url: url, memberId: entity.memberId, challengeId, - legacyChallengeId, created: currDate, updated: currDate, createdBy: authUser.handle || authUser.sub, updatedBy: authUser.handle || authUser.sub } + // Pure v5 challenges won't have a legacy challenge id + if (legacyChallengeId) { + item.legacyChallengeId = legacyChallengeId + } + if (entity.legacySubmissionId) { item.legacySubmissionId = entity.legacySubmissionId } @@ -405,6 +409,13 @@ function * _updateSubmission (authUser, submissionId, entity) { challengeId = yield helper.getV5ChallengeId(entity.challengeId) legacyChallengeId = yield helper.getLegacyChallengeId(entity.challengeId) } + if (exist.legacyChallengeId && !legacyChallengeId) { + // Original submission contains a legacy challenge id + // But with this update, it does not + // Prevent updates to current submission + // else we will be left with a submission with wrong legacy challenge id + throw new errors.HttpStatusError(400, `Cannot update submission with v5 challenge id since it already has a legacy challenge id associated with it`) + } // Record used for updating in Database const record = { TableName: table, @@ -412,13 +423,12 @@ function * _updateSubmission (authUser, submissionId, entity) { id: submissionId }, UpdateExpression: `set #type = :t, #url = :u, memberId = :m, challengeId = :c, - legacyChallengeId = :lc, updated = :ua, updatedBy = :ub, submittedDate = :sb`, + updated = :ua, updatedBy = :ub, submittedDate = :sb`, ExpressionAttributeValues: { ':t': entity.type || exist.type, ':u': entity.url || exist.url, ':m': entity.memberId || exist.memberId, ':c': challengeId, - ':lc': legacyChallengeId, ':ua': currDate, ':ub': authUser.handle || authUser.sub, ':sb': entity.submittedDate || exist.submittedDate || exist.created @@ -429,6 +439,11 @@ function * _updateSubmission (authUser, submissionId, entity) { } } + if (legacyChallengeId) { + record.UpdateExpression = record.UpdateExpression + ', legacyChallengeId = :lc' + record.ExpressionAttributeValues[':lc'] = legacyChallengeId + } + // If legacy submission ID exists, add it to the update expression if (entity.legacySubmissionId || exist.legacySubmissionId) { record.UpdateExpression = record.UpdateExpression + ', legacySubmissionId = :ls'