Skip to content

Commit 3969e11

Browse files
committed
Added hydration to GETs
1 parent 49f4a90 commit 3969e11

4 files changed

Lines changed: 38 additions & 1 deletion

File tree

schemas/registry-user/get-registry-user-response.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@
3838
"last"
3939
]
4040
},
41+
"role": {
42+
"type": "string",
43+
"enum": ["ADMIN"],
44+
"description": "The role of the user in the organization. Currently only 'ADMIN' is supported."
45+
},
4146
"org_affiliations": {
4247
"type": "array",
4348
"items": {

schemas/registry-user/list-registry-users-response.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@
6666
},
6767
"role": {
6868
"type": "string",
69-
"description": "Unique identifier for the user"
69+
"enum": ["ADMIN"],
70+
"description": "The role of the user in the organization. Currently only 'ADMIN' is supported."
7071
},
7172
"secret": {
7273
"type": "string",

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,12 @@ async function getUsers (req, res, next) {
470470
// This should always return Registry typed
471471
const payload = await userRepo.getAllUsersByOrgShortname(orgShortName, options, true)
472472

473+
// Hydrate the role field
474+
const org = await orgRepo.findOneByShortName(orgShortName)
475+
payload.users.forEach(user => {
476+
user.role = org.admins.includes(user.UUID) ? 'ADMIN' : user.role // Default to existing role if not admin
477+
})
478+
473479
logger.info({ uuid: req.ctx.uuid, message: `The users of ${orgShortName} organization were sent to the user.` })
474480
return res.status(200).json(payload)
475481
} catch (err) {

src/controller/registry-user.controller/registry-user.controller.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,30 @@ async function getAllUsers (req, res, next) {
2525

2626
try {
2727
returnValue = await repo.getAllUsers(options)
28+
// Hydrate roles
29+
const orgRepo = req.ctx.repositories.getBaseOrgRepository()
30+
const distinctOrgUUIDs = [...new Set(returnValue.users.map(u => u.org_UUID))]
31+
32+
// Fetch all relevant orgs in one go (or in parallel) if possible, but map is easy for now
33+
// Since we don't have a "getManyOrgsByUUID", we might need to do it one by one or improve repository
34+
// For now, let's iterate and fetch. It's not optimal but safe given repo limitations.
35+
// Optimization: We can build a map of orgUUID -> orgObject
36+
const orgMap = {}
37+
for (const uuid of distinctOrgUUIDs) {
38+
// We need the org content to get admins
39+
const org = await orgRepo.findOneByUUID(uuid)
40+
if (org) {
41+
orgMap[uuid] = org
42+
}
43+
}
44+
45+
returnValue.users.forEach(user => {
46+
const org = orgMap[user.org_UUID]
47+
if (org && org.admins && org.admins.includes(user.UUID)) {
48+
user.role = 'ADMIN'
49+
}
50+
// If not admin, leave as is (undefined or empty or whatever it was)
51+
})
2852
} finally {
2953
await session.endSession()
3054
}
@@ -85,6 +109,7 @@ async function getUser (req, res, next) {
85109

86110
const user = result.toObject ? result.toObject() : result
87111
const userPayload = _.omit(user, ['secret', '_id', '__v'])
112+
userPayload.role = org.admins.includes(userPayload.UUID) ? 'ADMIN' : userPayload.role
88113
return res.status(200).json(userPayload)
89114
} catch (err) {
90115
next(err)

0 commit comments

Comments
 (0)