Skip to content

Commit

Permalink
Fix user add endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
NickPhura committed Sep 4, 2024
1 parent a2be81a commit 2caea88
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
21 changes: 12 additions & 9 deletions api/src/paths/user/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,9 @@ export function addSystemRoleUser(): RequestHandler {

const userService = new UserService(connection);

// If user already exists, do nothing and return early
const user = await userService.getUserByIdentifier(userIdentifier, identitySource);

if (user) {
if (user?.record_end_date === null) {
// User already exists and is active, do nothing and return early
throw new HTTP409('Failed to add user. User with matching identifier already exists.');
}

Expand All @@ -166,12 +165,16 @@ export function addSystemRoleUser(): RequestHandler {
family_name
);

if (userObject) {
if (role_name) {
await userService.addUserSystemRoleByName(userObject.system_user_id, role_name);
} else {
await userService.addUserSystemRoles(userObject.system_user_id, [roleId]);
}
// Delete existin role
if (userObject.role_ids.length) {
await userService.deleteUserSystemRoles(userObject.system_user_id);
}

// Add the new role
if (role_name) {
await userService.addUserSystemRoleByName(userObject.system_user_id, role_name);

Check warning on line 175 in api/src/paths/user/add.ts

View check run for this annotation

Codecov / codecov/patch

api/src/paths/user/add.ts#L175

Added line #L175 was not covered by tests
} else {
await userService.addUserSystemRoles(userObject.system_user_id, [roleId]);
}

await connection.commit();
Expand Down
2 changes: 1 addition & 1 deletion database/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"strict": true,
"typeRoots": ["node_modules/@types"]
},
"include": ["src", "20240722000002_remove_duplicate_users.ts", "20240722000002_remove_duplicate_users.ts"],
"include": ["src"],
"ts-node": {
"swc": true
}
Expand Down

0 comments on commit 2caea88

Please sign in to comment.