({
userId,
role,
}: {
userId: string;
role: Roles;
})
| 16 | |
| 17 | export default class UsersService { |
| 18 | static async updateUserRole({ |
| 19 | userId, |
| 20 | role, |
| 21 | }: { |
| 22 | userId: string; |
| 23 | role: Roles; |
| 24 | }) { |
| 25 | const userToUpdate = await prisma.users.findUnique({ |
| 26 | where: { id: userId }, |
| 27 | }); |
| 28 | if (!userToUpdate) { |
| 29 | throw 'user not found'; |
| 30 | } |
| 31 | |
| 32 | // the account it should have at least another owner |
| 33 | if (userToUpdate.role === Roles.OWNER && role !== Roles.OWNER) { |
| 34 | const anotherOwner = await prisma.users.findFirst({ |
| 35 | where: { |
| 36 | accountsId: userToUpdate.accountsId, |
| 37 | role: Roles.OWNER, |
| 38 | NOT: { |
| 39 | id: userToUpdate.id, |
| 40 | }, |
| 41 | }, |
| 42 | }); |
| 43 | if (!anotherOwner) { |
| 44 | throw 'the account need at least one owner'; |
| 45 | } |
| 46 | } |
| 47 | return await prisma.users.update({ |
| 48 | where: { id: userToUpdate.id }, |
| 49 | data: { |
| 50 | role, |
| 51 | }, |
| 52 | }); |
| 53 | } |
| 54 | static async updateTenant({ |
| 55 | authId, |
| 56 | accountId, |
no test coverage detected