From 2171507bed6bcd89d4c48627dc25839b412a04b5 Mon Sep 17 00:00:00 2001 From: Thomas Kranitsas Date: Thu, 9 Mar 2023 17:29:57 +0200 Subject: [PATCH] integrate submission.update, review and reviewSummation with dal --- src/services/ReviewService.js | 118 ++++++++----------------- src/services/ReviewSummationService.js | 115 ++++++++---------------- src/services/SubmissionService.js | 78 ++++++---------- 3 files changed, 103 insertions(+), 208 deletions(-) diff --git a/src/services/ReviewService.js b/src/services/ReviewService.js index 12b96b21..31c13705 100644 --- a/src/services/ReviewService.js +++ b/src/services/ReviewService.js @@ -7,6 +7,7 @@ const _ = require('lodash') const uuid = require('uuid/v4') const joi = require('joi') const logger = require('winston') +const config = require('config') const dbhelper = require('../common/dbhelper') const helper = require('../common/helper') @@ -14,6 +15,17 @@ const { originator, mimeType, events } = require('../../constants').busApiMeta const HelperService = require('./HelperService') const SubmissionService = require('./SubmissionService') +const { ReviewDomain } = require("@topcoder-framework/domain-submission"); + +const { + DomainHelper: { getLookupCriteria, getScanCriteria }, +} = require("@topcoder-framework/lib-common"); + +const reviewDomain = new ReviewDomain( + config.GRPC_SUBMISSION_SERVER_HOST, + config.GRPC_SUBMISSION_SERVER_PORT +); + const table = 'Review' /** @@ -24,14 +36,7 @@ const table = 'Review' */ async function _getReview(reviewId) { // Construct filter to retrieve record from Database - const filter = { - TableName: table, - Key: { - id: reviewId - } - } - const result = await dbhelper.getRecord(filter) - return result.Item + return reviewDomain.lookup(getLookupCriteria("id", reviewId)) } /** @@ -157,33 +162,17 @@ async function createReview(authUser, entity) { entity.v5ScoreCardId = possibleV5ScoreCardId } - const item = _.extend( - { - id: uuid(), - created: currDate, - updated: currDate, - createdBy: authUser.handle || authUser.sub, - updatedBy: authUser.handle || authUser.sub, - status: entity.status || 'completed' - }, - entity - ) - if (_.intersection(authUser.roles, ['Administrator', 'administrator']).length === 0 && !authUser.scopes) { if (entity.reviewedDate) { throw new errors.HttpStatusError(403, 'You are not allowed to set the `reviewedDate` attribute on a review') } } - item.reviewedDate = entity.reviewedDate || item.created - - // Prepare record to be inserted - const record = { - TableName: table, - Item: item - } - - await dbhelper.insertRecord(record) + const createdItem = await reviewDomain.create({ + ...entity, + status: entity.status || 'completed', + reviewedDate: entity.reviewedDate || item.created, + }) // Push Review created event to Bus API // Request body for Posting to Bus API @@ -196,7 +185,7 @@ async function createReview(authUser, entity) { { resource: helper.camelize(table) }, - item + createdItem ) } @@ -205,7 +194,7 @@ async function createReview(authUser, entity) { // Inserting records in DynamoDB doesn't return any response // Hence returning the same entity to be in compliance with Swagger - return item + return createdItem } createReview.schema = { @@ -285,45 +274,24 @@ async function _updateReview(authUser, reviewId, entity) { } } - // Record used for updating in Database - const record = { - TableName: table, - Key: { - id: reviewId - }, - UpdateExpression: `set score = :s, scoreCardId = :sc, submissionId = :su, - typeId = :t, reviewerId = :r, #st = :st, - updated = :ua, updatedBy = :ub, reviewedDate = :rd - ${v5ScoreCardId ? ', v5ScoreCardId = :v5s' : ''}`, - ExpressionAttributeValues: { - ':s': entity.score || exist.score, - ':sc': scoreCardId, - ':su': entity.submissionId || exist.submissionId, - ':t': entity.typeId || exist.typeId, - ':r': entity.reviewerId || exist.reviewerId, - ':st': entity.status || exist.status || 'completed', - ':ua': currDate, - ':ub': authUser.handle || authUser.sub, - ':rd': entity.reviewedDate || exist.reviewedDate || exist.created, - ...(v5ScoreCardId ? { ':v5s': v5ScoreCardId } : {}) - }, - ExpressionAttributeNames: { - '#st': 'status' - } - } - - // If metadata exists, add it to the update expression - if (entity.metadata || exist.metadata) { - record.UpdateExpression = - record.UpdateExpression + ', metadata = :ma' - record.ExpressionAttributeValues[':ma'] = _.merge( - {}, - exist.metadata, - entity.metadata - ) + const updatedData = { + score: entity.score || exist.score, + scoreCardId, + submissionId: entity.submissionId || exist.submissionId, + typeId: entity.typeId || exist.typeId, + reviewerId: entity.reviewerId || exist.reviewerId, + status: entity.status || exist.status || 'completed', + reviewedDate: entity.reviewedDate || exist.reviewedDate || exist.created, + ...(v5ScoreCardId ? { v5ScoreCardId } : {}), + ...(entity.metadata || exist.metadata ? { metadata: _.merge({}, exist.metadata, entity.metadata) } : {}) } - await dbhelper.updateRecord(record) + await reviewDomain.update({ + filterCriteria: getScanCriteria({ + id: reviewId, + }), + updateInput: updatedData + }) // Push Review updated event to Bus API // Request body for Posting to Bus API @@ -340,7 +308,7 @@ async function _updateReview(authUser, reviewId, entity) { updatedBy: authUser.handle || authUser.sub, reviewedDate: entity.reviewedDate || exist.reviewedDate || exist.created }, - entity, + updatedData, { scoreCardId, v5ScoreCardId @@ -353,7 +321,7 @@ async function _updateReview(authUser, reviewId, entity) { // Updating records in DynamoDB doesn't return any response // Hence returning the response which will be in compliance with Swagger - return _.extend(exist, entity, { + return _.extend(exist, updatedData, { updated: currDate, updatedBy: authUser.handle || authUser.sub, reviewedDate: entity.reviewedDate || exist.reviewedDate || exist.created, @@ -446,15 +414,7 @@ async function deleteReview(reviewId) { ) } - // Filter used to delete the record - const filter = { - TableName: table, - Key: { - id: reviewId - } - } - - await dbhelper.deleteRecord(filter) + await reviewDomain.delete(getLookupCriteria("id", reviewId)) // Push Review deleted event to Bus API // Request body for Posting to Bus API diff --git a/src/services/ReviewSummationService.js b/src/services/ReviewSummationService.js index b06353e2..2450169d 100644 --- a/src/services/ReviewSummationService.js +++ b/src/services/ReviewSummationService.js @@ -10,9 +10,21 @@ const dbhelper = require('../common/dbhelper') const helper = require('../common/helper') const { originator, mimeType, events } = require('../../constants').busApiMeta const HelperService = require('./HelperService') +const config = require('config') const table = 'ReviewSummation' +const { ReviewSummationDomain } = require("@topcoder-framework/domain-submission"); + +const { + DomainHelper: { getLookupCriteria, getScanCriteria }, +} = require("@topcoder-framework/lib-common"); + +const reviewSummationDomain = new ReviewSummationDomain( + config.GRPC_SUBMISSION_SERVER_HOST, + config.GRPC_SUBMISSION_SERVER_PORT +); + /** * Function to get Review summation based on ID from DynamoDB * This function will be used all by other functions to check existence of Review summation @@ -21,14 +33,7 @@ const table = 'ReviewSummation' */ async function _getReviewSummation(reviewSummationId) { // Construct filter to retrieve record from Database - const filter = { - TableName: table, - Key: { - id: reviewSummationId - } - } - const result = await dbhelper.getRecord(filter) - return result.Item + return reviewSummationDomain.lookup(getLookupCriteria("id", reviewSummationId)) } /** @@ -90,32 +95,16 @@ async function createReviewSummation(authUser, entity) { const currDate = (new Date()).toISOString() - const item = _.extend({ - id: uuid(), - created: currDate, - updated: currDate, - createdBy: authUser.handle || authUser.sub, - updatedBy: authUser.handle || authUser.sub - }, entity) - - if (entity.isFinal) { - item.isFinal = entity.isFinal - } - if (_.intersection(authUser.roles, ['Administrator', 'administrator']).length === 0 && !authUser.scopes) { if (entity.reviewedDate) { throw new errors.HttpStatusError(403, 'You are not allowed to set the `reviewedDate` attribute on a review summation') } } - item.reviewedDate = entity.reviewedDate || item.created - - const record = { - TableName: table, - Item: item - } - - await dbhelper.insertRecord(record) + const item = await reviewSummationDomain.create({ + ...entity, + reviewedDate: entity.reviewedDate || currDate, + }); // Push Review Summation created event to Bus API // Request body for Posting to Bus API @@ -175,46 +164,22 @@ async function _updateReviewSummation(authUser, reviewSummationId, entity) { const currDate = (new Date()).toISOString() - // Record used for updating in Database - const record = { - TableName: table, - Key: { - id: reviewSummationId - }, - UpdateExpression: `set aggregateScore = :s, scoreCardId = :sc, submissionId = :su, - isPassing = :ip, updated = :ua, updatedBy = :ub, reviewedDate = :rd`, - ExpressionAttributeValues: { - ':s': entity.aggregateScore || exist.aggregateScore, - ':sc': entity.scoreCardId || exist.scoreCardId, - ':su': entity.submissionId || exist.submissionId, - ':ip': isPassing, - ':ua': currDate, - ':ub': authUser.handle || authUser.sub, - ':rd': entity.reviewedDate || exist.reviewedDate || exist.created - } - } - - // If metadata exists, add it to the update expression - if (entity.metadata || exist.metadata) { - record.UpdateExpression = record.UpdateExpression + ', metadata = :ma' - record.ExpressionAttributeValues[':ma'] = _.merge({}, exist.metadata, entity.metadata) - } - - // If legacy submission ID exists, add it to the update expression - if (entity.isFinal || exist.isFinal) { - let isFinal // Attribute to store boolean value - - if (entity.isFinal === undefined) { - isFinal = exist.isFinal - } else { - isFinal = entity.isFinal - } - - record.UpdateExpression = record.UpdateExpression + ', isFinal = :ls' - record.ExpressionAttributeValues[':ls'] = isFinal - } - - await dbhelper.updateRecord(record) + const updatedData = { + aggregateScore: entity.aggregateScore || exist.aggregateScore, + scoreCardId: entity.scoreCardId || exist.scoreCardId, + submissionId: entity.submissionId || exist.submissionId, + isPassing: isPassing, + reviewedDate: entity.reviewedDate || exist.reviewedDate || exist.created, + ...(entity.metadata || exist.metadata ? { metadata: _.merge({}, exist.metadata, entity.metadata) } : {}), + ...(entity.isFinal || exist.isFinal ? { isFinal: entity.isFinal || exist.isFinal } : {}) + }; + + await reviewSummationDomain.update({ + filterCriteria: getScanCriteria({ + id: reviewSummationId, + }), + updateInput: updatedData + }) // Push Review Summation updated event to Bus API // Request body for Posting to Bus API @@ -229,7 +194,7 @@ async function _updateReviewSummation(authUser, reviewSummationId, entity) { updated: currDate, updatedBy: authUser.handle || authUser.sub, reviewedDate: entity.reviewedDate || exist.reviewedDate || exist.created - }, entity) + }, updatedData) } // Post to Bus API using Client @@ -239,7 +204,7 @@ async function _updateReviewSummation(authUser, reviewSummationId, entity) { // Hence returning the response which will be in compliance with Swagger return _.extend( exist, - entity, + updatedData, { updated: currDate, updatedBy: authUser.handle || authUser.sub, @@ -309,15 +274,7 @@ async function deleteReviewSummation(reviewSummationId) { throw new errors.HttpStatusError(404, `Review summation with ID = ${reviewSummationId} is not found`) } - // Filter used to delete the record - const filter = { - TableName: table, - Key: { - id: reviewSummationId - } - } - - await dbhelper.deleteRecord(filter) + await reviewSummationDomain.delete(getLookupCriteria("id", reviewSummationId)) // Push Review Summation deleted event to Bus API // Request body for Posting to Bus API diff --git a/src/services/SubmissionService.js b/src/services/SubmissionService.js index 2d383b1a..d4e09741 100755 --- a/src/services/SubmissionService.js +++ b/src/services/SubmissionService.js @@ -88,7 +88,7 @@ async function _getSubmission(submissionId, fetchReview = true) { id: submissionId, }); - const result = await submissionDomain.scan({ + const result = await submissionDomain.scan({ // TODO: @Hamid, why scan? why not lookup? scanCriteria, }); const submission = result[0]; @@ -542,63 +542,39 @@ async function _updateSubmission(authUser, submissionId, entity) { `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, - Key: { - id: submissionId, - }, - UpdateExpression: `set #type = :t, #url = :u, memberId = :m, challengeId = :c, updated = :ua, updatedBy = :ub, submittedDate = :sb`, - ExpressionAttributeValues: { - ":t": entity.type || exist.type, - ":u": entity.url || exist.url, - ":m": entity.memberId || exist.memberId, - ":c": challengeId, - ":ua": currDate, - ":ub": authUser.handle || authUser.sub, - ":sb": entity.submittedDate || exist.submittedDate || exist.created, - }, - ExpressionAttributeNames: { - "#type": "type", - "#url": "url", - }, - }; - + const updatedData = { + type: entity.type || exist.type, + url: entity.url || exist.url, + memberId: entity.memberId || exist.memberId, + challengeId, + submittedDate: entity.submittedDate || exist.submittedDate || exist.created, + } if (legacyChallengeId) { - record.UpdateExpression = - record.UpdateExpression + ", legacyChallengeId = :lc"; - record.ExpressionAttributeValues[":lc"] = legacyChallengeId; + updatedData.legacyChallengeId = legacyChallengeId; } - - // If legacy submission ID exists, add it to the update expression if (entity.legacySubmissionId || exist.legacySubmissionId) { - record.UpdateExpression = - record.UpdateExpression + ", legacySubmissionId = :ls"; - record.ExpressionAttributeValues[":ls"] = - entity.legacySubmissionId || exist.legacySubmissionId; + updatedData.legacySubmissionId = entity.legacySubmissionId || exist.legacySubmissionId; } // If legacy upload ID exists, add it to the update expression if (entity.legacyUploadId || exist.legacyUploadId) { - record.UpdateExpression = - record.UpdateExpression + ", legacyUploadId = :lu"; - record.ExpressionAttributeValues[":lu"] = - entity.legacyUploadId || exist.legacyUploadId; + updatedData.legacyUploadId = entity.legacyUploadId || exist.legacyUploadId; } // If submissionPhaseId exists, add it to the update expression if (entity.submissionPhaseId || exist.submissionPhaseId) { - record.UpdateExpression = - record.UpdateExpression + ", submissionPhaseId = :sp"; - record.ExpressionAttributeValues[":sp"] = - entity.submissionPhaseId || exist.submissionPhaseId; + updatedData.submissionPhaseId = entity.submissionPhaseId || exist.submissionPhaseId; } logger.info("Prepared submission item to update in Dynamodb. Updating..."); - //TODO: upgrade to GRPC - await dbhelper.updateRecord(record); - const updatedSub = await _getSubmission(submissionId); + const { items } = await submissionDomain.update({ + filterCriteria: getScanCriteria({ + id: submissionId, + }), + updateInput: updatedData + }) + const [updatedSub] = items; helper.adjustSubmissionChallengeId(updatedSub); // Push Submission updated event to Bus API @@ -612,13 +588,15 @@ async function _updateSubmission(authUser, submissionId, entity) { { resource: helper.camelize(table), id: submissionId, - challengeId: updatedSub.challengeId, - v5ChallengeId: updatedSub.v5ChallengeId, - memberId: updatedSub.memberId, - submissionPhaseId: updatedSub.submissionPhaseId, - type: updatedSub.type, - submittedDate: updatedSub.submittedDate, - legacyChallengeId: updatedSub.legacyChallengeId, + ..._.pick(updatedSub, [ + 'challengeId', + 'v5ChallengeId', + 'memberId', + 'submissionPhaseId', + 'type', + 'submittedDate', + 'legacyChallengeId' + ]) }, entity ),