@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.
@@ -0,0 +1,178 @@
1
+ // file: client side ./algorithm/tableHelpers.js
2
+
3
+ // --- Time and Shift Helpers ---
4
+
5
+ /**
6
+ * Parses a time string ("HH:MM") into minutes since midnight.
7
+ * Returns NaN if the format is invalid.
8
+ */
9
+ function parseTime(timeStr) {
10
+ if (!timeStr || typeof timeStr !== 'string') return NaN;
11
+ const parts = timeStr.split(':');
12
+ if (parts.length !== 2) return NaN;
13
+ const hours = parseInt(parts[0], 10);
14
+ const minutes = parseInt(parts[1], 10);
15
+ if (isNaN(hours) || isNaN(minutes) || hours < 0 || hours > 23 || minutes < 0 || minutes > 59) {
16
+ return NaN;
17
+ }
18
+ return hours * 60 + minutes;
19
+ }
20
+
21
+ const shifts = {
22
+ breakfast: { start: '07:00', end: '11:00' },
23
+ lunch: { start: '11:00', end: '16:00' },
24
+ dinner: { start: '16:00', end: '23:00' }, // Adjust end time if needed
25
+ };
26
+
27
+ /**
28
+ * Determines the meal type ('breakfast', 'lunch', 'dinner') for a given time string ("HH:MM").
29
+ * Returns null if the time doesn't fall into a defined shift.
30
+ */
31
+ function getMealTypeByTime(timeStr) {
32
+ const time = parseTime(timeStr);
33
+ if (isNaN(time)) return null;
34
+
35
+ for (const [mealType, shift] of Object.entries(shifts)) {
36
+ const start = parseTime(shift.start);
37
+ const end = parseTime(shift.end);
38
+ // Handle potential errors from parseTime if shift definitions are invalid
39
+ if (isNaN(start) || isNaN(end)) continue;
40
+
41
+ // Check if time falls within the shift range [start, end)
42
+ if (time >= start && time < end) {
43
+ return mealType;
44
+ }
45
+ }
46
+ return null; // Return null if no matching shift is found
47
+ }
48
+
49
+
50
+ // --- Table Fetching ---
51
+
52
+ /**
53
+ * Extracts and processes table data from the restaurantData object.
54
+ * Includes temporary table properties and sorts tables.
55
+ * @param {Object} restaurantData - The main restaurant data object.
56
+ * @returns {Array} An array of processed table objects.
57
+ */
58
+ function getAllTables(restaurantData) {
59
+ let allTables = [];
60
+ if (restaurantData?.floors && Array.isArray(restaurantData.floors)) {
61
+ restaurantData.floors.forEach(floor => {
62
+ if (floor?.tables && Array.isArray(floor.tables)) {
63
+ floor.tables.forEach(tbl => {
64
+ // Ensure table number, capacities, priority exist before parsing
65
+ const tableNumberRaw = tbl.tableNumber?.$numberInt ?? tbl.tableNumber;
66
+ const minCapacityRaw = tbl.minCapacity?.$numberInt ?? tbl.minCapacity;
67
+ const maxCapacityRaw = tbl.maxCapacity?.$numberInt ?? tbl.maxCapacity;
68
+ const priorityRaw = tbl.priority?.$numberInt ?? tbl.priority;
69
+ const xRaw = tbl.x?.$numberInt ?? tbl.x;
70
+ const yRaw = tbl.y?.$numberInt ?? tbl.y;
71
+
72
+ if (tbl.objectType === "Tafel" &&
73
+ tableNumberRaw !== undefined &&
74
+ minCapacityRaw !== undefined &&
75
+ maxCapacityRaw !== undefined &&
76
+ priorityRaw !== undefined &&
77
+ xRaw !== undefined &&
78
+ yRaw !== undefined)
79
+ {
80
+ allTables.push({
81
+ tableId: tbl.id,
82
+ tableNumber: parseInt(tableNumberRaw, 10),
83
+ minCapacity: parseInt(minCapacityRaw, 10),
84
+ maxCapacity: parseInt(maxCapacityRaw, 10),
85
+ priority: parseInt(priorityRaw, 10),
86
+ x: parseInt(xRaw, 10),
87
+ y: parseInt(yRaw, 10),
88
+ isTemporary: tbl.isTemporary === true, // Ensure boolean
89
+ startDate: tbl.startDate || null, // Expects 'YYYY-MM-DD'
90
+ endDate: tbl.endDate || null, // Expects 'YYYY-MM-DD'
91
+ application: tbl.application || null // Expects 'breakfast', 'lunch', or 'dinner'
92
+ });
93
+ } else if (tbl.objectType === "Tafel") {
94
+ console.warn(`Skipping table due to missing essential properties: ${JSON.stringify(tbl)}`);
95
+ }
96
+ });
97
+ }
98
+ });
99
+ } else {
100
+ console.warn("Restaurant data is missing 'floors' array.");
101
+ }
102
+
103
+ // Sort tables
104
+ allTables.sort((a, b) => {
105
+ if (a.maxCapacity !== b.maxCapacity) {
106
+ return a.maxCapacity - b.maxCapacity;
107
+ }
108
+ if (a.priority !== b.priority) {
109
+ // Assuming lower priority number means higher priority
110
+ return a.priority - b.priority;
111
+ }
112
+ return a.minCapacity - b.minCapacity;
113
+ });
114
+
115
+ // Filter out tables where parsing failed (resulted in NaN)
116
+ allTables = allTables.filter(t =>
117
+ !isNaN(t.tableNumber) &&
118
+ !isNaN(t.minCapacity) &&
119
+ !isNaN(t.maxCapacity) &&
120
+ !isNaN(t.priority) &&
121
+ !isNaN(t.x) &&
122
+ !isNaN(t.y)
123
+ );
124
+
125
+ return allTables;
126
+ }
127
+
128
+
129
+ // --- Temporary Table Validation ---
130
+
131
+ /**
132
+ * Checks if a temporary table is valid for a specific reservation date and time.
133
+ * @param {Object} table - The table object (must include isTemporary, startDate, endDate, application).
134
+ * @param {string} reservationDateStr - The date of the reservation ("YYYY-MM-DD").
135
+ * @param {string} reservationTimeStr - The time of the reservation ("HH:MM").
136
+ * @returns {boolean} True if the table is valid, false otherwise.
137
+ */
138
+ function isTemporaryTableValid(table, reservationDateStr, reservationTimeStr) {
139
+ if (!table.isTemporary) {
140
+ return true; // Not temporary, always valid (subject to other checks like availability)
141
+ }
142
+
143
+ // Check date range
144
+ if (!table.startDate || !table.endDate) {
145
+ console.log(`Temporary Table ${table.tableNumber} skipped: Missing start/end date.`);
146
+ return false; // Invalid temporary table definition
147
+ }
148
+ // Basic date string comparison (YYYY-MM-DD format)
149
+ if (reservationDateStr < table.startDate || reservationDateStr > table.endDate) {
150
+ // console.log(`Temporary Table ${table.tableNumber} skipped: Date ${reservationDateStr} outside range ${table.startDate}-${table.endDate}.`); // Optional verbose logging
151
+ return false;
152
+ }
153
+
154
+ // Check application (meal type/shift)
155
+ const reservationMealType = getMealTypeByTime(reservationTimeStr);
156
+ if (!reservationMealType) {
157
+ console.log(`Temporary Table ${table.tableNumber} skipped: Could not determine meal type for time ${reservationTimeStr}.`);
158
+ return false; // Cannot determine meal type for the reservation
159
+ }
160
+ if (table.application !== reservationMealType) {
161
+ // console.log(`Temporary Table ${table.tableNumber} skipped: Application '${table.application}' does not match reservation meal type '${reservationMealType}'.`); // Optional verbose logging
162
+ return false;
163
+ }
164
+
165
+ // console.log(`Temporary Table ${table.tableNumber} is valid for ${reservationDateStr} at ${reservationTimeStr} (${reservationMealType}).`); // Optional verbose logging
166
+ return true;
167
+ }
168
+
169
+
170
+ // --- Exports ---
171
+ // Use CommonJS exports (adjust if using ES6 modules)
172
+ module.exports = {
173
+ shifts,
174
+ parseTime, // Export if needed elsewhere, otherwise could be kept internal
175
+ getMealTypeByTime,
176
+ getAllTables,
177
+ isTemporaryTableValid
178
+ };
@@ -0,0 +1,194 @@
1
+ // test.js
2
+
3
+ const { getGuestCountAtHour } = require('../reservation_data/counter');
4
+
5
+ // Sample reservations
6
+ const reservations = [
7
+ { guests: "1", time: "10:01", date: "2025-01-16" },
8
+ { guests: "1", time: "11:59", date: "2025-01-16" },
9
+ { guests: "34", time: "13:00", date: "2025-01-16" },
10
+ { guests: "40", time: "13:00", date: "2025-01-16" },
11
+ ];
12
+
13
+ // Data including exceptions
14
+ const data = {
15
+ "_id": "demo",
16
+ "general-settings": {
17
+ "zitplaatsen": 10,
18
+ "duurReservatie": "10"
19
+ },
20
+ "openinghours-breakfast": {
21
+ "schemeSettings": {
22
+ "Monday": {
23
+ "enabled": true,
24
+ "startTime": "07:00",
25
+ "endTime": "09:00",
26
+ "maxCapacityEnabled": true,
27
+ "maxCapacity": "12",
28
+ "shiftsEnabled": false,
29
+ "shifts": []
30
+ },
31
+ "Wednesday": {
32
+ "enabled": true,
33
+ "startTime": "07:00",
34
+ "endTime": "10:00",
35
+ "maxCapacityEnabled": true,
36
+ "maxCapacity": "12",
37
+ "shiftsEnabled": false,
38
+ "shifts": []
39
+ }
40
+ },
41
+ },
42
+ "openinghours-lunch": {
43
+ "schemeSettings": {
44
+ "Thursday": {
45
+ "enabled": false, // Disabled lunch on Thursday
46
+ "startTime": "13:00",
47
+ "endTime": "14:00",
48
+ "maxCapacityEnabled": true,
49
+ "maxCapacity": "10",
50
+ "shiftsEnabled": false,
51
+ "shifts": []
52
+ }
53
+ },
54
+ },
55
+ "openinghours-dinner": {
56
+ "schemeSettings": {
57
+ "Thursday": {
58
+ "enabled": true,
59
+ "startTime": "16:00",
60
+ "endTime": "17:30",
61
+ "maxCapacityEnabled": true,
62
+ "maxCapacity": "10",
63
+ "shiftsEnabled": false,
64
+ "shifts": []
65
+ },
66
+ "Friday": {
67
+ "enabled": true,
68
+ "startTime": "16:00",
69
+ "endTime": "17:00",
70
+ "maxCapacityEnabled": true,
71
+ "maxCapacity": "10",
72
+ "shiftsEnabled": false,
73
+ "shifts": []
74
+ },
75
+ "Saturday": {
76
+ "enabled": false, // Disabled day
77
+ "startTime": "16:00",
78
+ "endTime": "18:00",
79
+ "maxCapacityEnabled": true,
80
+ "maxCapacity": "10",
81
+ "shiftsEnabled": false,
82
+ "shifts": []
83
+ }
84
+ },
85
+ },
86
+ // Exceptions included in the data
87
+ "exceptions": [
88
+ {
89
+ "title": "Opening 4 tot 10 December",
90
+ "type": "Uitzondering",
91
+ "timeframe": "breakfast",
92
+ "startDate": "2024-12-04",
93
+ "endDate": "2024-12-10",
94
+ "startHour": "07:00",
95
+ "endHour": "09:00",
96
+ "maxSeats": "333",
97
+ "daysOfWeek": [
98
+
99
+ ],
100
+ },
101
+ {
102
+ "title": "Opening 4 tot 10 December",
103
+ "type": "Uitzondering",
104
+ "timeframe": "breakfast",
105
+ "startDate": "2024-12-04",
106
+ "endDate": "2024-12-10",
107
+ "startHour": "07:00",
108
+ "endHour": "09:00",
109
+ "maxSeats": "444",
110
+ "daysOfWeek": [
111
+
112
+ ],
113
+ },
114
+ {
115
+ "title": "Opening 4 tot 10 December",
116
+ "type": "Sluiting",
117
+ "timeframe": "breakfast",
118
+ "startDate": "2024-12-04",
119
+ "endDate": "2024-12-10",
120
+ "startHour": "07:00",
121
+ "endHour": "09:00",
122
+ "maxSeats": "333",
123
+ "daysOfWeek": [
124
+
125
+ ],
126
+ },
127
+ {
128
+ "title": "Closure on December 2nd",
129
+ "type": "Sluiting",
130
+ "timeframe": "dinner",
131
+ "startDate": "2024-12-02",
132
+ "endDate": "2024-12-04",
133
+ "daysOfWeek": [
134
+
135
+ ]
136
+ },
137
+ {
138
+ "title": "Uitzondering on December 4th",
139
+ "type": "Uitzondering",
140
+ "timeframe": "breakfast",
141
+ "startDate": "2024-12-04",
142
+ "endDate": "2024-12-04",
143
+ "startHour": "07:00",
144
+ "endHour": "08:00",
145
+ "maxSeats": "12",
146
+ "daysOfWeek": [
147
+ "woensdag"
148
+ ]
149
+ }
150
+ ]
151
+ };
152
+
153
+ // Test cases
154
+ console.log('--- Reservation Guest Count Tests ---');
155
+
156
+ // Test 1: Guests at 12:00
157
+ console.log('Test 1 - Guests at 12:00:', getGuestCountAtHour(data, reservations, '12:00'));
158
+ // Expected Output: 1 (Guest from 11:00 reservation)
159
+
160
+ // Test 2: Guests at 11:00
161
+ console.log('Test 2 - Guests at 11:00:', getGuestCountAtHour(data, reservations, '11:00'));
162
+ // Expected Output: 2 (Guests from 10:00 and 11:00 reservations)
163
+
164
+ // Test 3: Guests at 10:00
165
+ console.log('Test 3 - Guests at 10:00:', getGuestCountAtHour(data, reservations, '10:00'));
166
+ // Expected Output: 1 (Guest from 10:00 reservation)
167
+
168
+ // Test 4: Guests at 12:00 (Original reservations)
169
+ console.log('Test 4 - Guests at 12:00:', getGuestCountAtHour(data, reservations.slice(2), '12:00'));
170
+ // Expected Output: 35
171
+
172
+ // Test 5: Guests at 13:00
173
+ console.log('Test 5 - Guests at 13:00:', getGuestCountAtHour(data, reservations.slice(2), '13:00'));
174
+ // Expected Output: 75 (35 from 12:00 reservation and 40 from 13:00 reservation)
175
+
176
+ // Test 6: Guests at 14:00
177
+ console.log('Test 6 - Guests at 14:00:', getGuestCountAtHour(data, reservations.slice(2), '14:00'));
178
+ // Expected Output: 40
179
+
180
+ // Test 7: Guests at 14:00 (All reservations)
181
+ console.log('Test 7 - Guests at 14:00:', getGuestCountAtHour(data, reservations, '14:00'));
182
+ // Expected Output: 40
183
+
184
+ // Test 8: Guests at 11:59
185
+ console.log('Test 8 - Guests at 11:59:', getGuestCountAtHour(data, reservations, '11:59'));
186
+ // Expected Output: 1
187
+
188
+ // Test 9: Guests at 12:00 (New scenario)
189
+ console.log('Test 9 - Guests at 12:00:', getGuestCountAtHour(data, reservations, '12:00'));
190
+ // Expected Output: 1
191
+
192
+ // Test 10: Guests at 13:59
193
+ console.log('Test 10 - Guests at 13:59:', getGuestCountAtHour(data, reservations, '13:59'));
194
+ // Expected Output: 75
@@ -0,0 +1,81 @@
1
+ // dailyGuestCountsTests.js
2
+
3
+ const { getDailyGuestCounts } = require('../processing/dailyGuestCounts');
4
+
5
+ // Sample data including overlapping meal times
6
+ const data = {
7
+ "_id": "demo",
8
+ "general-settings": {
9
+ "zitplaatsen": 10,
10
+ "duurReservatie": "1000"
11
+ },
12
+ "openinghours-breakfast": {
13
+ "schemeSettings": {
14
+ "Monday": {
15
+ "enabled": true,
16
+ "startTime": "07:00",
17
+ "endTime": "11:00", // Extended breakfast to overlap with lunch
18
+ "maxCapacityEnabled": true,
19
+ "maxCapacity": "0",
20
+ "shiftsEnabled": false,
21
+ "shifts": []
22
+ }
23
+ },
24
+ "storedNumber": {
25
+ "$numberInt": "0"
26
+ }
27
+ },
28
+ "openinghours-lunch": {
29
+ "schemeSettings": {
30
+ "Monday": {
31
+ "enabled": true,
32
+ "startTime": "11:00", // Lunch starts earlier to overlap with breakfast and dinner
33
+ "endTime": "14:00",
34
+ "maxCapacityEnabled": true,
35
+ "maxCapacity": "0",
36
+ "shiftsEnabled": false,
37
+ "shifts": []
38
+ }
39
+ },
40
+ "storedNumber": {
41
+ "$numberInt": "0"
42
+ }
43
+ },
44
+ "openinghours-dinner": {
45
+ "schemeSettings": {
46
+ "Monday": {
47
+ "enabled": true,
48
+ "startTime": "14:00", // Dinner starts earlier to overlap with lunch
49
+ "endTime": "16:00",
50
+ "maxCapacityEnabled": true,
51
+ "maxCapacity": "0",
52
+ "shiftsEnabled": false,
53
+ "shifts": []
54
+ }
55
+ },
56
+ "storedNumber": {
57
+ "$numberInt": "0"
58
+ }
59
+ },
60
+ // Any exceptions
61
+ "exceptions": [
62
+ // Add exceptions here if needed
63
+ ]
64
+ };
65
+
66
+ // Sample reservations
67
+ const reservations = [
68
+ // Breakfast reservations
69
+
70
+ { guests: "10", time: "13:00", date: "2024-12-02" },
71
+
72
+ ];
73
+
74
+ console.log('--- Daily Guest Counts Tests ---');
75
+
76
+ const dateStr = '2024-12-02';
77
+
78
+ const combinedGuestCounts = getDailyGuestCounts(data, dateStr, reservations);
79
+
80
+ console.log('Combined Guest Counts for All Meals on', dateStr, ':');
81
+ console.log(JSON.stringify(combinedGuestCounts, null, 2));
@@ -0,0 +1,106 @@
1
+ // dateAvailabilityTests.js
2
+
3
+ const { isDateAvailable } = require('../isDateAvailable');
4
+ const { getDailyGuestCounts } = require('../processing/dailyGuestCounts');
5
+
6
+ // Sample data
7
+ const data = {
8
+ "_id": "demo",
9
+ "general-settings": {
10
+ "zitplaatsen": "5",
11
+ "duurReservatie": "120", // Reservation duration of 120 minutes
12
+ "intervalReservatie": "30" // Time increment of 30 minutes
13
+ },
14
+ "openinghours-breakfast": {
15
+ "schemeSettings": {
16
+ "Monday": {
17
+ "enabled": false,
18
+ "startTime": "07:00",
19
+ "endTime": "11:00",
20
+ "maxCapacityEnabled": false, // Will fallback to zitplaatsen
21
+ "maxCapacity": "0",
22
+ "shiftsEnabled": false,
23
+ "shifts": []
24
+ }
25
+ },
26
+ "storedNumber": {
27
+ "$numberInt": "0"
28
+ }
29
+ },
30
+ "openinghours-lunch": {
31
+ "schemeSettings": {
32
+ "Monday": {
33
+ "enabled": false,
34
+ "startTime": "13:00", // Lunch starts at 13:00
35
+ "endTime": "16:00",
36
+ "maxCapacityEnabled": true,
37
+ "maxCapacity": "20",
38
+ "shiftsEnabled": false,
39
+ "shifts": []
40
+ }
41
+ },
42
+ "storedNumber": {
43
+ "$numberInt": "0"
44
+ }
45
+ },
46
+ "openinghours-dinner": {
47
+ "schemeSettings": {
48
+ "Monday": {
49
+ "enabled": true,
50
+ "startTime": "16:00",
51
+ "endTime": "23:00",
52
+ "maxCapacityEnabled": true,
53
+ "maxCapacity": "5",
54
+ "shiftsEnabled": false,
55
+ }
56
+ },
57
+ "storedNumber": {
58
+ "$numberInt": "0"
59
+ }
60
+ },
61
+ "exceptions": []
62
+ };
63
+
64
+ // Sample reservations
65
+ const reservations = [
66
+ // Dinner reservations
67
+ { guests: "5", time: "18:00", date: "2024-12-02" }, // Shift 1 is fully booked
68
+ { guests: "2", time: "20:00", date: "2024-12-02" }, // Shift 2 has available seats
69
+ ];
70
+
71
+ console.log('--- Date Availability Tests ---');
72
+
73
+ const guests = 3;
74
+
75
+ // Test 1: Check availability on 2024-12-02
76
+ const dateStr1 = '2024-12-02';
77
+ const { guestCounts: dailyGuestCounts1, shiftsInfo } = getDailyGuestCounts(data, dateStr1, reservations);
78
+ console.log(`Daily Guest Counts for ${dateStr1}:`);
79
+ console.log(dailyGuestCounts1);
80
+ console.log('Shifts Info:', shiftsInfo);
81
+
82
+ const isAvailable1 = isDateAvailable(data, dateStr1, reservations, guests);
83
+ console.log(`Is date ${dateStr1} available for ${guests} guests? ${isAvailable1}`);
84
+ console.log('---');
85
+
86
+ // Test 2: Check availability on 2024-12-03
87
+ const dateStr2 = '2024-12-03';
88
+ const { guestCounts: dailyGuestCounts2 } = getDailyGuestCounts(data, dateStr2, reservations);
89
+ console.log(`Daily Guest Counts for ${dateStr2}:`);
90
+ console.log(dailyGuestCounts2);
91
+
92
+ const isAvailable2 = isDateAvailable(data, dateStr2, reservations, guests);
93
+ console.log(`Is date ${dateStr2} available for ${guests} guests? ${isAvailable2}`);
94
+ console.log('---');
95
+
96
+ // Test 3: Check availability on 2024-12-02 for 5 guests
97
+ const guests3 = 5;
98
+ const isAvailable3 = isDateAvailable(data, dateStr1, reservations, guests3);
99
+ console.log(`Is date ${dateStr1} available for ${guests3} guests? ${isAvailable3}`);
100
+ console.log('---');
101
+
102
+ // Test 4: Check availability on 2024-12-02 for 2 guests
103
+ const guests4 = 2;
104
+ const isAvailable4 = isDateAvailable(data, dateStr1, reservations, guests4);
105
+ console.log(`Is date ${dateStr1} available for ${guests4} guests? ${isAvailable4}`);
106
+ console.log('---');