@happychef/algorithm 1.0.4 → 1.1.1

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.
@@ -1,106 +0,0 @@
1
- const safeParseInt = require('../utils/safeParseInt');
2
- const computeRequiredSlots = require('../time/computeRequiredSlots');
3
- const getAllTables = require('../data/getAllTables');
4
- const isTemporaryTableValid = require('../utils/isTemporaryTableValid');
5
- const getActualTableAssignment = require('../data/getActualTableAssignment');
6
- const assignTablesForGivenTime = require('../assignment/assignTablesForGivenTime');
7
-
8
- /**
9
- * Returns array of available individual table objects for a specific time.
10
- */
11
- function getAvailableTablesForTime(restaurantData, date, time, guests, reservations, selectedZitplaats = null) {
12
- try {
13
- if (guests < 0) {
14
- guests = 0;
15
- }
16
-
17
- const tableSettings = restaurantData?.['table-settings'] || {};
18
- const isAutomaticAssignment = tableSettings.isInstalled === true &&
19
- tableSettings.assignmentMode === "automatic";
20
-
21
- if (!isAutomaticAssignment) {
22
- return [];
23
- }
24
-
25
- if (!restaurantData?.floors || !Array.isArray(restaurantData.floors)) {
26
- return [];
27
- }
28
- if (guests <= 0) return [];
29
-
30
- const generalSettings = restaurantData["general-settings"] || {};
31
- const duurReservatie = safeParseInt(generalSettings.duurReservatie, 120);
32
- const intervalReservatie = safeParseInt(generalSettings.intervalReservatie, 15);
33
-
34
- if (intervalReservatie <= 0) {
35
- return [];
36
- }
37
-
38
- const requiredSlots = computeRequiredSlots(time, duurReservatie, intervalReservatie);
39
- if (!requiredSlots || requiredSlots.length === 0) {
40
- return [];
41
- }
42
-
43
- const tableOccupiedSlots = {};
44
- const reservationsForDate = reservations
45
- .filter(r => r.date === date && r.time && r.guests > 0)
46
- .sort((a, b) => {
47
- const timeA = a.time.split(':').map(Number);
48
- const timeB = b.time.split(':').map(Number);
49
- return (timeA[0] * 60 + timeA[1]) - (timeB[0] * 60 + timeB[1]);
50
- });
51
-
52
- for (const r of reservationsForDate) {
53
- const actualTables = getActualTableAssignment(r);
54
- let assignedTables = [];
55
-
56
- if (actualTables.length > 0) {
57
- assignedTables = actualTables;
58
- } else {
59
- assignedTables = assignTablesForGivenTime(restaurantData, r.date, r.time, r.guests, tableOccupiedSlots, null);
60
- }
61
-
62
- if (assignedTables.length > 0) {
63
- const rSlots = computeRequiredSlots(r.time, duurReservatie, intervalReservatie);
64
- if (!rSlots || rSlots.length === 0) continue;
65
- assignedTables.forEach(tableNumber => {
66
- if (!tableOccupiedSlots[tableNumber]) {
67
- tableOccupiedSlots[tableNumber] = new Set();
68
- }
69
- rSlots.forEach(slot => tableOccupiedSlots[tableNumber].add(slot));
70
- });
71
- }
72
- }
73
-
74
- const allTables = getAllTables(restaurantData, selectedZitplaats);
75
- const availableIndividualTables = [];
76
-
77
- for (const t of allTables) {
78
- if (!isTemporaryTableValid(t, date, time)) continue;
79
- if (t.minCapacity <= guests && guests <= t.maxCapacity) {
80
- if (tableOccupiedSlots && requiredSlots && !Array.isArray(requiredSlots)) {
81
- // (no-op guard, keeps logic consistent)
82
- }
83
- const isFree = (function(){
84
- const occupied = tableOccupiedSlots[t.tableNumber] || new Set();
85
- for (const s of requiredSlots) {
86
- if (occupied.has(s)) return false;
87
- }
88
- return true;
89
- })();
90
-
91
- if (isFree) {
92
- availableIndividualTables.push(t);
93
- }
94
- }
95
- }
96
-
97
- availableIndividualTables.sort((a, b) => a.tableNumber - b.tableNumber);
98
-
99
- return availableIndividualTables;
100
-
101
- } catch (error) {
102
- return []; // Return empty on error
103
- }
104
- }
105
-
106
- module.exports = getAvailableTablesForTime;
@@ -1,128 +0,0 @@
1
- const safeParseInt = require('../utils/safeParseInt');
2
- const computeRequiredSlots = require('../time/computeRequiredSlots');
3
- const isTemporaryTableValid = require('../utils/isTemporaryTableValid');
4
- const getAllTables = require('../data/getAllTables');
5
- const getActualTableAssignment = require('../data/getActualTableAssignment');
6
- const assignTablesForGivenTime = require('../assignment/assignTablesForGivenTime');
7
- const isTableFreeForAllSlots = require('../utils/isTableFreeForAllSlots');
8
- const findMultiTableCombination = require('./findMultiTableCombination');
9
-
10
- /**
11
- * Synchronous availability check using table assignment simulation and/or actual assignments.
12
- */
13
- function isTimeAvailableSync(restaurantData, date, time, guests, reservations, selectedZitplaats = null) {
14
-
15
- if (guests < 0) {
16
- guests = 2; // Use a reasonable default
17
- }
18
-
19
- const tableSettings = restaurantData?.['table-settings'] || {};
20
- const isTableAssignmentEnabled = tableSettings.isInstalled === true &&
21
- tableSettings.assignmentMode === "automatic";
22
-
23
- if (!isTableAssignmentEnabled) {
24
- return true;
25
- }
26
-
27
- try {
28
- // Basic data check
29
- if (!restaurantData?.floors || !Array.isArray(restaurantData.floors)) {
30
- console.error(`[isTimeAvailableSync] Missing floors data for ${date} ${time}`);
31
- return false;
32
- }
33
-
34
- if (guests <= 0) {
35
- console.error(`[isTimeAvailableSync] Invalid guest count: ${guests}`);
36
- return false;
37
- }
38
-
39
- const generalSettings = restaurantData["general-settings"] || {};
40
- let duurReservatie = 120; // Default: 2 hours in minutes
41
- let intervalReservatie = 15; // Default: 15 minute intervals
42
- duurReservatie = safeParseInt(generalSettings.duurReservatie, 120);
43
- intervalReservatie = safeParseInt(generalSettings.intervalReservatie, 15);
44
-
45
- if (intervalReservatie <= 0) {
46
- console.error(`[isTimeAvailableSync] Invalid interval settings for ${date} ${time}`);
47
- return false;
48
- }
49
-
50
- const requiredSlots = computeRequiredSlots(time, duurReservatie, intervalReservatie);
51
- if (!requiredSlots || requiredSlots.length === 0) {
52
- console.error(`[isTimeAvailableSync] Could not compute required slots for ${date} ${time}`);
53
- return false;
54
- }
55
-
56
- // Build Occupancy Map using ACTUAL table assignments when available
57
- const tableOccupiedSlots = {}; // { tableNumber: Set<slotMinutes> }
58
- const reservationsForDate = reservations
59
- .filter(r => r.date === date && r.time && r.guests > 0)
60
- .sort((a, b) => {
61
- const timeA = a.time.split(':').map(Number);
62
- const timeB = b.time.split(':').map(Number);
63
- return (timeA[0] * 60 + timeA[1]) - (timeB[0] * 60 + timeB[1]);
64
- });
65
-
66
- for (const r of reservationsForDate) {
67
- // Try to get actual table assignment first
68
- const actualTables = getActualTableAssignment(r);
69
- let assignedTables = [];
70
-
71
- if (actualTables.length > 0) {
72
- assignedTables = actualTables;
73
- } else {
74
- // Fall back to simulation for backwards compatibility
75
- assignedTables = assignTablesForGivenTime(restaurantData, r.date, r.time, r.guests, tableOccupiedSlots);
76
- }
77
-
78
- // Update the occupancy map based on the actual or simulated assignment
79
- if (assignedTables.length > 0) {
80
- const rSlots = computeRequiredSlots(r.time, duurReservatie, intervalReservatie);
81
- if (!rSlots || rSlots.length === 0) continue;
82
-
83
- assignedTables.forEach(tableNumber => {
84
- if (!tableOccupiedSlots[tableNumber]) {
85
- tableOccupiedSlots[tableNumber] = new Set();
86
- }
87
- rSlots.forEach(slot => tableOccupiedSlots[tableNumber].add(slot));
88
- });
89
- }
90
- }
91
-
92
- const allTables = getAllTables(restaurantData, selectedZitplaats);
93
-
94
- // 1. Try single table assignment
95
- for (const t of allTables) {
96
- if (!isTemporaryTableValid(t, date, time)) continue;
97
-
98
- if (t.minCapacity <= guests &&
99
- guests <= t.maxCapacity &&
100
- isTableFreeForAllSlots(t.tableNumber, requiredSlots, tableOccupiedSlots))
101
- {
102
- return true;
103
- }
104
- }
105
-
106
- // 2. Try multi-table assignment
107
- const best = { minDistance: Infinity, tables: [], tableCount: Infinity };
108
- findMultiTableCombination(
109
- allTables,
110
- guests,
111
- 0, [], best,
112
- requiredSlots,
113
- tableOccupiedSlots,
114
- date, time
115
- );
116
-
117
- const result = best.tables.length > 0;
118
- if (result) {
119
- } else {
120
- }
121
-
122
- return result;
123
- } catch (error) {
124
- return false; // Assume unavailable on error
125
- }
126
- }
127
-
128
- module.exports = isTimeAvailableSync;
@@ -1,17 +0,0 @@
1
- const safeParseInt = require('../utils/safeParseInt');
2
-
3
- /**
4
- * Extracts actual table numbers from a reservation's tables array.
5
- * Handles MongoDB $numberInt format.
6
- */
7
- function extractTableNumbers(tablesArray) {
8
- if (!Array.isArray(tablesArray)) {
9
- return [];
10
- }
11
-
12
- return tablesArray
13
- .map(table => safeParseInt(table, null))
14
- .filter(tableNum => tableNum !== null);
15
- }
16
-
17
- module.exports = extractTableNumbers;
@@ -1,16 +0,0 @@
1
- const extractTableNumbers = require('./extractTableNumbers');
2
-
3
- /**
4
- * Gets actual table assignments from reservation data if available.
5
- * @param {Object} reservation - The reservation object
6
- * @returns {Array} Array of table numbers that are actually assigned, or empty array if no data
7
- */
8
- function getActualTableAssignment(reservation) {
9
- if (reservation.tables && Array.isArray(reservation.tables)) {
10
- const tableNumbers = extractTableNumbers(reservation.tables);
11
- return tableNumbers;
12
- }
13
- return [];
14
- }
15
-
16
- module.exports = getActualTableAssignment;
@@ -1,84 +0,0 @@
1
- /**
2
- * Extracts and processes table data from the restaurantData object.
3
- * Includes temporary table properties, zitplaats filtering, and sorts tables.
4
- * @param {Object} restaurantData - The main restaurant data object.
5
- * @param {string} selectedZitplaats - Optional zitplaats value to filter tables by.
6
- * @returns {Array} An array of processed table objects.
7
- */
8
- function getAllTables(restaurantData, selectedZitplaats = null) {
9
- let allTables = [];
10
- if (restaurantData?.floors && Array.isArray(restaurantData.floors)) {
11
- restaurantData.floors.forEach(floor => {
12
- if (floor?.tables && Array.isArray(floor.tables)) {
13
- floor.tables.forEach(tbl => {
14
- // Ensure table number, capacities, priority exist before parsing
15
- const tableNumberRaw = tbl.tableNumber?.$numberInt ?? tbl.tableNumber;
16
- const minCapacityRaw = tbl.minCapacity?.$numberInt ?? tbl.minCapacity;
17
- const maxCapacityRaw = tbl.maxCapacity?.$numberInt ?? tbl.maxCapacity;
18
- const priorityRaw = tbl.priority?.$numberInt ?? tbl.priority;
19
- const xRaw = tbl.x?.$numberInt ?? tbl.x;
20
- const yRaw = tbl.y?.$numberInt ?? tbl.y;
21
-
22
- // Skip tables that don't match the selected zitplaats (if one is selected)
23
- if (selectedZitplaats && selectedZitplaats.trim() !== '' && tbl.zitplaats !== selectedZitplaats) {
24
- return; // Skip this table
25
- }
26
-
27
- if (tbl.objectType === "Tafel" &&
28
- tableNumberRaw !== undefined &&
29
- minCapacityRaw !== undefined &&
30
- maxCapacityRaw !== undefined &&
31
- priorityRaw !== undefined &&
32
- xRaw !== undefined &&
33
- yRaw !== undefined)
34
- {
35
- allTables.push({
36
- tableId: tbl.id,
37
- tableNumber: parseInt(tableNumberRaw, 10),
38
- minCapacity: parseInt(minCapacityRaw, 10),
39
- maxCapacity: parseInt(maxCapacityRaw, 10),
40
- priority: parseInt(priorityRaw, 10),
41
- x: parseInt(xRaw, 10),
42
- y: parseInt(yRaw, 10),
43
- isTemporary: tbl.isTemporary === true, // Ensure boolean
44
- startDate: tbl.startDate || null, // Expects 'YYYY-MM-DD'
45
- endDate: tbl.endDate || null, // Expects 'YYYY-MM-DD'
46
- application: tbl.application || null, // Expects 'breakfast', 'lunch', or 'dinner'
47
- zitplaats: tbl.zitplaats || null // Add zitplaats to the table object
48
- });
49
- } else if (tbl.objectType === "Tafel") {
50
- console.warn(`Skipping table due to missing essential properties: ${JSON.stringify(tbl)}`);
51
- }
52
- });
53
- }
54
- });
55
- } else {
56
- console.warn("Restaurant data is missing 'floors' array.");
57
- }
58
-
59
- // Sort tables
60
- allTables.sort((a, b) => {
61
- if (a.maxCapacity !== b.maxCapacity) {
62
- return a.maxCapacity - b.maxCapacity;
63
- }
64
- if (a.priority !== b.priority) {
65
- // Assuming lower priority number means higher priority
66
- return a.priority - b.priority;
67
- }
68
- return a.minCapacity - b.minCapacity;
69
- });
70
-
71
- // Filter out tables where parsing failed (resulted in NaN)
72
- allTables = allTables.filter(t =>
73
- !isNaN(t.tableNumber) &&
74
- !isNaN(t.minCapacity) &&
75
- !isNaN(t.maxCapacity) &&
76
- !isNaN(t.priority) &&
77
- !isNaN(t.x) &&
78
- !isNaN(t.y)
79
- );
80
-
81
- return allTables;
82
- }
83
-
84
- module.exports = getAllTables;
package/tables/main.js DELETED
@@ -1,33 +0,0 @@
1
- // Central public API for all table logic (replaces simulateTableAssignment.js)
2
-
3
- // Import primitives directly from their files to avoid circular deps.
4
- const isTimeAvailableSync = require('./availability/isTimeAvailableSync');
5
- const getAvailableTablesForTime = require('./availability/getAvailableTablesForTime');
6
- const filterTimeblocksByTableAvailability = require('./assignment/filterTimeblocksByTableAvailability');
7
-
8
- // Keep backward compatibility: expose getAvailableTimeblocksWithTableCheck
9
- // as an alias of filterTimeblocksByTableAvailability so existing imports continue to work.
10
- const getAvailableTimeblocksWithTableCheck = (
11
- restaurantData,
12
- date,
13
- timeblocks,
14
- guests,
15
- reservations,
16
- selectedZitplaats = null
17
- ) =>
18
- filterTimeblocksByTableAvailability(
19
- restaurantData,
20
- date,
21
- timeblocks,
22
- guests,
23
- reservations,
24
- selectedZitplaats
25
- );
26
-
27
- module.exports = {
28
- isTimeAvailableSync,
29
- filterTimeblocksByTableAvailability,
30
- getAvailableTimeblocksWithTableCheck,
31
- getAvailableTablesForTime,
32
- // assignTablesForGivenTime
33
- };
@@ -1,27 +0,0 @@
1
- /**
2
- * Converts a reservation's start time and duration into discrete time slots (in minutes from midnight).
3
- */
4
- function computeRequiredSlots(timeString, durationMinutes, intervalMinutes) {
5
- const timePattern = /^([01]\d|2[0-3]):([0-5]\d)$/;
6
- if (!timeString || !timePattern.test(timeString)) {
7
- return [];
8
- }
9
-
10
- const [hour, minute] = timeString.split(":").map(Number);
11
- const startMinutes = hour * 60 + minute;
12
-
13
- if (intervalMinutes <= 0) {
14
- return [startMinutes];
15
- }
16
-
17
- const slotCount = Math.ceil(durationMinutes / intervalMinutes);
18
-
19
- const slots = [];
20
- for (let i = 0; i < slotCount; i++) {
21
- slots.push(startMinutes + i * intervalMinutes);
22
- }
23
-
24
- return slots;
25
- }
26
-
27
- module.exports = computeRequiredSlots;
@@ -1,26 +0,0 @@
1
- const parseTime = require('./parseTime');
2
- const { shifts } = require('./shifts');
3
-
4
- /**
5
- * Determines the meal type ('breakfast', 'lunch', 'dinner') for a given time string ("HH:MM").
6
- * Returns null if the time doesn't fall into a defined shift.
7
- */
8
- function getMealTypeByTime(timeStr) {
9
- const time = parseTime(timeStr);
10
- if (isNaN(time)) return null;
11
-
12
- for (const [mealType, shift] of Object.entries(shifts)) {
13
- const start = parseTime(shift.start);
14
- const end = parseTime(shift.end);
15
- // Handle potential errors from parseTime if shift definitions are invalid
16
- if (isNaN(start) || isNaN(end)) continue;
17
-
18
- // Check if time falls within the shift range [start, end)
19
- if (time >= start && time < end) {
20
- return mealType;
21
- }
22
- }
23
- return null; // Return null if no matching shift is found
24
- }
25
-
26
- module.exports = getMealTypeByTime;
@@ -1,29 +0,0 @@
1
- /**
2
- * Robust helper for parsing numeric values from various formats.
3
- * Handles MongoDB $numberInt format and regular values.
4
- */
5
- function safeParseInt(val, defaultValue) {
6
- if (val === undefined || val === null) return defaultValue;
7
-
8
- if (typeof val === 'object' && val !== null && '$numberInt' in val) {
9
- try {
10
- const parsed = parseInt(val.$numberInt, 10);
11
- if (!isNaN(parsed)) return parsed;
12
- } catch (e) {}
13
- return defaultValue;
14
- }
15
-
16
- if (typeof val === 'number' && !isNaN(val)) {
17
- return val;
18
- }
19
-
20
- if (typeof val === 'string') {
21
- try {
22
- const parsed = parseInt(val, 10);
23
- if (!isNaN(parsed)) return parsed;
24
- } catch (e) {}
25
- }
26
- return defaultValue;
27
- }
28
-
29
- module.exports = safeParseInt;