From 1a39f86ba2feb35d3bad84a50c00dbf17c0b9d73 Mon Sep 17 00:00:00 2001 From: bountyCoder Date: Mon, 9 Jan 2023 08:04:07 +0000 Subject: [PATCH] added the new endpoint to patch the group --- src/controllers/GroupController.js | 11 ++++++ src/routes.js | 7 ++++ src/services/GroupService.js | 61 ++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+) diff --git a/src/controllers/GroupController.js b/src/controllers/GroupController.js index 996ebe0..f9e21db 100644 --- a/src/controllers/GroupController.js +++ b/src/controllers/GroupController.js @@ -43,6 +43,16 @@ async function updateGroup(req, res) { res.send(result) } +/** + * Patch group + * @param req the request + * @param res the response + */ +async function patchGroup(req, res) { + const result = await service.patchGroup(req.authUser.isMachine ? 'M2M' : req.authUser, req.params.groupId, req.body) + res.send(result) +} + /** * Get group * @param req the request @@ -99,6 +109,7 @@ module.exports = { searchGroups, createGroup, updateGroup, + patchGroup, getGroup, deleteGroup, getGroupByOldId, diff --git a/src/routes.js b/src/routes.js index 95aaf56..6feb464 100644 --- a/src/routes.js +++ b/src/routes.js @@ -50,6 +50,13 @@ module.exports = { access: [constants.UserRoles.Admin], scopes: ['write:groups', 'all:groups'] }, + patch: { + controller: 'GroupController', + method: 'patchGroup', + auth: 'jwt', + access: [constants.UserRoles.Admin], + scopes: ['write:groups', 'all:groups'] + }, delete: { controller: 'GroupController', method: 'deleteGroup', diff --git a/src/services/GroupService.js b/src/services/GroupService.js index 764a7b2..9def382 100644 --- a/src/services/GroupService.js +++ b/src/services/GroupService.js @@ -300,6 +300,66 @@ updateGroup.schema = { }) } +/** + * Patch group + * @param {Object} currentUser the current user + * @param {String} groupId the id of group to update + * @param {Object} data the data to update group + * @returns {Object} the updated group + */ +async function patchGroup(currentUser, groupId, data) { + const session = helper.createDBSession() + const tx = session.beginTransaction() + try { + logger.debug(`Patch Group - user - ${currentUser} , data - ${JSON.stringify(data)}`) + const group = await helper.ensureExists( + tx, + 'Group', + groupId, + currentUser === 'M2M' || helper.hasAdminRole(currentUser) + ) + + const groupData = data + groupData.id = groupId + groupData.updatedAt = new Date().toISOString() + groupData.updatedBy = currentUser === 'M2M' ? '00000000' : currentUser.userId + groupData.oldId = data.oldId ? data.oldId : '' + + const updateRes = await tx.run( + 'MATCH (g:Group {id: {id}}) SET g.updatedAt={updatedAt}, g.updatedBy={updatedBy}, g.oldId={oldId} RETURN g', + groupData + ) + + const updatedGroup = updateRes.records[0].get(0).properties + logger.debug(`Group = ${JSON.stringify(updatedGroup)}`) + + await tx.commit() + + // update the cache + const cache = await helper.getCacheInstance() + cache.set(group.id, updatedGroup) + + return updatedGroup + } catch (error) { + logger.error(error) + logger.debug('Transaction Rollback') + await tx.rollback() + throw error + } finally { + logger.debug('Session Close') + await session.close() + } +} + +patchGroup.schema = { + currentUser: Joi.any(), + groupId: Joi.string(), // defined in app-bootstrap + data: Joi.object() + .keys({ + oldId: Joi.string(), + }) +} + /** * Get group. * @param {Object} currentUser the current user @@ -532,6 +592,7 @@ module.exports = { searchGroups, createGroup, updateGroup, + patchGroup, getGroup, deleteGroup }