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

Independent Table Number and Table Group Assignment #205

Merged
merged 2 commits into from
Apr 16, 2024
Merged
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
40 changes: 20 additions & 20 deletions services/expo/src/routes/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,39 +199,39 @@ projectRoutes.route("/").post(
},
});

let minCapacityTableGroup: undefined | TableGroup;
let minCapacityValue = 1;
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
);
if (
projectsInCurrentExpoAndTableGroup.length < tableGroup.tableCapacity &&
projectsInCurrentExpoAndTableGroup.length / tableGroup.tableCapacity < minCapacityValue
) {
minCapacityTableGroup = tableGroup;
minCapacityValue = projectsInCurrentExpoAndTableGroup.length / tableGroup.tableCapacity;

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;
}

if (!minCapacityTableGroup) {
// 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"
);
}

const projectsInCurrentExpoAndTableGroup = projectsInCurrentExpo.filter(
project => project.tableGroupId === minCapacityTableGroup?.id
);

// assigns table to first unused number
let tableNumber;
const tableNumberSet = new Set();
for (const project of projectsInCurrentExpoAndTableGroup) {
tableNumberSet.add(project.table);
}

for (let i = 1; i <= minCapacityTableGroup.tableCapacity; i++) {
for (let i = 1; i <= totalCapacity; i++) {
if (!tableNumberSet.has(i)) {
tableNumber = i;
break;
Expand Down Expand Up @@ -273,7 +273,7 @@ projectRoutes.route("/").post(
connect: data.prizes.map((prizeId: any) => ({ id: prizeId })),
},
tableGroup: {
connect: { id: minCapacityTableGroup.id },
connect: { id: firstFreeTableGroup.id },
},
},
});
Expand Down
Loading