@happychef/algorithm 1.3.0 → 1.4.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.
Files changed (71) hide show
  1. package/.claude/settings.local.json +16 -0
  2. package/.github/workflows/ci-cd.yml +80 -80
  3. package/BRANCH_PROTECTION_SETUP.md +167 -167
  4. package/CHANGELOG.md +8 -8
  5. package/README.md +144 -144
  6. package/RESERVERINGEN_GIDS.md +986 -986
  7. package/__tests__/crossMidnight.test.js +63 -0
  8. package/__tests__/crossMidnightTimeblocks.test.js +312 -0
  9. package/__tests__/edgeCases.test.js +271 -0
  10. package/__tests__/filters.test.js +276 -276
  11. package/__tests__/isDateAvailable.test.js +179 -175
  12. package/__tests__/isTimeAvailable.test.js +174 -168
  13. package/__tests__/restaurantData.test.js +422 -422
  14. package/__tests__/tableHelpers.test.js +247 -247
  15. package/assignTables.js +506 -444
  16. package/changes/2025/December/PR2___change.md +14 -14
  17. package/changes/2025/December/PR3_add__change.md +20 -20
  18. package/changes/2025/December/PR4___.md +15 -15
  19. package/changes/2025/December/PR5___.md +15 -15
  20. package/changes/2025/December/PR6__del_.md +17 -17
  21. package/changes/2025/December/PR7_add__change.md +21 -21
  22. package/changes/2026/February/PR15_add__change.md +21 -21
  23. package/changes/2026/February/PR16_add__.md +20 -0
  24. package/changes/2026/February/PR16_add_getDateClosingReasons.md +31 -31
  25. package/changes/2026/January/PR10_add__change.md +21 -21
  26. package/changes/2026/January/PR11_add__change.md +19 -19
  27. package/changes/2026/January/PR12_add__.md +21 -21
  28. package/changes/2026/January/PR13_add__change.md +20 -20
  29. package/changes/2026/January/PR14_add__change.md +19 -19
  30. package/changes/2026/January/PR8_add__change.md +38 -38
  31. package/changes/2026/January/PR9_add__change.md +19 -19
  32. package/dateHelpers.js +31 -0
  33. package/filters/maxArrivalsFilter.js +114 -114
  34. package/filters/maxGroupsFilter.js +221 -221
  35. package/filters/timeFilter.js +89 -89
  36. package/getAvailableTimeblocks.js +158 -158
  37. package/getDateClosingReasons.js +193 -193
  38. package/grouping.js +162 -162
  39. package/index.js +48 -43
  40. package/isDateAvailable.js +80 -80
  41. package/isDateAvailableWithTableCheck.js +172 -172
  42. package/isTimeAvailable.js +26 -26
  43. package/jest.config.js +23 -23
  44. package/package.json +27 -27
  45. package/processing/dailyGuestCounts.js +73 -73
  46. package/processing/mealTypeCount.js +133 -133
  47. package/processing/timeblocksAvailable.js +344 -182
  48. package/reservation_data/counter.js +82 -75
  49. package/restaurant_data/exceptions.js +150 -150
  50. package/restaurant_data/openinghours.js +142 -142
  51. package/simulateTableAssignment.js +833 -726
  52. package/tableHelpers.js +209 -209
  53. package/tables/time/parseTime.js +19 -19
  54. package/tables/time/shifts.js +7 -7
  55. package/tables/utils/calculateDistance.js +13 -13
  56. package/tables/utils/isTableFreeForAllSlots.js +14 -14
  57. package/tables/utils/isTemporaryTableValid.js +39 -39
  58. package/test/test_counter.js +194 -194
  59. package/test/test_dailyCount.js +81 -81
  60. package/test/test_datesAvailable.js +106 -106
  61. package/test/test_exceptions.js +172 -172
  62. package/test/test_isDateAvailable.js +330 -330
  63. package/test/test_mealTypeCount.js +54 -54
  64. package/test/test_timesAvailable.js +88 -88
  65. package/test-detailed-filter.js +100 -100
  66. package/test-lunch-debug.js +110 -110
  67. package/test-max-arrivals-filter.js +79 -79
  68. package/test-meal-stop-fix.js +147 -147
  69. package/test-meal-stop-simple.js +93 -93
  70. package/test-timezone-debug.js +47 -47
  71. package/test.js +336 -336
