Skip to content

Commit 51ae5bf

Browse files
Merge pull request #302 from topcoder-platform/feat/plat-2428
integrate submission.update, review and reviewSummation with dal
2 parents 2fffc18 + 2171507 commit 51ae5bf

File tree

3 files changed

+103
-208
lines changed

3 files changed

+103
-208
lines changed

src/services/ReviewService.js

+39-79
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,25 @@ const _ = require('lodash')
77
const uuid = require('uuid/v4')
88
const joi = require('joi')
99
const logger = require('winston')
10+
const config = require('config')
1011

1112
const dbhelper = require('../common/dbhelper')
1213
const helper = require('../common/helper')
1314
const { originator, mimeType, events } = require('../../constants').busApiMeta
1415
const HelperService = require('./HelperService')
1516
const SubmissionService = require('./SubmissionService')
1617

18+
const { ReviewDomain } = require("@topcoder-framework/domain-submission");
19+
20+
const {
21+
DomainHelper: { getLookupCriteria, getScanCriteria },
22+
} = require("@topcoder-framework/lib-common");
23+
24+
const reviewDomain = new ReviewDomain(
25+
config.GRPC_SUBMISSION_SERVER_HOST,
26+
config.GRPC_SUBMISSION_SERVER_PORT
27+
);
28+
1729
const table = 'Review'
1830

1931
/**
@@ -24,14 +36,7 @@ const table = 'Review'
2436
*/
2537
async function _getReview(reviewId) {
2638
// Construct filter to retrieve record from Database
27-
const filter = {
28-
TableName: table,
29-
Key: {
30-
id: reviewId
31-
}
32-
}
33-
const result = await dbhelper.getRecord(filter)
34-
return result.Item
39+
return reviewDomain.lookup(getLookupCriteria("id", reviewId))
3540
}
3641

