Skip to content

Commit 1ff0fdf

Browse files
Read review from Dynamo DB too
1 parent 83f2a9d commit 1ff0fdf

File tree

5 files changed

+96
-7
lines changed

5 files changed

+96
-7
lines changed

constants.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ const busApiMeta = {
1515
}
1616
}
1717

18+
const submissionIndex = 'submissionId-index'
19+
1820
module.exports = {
19-
busApiMeta
21+
busApiMeta,
22+
submissionIndex
2023
}

src/common/dbhelper.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,23 @@ function * scanRecords (params) {
158158
})
159159
}
160160

161+
/**
162+
* Get records from DynamoDB based on the secondary index filter
163+
* @param {object} filter Secondary index filter to be applied on the database
164+
* @return {promise}
165+
*/
166+
function * queryRecords (filter) {
167+
const dbClient = getDbClient()
168+
return new Promise((resolve, reject) => {
169+
dbClient.query(filter, (err, data) => {
170+
if (err) {
171+
reject(err)
172+
}
173+
resolve(data)
174+
})
175+
})
176+
}
177+
161178
// exports the functions
162179
module.exports = {
163180
getDb,
@@ -168,5 +185,6 @@ module.exports = {
168185
getRecord,
169186
updateRecord,
170187
deleteRecord,
171-
scanRecords
188+
scanRecords,
189+
queryRecords
172190
}

src/models/Review.js

+22-2
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,39 @@
33
*/
44

55
const config = require('config')
6+
const { submissionIndex } = require('../../constants')
67

78
const Review = {
89
TableName: 'Review',
910
KeySchema: [
1011
{ AttributeName: 'id', KeyType: 'HASH' } // Partition key
1112
],
1213
AttributeDefinitions: [
13-
{ AttributeName: 'id', AttributeType: 'S' } // S -> String
14+
{ AttributeName: 'id', AttributeType: 'S' }, // S -> String
15+
{ AttributeName: 'submissionId', AttributeType: 'S' }
1416
],
1517
ProvisionedThroughput: {
1618
ReadCapacityUnits: config.aws.AWS_READ_UNITS,
1719
WriteCapacityUnits: config.aws.AWS_WRITE_UNITS
18-
}
20+
},
21+
GlobalSecondaryIndexes: [
22+
{
23+
IndexName: submissionIndex,
24+
KeySchema: [
25+
{
26+
AttributeName: 'submissionId',
27+
KeyType: 'HASH'
28+
}
29+
],
30+
Projection: {
31+
ProjectionType: 'ALL'
32+
},
33+
ProvisionedThroughput: {
34+
ReadCapacityUnits: 1,
35+
WriteCapacityUnits: 1
36+
}
37+
}
38+
]
1939
}
2040

2141
module.exports = {

src/models/ReviewSummation.js

+22-2
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,39 @@
33
*/
44

55
const config = require('config')
6+
const { submissionIndex } = require('../../constants')
67

78
const ReviewSummation = {
89
TableName: 'ReviewSummation',
910
KeySchema: [
1011
{ AttributeName: 'id', KeyType: 'HASH' } // Partition key
1112
],
1213
AttributeDefinitions: [
13-
{ AttributeName: 'id', AttributeType: 'S' } // S -> String
14+
{ AttributeName: 'id', AttributeType: 'S' }, // S -> String
15+
{ AttributeName: 'submissionId', AttributeType: 'S' }
1416
],
1517
ProvisionedThroughput: {
1618
ReadCapacityUnits: config.aws.AWS_READ_UNITS,
1719
WriteCapacityUnits: config.aws.AWS_WRITE_UNITS
18-
}
20+
},
21+
GlobalSecondaryIndexes: [
22+
{
23+
IndexName: submissionIndex,
24+
KeySchema: [
25+
{
26+
AttributeName: 'submissionId',
27+
KeyType: 'HASH'
28+
}
29+
],
30+
Projection: {
31+
ProjectionType: 'ALL'
32+
},
33+
ProvisionedThroughput: {
34+
ReadCapacityUnits: 1,
35+
WriteCapacityUnits: 1
36+
}
37+
}
38+
]
1939
}
2040

2141
module.exports = {

src/services/SubmissionService.js

+29-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const uuid = require('uuid/v4')
1212
const dbhelper = require('../common/dbhelper')
1313
const helper = require('../common/helper')
1414
const { originator, mimeType, fileType, events } = require('../../constants').busApiMeta
15+
const { submissionIndex } = require('../../constants')
1516
const s3 = new AWS.S3()
1617
const logger = require('winston')
1718

@@ -58,7 +59,34 @@ function * _getSubmission (submissionId) {
5859
}
5960
}
6061
const result = yield dbhelper.getRecord(filter)
61-
return result.Item
62+
const submission = result.Item
63+
// Fetch associated reviews
64+
const reviewFilter = {
65+
TableName: 'Review',
66+
IndexName: submissionIndex,
67+
KeyConditionExpression: 'submissionId = :p_submissionId',
68+
ExpressionAttributeValues: {
69+
':p_submissionId': submissionId
70+
}
71+
}
72+
const review = yield dbhelper.queryRecords(reviewFilter)
73+
if (review.Count !== 0) {
74+
submission.review = review.Items
75+
}
76+
// Fetch associated review summations
77+
const reviewSummationFilter = {
78+
TableName: 'ReviewSummation',
79+
IndexName: submissionIndex,
80+
KeyConditionExpression: 'submissionId = :p_submissionId',
81+
ExpressionAttributeValues: {
82+
':p_submissionId': submissionId
83+
}
84+
}
85+
const reviewSummation = yield dbhelper.queryRecords(reviewSummationFilter)
86+
if (reviewSummation.Count !== 0) {
87+
submission.reviewSummation = reviewSummation.Items
88+
}
89+
return submission
6290
}
6391

6492
/**

0 commit comments

Comments
 (0)