From 952d897a8acdf3edd0fd082781f94df33df92fa9 Mon Sep 17 00:00:00 2001 From: Tanner Shaw Date: Fri, 25 Aug 2023 02:09:40 -0500 Subject: [PATCH] refactor(claimCodes) /addcode now properly creates claimCodes and rooms associations --- src/data/db.ts | 97 +++++++++++++++++++++--------------------- src/endpoints/index.ts | 46 +++++++++++--------- 2 files changed, 75 insertions(+), 68 deletions(-) diff --git a/src/data/db.ts b/src/data/db.ts index 36c8ad6..fe36f17 100644 --- a/src/data/db.ts +++ b/src/data/db.ts @@ -94,7 +94,6 @@ export async function getRoomsByIdentity(identity: string): Promise { } } - /** * Finds a claim code in the database. * @@ -109,10 +108,10 @@ export function findClaimCode(code: string): Promise { } /** -* Update the claim_code table to mark the given code as claimed. -* @param {string} code - The code to update -* @returns {Promise} - The rooms associated with the claim code -*/ + * Update the claim_code table to mark the given code as claimed. + * @param {string} code - The code to update + * @returns {Promise} - The rooms associated with the claim code + */ export function updateClaimCode(code: string): Promise { return prisma.claimCodes.update({ @@ -121,7 +120,6 @@ export function updateClaimCode(code: string): Promise { }); } - /* The sanitizeIDC function takes a string and returns a string. The string is converted to a BigInt and then back to a string. @@ -144,14 +142,14 @@ function sanitizeIDC(idc: string): string { } /** -* This code updates the identity commitments of a list of rooms. -* 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 -* @returns {Promise} - A promise that resolves when the update is complete -*/ + * This code updates the identity commitments of a list of rooms. + * 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 + * @returns {Promise} - A promise that resolves when the update is complete + */ export async function updateRoomIdentities( idc: string, @@ -163,6 +161,7 @@ export async function updateRoomIdentities( where: { id: { in: roomIds } } }) .then(async (rooms) => { + console.log(rooms); await addIdentityToIdentityListRooms(rooms, identityCommitment); addIdentityToBandadaRooms(rooms, identityCommitment); }) @@ -176,10 +175,10 @@ export async function updateRoomIdentities( * @param {rooms} - The list of rooms that the user is in * @param {string} identityCommitment - The user's identity commitment */ -function addIdentityToIdentityListRooms( +async function addIdentityToIdentityListRooms( rooms, identityCommitment: string -): unknown { +): Promise { const identityListRooms = rooms .filter( (room: RoomI) => @@ -188,9 +187,10 @@ function addIdentityToIdentityListRooms( ) .map((room) => room.id as string); + console.log(identityListRooms.length); if (identityListRooms.length > 0) { for (const room of rooms) { - return prisma.rooms + return await prisma.rooms .update({ where: { id: room.id }, data: { @@ -215,8 +215,6 @@ function addIdentityToIdentityListRooms( } } - - /** * This code adds a new identity commitment to the list of identities in a bandada room. * First we get the list of bandada rooms that contain the identity commitment. @@ -241,7 +239,7 @@ function addIdentityToBandadaRooms(rooms, identityCommitment: string): void { const rateCommitment = getRateCommitmentHash( BigInt(identityCommitment), BigInt((room.userMessageLimit as number) ?? 1) - ).toString() + ).toString(); if (!room.bandadaAPIKey) { console.error('API key is missing for room:', room); return; @@ -279,12 +277,12 @@ function addIdentityToBandadaRooms(rooms, identityCommitment: string): void { } /** -* This function is used to find rooms that have been updated -* It is used in the findUpdatedRooms function -* It is important because it allows the user to see which rooms have been updated -* @param {string[]} roomIds - The list of roomIds that the user is in -* @returns {Promise} - A promise that resolves to a list of rooms -*/ + * This function is used to find rooms that have been updated + * It is used in the findUpdatedRooms function + * It is important because it allows the user to see which rooms have been updated + * @param {string[]} roomIds - The list of roomIds that the user is in + * @returns {Promise} - A promise that resolves to a list of rooms + */ export async function findUpdatedRooms(roomIds: string[]): Promise { const rooms = await prisma.rooms.findMany({ @@ -298,19 +296,17 @@ export async function findUpdatedRooms(roomIds: string[]): Promise { }); } - /** -* This function creates a system message in a room. -* The message will be the same in all rooms if no roomId is passed. -* If a roomId is passed, the message will be created in that room. -* @param {string} message - The message to be created -* @param {string} roomId - The roomId to create the message in -*/ + * This function creates a system message in a room. + * The message will be the same in all rooms if no roomId is passed. + * If a roomId is passed, the message will be created in that room. + * @param {string} message - The message to be created + * @param {string} roomId - The roomId to create the message in + */ export function createSystemMessages( message: string, roomId?: string - ): Promise { - +): Promise { const query = roomId ? { where: { roomId } } : undefined; return prisma.rooms .findMany(query) @@ -338,26 +334,31 @@ export function createSystemMessages( } /** -* This function takes in an identity and a room and removes the identity from the room -* by setting its semaphoreIdentities to 0n and identities to 0n -* @param {string} idc - The identity of the user -* @param {RoomI} room - The room to remove the identity from -* @returns {Promise} - A promise that resolves to the room -*/ + * This function takes in an identity and a room and removes the identity from the room + * by setting its semaphoreIdentities to 0n and identities to 0n + * @param {string} idc - The identity of the user + * @param {RoomI} room - The room to remove the identity from + * @returns {Promise} - A promise that resolves to the room + */ export function removeIdentityFromRoom( idc: string, room: RoomI ): Promise { - const updateSemaphoreIdentities = room.semaphoreIdentities?.map((identity) => - identity === idc ? '0n' : identity as string - ) ?? []; + const updateSemaphoreIdentities = + room.semaphoreIdentities?.map((identity) => + identity === idc ? '0n' : (identity as string) + ) ?? []; - const rateCommitmentsToUpdate = getRateCommitmentHash(BigInt(idc), BigInt(room.userMessageLimit!)).toString() + const rateCommitmentsToUpdate = getRateCommitmentHash( + BigInt(idc), + BigInt(room.userMessageLimit!) + ).toString(); - const updatedRateCommitments = room.identities?.map((limiter) => - limiter == rateCommitmentsToUpdate ? '0n' : limiter as string - ) ?? [] + const updatedRateCommitments = + room.identities?.map((limiter) => + limiter == rateCommitmentsToUpdate ? '0n' : (limiter as string) + ) ?? []; return prisma.rooms .update({ diff --git a/src/endpoints/index.ts b/src/endpoints/index.ts index c01e316..ac0d33f 100644 --- a/src/endpoints/index.ts +++ b/src/endpoints/index.ts @@ -316,29 +316,36 @@ export function initEndpoints(app: Express, adminAuth: RequestHandler) { const codes = genClaimCodeArray(numCodes); return await prisma.rooms.findMany(query).then((rooms) => { const roomIds = rooms.map((room) => room.id); - console.log(roomIds); - console.log(codes); - const createCodes = rooms.flatMap((room) => - codes.map((code) => - prisma.claimCodes.create({ - data: { - claimcode: code.claimcode, - claimed: false, - roomIds: [room.id], - rooms: { - connect: { - roomId: room.roomId + const createCodes = codes.map((code) => { + return prisma.claimCodes.create({ + data: { + claimcode: code.claimcode, + claimed: false, + roomIds: roomIds + } + }).then((newCode) => { + const updatePromises = rooms.map((room) => { + return prisma.rooms.update({ + where: { + roomId: room.roomId + }, + data: { + claimCodeIds: { + push: newCode.id } } - } - }) - ) - ); + }); + }); + return Promise.all(updatePromises); + }).catch((err) => { + console.error(err); + res.status(500).json({ error: 'Internal Server Error' }); + }); + }); + return Promise.all(createCodes) .then(() => { - res - .status(200) - .json({ message: 'Claim codes added successfully', codes }); + res.status(200).json({ message: 'Claim codes added successfully', codes }); }) .catch((err) => { console.error(err); @@ -347,7 +354,6 @@ export function initEndpoints(app: Express, adminAuth: RequestHandler) { }); }) ); - /** * Adds claim codes to a room *