diff --git a/src/common/controller-helper.js b/src/common/controller-helper.js index d00f8c4..1aa570a 100644 --- a/src/common/controller-helper.js +++ b/src/common/controller-helper.js @@ -4,7 +4,7 @@ * @return {{patch: patch, search: search, get: get, create: create, update: update, remove: remove}} the common controller methods */ function getControllerMethods (service) { - const { injectSearchMeta } = require('./helper') + const { injectSearchMeta, setLastModifiedHeader } = require('./helper') /** * create entity by request data @@ -39,7 +39,9 @@ function getControllerMethods (service) { * @param res the http response */ async function get (req, res) { - res.json(await service.get(req.params.id, req.query)) + const result = await service.get(req.params.id, req.query) + setLastModifiedHeader(req, res, result) + res.json(result) } /** @@ -50,6 +52,7 @@ function getControllerMethods (service) { async function search (req, res) { const result = await service.search(req.query) injectSearchMeta(req, res, result) + setLastModifiedHeader(req, res, result.result) res.send(result.result) } diff --git a/src/common/helper.js b/src/common/helper.js index 0d5cb97..6cf0f1b 100644 --- a/src/common/helper.js +++ b/src/common/helper.js @@ -72,6 +72,22 @@ function injectSearchMeta (req, res, result) { res.set('Access-Control-Expose-Headers', accessControlExposeHeaders) } +/** + * Set the Last-Modified response header from result. + * @param {Object} req the HTTP request + * @param {Object} res the HTTP response + * @param {Array|Object} result the operation result + */ +function setLastModifiedHeader (req, res, result) { + if (!Array.isArray(result)) { + res.set('Last-Modified', new Date(_.get(result, 'metadata.updated'))) + return + } + if (result.length) { + res.set('Last-Modified', new Date(Math.max(...result.map(entity => new Date(_.get(entity, 'metadata.updated')))))) + } +} + /** * Removes the audit fields created, createdBy, updatedBy from the given entity or an array of entities * and moves the updated to metadata @@ -117,6 +133,7 @@ async function publishError (topic, payload, action) { module.exports = { getAuthUser, injectSearchMeta, + setLastModifiedHeader, getControllerMethods, omitAuditFields, publishError diff --git a/src/modules/skill/service.js b/src/modules/skill/service.js index 594d362..2ebdb84 100644 --- a/src/modules/skill/service.js +++ b/src/modules/skill/service.js @@ -121,7 +121,7 @@ async function patch (id, entity, auth) { } // check if the skill has conflict or not - await dbHelper.makeSureUnique(Skill, entity, uniqueFields) + await dbHelper.makeSureUnique(Skill, { ...entity, id }, uniqueFields) if (entity.metadata) { const inputFields = Object.keys(entity.metadata) @@ -177,7 +177,7 @@ async function fullyUpdate (id, entity, auth) { } // check if the skill has conflict or not - await dbHelper.makeSureUnique(Skill, entity, uniqueFields) + await dbHelper.makeSureUnique(Skill, { ...entity, id }, uniqueFields) if (Object.keys(entity.metadata).length) { // check permission for adding new metadata fields