From f6379b2bda1727eab2a0afd6fce323892a93413b Mon Sep 17 00:00:00 2001 From: Lauren Ji Date: Sat, 14 Sep 2024 11:02:30 -0400 Subject: [PATCH 1/4] initial --- services/expo/src/routes/project.ts | 52 +++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/services/expo/src/routes/project.ts b/services/expo/src/routes/project.ts index 570bf33..ab927a2 100644 --- a/services/expo/src/routes/project.ts +++ b/services/expo/src/routes/project.ts @@ -214,7 +214,7 @@ projectRoutes.route("/").post( } // check for first free table group to assign table number to - const isFreeTableGroup = projectsInCurrentExpoAndTableGroup.length < tableGroup.tableCapacity; + const isFreeTableGroup = projectsInCurrentExpoAndTableGroup.length < tableGroup.tableCapacity; if (isFreeTableGroup && firstFreeTableGroup === undefined) { firstFreeTableGroup = tableGroup; } @@ -229,7 +229,7 @@ projectRoutes.route("/").post( ); } - // assigns table to first unused number + // assigns table to first unused number let tableNumber; for (let i = 1; i <= totalCapacity; i++) { if (!tableNumberSet.has(i)) { @@ -368,6 +368,54 @@ projectRoutes.route("/:id").patch( }); return; } + } else if (req.body.tableGroupId || req.body.expo) { + const currentHexathon = await getCurrentHexathon(req); + + const tableGroups = await prisma.tableGroup.findMany({ + where: { + hexathon: currentHexathon.id, + }, + }); + + const projectsInCurrentExpo = await prisma.project.findMany({ + where: { + hexathon: currentHexathon.id, + expo, + }, + }); + + let firstFreeTableGroup: undefined | TableGroup; + let totalCapacity = 0; + const tableNumberSet = new Set(); + + // select first non-empty tableGroup + for (const tableGroup of tableGroups) { + const projectsInCurrentExpoAndTableGroup = projectsInCurrentExpo.filter( + project => project.tableGroupId === tableGroup.id + ); + + for (const project of projectsInCurrentExpoAndTableGroup) { + tableNumberSet.add(project.table); + } + + // check for first free table group to assign table number to + const isFreeTableGroup = + projectsInCurrentExpoAndTableGroup.length < tableGroup.tableCapacity; + if (isFreeTableGroup && firstFreeTableGroup === undefined) { + firstFreeTableGroup = tableGroup; + } + + totalCapacity += tableGroup.tableCapacity; + } + + // no free table could be found; all table groups' capacities are full + if (!firstFreeTableGroup) { + throw new BadRequestError( + "Submission could not be saved due to issue with table groups - please contact help desk" + ); + } + + tableGroup = firstFreeTableGroup.id; } const dbCategories = await prisma.category.findMany({ From a3daa4c1b6d1b53d62bbf3088902776f6cb494ee Mon Sep 17 00:00:00 2001 From: Lauren Ji Date: Fri, 20 Sep 2024 12:15:00 -0400 Subject: [PATCH 2/4] finish --- services/expo/src/routes/project.ts | 76 +++++++++++++++++------------ 1 file changed, 46 insertions(+), 30 deletions(-) diff --git a/services/expo/src/routes/project.ts b/services/expo/src/routes/project.ts index ab927a2..5f262b9 100644 --- a/services/expo/src/routes/project.ts +++ b/services/expo/src/routes/project.ts @@ -309,9 +309,12 @@ projectRoutes.route("/").post( projectRoutes.route("/:id").patch( isAdmin, asyncHandler(async (req, res) => { + console.log("BODY"); + console.log(req.body); let members: any[] = []; let categories: any[] = []; let tableGroup; + let newTableNumber = -1; const config = await getConfig(); const currentProject = await prisma.project.findUnique({ @@ -339,36 +342,7 @@ projectRoutes.route("/:id").patch( } } - if (req.body.table) { - const tableNumber = parseInt(req.body.table); - const projectsInSameGroup = await prisma.project.findMany({ - where: { tableGroupId: tableGroup, expo }, - }); - - const tableGroups = await prisma.tableGroup.findMany(); - - const maxTableCap = Math.max(...tableGroups.map((group: TableGroup) => group.tableCapacity)); - console.log("Max table cap", maxTableCap); - if (tableNumber > maxTableCap) { - res.status(200).send({ - error: true, - message: "Error: Table Number Too Large.", - }); - return; - } - - const isDuplicate = projectsInSameGroup.some( - project => project.id !== parseInt(req.params.id) && project.table === tableNumber - ); - - if (isDuplicate) { - res.status(200).send({ - error: true, - message: "Error: Duplicate Table Number.", - }); - return; - } - } else if (req.body.tableGroupId || req.body.expo) { + const reassignTable = async () => { const currentHexathon = await getCurrentHexathon(req); const tableGroups = await prisma.tableGroup.findMany({ @@ -416,6 +390,47 @@ projectRoutes.route("/:id").patch( } tableGroup = firstFreeTableGroup.id; + for (let i = 1; i <= totalCapacity; i++) { + if (!tableNumberSet.has(i)) { + newTableNumber = i; + break; + } + } + console.log("table reassigned to:", firstFreeTableGroup.name, newTableNumber); + }; + + if (req.body.table) { + const tableNumber = parseInt(req.body.table); + const projectsInSameGroup = await prisma.project.findMany({ + where: { tableGroupId: tableGroup, expo }, + }); + + const tableGroups = await prisma.tableGroup.findMany(); + + const maxTableCap = Math.max(...tableGroups.map((group: TableGroup) => group.tableCapacity)); + console.log("Max table cap", maxTableCap); + if (tableNumber > maxTableCap) { + res.status(200).send({ + error: true, + message: "Error: Table Number Too Large.", + }); + return; + } + + const isDuplicate = projectsInSameGroup.some( + project => project.id !== parseInt(req.params.id) && project.table === tableNumber + ); + + if (isDuplicate) { + await reassignTable(); + // res.status(200).send({ + // error: true, + // message: "Error: Duplicate Table Number.", + // }); + // return; + } + } else if (req.body.tableGroupId || req.body.expo) { + await reassignTable(); } const dbCategories = await prisma.category.findMany({ @@ -464,6 +479,7 @@ projectRoutes.route("/:id").patch( where: { id: parseInt(req.params.id) }, data: { ...req.body, + table: newTableNumber >= 0 ? newTableNumber : req.body.table, members: { connectOrCreate: applications.map((application: any) => ({ where: { From 0e94e480d915a82b316aee3d5cc6afb7a27ea739 Mon Sep 17 00:00:00 2001 From: Lauren Ji Date: Fri, 20 Sep 2024 12:16:37 -0400 Subject: [PATCH 3/4] remove comment --- services/expo/src/routes/project.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/services/expo/src/routes/project.ts b/services/expo/src/routes/project.ts index 5f262b9..969e262 100644 --- a/services/expo/src/routes/project.ts +++ b/services/expo/src/routes/project.ts @@ -423,11 +423,6 @@ projectRoutes.route("/:id").patch( if (isDuplicate) { await reassignTable(); - // res.status(200).send({ - // error: true, - // message: "Error: Duplicate Table Number.", - // }); - // return; } } else if (req.body.tableGroupId || req.body.expo) { await reassignTable(); From df928cc5cc5149e3764a5d6a9d16a25cc7bcc9af Mon Sep 17 00:00:00 2001 From: Lauren Ji Date: Fri, 20 Sep 2024 12:20:42 -0400 Subject: [PATCH 4/4] remove console logs --- services/expo/src/routes/project.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/services/expo/src/routes/project.ts b/services/expo/src/routes/project.ts index 969e262..8398c8d 100644 --- a/services/expo/src/routes/project.ts +++ b/services/expo/src/routes/project.ts @@ -309,8 +309,6 @@ projectRoutes.route("/").post( projectRoutes.route("/:id").patch( isAdmin, asyncHandler(async (req, res) => { - console.log("BODY"); - console.log(req.body); let members: any[] = []; let categories: any[] = []; let tableGroup;