From 6e0f7ae9435157a86f255d3ad67238e6fad5f332 Mon Sep 17 00:00:00 2001 From: CodeTrauma Date: Mon, 13 Nov 2023 13:09:19 +0300 Subject: [PATCH] feat(admin) route for removing admins --- src/data/db/create.ts | 2 +- src/data/db/find.ts | 4 ++-- src/data/db/update.ts | 4 ++-- src/endpoints/admin/admin.ts | 36 +++++++++++++++++++++++++++++++++++- 4 files changed, 40 insertions(+), 6 deletions(-) diff --git a/src/data/db/create.ts b/src/data/db/create.ts index ac0cc0b..f79a4eb 100644 --- a/src/data/db/create.ts +++ b/src/data/db/create.ts @@ -7,7 +7,7 @@ const prisma = new PrismaClient(); /** * Creates a new room with the given name and optional parameters. - * @param {string} name - The name of the room. + * @param {string} roomName - The name of the room. * @param {number} [rateLimit=10000] - The length of an epoch in milliseconds * @param {number} [userMessageLimit=12] - The message limit per user per epoch * @param {number} [numClaimCodes=0] - The number of claim codes to generate for the room. diff --git a/src/data/db/find.ts b/src/data/db/find.ts index 69f3984..fa9d284 100644 --- a/src/data/db/find.ts +++ b/src/data/db/find.ts @@ -55,7 +55,7 @@ https://github.com/Discreetly/IdentityCommitmentNullifierCircuit <- Circuit and */ /** * This function takes in an identity and returns the rooms the identity is in. - * @param identity - the identity of a user + * @param {string} identity - the identity of a user * @returns an array of roomIds */ export async function findRoomsByIdentity(identity: string): Promise { @@ -146,7 +146,7 @@ export async function findRoomWithMessageId( if (room.epochs[0]) { return room.epochs[0].messages[0] as MessageI; } else { - console.debug('Epoch not found'); + console.debug('Epoch not found, new epoch'); return null; } } catch (err) { diff --git a/src/data/db/update.ts b/src/data/db/update.ts index 0c1a358..13db1bf 100644 --- a/src/data/db/update.ts +++ b/src/data/db/update.ts @@ -13,8 +13,8 @@ const prisma = new PrismaClient(); * It adds the identity commitment to the identity list of each room, * and also adds it to the bandada of each room. The identity commitment is * sanitized before being added to the database. - * @param idc - The identity commitment of the user - * @param roomIds - The list of roomIds that the user is in + * @param {string} idc - The identity commitment of the user + * @param {string[]} roomIds - The list of roomIds that the user is in * @returns {Promise} - A promise that resolves when the update is complete */ export async function updateRoomIdentities( diff --git a/src/endpoints/admin/admin.ts b/src/endpoints/admin/admin.ts index 59c80b5..d499a1e 100644 --- a/src/endpoints/admin/admin.ts +++ b/src/endpoints/admin/admin.ts @@ -304,5 +304,39 @@ router.post( } }) ); - +router.post('/:roomId/removeAdmin', adminAuth, asyncHandler(async (req: Request, res: Response) => { + const { roomId } = req.params; + const { idc } = req.body as { idc: string }; + try { + const admins = await prisma.rooms.findUnique({ + where: { + roomId: roomId + }, + select: { + adminIdentities: true + } + }) as { adminIdentities: string[] }; + if (!admins) { + res.status(400).json({ error: 'Room not found' }); + return; + } + if (!admins.adminIdentities.includes(idc)) { + res.status(400).json({ error: 'Admin not found in room' }); + return; + } + await prisma.rooms.update({ + where: { + roomId: roomId + }, + data: { + adminIdentities: { + set: admins.adminIdentities.filter((admin) => admin !== idc) + } + } + }); + res.status(200).json({ message: `Admin removed from room ${roomId}` }); + } catch (err) { + res.status(500).json({ error: 'Internal Server Error' }); + } +})); export default router;