3742
/**
@@ -157,33 +162,17 @@ async function createReview(authUser, entity) {
157162
entity.v5ScoreCardId = possibleV5ScoreCardId
158163
}
159164

160-
const item = _.extend(
161-
{
162-
id: uuid(),
163-
created: currDate,
164-
updated: currDate,
165-
createdBy: authUser.handle || authUser.sub,
166-
updatedBy: authUser.handle || authUser.sub,
167-
status: entity.status || 'completed'
168-
},
169-
entity
170-
)
171-
172165
if (_.intersection(authUser.roles, ['Administrator', 'administrator']).length === 0 && !authUser.scopes) {
173166
if (entity.reviewedDate) {
174167
throw new errors.HttpStatusError(403, 'You are not allowed to set the `reviewedDate` attribute on a review')
175168
}
176169
}
177170

178-
item.reviewedDate = entity.reviewedDate || item.created
179-
180-
// Prepare record to be inserted
181-
const record = {
182-
TableName: table,
183-
Item: item
184-
}
185-
186-
await dbhelper.insertRecord(record)
171+
const createdItem = await reviewDomain.create({
172+
...entity,
173+
status: entity.status || 'completed',
174+
reviewedDate: entity.reviewedDate || item.created,
175+
})
187176

188177
// Push Review created event to Bus API
189178
// Request body for Posting to Bus API
@@ -196,7 +185,7 @@ async function createReview(authUser, entity) {
196185
{
197186
resource: helper.camelize(table)
198187
},
199-
item
188+
createdItem
200189
)
201190
}
202191

@@ -205,7 +194,7 @@ async function createReview(authUser, entity) {
205194

206195
// Inserting records in DynamoDB doesn't return any response
207196
// Hence returning the same entity to be in compliance with Swagger
208-
return item
197+
return createdItem
209198
}
210199

211200
createReview.schema = {
@@ -285,45 +274,24 @@ async function _updateReview(authUser, reviewId, entity) {
285274
}
286275
}
287276

288-
// Record used for updating in Database
289-
const record = {
290-
TableName: table,
291-
Key: {
292-
id: reviewId
293-
},
294-
UpdateExpression: `set score = :s, scoreCardId = :sc, submissionId = :su,
295-
typeId = :t, reviewerId = :r, #st = :st,
296-
updated = :ua, updatedBy = :ub, reviewedDate = :rd
297-
${v5ScoreCardId ? ', v5ScoreCardId = :v5s' : ''}`,
298-
ExpressionAttributeValues: {
299-
':s': entity.score || exist.score,
300-
':sc': scoreCardId,
301-
':su': entity.submissionId || exist.submissionId,
302-
':t': entity.typeId || exist.typeId,
303-
':r': entity.reviewerId || exist.reviewerId,
304-
':st': entity.status || exist.status || 'completed',
305-
':ua': currDate,
306-
':ub': authUser.handle || authUser.sub,
307-
':rd': entity.reviewedDate || exist.reviewedDate || exist.created,
308-
...(v5ScoreCardId ? { ':v5s': v5ScoreCardId } : {})
309-
},
310-
ExpressionAttributeNames: {
311-
'#st': 'status'
312-
}
313-
}
314-
315-
// If metadata exists, add it to the update expression
316-
if (entity.metadata || exist.metadata) {
317-
record.UpdateExpression =
318-
record.UpdateExpression + ', metadata = :ma'
319-
record.ExpressionAttributeValues[':ma'] = _.merge(
320-
{},
321-
exist.metadata,
322-
entity.metadata
323-
)
277+
const updatedData = {
278+
score: entity.score || exist.score,
279+
scoreCardId,
280+
submissionId: entity.submissionId || exist.submissionId,
281+
typeId: entity.typeId || exist.typeId,
282+
reviewerId: entity.reviewerId || exist.reviewerId,
283+
status: entity.status || exist.status || 'completed',
284+
reviewedDate: entity.reviewedDate || exist.reviewedDate || exist.created,
285+
...(v5ScoreCardId ? { v5ScoreCardId } : {}),
286+
...(entity.metadata || exist.metadata ? { metadata: _.merge({}, exist.metadata, entity.metadata) } : {})
324287
}
325288

326-
await dbhelper.updateRecord(record)
289+
await reviewDomain.update({
290+
filterCriteria: getScanCriteria({
291+
id: reviewId,
292+
}),
293+
updateInput: updatedData
294+
})
327295

328296
// Push Review updated event to Bus API
329297
// Request body for Posting to Bus API
@@ -340,7 +308,7 @@ async function _updateReview(authUser, reviewId, entity) {
340308
updatedBy: authUser.handle || authUser.sub,
341309
reviewedDate: entity.reviewedDate || exist.reviewedDate || exist.created
342310
},
343-
entity,
311+
updatedData,
344312
{
345313
scoreCardId,
346314
v5ScoreCardId
@@ -353,7 +321,7 @@ async function _updateReview(authUser, reviewId, entity) {
353321

354322
// Updating records in DynamoDB doesn't return any response
355323
// Hence returning the response which will be in compliance with Swagger
356-
return _.extend(exist, entity, {
324+
return _.extend(exist, updatedData, {
357325
updated: currDate,
358326
updatedBy: authUser.handle || authUser.sub,
359327
reviewedDate: entity.reviewedDate || exist.reviewedDate || exist.created,
@@ -446,15 +414,7 @@ async function deleteReview(reviewId) {
446414
)
447415
}
448416

449-
// Filter used to delete the record
450-
const filter = {
451-
TableName: table,
452-
Key: {
453-
id: reviewId
454-
}
455-
}
456-
457-
await dbhelper.deleteRecord(filter)
417+
await reviewDomain.delete(getLookupCriteria("id", reviewId))
458418

459419
// Push Review deleted event to Bus API
460420
// Request body for Posting to Bus API

src/services/ReviewSummationService.js

+36-79
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,21 @@ const dbhelper = require('../common/dbhelper')
1010
const helper = require('../common/helper')
1111
const { originator, mimeType, events } = require('../../constants').busApiMeta
1212
const HelperService = require('./HelperService')
13+
const config = require('config')
1314

1415
const table = 'ReviewSummation'
1516

17+
const { ReviewSummationDomain } = require("@topcoder-framework/domain-submission");
18+
19+
const {
20+
DomainHelper: { getLookupCriteria, getScanCriteria },
21+
} = require("@topcoder-framework/lib-common");
22+
23+
const reviewSummationDomain = new ReviewSummationDomain(
24+
config.GRPC_SUBMISSION_SERVER_HOST,
25+
config.GRPC_SUBMISSION_SERVER_PORT
26+
);
27+
1628
/**
1729
* Function to get Review summation based on ID from DynamoDB
1830
* This function will be used all by other functions to check existence of Review summation
@@ -21,14 +33,7 @@ const table = 'ReviewSummation'
2133
*/
2234
async function _getReviewSummation(reviewSummationId) {
2335
// Construct filter to retrieve record from Database
24-
const filter = {
25-
TableName: table,
26-
Key: {
27-
id: reviewSummationId
28-
}
29-
}
30-
const result = await dbhelper.getRecord(filter)
31-
return result.Item
36+
return reviewSummationDomain.lookup(getLookupCriteria("id", reviewSummationId))
3237
}
3338

