Skip to content
This repository was archived by the owner on Mar 12, 2025. It is now read-only.

Commit 010451d

Browse files
author
vikasrohit
authored
Merge pull request #16 from topcoder-platform/feature/enrich
fix for #12 #13
2 parents 7ea0be4 + 52c35c3 commit 010451d

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

Diff for: src/common/controller-helper.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @return {{patch: patch, search: search, get: get, create: create, update: update, remove: remove}} the common controller methods
55
*/
66
function getControllerMethods (service) {
7-
const { injectSearchMeta } = require('./helper')
7+
const { injectSearchMeta, setLastModifiedHeader } = require('./helper')
88

99
/**
1010
* create entity by request data
@@ -39,7 +39,9 @@ function getControllerMethods (service) {
3939
* @param res the http response
4040
*/
4141
async function get (req, res) {
42-
res.json(await service.get(req.params.id, req.query))
42+
const result = await service.get(req.params.id, req.query)
43+
setLastModifiedHeader(req, res, result)
44+
res.json(result)
4345
}
4446

4547
/**
@@ -50,6 +52,7 @@ function getControllerMethods (service) {
5052
async function search (req, res) {
5153
const result = await service.search(req.query)
5254
injectSearchMeta(req, res, result)
55+
setLastModifiedHeader(req, res, result.result)
5356
res.send(result.result)
5457
}
5558

Diff for: src/common/helper.js

+17
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,22 @@ function injectSearchMeta (req, res, result) {
7272
res.set('Access-Control-Expose-Headers', accessControlExposeHeaders)
7373
}
7474

75+
/**
76+
* Set the Last-Modified response header from result.
77+
* @param {Object} req the HTTP request
78+
* @param {Object} res the HTTP response
79+
* @param {Array|Object} result the operation result
80+
*/
81+
function setLastModifiedHeader (req, res, result) {
82+
if (!Array.isArray(result)) {
83+
res.set('Last-Modified', new Date(_.get(result, 'metadata.updated')))
84+
return
85+
}
86+
if (result.length) {
87+
res.set('Last-Modified', new Date(Math.max(...result.map(entity => new Date(_.get(entity, 'metadata.updated'))))))
88+
}
89+
}
90+
7591
/**
7692
* Removes the audit fields created, createdBy, updatedBy from the given entity or an array of entities
7793
* and moves the updated to metadata
@@ -117,6 +133,7 @@ async function publishError (topic, payload, action) {
117133
module.exports = {
118134
getAuthUser,
119135
injectSearchMeta,
136+
setLastModifiedHeader,
120137
getControllerMethods,
121138
omitAuditFields,
122139
publishError

Diff for: src/modules/skill/service.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ async function patch (id, entity, auth) {
121121
}
122122

123123
// check if the skill has conflict or not
124-
await dbHelper.makeSureUnique(Skill, entity, uniqueFields)
124+
await dbHelper.makeSureUnique(Skill, { ...entity, id }, uniqueFields)
125125

126126
if (entity.metadata) {
127127
const inputFields = Object.keys(entity.metadata)
@@ -177,7 +177,7 @@ async function fullyUpdate (id, entity, auth) {
177177
}
178178

179179
// check if the skill has conflict or not
180-
await dbHelper.makeSureUnique(Skill, entity, uniqueFields)
180+
await dbHelper.makeSureUnique(Skill, { ...entity, id }, uniqueFields)
181181

182182
if (Object.keys(entity.metadata).length) {
183183
// check permission for adding new metadata fields

0 commit comments

Comments
 (0)