Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto table reassignment when expo/room changed #215

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 64 additions & 7 deletions services/expo/src/routes/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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)) {
Expand Down Expand Up @@ -312,6 +312,7 @@ projectRoutes.route("/:id").patch(
let members: any[] = [];
let categories: any[] = [];
let tableGroup;
let newTableNumber = -1;

const config = await getConfig();
const currentProject = await prisma.project.findUnique({
Expand Down Expand Up @@ -339,6 +340,63 @@ projectRoutes.route("/:id").patch(
}
}

const reassignTable = async () => {
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;
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({
Expand All @@ -362,12 +420,10 @@ projectRoutes.route("/:id").patch(
);

if (isDuplicate) {
res.status(200).send({
error: true,
message: "Error: Duplicate Table Number.",
});
return;
await reassignTable();
}
} else if (req.body.tableGroupId || req.body.expo) {
await reassignTable();
}

const dbCategories = await prisma.category.findMany({
Expand Down Expand Up @@ -416,6 +472,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: {
Expand Down