Skip to content

Commit

Permalink
reduce cognitive complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
m5r committed Apr 4, 2024
1 parent 1595a6a commit 6f51da9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 28 deletions.
57 changes: 30 additions & 27 deletions api/src/controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,35 @@ const convertUserListToV1 = (users=[]) => {
return users;
};

const getUserByUsername = async (req, res) => {
try {
const username = req.params.username;
const credentials = auth.basicAuthCredentials(req);
const [hasPermission, isGettingSelf] = await Promise.all([
hasPermissions(req, 'can_view_users'),
isUpdatingSelf(req, credentials, username),
]).catch(error => {
if (error.statusCode === 401) {
throw { code: 401, message: 'Not logged in', err: error };
}

throw error;
});

if (!hasPermission && !isGettingSelf) {
throw {
message: 'You do not have permissions to fetch this person',
code: 403,
};
}

const body = await users.getUser(username);
res.json(body);
} catch (error) {
serverUtils.error(error, req, res);
}
};

module.exports = {
get: (req, res) => {
return getUserList(req)
Expand Down Expand Up @@ -239,33 +268,7 @@ module.exports = {
v2: {
get: async (req, res) => {
if (req.params?.username) {
try {
const username = req.params.username;
const credentials = auth.basicAuthCredentials(req);
const [hasPermission, isGettingSelf] = await Promise.all([
hasPermissions(req, 'can_view_users'),
isUpdatingSelf(req, credentials, username),
]).catch(error => {
if (error.statusCode === 401) {
throw { code: 401, message: 'Not logged in', err: error };
}

throw error;
});

if (!hasPermission && !isGettingSelf) {
throw {
message: 'You do not have permissions to fetch this person',
code: 403,
};
}

const body = await users.getUser(username);
res.json(body);
} catch (error) {
serverUtils.error(error, req, res);
}
return;
return getUserByUsername(req, res);
}

try {
Expand Down
2 changes: 1 addition & 1 deletion shared-libs/user-management/src/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ module.exports = {
getUser: async (username) => {
const [user, setting] = await getUserDocsByName(username);
const facilities = await facility.list([user], [setting]);
return mapUsers([user], [setting], facilities);
return mapUsers([user], [setting], facilities)[0];
},
getList: async (filters) => {
let users;
Expand Down

0 comments on commit 6f51da9

Please sign in to comment.