Skip to content

Commit

Permalink
force to remove region from admin roles
Browse files Browse the repository at this point in the history
  • Loading branch information
Janderson Souza Matias authored and Janderson Souza Matias committed Jan 15, 2024
1 parent 4c4ba0b commit 4a77c9a
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions src/modules/user/service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,32 +37,28 @@ export class UserService {

static updateAdmin = async (
user_id: User["id"],
newUser: Partial<User & { currentPassword?: string }>
newUser: Partial<User>
): Promise<void> => {
const userRepository = dataSource.getRepository(User);
try {
if (!!newUser.currentPassword) {
const userDb = await userRepository.findOne({ where: { id: user_id } });
if (!userDb) throw new Error("User not found");
await userRepository.update(user_id as string, userDb);
return;
}
} catch (err) {
throw new Error("Current password wrong.");
}

const { name, email, role, region_id } = newUser;

await userRepository.update(user_id as string, {
name,
email,
role,
region_id,
region_id: role === "admin" ? null : region_id,
});
};

static signUpAdmin = async (newUser: Partial<User>): Promise<User> => {
const userRepository = dataSource.getRepository(User);
return await userRepository.save(userRepository.create(newUser));
return await userRepository.save(
userRepository.create({
...newUser,
region_id: newUser.role === "admin" ? undefined : newUser.region_id,
})
);
};

static removeAdmin = async (id: User["id"]): Promise<void> => {
Expand Down

0 comments on commit 4a77c9a

Please sign in to comment.