@happychef/algorithm 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/assignTables.js +398 -0
- package/filters/maxArrivalsFilter.js +115 -0
- package/filters/maxGroupsFilter.js +222 -0
- package/filters/timeFilter.js +90 -0
- package/getAvailableTimeblocks.js +109 -0
- package/grouping.js +162 -0
- package/index.js +47 -0
- package/isDateAvailable.js +77 -0
- package/isDateAvailableWithTableCheck.js +169 -0
- package/isTimeAvailable.js +22 -0
- package/package.json +18 -0
- package/processing/dailyGuestCounts.js +73 -0
- package/processing/mealTypeCount.js +133 -0
- package/processing/timeblocksAvailable.js +120 -0
- package/reservation_data/counter.js +65 -0
- package/restaurant_data/exceptions.js +149 -0
- package/restaurant_data/openinghours.js +123 -0
- package/simulateTableAssignment.js +594 -0
- package/tableHelpers.js +178 -0
- package/test/test_counter.js +194 -0
- package/test/test_dailyCount.js +81 -0
- package/test/test_datesAvailable.js +106 -0
- package/test/test_exceptions.js +173 -0
- package/test/test_isDateAvailable.js +330 -0
- package/test/test_mealTypeCount.js +54 -0
- package/test/test_timesAvailable.js +88 -0
- package/test.js +336 -0
package/assignTables.js
ADDED
|
@@ -0,0 +1,398 @@
|
|
|
1
|
+
const { tryGroupTables } = require("./grouping");
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* server side
|
|
5
|
+
* Parses a time string ("HH:MM") into minutes since midnight.
|
|
6
|
+
* Returns NaN if the format is invalid.
|
|
7
|
+
*/
|
|
8
|
+
function parseTime(timeStr) {
|
|
9
|
+
if (!timeStr || typeof timeStr !== 'string') return NaN;
|
|
10
|
+
const parts = timeStr.split(':');
|
|
11
|
+
if (parts.length !== 2) return NaN;
|
|
12
|
+
const hours = parseInt(parts[0], 10);
|
|
13
|
+
const minutes = parseInt(parts[1], 10);
|
|
14
|
+
if (isNaN(hours) || isNaN(minutes) || hours < 0 || hours > 23 || minutes < 0 || minutes > 59) {
|
|
15
|
+
return NaN;
|
|
16
|
+
}
|
|
17
|
+
return hours * 60 + minutes;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const shifts = {
|
|
21
|
+
breakfast: { start: '07:00', end: '11:00' },
|
|
22
|
+
lunch: { start: '11:00', end: '16:00' },
|
|
23
|
+
dinner: { start: '16:00', end: '23:00' },
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Determines the meal type ('breakfast', 'lunch', 'dinner') for a given time string ("HH:MM").
|
|
28
|
+
* Returns null if the time doesn't fall into a defined shift.
|
|
29
|
+
*/
|
|
30
|
+
function getMealTypeByTime(timeStr) {
|
|
31
|
+
const time = parseTime(timeStr);
|
|
32
|
+
if (isNaN(time)) return null;
|
|
33
|
+
|
|
34
|
+
for (const [mealType, shift] of Object.entries(shifts)) {
|
|
35
|
+
const start = parseTime(shift.start);
|
|
36
|
+
const end = parseTime(shift.end);
|
|
37
|
+
if (isNaN(start) || isNaN(end)) continue;
|
|
38
|
+
|
|
39
|
+
if (time >= start && time < end) {
|
|
40
|
+
return mealType;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Checks if a temporary table is valid for a specific reservation date and time.
|
|
48
|
+
* @param {Object} table - The table object with isTemporary, startDate, endDate, application properties.
|
|
49
|
+
* @param {string} reservationDateStr - The date of the reservation ("YYYY-MM-DD").
|
|
50
|
+
* @param {string} reservationTimeStr - The time of the reservation ("HH:MM").
|
|
51
|
+
* @returns {boolean} True if the table is valid, false otherwise.
|
|
52
|
+
*/
|
|
53
|
+
function isTemporaryTableValid(table, reservationDateStr, reservationTimeStr) {
|
|
54
|
+
if (!table.isTemporary) {
|
|
55
|
+
return true; // Not temporary, always valid (subject to other checks like availability)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Check date range
|
|
59
|
+
if (!table.startDate || !table.endDate) {
|
|
60
|
+
return false; // Invalid temporary table definition
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Basic date string comparison (YYYY-MM-DD format)
|
|
64
|
+
if (reservationDateStr < table.startDate || reservationDateStr > table.endDate) {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Check application (meal type/shift)
|
|
69
|
+
const reservationMealType = getMealTypeByTime(reservationTimeStr);
|
|
70
|
+
if (!reservationMealType) {
|
|
71
|
+
return false; // Cannot determine meal type for the reservation
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (table.application !== reservationMealType) {
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* computeRequiredSlots(timeString, durationMinutes, intervalMinutes)
|
|
83
|
+
* Converts a reservation's start time and duration into discrete time slots.
|
|
84
|
+
* e.g., timeString = "12:30", duration = 400 minutes, interval = 50 minutes
|
|
85
|
+
*/
|
|
86
|
+
function computeRequiredSlots(timeString, durationMinutes, intervalMinutes) {
|
|
87
|
+
// Validate time format
|
|
88
|
+
const timePattern = /^([01]\d|2[0-3]):([0-5]\d)$/;
|
|
89
|
+
if (!timePattern.test(timeString)) {
|
|
90
|
+
throw new Error("Invalid time format. Expected 'HH:MM'.");
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const [hour, minute] = timeString.split(":").map(Number);
|
|
94
|
+
const startMinutes = hour * 60 + minute;
|
|
95
|
+
const slotCount = Math.ceil(durationMinutes / intervalMinutes);
|
|
96
|
+
const slots = [];
|
|
97
|
+
for (let i = 0; i < slotCount; i++) {
|
|
98
|
+
slots.push(startMinutes + i * intervalMinutes);
|
|
99
|
+
}
|
|
100
|
+
return slots;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* isTableFreeForAllSlots(tableNumber, requiredSlots, tableOccupiedSlots)
|
|
105
|
+
* Checks if the given tableNumber is free (no overlapping) for all requiredSlots.
|
|
106
|
+
*/
|
|
107
|
+
function isTableFreeForAllSlots(tableNumber, requiredSlots, tableOccupiedSlots) {
|
|
108
|
+
const occupiedSlots = tableOccupiedSlots[tableNumber] || new Set();
|
|
109
|
+
// If any slot from requiredSlots is in occupiedSlots, the table is not free
|
|
110
|
+
return !requiredSlots.some(slot => occupiedSlots.has(slot));
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* calculateDistance(tableA, tableB)
|
|
115
|
+
* Euclidean distance between two tables (for multi-table distance minimization).
|
|
116
|
+
*/
|
|
117
|
+
function calculateDistance(tableA, tableB) {
|
|
118
|
+
const dx = tableA.x - tableB.x;
|
|
119
|
+
const dy = tableA.y - tableB.y;
|
|
120
|
+
return Math.sqrt(dx * dx + dy * dy);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function distanceSum(set) {
|
|
124
|
+
let sum = 0;
|
|
125
|
+
for (let i = 0; i < set.length; i++) {
|
|
126
|
+
for (let j = i + 1; j < set.length; j++) {
|
|
127
|
+
sum += calculateDistance(set[i], set[j]);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return sum;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function findMultiTableCombination(
|
|
134
|
+
tables,
|
|
135
|
+
guestsTotal,
|
|
136
|
+
startIndex,
|
|
137
|
+
currentSet,
|
|
138
|
+
best,
|
|
139
|
+
requiredSlots,
|
|
140
|
+
tableOccupiedSlots,
|
|
141
|
+
reservationDate,
|
|
142
|
+
reservationTime,
|
|
143
|
+
suffixMax // optional precomputed array
|
|
144
|
+
) {
|
|
145
|
+
// Precompute suffix max-capacity once
|
|
146
|
+
if (!suffixMax) {
|
|
147
|
+
suffixMax = new Array(tables.length + 1).fill(0);
|
|
148
|
+
for (let i = tables.length - 1; i >= 0; i--) {
|
|
149
|
+
suffixMax[i] = suffixMax[i + 1] + tables[i].maxCapacity;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Compute current set min/max sums
|
|
154
|
+
let curMin = 0, curMax = 0;
|
|
155
|
+
for (const t of currentSet) {
|
|
156
|
+
curMin += t.minCapacity;
|
|
157
|
+
curMax += t.maxCapacity;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// Prune: too many required seats already
|
|
161
|
+
if (curMin > guestsTotal) return;
|
|
162
|
+
// Prune: even with all remaining tables, can't reach guests
|
|
163
|
+
if (curMax + suffixMax[startIndex] < guestsTotal) return;
|
|
164
|
+
|
|
165
|
+
// Feasible set -> evaluate distance and update best
|
|
166
|
+
if (curMin <= guestsTotal && guestsTotal <= curMax) {
|
|
167
|
+
const d = distanceSum(currentSet);
|
|
168
|
+
if (d < best.minDistance) {
|
|
169
|
+
best.minDistance = d;
|
|
170
|
+
best.tables = [...currentSet];
|
|
171
|
+
}
|
|
172
|
+
// Continue searching; a tighter cluster may exist.
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// Try adding more tables
|
|
176
|
+
for (let i = startIndex; i < tables.length; i++) {
|
|
177
|
+
const tbl = tables[i];
|
|
178
|
+
|
|
179
|
+
if (!isTemporaryTableValid(tbl, reservationDate, reservationTime)) continue;
|
|
180
|
+
if (!isTableFreeForAllSlots(tbl.tableNumber, requiredSlots, tableOccupiedSlots)) continue;
|
|
181
|
+
|
|
182
|
+
currentSet.push(tbl);
|
|
183
|
+
findMultiTableCombination(
|
|
184
|
+
tables,
|
|
185
|
+
guestsTotal,
|
|
186
|
+
i + 1,
|
|
187
|
+
currentSet,
|
|
188
|
+
best,
|
|
189
|
+
requiredSlots,
|
|
190
|
+
tableOccupiedSlots,
|
|
191
|
+
reservationDate,
|
|
192
|
+
reservationTime,
|
|
193
|
+
suffixMax
|
|
194
|
+
);
|
|
195
|
+
currentSet.pop();
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* assignTablesIfPossible
|
|
202
|
+
* Attempts to assign tables to a reservation based on availability and constraints.
|
|
203
|
+
* Modifies the reservation object by attaching 'tables' and 'tableIds' if successful.
|
|
204
|
+
*/
|
|
205
|
+
async function assignTablesIfPossible({
|
|
206
|
+
db,
|
|
207
|
+
reservation,
|
|
208
|
+
enforceTableAvailability
|
|
209
|
+
}) {
|
|
210
|
+
const restaurantId = reservation.restaurantId;
|
|
211
|
+
const date = reservation.date;
|
|
212
|
+
const time = reservation.time;
|
|
213
|
+
const guests = reservation.guests;
|
|
214
|
+
|
|
215
|
+
// 1) First, fetch restaurant data (which contains the floors information)
|
|
216
|
+
const restaurantSettings = await db.collection('restaurants').findOne({ _id: restaurantId });
|
|
217
|
+
if (!restaurantSettings) {
|
|
218
|
+
if (enforceTableAvailability) {
|
|
219
|
+
throw new Error('Restaurant settings not found.');
|
|
220
|
+
} else {
|
|
221
|
+
return; // Non-enforcing: skip table assignment
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// 2) Get floors data directly from the restaurant document
|
|
226
|
+
const floorsData = restaurantSettings.floors;
|
|
227
|
+
if (!floorsData || !Array.isArray(floorsData) || floorsData.length === 0) {
|
|
228
|
+
if (enforceTableAvailability) {
|
|
229
|
+
throw new Error('No floors data found in restaurant document.');
|
|
230
|
+
} else {
|
|
231
|
+
return; // Non-enforcing: skip table assignment
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// 3) Collect all "Tafel" tables
|
|
236
|
+
let allTables = [];
|
|
237
|
+
floorsData.forEach(floor => {
|
|
238
|
+
if (floor.tables && Array.isArray(floor.tables)) {
|
|
239
|
+
floor.tables.forEach(tbl => {
|
|
240
|
+
if (tbl.objectType === "Tafel") {
|
|
241
|
+
allTables.push({
|
|
242
|
+
tableId: tbl.id,
|
|
243
|
+
tableNumber: parseInt(tbl.tableNumber.$numberInt || tbl.tableNumber),
|
|
244
|
+
minCapacity: parseInt(tbl.minCapacity.$numberInt || tbl.minCapacity),
|
|
245
|
+
maxCapacity: parseInt(tbl.maxCapacity.$numberInt || tbl.maxCapacity),
|
|
246
|
+
priority: parseInt(tbl.priority.$numberInt || tbl.priority),
|
|
247
|
+
x: parseInt(tbl.x.$numberInt || tbl.x),
|
|
248
|
+
y: parseInt(tbl.y.$numberInt || tbl.y),
|
|
249
|
+
isTemporary: tbl.isTemporary === true, // Added for temporary tables
|
|
250
|
+
startDate: tbl.startDate || null, // Added for temporary tables
|
|
251
|
+
endDate: tbl.endDate || null, // Added for temporary tables
|
|
252
|
+
application: tbl.application || null // Added for temporary tables
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
// 4) If no tables found
|
|
260
|
+
if (!allTables.length) {
|
|
261
|
+
if (enforceTableAvailability) {
|
|
262
|
+
throw new Error('No tables found for this restaurant.');
|
|
263
|
+
} else {
|
|
264
|
+
return; // Non-enforcing: skip table assignment
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// 5) Sort the tables
|
|
269
|
+
allTables.sort((a, b) => {
|
|
270
|
+
if (a.maxCapacity !== b.maxCapacity) {
|
|
271
|
+
return a.maxCapacity - b.maxCapacity;
|
|
272
|
+
}
|
|
273
|
+
if (a.priority !== b.priority) {
|
|
274
|
+
return a.priority - b.priority;
|
|
275
|
+
}
|
|
276
|
+
return a.minCapacity - b.minCapacity;
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
// 6) Get duration and interval settings
|
|
280
|
+
const duurReservatie = parseInt(
|
|
281
|
+
restaurantSettings["general-settings"]?.duurReservatie?.$numberInt || restaurantSettings["general-settings"]?.duurReservatie || 120
|
|
282
|
+
);
|
|
283
|
+
const intervalReservatie = parseInt(
|
|
284
|
+
restaurantSettings["general-settings"]?.intervalReservatie?.$numberInt || restaurantSettings["general-settings"]?.intervalReservatie || 30
|
|
285
|
+
);
|
|
286
|
+
|
|
287
|
+
// 7) Compute the requiredSlots for this reservation
|
|
288
|
+
const requiredSlots = computeRequiredSlots(time, duurReservatie, intervalReservatie);
|
|
289
|
+
|
|
290
|
+
// 8) Get overlapping reservations (same date, same restaurant)
|
|
291
|
+
const overlappingReservations = await db.collection('reservations').find({
|
|
292
|
+
restaurantId: restaurantId,
|
|
293
|
+
date: date
|
|
294
|
+
}).toArray();
|
|
295
|
+
|
|
296
|
+
// 9) Build tableOccupiedSlots map
|
|
297
|
+
let tableOccupiedSlots = {}; // { [tableNumber]: Set([...slots]) }
|
|
298
|
+
for (let r of overlappingReservations) {
|
|
299
|
+
// No need to skip the current reservation as it's not yet inserted
|
|
300
|
+
|
|
301
|
+
// compute that reservation's time slots
|
|
302
|
+
const rDuration = duurReservatie; // assuming same duration
|
|
303
|
+
const rSlots = computeRequiredSlots(r.time, rDuration, intervalReservatie);
|
|
304
|
+
|
|
305
|
+
if (r.tables) {
|
|
306
|
+
for (let tn of r.tables) {
|
|
307
|
+
if (!tableOccupiedSlots[tn]) {
|
|
308
|
+
tableOccupiedSlots[tn] = new Set();
|
|
309
|
+
}
|
|
310
|
+
rSlots.forEach(slot => {
|
|
311
|
+
tableOccupiedSlots[tn].add(slot);
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
// ===== 9.5) Table Grouping Attempt (moved to grouping.js) =====
|
|
317
|
+
try {
|
|
318
|
+
const groupingResult = tryGroupTables({
|
|
319
|
+
restaurantSettings,
|
|
320
|
+
allTables,
|
|
321
|
+
guests,
|
|
322
|
+
date,
|
|
323
|
+
time,
|
|
324
|
+
requiredSlots,
|
|
325
|
+
tableOccupiedSlots,
|
|
326
|
+
isTemporaryTableValid,
|
|
327
|
+
isTableFreeForAllSlots,
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
if (groupingResult) {
|
|
331
|
+
reservation.tables = groupingResult.tables;
|
|
332
|
+
reservation.tableIds = groupingResult.tableIds;
|
|
333
|
+
reservation._viaGroup = groupingResult.viaGroup;
|
|
334
|
+
console.log(
|
|
335
|
+
`[Grouping] via '${reservation._viaGroup}' -> tables ${reservation.tables.join(",")}`
|
|
336
|
+
);
|
|
337
|
+
return; // grouping wins
|
|
338
|
+
}
|
|
339
|
+
} catch (err) {
|
|
340
|
+
// strict grouping throws; preserve original behavior
|
|
341
|
+
throw err;
|
|
342
|
+
}
|
|
343
|
+
// ===== end 9.5) Table Grouping Attempt =====
|
|
344
|
+
|
|
345
|
+
// 10) Single-Table Attempt
|
|
346
|
+
for (let t of allTables) {
|
|
347
|
+
// Check if temporary table is valid for this date/time
|
|
348
|
+
if (!isTemporaryTableValid(t, date, time)) {
|
|
349
|
+
continue; // Skip invalid temporary table
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
if (
|
|
353
|
+
t.minCapacity <= guests &&
|
|
354
|
+
guests <= t.maxCapacity &&
|
|
355
|
+
isTableFreeForAllSlots(t.tableNumber, requiredSlots, tableOccupiedSlots)
|
|
356
|
+
) {
|
|
357
|
+
// Assign this table to the reservation
|
|
358
|
+
reservation.tables = [t.tableNumber];
|
|
359
|
+
reservation.tableIds = [t.tableId];
|
|
360
|
+
console.log(`Reservation assigned to table: ${t.tableNumber}`);
|
|
361
|
+
return; // Assignment successful
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// 11) Multi-Table Attempt
|
|
366
|
+
let best = { minDistance: Infinity, tables: [] };
|
|
367
|
+
|
|
368
|
+
findMultiTableCombination(
|
|
369
|
+
allTables,
|
|
370
|
+
guests,
|
|
371
|
+
0,
|
|
372
|
+
[],
|
|
373
|
+
best,
|
|
374
|
+
requiredSlots,
|
|
375
|
+
tableOccupiedSlots,
|
|
376
|
+
date, // Pass date for temporary table validation
|
|
377
|
+
time // Pass time for temporary table validation
|
|
378
|
+
);
|
|
379
|
+
|
|
380
|
+
if (best.tables.length > 0) {
|
|
381
|
+
// Assign the best combination to the reservation
|
|
382
|
+
reservation.tables = best.tables.map(t => t.tableNumber);
|
|
383
|
+
reservation.tableIds = best.tables.map(t => t.tableId);
|
|
384
|
+
console.log(`Reservation assigned to tables: ${reservation.tables.join(', ')}`);
|
|
385
|
+
return; // Assignment successful
|
|
386
|
+
} else {
|
|
387
|
+
// If no valid table combo found, either fail (if enforce) or do nothing (if not enforce)
|
|
388
|
+
if (enforceTableAvailability) {
|
|
389
|
+
throw new Error('Unable to find enough tables for this reservation with enforcement on.');
|
|
390
|
+
} else {
|
|
391
|
+
console.log('No tables available, but non-enforcing mode => continuing without assignment.');
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
module.exports = {
|
|
397
|
+
assignTablesIfPossible
|
|
398
|
+
};
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
// file: /src/Pages/NewReservation/StepOne/algorithm/maxArrivalsFilter.js
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Simple max arrivals filter for time slots
|
|
5
|
+
*
|
|
6
|
+
* Only considers exact arrivals at each time slot without factoring duration
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Get meal type based on time
|
|
11
|
+
* @param {string} time - Time string (HH:MM)
|
|
12
|
+
* @returns {string|null} - Meal type or null
|
|
13
|
+
*/
|
|
14
|
+
function getMealType(time) {
|
|
15
|
+
const hour = parseInt(time.split(':')[0], 10);
|
|
16
|
+
|
|
17
|
+
if (hour >= 4 && hour < 11) return 'breakfast';
|
|
18
|
+
if (hour >= 11 && hour < 16) return 'lunch';
|
|
19
|
+
if (hour >= 16 && hour < 23) return 'dinner';
|
|
20
|
+
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Extract number value from various data formats
|
|
26
|
+
* @param {*} value - Value from data object
|
|
27
|
+
* @returns {number|null} - Number or null
|
|
28
|
+
*/
|
|
29
|
+
function extractNumber(value) {
|
|
30
|
+
if (!value) return null;
|
|
31
|
+
|
|
32
|
+
// Handle MongoDB NumberInt format
|
|
33
|
+
if (value.$numberInt) {
|
|
34
|
+
return parseInt(value.$numberInt, 10);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Handle regular number
|
|
38
|
+
if (typeof value === 'number') {
|
|
39
|
+
return value;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Handle string number
|
|
43
|
+
if (typeof value === 'string') {
|
|
44
|
+
const parsed = parseInt(value, 10);
|
|
45
|
+
return isNaN(parsed) ? null : parsed;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Count guests arriving at exact time
|
|
53
|
+
* @param {Array} reservations - Reservation list
|
|
54
|
+
* @param {string} date - Date (YYYY-MM-DD)
|
|
55
|
+
* @param {string} time - Time (HH:MM)
|
|
56
|
+
* @returns {number} - Guest count
|
|
57
|
+
*/
|
|
58
|
+
function countArrivalsAtTime(reservations, date, time) {
|
|
59
|
+
return reservations
|
|
60
|
+
.filter(r => r.date === date && r.time === time)
|
|
61
|
+
.reduce((sum, r) => sum + (parseInt(r.guests, 10) || 0), 0);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Filter timeblocks based on max arrivals settings
|
|
66
|
+
* @param {Object} restaurantData - Restaurant data
|
|
67
|
+
* @param {string} date - Date string
|
|
68
|
+
* @param {Object} timeblocks - Available timeblocks
|
|
69
|
+
* @param {Array} reservations - Existing reservations
|
|
70
|
+
* @param {number} guests - New reservation guest count
|
|
71
|
+
* @returns {Object} - Filtered timeblocks
|
|
72
|
+
*/
|
|
73
|
+
function filterTimeblocksByMaxArrivals(restaurantData, date, timeblocks, reservations, guests) {
|
|
74
|
+
const filteredBlocks = {};
|
|
75
|
+
|
|
76
|
+
for (const [time, timeData] of Object.entries(timeblocks)) {
|
|
77
|
+
// Get meal type for this time
|
|
78
|
+
const mealType = getMealType(time);
|
|
79
|
+
if (!mealType) {
|
|
80
|
+
// Keep the timeblock if we can't determine its meal type
|
|
81
|
+
filteredBlocks[time] = timeData;
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Get max arrivals config for this meal type
|
|
86
|
+
const maxArrivalsConfig = restaurantData[`max-arrivals-${mealType}`];
|
|
87
|
+
if (!maxArrivalsConfig) {
|
|
88
|
+
// Keep the timeblock if no max arrivals config for this meal type
|
|
89
|
+
filteredBlocks[time] = timeData;
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Get max arrivals value for this specific time
|
|
94
|
+
const maxArrivals = extractNumber(maxArrivalsConfig[time]);
|
|
95
|
+
if (maxArrivals === null) {
|
|
96
|
+
// Keep the timeblock if no specific max arrivals for this time
|
|
97
|
+
filteredBlocks[time] = timeData;
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Count current arrivals at this exact time
|
|
102
|
+
const currentArrivals = countArrivalsAtTime(reservations, date, time);
|
|
103
|
+
|
|
104
|
+
// Only include timeblock if adding these guests doesn't exceed max arrivals
|
|
105
|
+
if (currentArrivals + guests <= maxArrivals) {
|
|
106
|
+
filteredBlocks[time] = timeData;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return filteredBlocks;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
module.exports = {
|
|
114
|
+
filterTimeblocksByMaxArrivals
|
|
115
|
+
};
|