Skip to content

Commit bb10cca

Browse files
committed
Merge branch 'dr_cb_joint_comments' into emathew/audit-org-log
2 parents f3a8241 + 239e475 commit bb10cca

File tree

22 files changed

+906
-717
lines changed

22 files changed

+906
-717
lines changed

src/constants/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ function getConstants () {
4444
USER_ROLES: [
4545
'ADMIN'
4646
],
47+
JOINT_APPROVAL_FIELDS: ['short_name', 'long_name'],
48+
JOINT_APPROVAL_FIELDS_LEGACY: ['short_name', 'name'],
4749
USER_ROLE_ENUM: {
4850
ADMIN: 'ADMIN'
4951
},

src/controller/conversation.controller/conversation.controller.js

Lines changed: 13 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -19,84 +19,38 @@ async function getAllConversations (req, res, next) {
1919
return res.status(200).json(response)
2020
}
2121

22-
async function getConversationsForOrg (req, res, next) {
23-
const session = await mongoose.startSession()
24-
25-
try {
26-
session.startTransaction()
27-
28-
const repo = req.ctx.repositories.getConversationRepository()
29-
const orgRepo = req.ctx.repositories.getBaseOrgRepository()
30-
const requesterOrg = req.ctx.org
31-
const targetOrgUUID = req.params.uuid
32-
33-
// Make sure target org matches user org if not secretariat
34-
const isSecretariat = await orgRepo.isSecretariatByShortName(requesterOrg, { session })
35-
const requesterOrgUUID = await orgRepo.getOrgUUID(requesterOrg, { session })
36-
if (!isSecretariat && (requesterOrgUUID !== targetOrgUUID)) {
37-
return res.status(400).json({ message: 'User is not secretariat or admin for target org' })
38-
}
39-
40-
// temporary measure to allow tests to work after fixing #920
41-
// tests required changing the global limit to force pagination
42-
if (req.TEST_PAGINATOR_LIMIT) {
43-
CONSTANTS.PAGINATOR_OPTIONS.limit = req.TEST_PAGINATOR_LIMIT
44-
}
45-
46-
const options = CONSTANTS.PAGINATOR_OPTIONS
47-
options.sort = { posted_at: 'desc' }
22+
async function getConversationsForTargetUUID (req, res, next) {
23+
const repo = req.ctx.repositories.getConversationRepository()
24+
const targetUUID = req.params.uuid
4825

49-
const response = await repo.getAllByTargetUUID(targetOrgUUID, options)
50-
await session.commitTransaction()
51-
return res.status(200).json(response)
52-
} catch (err) {
53-
if (session && session.inTransaction()) {
54-
await session.abortTransaction()
55-
}
56-
next(err)
57-
} finally {
58-
if (session && session.id) { // Check if session is still valid before trying to end
59-
try {
60-
await session.endSession()
61-
} catch (sessionEndError) {
62-
logger.error({ uuid: req.ctx.uuid, message: 'Error ending session in finally block', error: sessionEndError })
63-
}
64-
}
65-
}
26+
const response = await repo.getAllByTargetUUID(targetUUID)
27+
return res.status(200).json(response)
6628
}
6729

68-
async function createConversationForOrg (req, res, next) {
30+
async function createConversationForTargetUUID (req, res, next) {
6931
const session = await mongoose.startSession()
7032

7133
try {
7234
session.startTransaction()
7335

7436
const repo = req.ctx.repositories.getConversationRepository()
75-
const orgRepo = req.ctx.repositories.getBaseOrgRepository()
7637
const userRepo = req.ctx.repositories.getBaseUserRepository()
7738
const requesterOrg = req.ctx.org
7839
const requesterUsername = req.ctx.user
79-
const targetOrgUUID = req.params.uuid
40+
const targetUUID = req.params.uuid
8041
const body = req.body
8142

82-
// Make sure target org matches user org if not secretariat
83-
const isSecretariat = await orgRepo.isSecretariatByShortName(requesterOrg, { session })
84-
const requesterOrgUUID = await orgRepo.getOrgUUID(requesterOrg, { session })
85-
if (!isSecretariat && (requesterOrgUUID !== targetOrgUUID)) {
86-
return res.status(400).json({ message: 'User is not secretariat or admin for target org' })
87-
}
88-
8943
const user = await userRepo.findOneByUsernameAndOrgShortname(requesterUsername, requesterOrg, { session })
9044

9145
if (!body.body) {
9246
return res.status(400).json({ message: 'Missing required field body' })
9347
}
9448

9549
const conversationBody = {
96-
target_uuid: targetOrgUUID,
50+
target_uuid: targetUUID,
9751
author_id: user.UUID,
9852
author_name: [user.name.first, user.name.last].join(' '),
99-
author_role: isSecretariat ? 'Secretariat' : 'Partner',
53+
author_role: 'Secretariat',
10054
visibility: body.visibility ? body.visibility.toLowerCase() : 'private',
10155
body: body.body
10256
}
@@ -129,20 +83,20 @@ async function createConversationForOrg (req, res, next) {
12983

13084
async function updateMessage (req, res, next) {
13185
const repo = req.ctx.repositories.getConversationRepository()
132-
const targetOrgUUID = req.params.uuid
86+
const targetUUID = req.params.uuid
13387
const body = req.body
13488

13589
if (!body.body) {
13690
return res.status(400).json({ message: 'Missing required field body' })
13791
}
13892

139-
const result = await repo.updateConversation(body, targetOrgUUID)
93+
const result = await repo.updateConversation(body, targetUUID)
14094
return res.status(200).json(result)
14195
}
14296

14397
module.exports = {
14498
getAllConversations,
145-
getConversationsForOrg,
146-
createConversationForOrg,
99+
getConversationsForTargetUUID,
100+
createConversationForTargetUUID,
147101
updateMessage
148102
}

src/controller/conversation.controller/index.js

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,22 @@ router.get('/conversation',
1515
controller.getAllConversations
1616
)
1717

18-
// Get conversations for all orgs - SEC only
19-
router.get('/conversation/org',
18+
// Get all conversations for target UUID - SEC only
19+
router.get('/conversation/target/:uuid',
2020
mw.validateUser,
2121
mw.onlySecretariat,
2222
query().custom((query) => { return mw.validateQueryParameterNames(query, ['page']) }),
2323
query(['page']).custom((val) => { return mw.containsNoInvalidCharacters(val) }),
2424
query(['page']).optional().isInt({ min: CONSTANTS.PAGINATOR_PAGE }),
25-
controller.getAllConversations // TODO: for now, all conversations are targeted to orgs. Update this when conversations added for other objects
25+
controller.getConversationsForTargetUUID
2626
)
2727

28-
// Get conversations for org - SEC/ADMIN
29-
router.get('/conversation/org/:uuid',
28+
// Post conversation for target UUID - SEC only
29+
router.post('/conversation/target/:uuid',
3030
mw.validateUser,
31-
mw.onlySecretariatOrAdmin,
32-
query().custom((query) => { return mw.validateQueryParameterNames(query, ['page']) }),
33-
query(['page']).custom((val) => { return mw.containsNoInvalidCharacters(val) }),
34-
query(['page']).optional().isInt({ min: CONSTANTS.PAGINATOR_PAGE }),
35-
param(['uuid']).isUUID(4),
36-
controller.getConversationsForOrg
37-
)
38-
39-
// Post conversation for org - SEC/ADMIN
40-
router.post('/conversation/org/:uuid',
41-
mw.validateUser,
42-
mw.onlySecretariatOrAdmin,
31+
mw.onlySecretariat,
4332
param(['uuid']).isUUID(4),
44-
controller.createConversationForOrg
33+
controller.createConversationForTargetUUID
4534
)
4635

4736
// Update conversation message - SEC only

src/controller/org.controller/org.controller.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,8 @@ async function registryCreateOrg (req, res, next) {
249249
// If we get here, we know we are good to create
250250
const userRepo = req.ctx.repositories.getBaseUserRepository()
251251
const requestingUserUUID = await userRepo.getUserUUID(req.ctx.user, req.ctx.org, { session })
252-
returnValue = await repo.createOrg(req.ctx.body, { session, upsert: true }, false, requestingUserUUID)
252+
const isSecretariat = await repo.isSecretariatByShortName(req.ctx.org, { session })
253+
returnValue = await repo.createOrg(req.ctx.body, { session, upsert: true }, false, requestingUserUUID, isSecretariat)
253254

254255
await session.commitTransaction()
255256
logger.info({
@@ -293,7 +294,10 @@ async function createOrg (req, res, next) {
293294
await session.abortTransaction()
294295
return res.status(400).json(error.orgExists(body?.short_name))
295296
}
296-
returnValue = await repo.createOrg(req.ctx.body, { session, upsert: true }, true)
297+
const isSecretariat = await repo.isSecretariatByShortName(req.ctx.org, { session })
298+
const userRepo = req.ctx.repositories.getBaseUserRepository()
299+
const requestingUserUUID = await userRepo.getUserUUID(req.ctx.user, req.ctx.org, { session })
300+
returnValue = await repo.createOrg(req.ctx.body, { session, upsert: true }, true, requestingUserUUID, isSecretariat)
297301

298302
await session.commitTransaction()
299303
} catch (error) {
@@ -366,7 +370,9 @@ async function registryUpdateOrg (req, res, next) {
366370

367371
const userRepo = req.ctx.repositories.getBaseUserRepository()
368372
const requestingUserUUID = await userRepo.getUserUUID(req.ctx.user, req.ctx.org, { session })
369-
const updatedOrg = await orgRepository.updateOrg(shortNameUrlParameter, queryParametersJson, { session }, false, requestingUserUUID)
373+
const isSecretariat = await orgRepository.isSecretariatByShortName(req.ctx.org, { session })
374+
const isAdmin = await userRepo.isAdmin(req.ctx.user, req.ctx.org, { session })
375+
const updatedOrg = await orgRepository.updateOrg(shortNameUrlParameter, queryParametersJson, { session }, false, requestingUserUUID, isAdmin, isSecretariat)
370376

371377
responseMessage = { message: `${updatedOrg.short_name} organization was successfully updated.`, updated: updatedOrg } // Clarify message
372378
const payload = { action: 'update_org', change: `${updatedOrg.short_name} organization was successfully updated.`, org: updatedOrg }
@@ -413,9 +419,13 @@ async function updateOrg (req, res, next) {
413419
return res.status(403).json(error.duplicateShortname(queryParametersJson.new_short_name))
414420
}
415421

416-
const updatedOrg = await orgRepository.updateOrg(shortNameUrlParameter, queryParametersJson, { session }, true)
417-
418422
const userRepo = req.ctx.repositories.getBaseUserRepository()
423+
const isSecretariat = await orgRepository.isSecretariatByShortName(req.ctx.org, { session })
424+
const isAdmin = await userRepo.isAdmin(req.ctx.user, req.ctx.org, { session })
425+
const requestingUserUUID = await userRepo.getUserUUID(req.ctx.user, req.ctx.org, { session })
426+
427+
const updatedOrg = await orgRepository.updateOrg(shortNameUrlParameter, queryParametersJson, { session }, true, requestingUserUUID, isAdmin, isSecretariat)
428+
419429
responseMessage = { message: `${updatedOrg.short_name} organization was successfully updated.`, updated: updatedOrg } // Clarify message
420430
const payload = { action: 'update_org', change: `${updatedOrg.short_name} organization was successfully updated.`, org: updatedOrg }
421431
payload.user_UUID = await userRepo.getUserUUID(req.ctx.user, updatedOrg.UUID)

src/controller/registry-org.controller/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,6 @@ router.post('/registryOrg',
213213
*/
214214
mw.useRegistry(),
215215
mw.validateUser,
216-
mw.onlySecretariat,
217216
parseError,
218217
parsePostParams,
219218
controller.CREATE_ORG
@@ -300,7 +299,6 @@ router.put('/registryOrg/:shortname',
300299
*/
301300
mw.useRegistry(),
302301
mw.validateUser,
303-
mw.onlySecretariat,
304302
param(['shortname']).isString().trim(),
305303
parseError,
306304
parsePostParams,

0 commit comments

Comments
 (0)