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

fix for #12 #13 #16

Merged
merged 1 commit into from
Aug 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/common/controller-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

/**
Expand All @@ -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)
}

Expand Down
17 changes: 17 additions & 0 deletions src/common/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -117,6 +133,7 @@ async function publishError (topic, payload, action) {
module.exports = {
getAuthUser,
injectSearchMeta,
setLastModifiedHeader,
getControllerMethods,
omitAuditFields,
publishError
Expand Down
4 changes: 2 additions & 2 deletions src/modules/skill/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down