File tree Expand file tree Collapse file tree
services/hexathons/src/routes Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ import { HexathonUserModel } from "../models/hexathonUser";
1515export const foodBatchRouter = express . Router ( ) ;
1616
1717const batchCounts : { [ key : string ] : number } = { } ;
18+ const batchStarts : { [ key : string ] : Date } = { } ;
1819
1920foodBatchRouter . 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 ) ;
You can’t perform that action at this time.
0 commit comments