Skip to content

integrate submission.update, review and reviewSummation with dal #302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 39 additions & 79 deletions src/services/ReviewService.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,25 @@ 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')
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'

/**
Expand All @@ -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))
}

/**
Expand Down Expand Up @@ -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
Expand All @@ -196,7 +185,7 @@ async function createReview(authUser, entity) {
{
resource: helper.camelize(table)
},
item
createdItem
)
}

Expand All @@ -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 = {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down
115 changes: 36 additions & 79 deletions src/services/ReviewSummationService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
}

/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down
Loading