3439
/**
@@ -90,32 +95,16 @@ async function createReviewSummation(authUser, entity) {
9095

9196
const currDate = (new Date()).toISOString()
9297

93-
const item = _.extend({
94-
id: uuid(),
95-
created: currDate,
96-
updated: currDate,
97-
createdBy: authUser.handle || authUser.sub,
98-
updatedBy: authUser.handle || authUser.sub
99-
}, entity)
100-
101-
if (entity.isFinal) {
102-
item.isFinal = entity.isFinal
103-
}
104-
10598
if (_.intersection(authUser.roles, ['Administrator', 'administrator']).length === 0 && !authUser.scopes) {
10699
if (entity.reviewedDate) {
107100
throw new errors.HttpStatusError(403, 'You are not allowed to set the `reviewedDate` attribute on a review summation')
108101
}
109102
}
110103

111-
item.reviewedDate = entity.reviewedDate || item.created
112-
113-
const record = {
114-
TableName: table,
115-
Item: item
116-
}
117-
118-
await dbhelper.insertRecord(record)
104+
const item = await reviewSummationDomain.create({
105+
...entity,
106+
reviewedDate: entity.reviewedDate || currDate,
107+
});
119108

120109
// Push Review Summation created event to Bus API
121110
// Request body for Posting to Bus API
@@ -175,46 +164,22 @@ async function _updateReviewSummation(authUser, reviewSummationId, entity) {
175164

176165
const currDate = (new Date()).toISOString()
177166

178-
// Record used for updating in Database
179-
const record = {
180-
TableName: table,
181-
Key: {
182-
id: reviewSummationId
183-
},
184-
UpdateExpression: `set aggregateScore = :s, scoreCardId = :sc, submissionId = :su,
185-
isPassing = :ip, updated = :ua, updatedBy = :ub, reviewedDate = :rd`,
186-
ExpressionAttributeValues: {
187-
':s': entity.aggregateScore || exist.aggregateScore,
188-
':sc': entity.scoreCardId || exist.scoreCardId,
189-
':su': entity.submissionId || exist.submissionId,
190-
':ip': isPassing,
191-
':ua': currDate,
192-
':ub': authUser.handle || authUser.sub,
193-
':rd': entity.reviewedDate || exist.reviewedDate || exist.created
194-
}
195-
}
196-
197-
// If metadata exists, add it to the update expression
198-
if (entity.metadata || exist.metadata) {
199-
record.UpdateExpression = record.UpdateExpression + ', metadata = :ma'
200-
record.ExpressionAttributeValues[':ma'] = _.merge({}, exist.metadata, entity.metadata)
201-
}
202-
203-
// If legacy submission ID exists, add it to the update expression
204-
if (entity.isFinal || exist.isFinal) {
205-
let isFinal // Attribute to store boolean value
206-
207-
if (entity.isFinal === undefined) {
208-
isFinal = exist.isFinal
209-
} else {
210-
isFinal = entity.isFinal
211-
}
212-
213-
record.UpdateExpression = record.UpdateExpression + ', isFinal = :ls'
214-
record.ExpressionAttributeValues[':ls'] = isFinal
215-
}
216-
217-
await dbhelper.updateRecord(record)
167+
const updatedData = {
168+
aggregateScore: entity.aggregateScore || exist.aggregateScore,
169+
scoreCardId: entity.scoreCardId || exist.scoreCardId,
170+
submissionId: entity.submissionId || exist.submissionId,
171+
isPassing: isPassing,
172+
reviewedDate: entity.reviewedDate || exist.reviewedDate || exist.created,
173+
...(entity.metadata || exist.metadata ? { metadata: _.merge({}, exist.metadata, entity.metadata) } : {}),
174+
...(entity.isFinal || exist.isFinal ? { isFinal: entity.isFinal || exist.isFinal } : {})
175+
};
176+
177+
await reviewSummationDomain.update({
178+
filterCriteria: getScanCriteria({
179+
id: reviewSummationId,
180+
}),
181+
updateInput: updatedData
182+
})
218183

219184
// Push Review Summation updated event to Bus API
220185
// Request body for Posting to Bus API
@@ -229,7 +194,7 @@ async function _updateReviewSummation(authUser, reviewSummationId, entity) {
229194
updated: currDate,
230195
updatedBy: authUser.handle || authUser.sub,
231196
reviewedDate: entity.reviewedDate || exist.reviewedDate || exist.created
232-
}, entity)
197+
}, updatedData)
233198
}
234199

235200
// Post to Bus API using Client
@@ -239,7 +204,7 @@ async function _updateReviewSummation(authUser, reviewSummationId, entity) {
239204
// Hence returning the response which will be in compliance with Swagger
240205
return _.extend(
241206
exist,
242-
entity,
207+
updatedData,
243208
{
244209
updated: currDate,
245210
updatedBy: authUser.handle || authUser.sub,
@@ -309,15 +274,7 @@ async function deleteReviewSummation(reviewSummationId) {
309274
throw new errors.HttpStatusError(404, `Review summation with ID = ${reviewSummationId} is not found`)
310275
}
311276

312-
// Filter used to delete the record
313-
const filter = {
314-
TableName: table,
315-
Key: {
316-
id: reviewSummationId
317-
}
318-
}
319-
320-
await dbhelper.deleteRecord(filter)
277+
await reviewSummationDomain.delete(getLookupCriteria("id", reviewSummationId))
321278

322279
// Push Review Summation deleted event to Bus API
323280
// Request body for Posting to Bus API

0 commit comments

Comments
 (0)