Skip to content

Commit 9331d46

Browse files
committed
only join batches which start in the future
1 parent f488ef6 commit 9331d46

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

services/hexathons/src/routes/food-batch.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { HexathonUserModel } from "../models/hexathonUser";
1515
export const foodBatchRouter = express.Router();
1616

1717
const batchCounts: { [key: string]: number } = {};
18+
const batchStarts: { [key: string]: Date } = {};
1819

1920
foodBatchRouter.route("/").get(
2021
checkAbility("read", "FoodBatch"),
@@ -149,14 +150,17 @@ foodBatchRouter.route("/join").post(
149150
let lowestCount = Infinity;
150151

151152
for (const [batchId, count] of Object.entries(batchCounts)) {
152-
console.log(batchId, count);
153-
if (count < lowestCount) {
153+
if (count < lowestCount && batchStarts[batchId] > new Date()) {
154154
batchToJoin = batchId;
155155
lowestCount = count;
156156
}
157157
}
158158
}
159159

160+
if (!batchToJoin) {
161+
throw new ServerError("Failed to find suitable batch to join.");
162+
}
163+
160164
await TeamModel.findByIdAndUpdate(req.body.teamId, { batch: new Types.ObjectId(batchToJoin) });
161165

162166
batchCounts[batchToJoin] += 1;
@@ -176,6 +180,7 @@ const updateLocalBatchCounts = async () => {
176180

177181
const promises = batches.map(async batch => {
178182
batchCounts[batch.id.toString()] = await countBatchMembers(batch.id);
183+
batchStarts[batch.id.toString()] = batch.start;
179184
});
180185

181186
await Promise.all(promises);

0 commit comments

Comments
 (0)