Skip to content

Commit 8314e52

Browse files
authored
Merge pull request #128 from bountyC0d3r/develop
patch group
2 parents 7e6fd27 + afeeea1 commit 8314e52

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

src/controllers/GroupController.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ async function updateGroup(req, res) {
4343
res.send(result)
4444
}
4545

46+
/**
47+
* Patch group
48+
* @param req the request
49+
* @param res the response
50+
*/
51+
async function patchGroup(req, res) {
52+
const result = await service.patchGroup(req.authUser.isMachine ? 'M2M' : req.authUser, req.params.groupId, req.body)
53+
res.send(result)
54+
}
55+
4656
/**
4757
* Get group
4858
* @param req the request
@@ -99,6 +109,7 @@ module.exports = {
99109
searchGroups,
100110
createGroup,
101111
updateGroup,
112+
patchGroup,
102113
getGroup,
103114
deleteGroup,
104115
getGroupByOldId,

src/routes.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ module.exports = {
5050
access: [constants.UserRoles.Admin],
5151
scopes: ['write:groups', 'all:groups']
5252
},
53+
patch: {
54+
controller: 'GroupController',
55+
method: 'patchGroup',
56+
auth: 'jwt',
57+
access: [constants.UserRoles.Admin],
58+
scopes: ['write:groups', 'all:groups']
59+
},
5360
delete: {
5461
controller: 'GroupController',
5562
method: 'deleteGroup',

src/services/GroupService.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,66 @@ updateGroup.schema = {
300300
})
301301
}
302302

303+
/**
304+
* Patch group
305+
* @param {Object} currentUser the current user
306+
* @param {String} groupId the id of group to update
307+
* @param {Object} data the data to update group
308+
* @returns {Object} the updated group
309+
*/
310+
async function patchGroup(currentUser, groupId, data) {
311+
const session = helper.createDBSession()
312+
const tx = session.beginTransaction()
313+
try {
314+
logger.debug(`Patch Group - user - ${currentUser} , data - ${JSON.stringify(data)}`)
315+
const group = await helper.ensureExists(
316+
tx,
317+
'Group',
318+
groupId,
319+
currentUser === 'M2M' || helper.hasAdminRole(currentUser)
320+
)
321+
322+
const groupData = data
323+
groupData.id = groupId
324+
groupData.updatedAt = new Date().toISOString()
325+
groupData.updatedBy = currentUser === 'M2M' ? '00000000' : currentUser.userId
326+
groupData.oldId = data.oldId ? data.oldId : ''
327+
328+
const updateRes = await tx.run(
329+
'MATCH (g:Group {id: {id}}) SET g.updatedAt={updatedAt}, g.updatedBy={updatedBy}, g.oldId={oldId} RETURN g',
330+
groupData
331+
)
332+
333+
const updatedGroup = updateRes.records[0].get(0).properties
334+
logger.debug(`Group = ${JSON.stringify(updatedGroup)}`)
335+
336+
await tx.commit()
337+
338+
// update the cache
339+
const cache = await helper.getCacheInstance()
340+
cache.set(group.id, updatedGroup)
341+
342+
return updatedGroup
343+
} catch (error) {
344+
logger.error(error)
345+
logger.debug('Transaction Rollback')
346+
await tx.rollback()
347+
throw error
348+
} finally {
349+
logger.debug('Session Close')
350+
await session.close()
351+
}
352+
}
353+
354+
patchGroup.schema = {
355+
currentUser: Joi.any(),
356+
groupId: Joi.string(), // defined in app-bootstrap
357+
data: Joi.object()
358+
.keys({
359+
oldId: Joi.string(),
360+
})
361+
}
362+
303363
/**
304364
* Get group.
305365
* @param {Object} currentUser the current user
@@ -532,6 +592,7 @@ module.exports = {
532592
searchGroups,
533593
createGroup,
534594
updateGroup,
595+
patchGroup,
535596
getGroup,
536597
deleteGroup
537598
}

0 commit comments

Comments
 (0)