@@ -1,158 +1,158 @@
1
- // getAvailableTimeblocks.js
2
-
3
- const { timeblocksAvailable } = require('./processing/timeblocksAvailable');
4
- const { parseTime, getMealTypeByTime } = require('./tableHelpers');
5
-
6
- /**
7
- * Parses a time string in "HH:MM" format into a Date object on a specific date.
8
- * @param {string} dateStr - The date string in "YYYY-MM-DD" format.
9
- * @param {string} timeStr - Time string in "HH:MM" format.
10
- * @param {string} timeZone - The IANA time zone identifier (not used, kept for compatibility).
11
- * @returns {Date} Date object representing the time on the specified date.
12
- */
13
- function parseDateTimeInTimeZone(dateStr, timeStr, timeZone) {
14
- const [year, month, day] = dateStr.split('-').map(Number);
15
- const [hours, minutes] = timeStr.split(':').map(Number);
16
-
17
- // Create a simple date object for the given date and time
18
- // This represents the local time on that date
19
- return new Date(year, month - 1, day, hours, minutes);
20
- }
21
-
22
- /**
23
- * Gets the available time blocks or shifts for a reservation, considering 'uurOpVoorhand' and 'dagenInToekomst'.
24
- * @param {Object} data - The main data object containing settings and meal information.
25
- * @param {string} dateStr - The date string in "YYYY-MM-DD" format.
26
- * @param {Array} reservations - An array of reservation objects.
27
- * @param {number} guests - The number of guests for the reservation.
28
- * @param {Array} blockedSlots - Optional array of blocked time slots to exclude.
29
- * @param {string|null} giftcard - Optional giftcard to filter times by meal.
30
- * @param {boolean} isAdmin - Optional flag to bypass time restrictions for admin users.
31
- * @returns {Object} - Returns a pruned object of available time blocks or shifts, or an empty object if out of range.
32
- */
33
- function getAvailableTimeblocks(data, dateStr, reservations, guests, blockedSlots = [], giftcard = null, isAdmin = false, duration = null) {
34
- // Get 'uurOpVoorhand' from general settings
35
- let uurOpVoorhand = 0;
36
- if (
37
- data['general-settings'] &&
38
- data['general-settings'].uurOpVoorhand &&
39
- parseInt(data['general-settings'].uurOpVoorhand, 10) >= 0
40
- ) {
41
- uurOpVoorhand = parseInt(data['general-settings'].uurOpVoorhand, 10);
42
- }
43
-
44
- // Get 'dagenInToekomst' from general settings
45
- let dagenInToekomst = 90; // Default if not defined
46
- if (
47
- data['general-settings'] &&
48
- data['general-settings'].dagenInToekomst &&
49
- parseInt(data['general-settings'].dagenInToekomst, 10) > 0
50
- ) {
51
- dagenInToekomst = parseInt(data['general-settings'].dagenInToekomst, 10);
52
- }
53
-
54
- // Time zone for CEST/CET (Europe/Amsterdam)
55
- const timeZone = 'Europe/Amsterdam';
56
-
57
- // Current date/time in local timezone (system time)
58
- // Note: We assume the server is running in the correct timezone
59
- const now = new Date();
60
- const currentTimeInTimeZone = now;
61
-
62
- // Calculate the maximum allowed date
63
- const maxAllowedDate = new Date(currentTimeInTimeZone.getTime());
64
- maxAllowedDate.setDate(maxAllowedDate.getDate() + dagenInToekomst);
65
- maxAllowedDate.setHours(23, 59, 59, 999);
66
-
67
- // Parse the target date (just the date part, no time)
68
- const [year, month, day] = dateStr.split('-').map(Number);
69
- const targetDateInTimeZone = new Date(year, month - 1, day);
70
-
71
- // Check if targetDateInTimeZone is within dagenInToekomst (skip for admin)
72
- if (!isAdmin && targetDateInTimeZone > maxAllowedDate) {
73
- // Out of allowed range, return empty object
74
- return {};
75
- }
76
-
77
- // Check if the target date is today in the specified time zone
78
- const isToday =
79
- currentTimeInTimeZone.toDateString() === targetDateInTimeZone.toDateString();
80
-
81
- // Get available time blocks or shifts
82
- const availableTimeblocks = timeblocksAvailable(data, dateStr, reservations, guests, blockedSlots, giftcard, isAdmin, duration);
83
-
84
- // If the date is today and uurOpVoorhand is greater than zero, prune time blocks (skip for admin)
85
- if (!isAdmin && isToday && uurOpVoorhand >= 0) {
86
- const cutoffTime = new Date(currentTimeInTimeZone.getTime());
87
- cutoffTime.setHours(cutoffTime.getHours() + uurOpVoorhand);
88
-
89
- for (const [key, value] of Object.entries(availableTimeblocks)) {
90
- let timeStr = key;
91
-
92
- const timeBlockDateTime = parseDateTimeInTimeZone(dateStr, timeStr, timeZone);
93
-
94
- if (timeBlockDateTime < cutoffTime) {
95
- delete availableTimeblocks[key];
96
- }
97
- }
98
- }
99
-
100
- // Apply last booking time filter (lunchStop, dinerStop, ontbijtStop) - skip for admin
101
- // IMPORTANT: This filter should only apply to TODAY, just like uurOpVoorhand
102
- // The filter compares CURRENT TIME in Brussels against the stop time,
103
- // not the reservation time slot against the stop time
104
- if (!isAdmin && isToday) {
105
- const settings = data?.['general-settings'] || {};
106
- const breakfastStop = settings.ontbijtStop || null;
107
- const lunchStop = settings.lunchStop || null;
108
- const dinnerStop = settings.dinerStop || null;
109
-
110
- // Only apply if at least one stop time is configured
111
- if (breakfastStop || lunchStop || dinnerStop) {
112
- // Get current time in Brussels timezone (in minutes since midnight)
113
- // Using Intl.DateTimeFormat for reliable timezone conversion
114
- const formatter = new Intl.DateTimeFormat('en-US', {
115
- timeZone: 'Europe/Brussels',
116
- hour: '2-digit',
117
- minute: '2-digit',
118
- hour12: false,
119
- });
120
- const parts = formatter.formatToParts(now);
121
- const timeParts = Object.fromEntries(parts.map(p => [p.type, p.value]));
122
- const currentTimeMinutes = parseInt(timeParts.hour, 10) * 60 + parseInt(timeParts.minute, 10);
123
-
124
- // Check if current time has passed any stop times
125
- const breakfastStopMinutes = breakfastStop ? parseTime(breakfastStop) : null;
126
- const lunchStopMinutes = lunchStop ? parseTime(lunchStop) : null;
127
- const dinnerStopMinutes = dinnerStop ? parseTime(dinnerStop) : null;
128
-
129
- const breakfastStopped = breakfastStopMinutes !== null && currentTimeMinutes >= breakfastStopMinutes;
130
- const lunchStopped = lunchStopMinutes !== null && currentTimeMinutes >= lunchStopMinutes;
131
- const dinnerStopped = dinnerStopMinutes !== null && currentTimeMinutes >= dinnerStopMinutes;
132
-
133
- // Remove ALL slots of a meal type if current time >= stop time for that meal
134
- for (const time in availableTimeblocks) {
135
- const mealType = getMealTypeByTime(time);
136
- let shouldRemove = false;
137
-
138
- if (mealType === 'breakfast' && breakfastStopped) {
139
- shouldRemove = true;
140
- } else if (mealType === 'lunch' && lunchStopped) {
141
- shouldRemove = true;
142
- } else if (mealType === 'dinner' && dinnerStopped) {
143
- shouldRemove = true;
144
- }
145
-
146
- if (shouldRemove) {
147
- delete availableTimeblocks[time];
148
- }
149
- }
150
- }
151
- }
152
-
153
- return availableTimeblocks;
154
- }
155
-
156
- module.exports = {
157
- getAvailableTimeblocks,
158
- };
1
+ // getAvailableTimeblocks.js
2
+
3
+ const { timeblocksAvailable } = require('./processing/timeblocksAvailable');
4
+ const { parseTime, getMealTypeByTime } = require('./tableHelpers');
5
+
6
+ /**
7
+ * Parses a time string in "HH:MM" format into a Date object on a specific date.
8
+ * @param {string} dateStr - The date string in "YYYY-MM-DD" format.
9
+ * @param {string} timeStr - Time string in "HH:MM" format.
10
+ * @param {string} timeZone - The IANA time zone identifier (not used, kept for compatibility).
11
+ * @returns {Date} Date object representing the time on the specified date.
12
+ */
13
+ function parseDateTimeInTimeZone(dateStr, timeStr, timeZone) {
14
+ const [year, month, day] = dateStr.split('-').map(Number);
15
+ const [hours, minutes] = timeStr.split(':').map(Number);
16
+
17
+ // Create a simple date object for the given date and time
18
+ // This represents the local time on that date
19
+ return new Date(year, month - 1, day, hours, minutes);
20
+ }
21
+
22
+ /**
23
+ * Gets the available time blocks or shifts for a reservation, considering 'uurOpVoorhand' and 'dagenInToekomst'.
24
+ * @param {Object} data - The main data object containing settings and meal information.
25
+ * @param {string} dateStr - The date string in "YYYY-MM-DD" format.
26
+ * @param {Array} reservations - An array of reservation objects.
27
+ * @param {number} guests - The number of guests for the reservation.
28
+ * @param {Array} blockedSlots - Optional array of blocked time slots to exclude.
29
+ * @param {string|null} giftcard - Optional giftcard to filter times by meal.
30
+ * @param {boolean} isAdmin - Optional flag to bypass time restrictions for admin users.
31
+ * @returns {Object} - Returns a pruned object of available time blocks or shifts, or an empty object if out of range.
32
+ */
33
+ function getAvailableTimeblocks(data, dateStr, reservations, guests, blockedSlots = [], giftcard = null, isAdmin = false, duration = null) {
34
+ // Get 'uurOpVoorhand' from general settings
35
+ let uurOpVoorhand = 0;
36
+ if (
37
+ data['general-settings'] &&
38
+ data['general-settings'].uurOpVoorhand &&
39
+ parseInt(data['general-settings'].uurOpVoorhand, 10) >= 0
40
+ ) {
41
+ uurOpVoorhand = parseInt(data['general-settings'].uurOpVoorhand, 10);
42
+ }
43
+
44
+ // Get 'dagenInToekomst' from general settings
45
+ let dagenInToekomst = 90; // Default if not defined
46
+ if (
47
+ data['general-settings'] &&
48
+ data['general-settings'].dagenInToekomst &&
49
+ parseInt(data['general-settings'].dagenInToekomst, 10) > 0
50
+ ) {
51
+ dagenInToekomst = parseInt(data['general-settings'].dagenInToekomst, 10);
52
+ }
53
+
54
+ // Time zone for CEST/CET (Europe/Amsterdam)
55
+ const timeZone = 'Europe/Amsterdam';
56
+
57
+ // Current date/time in local timezone (system time)
58
+ // Note: We assume the server is running in the correct timezone
59
+ const now = new Date();
60
+ const currentTimeInTimeZone = now;
61
+
62
+ // Calculate the maximum allowed date
63
+ const maxAllowedDate = new Date(currentTimeInTimeZone.getTime());
64
+ maxAllowedDate.setDate(maxAllowedDate.getDate() + dagenInToekomst);
65
+ maxAllowedDate.setHours(23, 59, 59, 999);
66
+
67
+ // Parse the target date (just the date part, no time)
68
+ const [year, month, day] = dateStr.split('-').map(Number);
69
+ const targetDateInTimeZone = new Date(year, month - 1, day);
70
+
71
+ // Check if targetDateInTimeZone is within dagenInToekomst (skip for admin)
72
+ if (!isAdmin && targetDateInTimeZone > maxAllowedDate) {
73
+ // Out of allowed range, return empty object
74
+ return {};
75
+ }
76
+
77
+ // Check if the target date is today in the specified time zone
78
+ const isToday =
79
+ currentTimeInTimeZone.toDateString() === targetDateInTimeZone.toDateString();
80
+
81
+ // Get available time blocks or shifts
82
+ const availableTimeblocks = timeblocksAvailable(data, dateStr, reservations, guests, blockedSlots, giftcard, isAdmin, duration);
83
+
84
+ // If the date is today and uurOpVoorhand is greater than zero, prune time blocks (skip for admin)
85
+ if (!isAdmin && isToday && uurOpVoorhand >= 0) {
86
+ const cutoffTime = new Date(currentTimeInTimeZone.getTime());
87
+ cutoffTime.setHours(cutoffTime.getHours() + uurOpVoorhand);
88
+
89
+ for (const [key, value] of Object.entries(availableTimeblocks)) {
90
+ let timeStr = key;
91
+
92
+ const timeBlockDateTime = parseDateTimeInTimeZone(dateStr, timeStr, timeZone);
93
+
94
+ if (timeBlockDateTime < cutoffTime) {
95
+ delete availableTimeblocks[key];
96
+ }
97
+ }
98
+ }
99
+
100
+ // Apply last booking time filter (lunchStop, dinerStop, ontbijtStop) - skip for admin
101
+ // IMPORTANT: This filter should only apply to TODAY, just like uurOpVoorhand
102
+ // The filter compares CURRENT TIME in Brussels against the stop time,
103
+ // not the reservation time slot against the stop time
104
+ if (!isAdmin && isToday) {
105
+ const settings = data?.['general-settings'] || {};
106
+ const breakfastStop = settings.ontbijtStop || null;
107
+ const lunchStop = settings.lunchStop || null;
108
+ const dinnerStop = settings.dinerStop || null;
109
+
110
+ // Only apply if at least one stop time is configured
111
+ if (breakfastStop || lunchStop || dinnerStop) {
112
+ // Get current time in Brussels timezone (in minutes since midnight)
113
+ // Using Intl.DateTimeFormat for reliable timezone conversion
114
+ const formatter = new Intl.DateTimeFormat('en-US', {
115
+ timeZone: 'Europe/Brussels',
116
+ hour: '2-digit',
117
+ minute: '2-digit',
118
+ hour12: false,
119
+ });
120
+ const parts = formatter.formatToParts(now);
121
+ const timeParts = Object.fromEntries(parts.map(p => [p.type, p.value]));
122
+ const currentTimeMinutes = parseInt(timeParts.hour, 10) * 60 + parseInt(timeParts.minute, 10);
123
+
124
+ // Check if current time has passed any stop times
125
+ const breakfastStopMinutes = breakfastStop ? parseTime(breakfastStop) : null;
126
+ const lunchStopMinutes = lunchStop ? parseTime(lunchStop) : null;
127
+ const dinnerStopMinutes = dinnerStop ? parseTime(dinnerStop) : null;
128
+
129
+ const breakfastStopped = breakfastStopMinutes !== null && currentTimeMinutes >= breakfastStopMinutes;
130
+ const lunchStopped = lunchStopMinutes !== null && currentTimeMinutes >= lunchStopMinutes;
131
+ const dinnerStopped = dinnerStopMinutes !== null && currentTimeMinutes >= dinnerStopMinutes;
132
+
133
+ // Remove ALL slots of a meal type if current time >= stop time for that meal
134
+ for (const time in availableTimeblocks) {
135
+ const mealType = getMealTypeByTime(time);
136
+ let shouldRemove = false;
137
+
138
+ if (mealType === 'breakfast' && breakfastStopped) {
139
+ shouldRemove = true;
140
+ } else if (mealType === 'lunch' && lunchStopped) {
141
+ shouldRemove = true;
142
+ } else if (mealType === 'dinner' && dinnerStopped) {
143
+ shouldRemove = true;
144
+ }
145
+
146
+ if (shouldRemove) {
147
+ delete availableTimeblocks[time];
148
+ }
149
+ }
150
+ }
151
+ }
152
+
153
+ return availableTimeblocks;
154
+ }
155
+
156
+ module.exports = {
157
+ getAvailableTimeblocks,
158
+ };