Skip to content

patch group #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 9, 2023
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
11 changes: 11 additions & 0 deletions src/controllers/GroupController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -99,6 +109,7 @@ module.exports = {
searchGroups,
createGroup,
updateGroup,
patchGroup,
getGroup,
deleteGroup,
getGroupByOldId,
Expand Down
7 changes: 7 additions & 0 deletions src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
61 changes: 61 additions & 0 deletions src/services/GroupService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -532,6 +592,7 @@ module.exports = {
searchGroups,
createGroup,
updateGroup,
patchGroup,
getGroup,
deleteGroup
}
Expand Down