From 8858dcf7c680feb03cf034ac9a5ad483b99bd2c9 Mon Sep 17 00:00:00 2001 From: Tanner Shaw Date: Tue, 5 Sep 2023 19:49:51 -0500 Subject: [PATCH] refactor(claimcodes) Refactored /join route to check, update, and delete claimcodes based on UNIX time and or uses left --- prisma/schema.prisma | 3 +-- src/data/db.ts | 25 ++++++++++++++++--------- src/endpoints/index.ts | 34 +++++++++++++++++++++++++--------- 3 files changed, 42 insertions(+), 20 deletions(-) diff --git a/prisma/schema.prisma b/prisma/schema.prisma index a495d75..74ef2be 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -34,9 +34,8 @@ model Rooms { model ClaimCodes { id String @id @default(auto()) @map("_id") @db.ObjectId claimcode String @unique - claimed Boolean @default(false) roomIds String[] @default([]) @db.ObjectId - expiresAt Int + expiresAt Int @default(0) usesLeft Int @default(-1) rooms Rooms[] @relation(fields: [roomIds], references: [id]) } diff --git a/src/data/db.ts b/src/data/db.ts index 5cd5e0b..a26bdaa 100644 --- a/src/data/db.ts +++ b/src/data/db.ts @@ -11,14 +11,14 @@ import getRateCommitmentHash from '../crypto/rateCommitmentHasher'; const prisma = new PrismaClient(); interface CodeStatus { - claimed: boolean; roomIds: string[]; expiresAt: number; + usesLeft: number; } -interface RoomsFromClaimCode { - roomIds: string[]; -} +// interface RoomsFromClaimCode { +// roomIds: string[]; +// } /** * Gets a room by id @@ -114,11 +114,18 @@ export function findClaimCode(code: string): Promise { * @returns {Promise} - The rooms associated with the claim code */ -export function updateClaimCode(code: string): Promise { - return prisma.claimCodes.update({ - where: { claimcode: code }, - data: { claimed: true } - }); +export async function updateClaimCode(code: string): Promise { + const claimCode = await findClaimCode(code); + if (!claimCode) { + return; + } else { + return await prisma.claimCodes.update({ + where: { claimcode: code }, + data: { + usesLeft: claimCode.usesLeft - 1 + } + }) + } } /* diff --git a/src/endpoints/index.ts b/src/endpoints/index.ts index a6580ca..9f7f727 100644 --- a/src/endpoints/index.ts +++ b/src/endpoints/index.ts @@ -144,12 +144,28 @@ export function initEndpoints(app: Express, adminAuth: RequestHandler) { const codeStatus = await findClaimCode(code); if (!codeStatus || codeStatus.expiresAt < Date.now()) { + await prisma.claimCodes.delete({ + where: { + claimcode: code + } + }); res.status(400).json({ message: 'Claim code already used' }); return; } - - const claimCode = await updateClaimCode(code); - const roomIds = claimCode.roomIds; + if (codeStatus.usesLeft !== -1 && codeStatus.usesLeft >= 0) { + const updatedCode = await updateClaimCode(code); + if (updatedCode && updatedCode.usesLeft === 0) { + await prisma.claimCodes.delete({ + where: { + claimcode: code + } + }); + } + } else { + res.status(400).json({ message: 'Claim code already used' }); + return; + } + const roomIds = codeStatus.roomIds; const addedRooms = await updateRoomIdentities(idc, roomIds); @@ -309,17 +325,18 @@ export function initEndpoints(app: Express, adminAuth: RequestHandler) { ['/addcode', '/api/addcode'], adminAuth, asyncHandler(async (req: Request, res: Response) => { - const { numCodes, rooms, all, expires } = req.body as { + const { numCodes, rooms, all, expiresAt, usesLeft } = req.body as { numCodes: number; rooms: string[]; all: boolean; - expires: number; + expiresAt: number; + usesLeft: number; }; const currentDate = new Date(); const threeMonthsLater = new Date(currentDate).setMonth(currentDate.getMonth() + 3); - const codeExpires = expires ? expires : threeMonthsLater + const codeExpires = expiresAt ? expiresAt : threeMonthsLater const query = all ? undefined : { where: { roomId: { in: rooms } } }; const codes = genClaimCodeArray(numCodes); @@ -329,9 +346,9 @@ export function initEndpoints(app: Express, adminAuth: RequestHandler) { return prisma.claimCodes.create({ data: { claimcode: code.claimcode, - claimed: false, roomIds: roomIds, - expiresAt: codeExpires + expiresAt: codeExpires, + usesLeft: usesLeft } }).then((newCode) => { const updatePromises = rooms.map((room) => { @@ -403,7 +420,6 @@ export function initEndpoints(app: Express, adminAuth: RequestHandler) { return prisma.claimCodes.create({ data: { claimcode: code.claimcode, - claimed: false, expiresAt: codeExpires, rooms: { connect: {