Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Edit Role API Endpoint #37

Merged
merged 5 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backend/graphql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const graphQLMiddlewares = {
deleteSimpleEntity: authorizedByAllRoles(),
createUser: authorizedByAdmin(),
updateUser: authorizedByAdmin(),
updateUserRole: authorizedByAdmin(),
deleteUserById: authorizedByAdmin(),
deleteUserByEmail: authorizedByAdmin(),
logout: isAuthorizedByUserId("userId"),
Expand Down
8 changes: 7 additions & 1 deletion backend/graphql/resolvers/userResolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import UserService from "../../services/implementations/userService";
import IAuthService from "../../services/interfaces/authService";
import IEmailService from "../../services/interfaces/emailService";
import IUserService from "../../services/interfaces/userService";
import { CreateUserDTO, UpdateUserDTO, UserDTO } from "../../types";
import { CreateUserDTO, Role, UpdateUserDTO, UserDTO } from "../../types";
import { generateCSV } from "../../utilities/CSVUtils";

const userService: IUserService = new UserService();
Expand Down Expand Up @@ -50,6 +50,12 @@ const userResolvers = {
): Promise<UserDTO> => {
return userService.updateUserById(id, user);
},
updateUserRole: async (
_parent: undefined,
{ id, role }: { id: string; role: Role },
): Promise<void> => {
return userService.updateUserRoleById(id, role);
},
deleteUserById: async (
_parent: undefined,
{ id }: { id: string },
Expand Down
2 changes: 1 addition & 1 deletion backend/graphql/types/userType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ const userType = gql`
firstName: String!
lastName: String!
email: String!
role: Role!
}

extend type Query {
Expand All @@ -40,6 +39,7 @@ const userType = gql`
extend type Mutation {
createUser(user: CreateUserDTO!): UserDTO!
updateUser(id: ID!, user: UpdateUserDTO!): UserDTO!
updateUserRole(id: ID!, role: Role!): ID
deleteUserById(id: ID!): ID
deleteUserByEmail(email: String!): ID
}
Expand Down
26 changes: 23 additions & 3 deletions backend/services/implementations/userService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ class UserService implements IUserService {
// must explicitly specify runValidators when updating through findByIdAndUpdate
oldUser = await MgUser.findByIdAndUpdate(
userId,
{ firstName: user.firstName, lastName: user.lastName, role: user.role },
{ firstName: user.firstName, lastName: user.lastName },
{ runValidators: true },
);

Expand All @@ -225,7 +225,6 @@ class UserService implements IUserService {
{
firstName: oldUser.firstName,
lastName: oldUser.lastName,
role: oldUser.role,
},
{ runValidators: true },
);
Expand All @@ -251,10 +250,31 @@ class UserService implements IUserService {
firstName: user.firstName,
lastName: user.lastName,
email: updatedFirebaseUser.email ?? "",
role: user.role,
role: oldUser.role,
};
}

async updateUserRoleById(userId: string, newRole: Role): Promise<void> {
try {
const user = await MgUser.findById(userId);

if (!user) {
throw new Error(`userId ${userId} not found.`);
}
// must explicitly specify runValidators when updating through findByIdAndUpdate
await MgUser.findByIdAndUpdate(
userId,
{ role: newRole },
{ runValidators: true },
);
} catch (error: unknown) {
Logger.error(
`Failed to update user role. Reason = ${getErrorMessage(error)}`,
);
throw error;
}
}

async deleteUserById(userId: string): Promise<void> {
try {
const deletedUser: User | null = await MgUser.findByIdAndDelete(userId);
Expand Down
8 changes: 8 additions & 0 deletions backend/services/interfaces/userService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ interface IUserService {
*/
updateUserById(userId: string, user: UpdateUserDTO): Promise<UserDTO>;

/**
* Update a user's role - only accessible by Admin.
* @param userId user's id
* @param role the new role for the user
* @throws Error if role update fails
*/
updateUserRoleById(userId: string, role: string): void | PromiseLike<void>;

/**
* Delete a user by id
* @param userId user's userId
Expand Down
Loading