Skip to content

Commit b290e4c

Browse files
committed
backfill logic for when review was not found in ES
1 parent 71a6bb9 commit b290e4c

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

src/services/ReviewService.js

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ const errors = require('common-errors')
66
const _ = require('lodash')
77
const uuid = require('uuid/v4')
88
const joi = require('joi')
9+
const logger = require('winston')
10+
911
const dbhelper = require('../common/dbhelper')
1012
const helper = require('../common/helper')
11-
const { originator, mimeType, events } = require('../../constants').busApiMeta
13+
const {
14+
originator, mimeType, events
15+
} = require('../../constants').busApiMeta
1216
const HelperService = require('./HelperService')
1317

1418
const busApiClient = helper.getBusApiClient()
@@ -38,8 +42,19 @@ function * _getReview (reviewId) {
3842
* @return {Object} Data found from database
3943
*/
4044
function * getReview (reviewId) {
41-
const response = yield helper.fetchFromES({id: reviewId}, helper.camelize(table))
45+
const response = yield helper.fetchFromES({
46+
id: reviewId
47+
}, helper.camelize(table))
48+
4249
if (response.total === 0) {
50+
logger.info(`Couldn't find review ${reviewId} in ES. Checking db`)
51+
const review = yield _getReview(reviewId)
52+
logger.debug(`Review: ${review}`)
53+
54+
if (review) {
55+
return review
56+
}
57+
4358
throw new errors.HttpStatusError(404, `Review with ID = ${reviewId} is not found`)
4459
}
4560
// Return the retrieved review
@@ -87,7 +102,8 @@ function * createReview (authUser, entity) {
87102
'created': currDate,
88103
'updated': currDate,
89104
'createdBy': authUser.handle || authUser.sub,
90-
'updatedBy': authUser.handle || authUser.sub }, entity)
105+
'updatedBy': authUser.handle || authUser.sub
106+
}, entity)
91107

92108
// Prepare record to be inserted
93109
const record = {
@@ -104,7 +120,9 @@ function * createReview (authUser, entity) {
104120
'originator': originator,
105121
'timestamp': currDate, // time when submission was created
106122
'mime-type': mimeType,
107-
'payload': _.extend({ 'resource': helper.camelize(table) }, item)
123+
'payload': _.extend({
124+
'resource': helper.camelize(table)
125+
}, item)
108126
}
109127

110128
// Post to Bus API using Client
@@ -181,18 +199,23 @@ function * _updateReview (authUser, reviewId, entity) {
181199
'originator': originator,
182200
'timestamp': currDate, // time when submission was updated
183201
'mime-type': mimeType,
184-
'payload': _.extend({ 'resource': helper.camelize(table),
202+
'payload': _.extend({
203+
'resource': helper.camelize(table),
185204
'id': reviewId,
186205
'updated': currDate,
187-
'updatedBy': authUser.handle || authUser.sub }, entity)
206+
'updatedBy': authUser.handle || authUser.sub
207+
}, entity)
188208
}
189209

190210
// Post to Bus API using Client
191211
yield busApiClient.postEvent(reqBody)
192212

193213
// Updating records in DynamoDB doesn't return any response
194214
// Hence returning the response which will be in compliance with Swagger
195-
return _.extend(exist, entity, { 'updated': currDate, 'updatedBy': authUser.handle || authUser.sub })
215+
return _.extend(exist, entity, {
216+
'updated': currDate,
217+
'updatedBy': authUser.handle || authUser.sub
218+
})
196219
}
197220

198221
/**

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy