@happychef/algorithm 1.2.30 → 1.3.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/.github/workflows/ci-cd.yml +80 -80
- package/CHANGELOG.md +8 -8
- package/RESERVERINGEN_GIDS.md +986 -986
- package/assignTables.js +444 -456
- package/changes/2025/December/PR2___change.md +14 -14
- package/changes/2025/December/PR3_add__change.md +20 -20
- package/changes/2025/December/PR4___.md +15 -15
- package/changes/2025/December/PR5___.md +15 -15
- package/changes/2025/December/PR6__del_.md +17 -17
- package/changes/2025/December/PR7_add__change.md +21 -21
- package/changes/2026/February/PR15_add__change.md +22 -0
- package/changes/2026/February/PR16_add_getDateClosingReasons.md +31 -0
- package/changes/2026/January/PR10_add__change.md +21 -21
- package/changes/2026/January/PR11_add__change.md +19 -19
- package/changes/2026/January/PR12_add__.md +21 -21
- package/changes/2026/January/PR13_add__change.md +20 -20
- package/changes/2026/January/PR14_add__change.md +19 -19
- package/changes/2026/January/PR8_add__change.md +38 -38
- package/changes/2026/January/PR9_add__change.md +19 -19
- package/filters/maxArrivalsFilter.js +114 -114
- package/filters/maxGroupsFilter.js +221 -221
- package/filters/timeFilter.js +89 -89
- package/getAvailableTimeblocks.js +158 -158
- package/getDateClosingReasons.js +193 -0
- package/grouping.js +162 -162
- package/index.js +43 -42
- package/isDateAvailable.js +80 -80
- package/isDateAvailableWithTableCheck.js +172 -172
- package/isTimeAvailable.js +26 -26
- package/package.json +27 -27
- package/processing/dailyGuestCounts.js +73 -73
- package/processing/mealTypeCount.js +133 -133
- package/processing/timeblocksAvailable.js +182 -182
- package/reservation_data/counter.js +74 -74
- package/restaurant_data/exceptions.js +150 -150
- package/restaurant_data/openinghours.js +142 -139
- package/simulateTableAssignment.js +726 -726
- package/tableHelpers.js +209 -206
- package/tables/time/parseTime.js +19 -19
- package/tables/time/shifts.js +7 -7
- package/tables/utils/calculateDistance.js +13 -13
- package/tables/utils/isTableFreeForAllSlots.js +14 -14
- package/tables/utils/isTemporaryTableValid.js +39 -39
- package/test/test_counter.js +194 -194
- package/test/test_dailyCount.js +81 -81
- package/test/test_datesAvailable.js +106 -106
- package/test/test_exceptions.js +172 -172
- package/test/test_isDateAvailable.js +330 -330
- package/test/test_mealTypeCount.js +54 -54
- package/test/test_timesAvailable.js +88 -88
- package/test-meal-stop-fix.js +147 -147
- package/test-meal-stop-simple.js +93 -93
- package/test.js +336 -336
- package/bundle_entry.js +0 -100
- package/moment-timezone-shim.js +0 -179
- package/nul +0 -0
|
@@ -1,133 +1,133 @@
|
|
|
1
|
-
// mealTypeCount.js
|
|
2
|
-
|
|
3
|
-
const { getDataByDateAndMealWithExceptions } = require('../restaurant_data/exceptions');
|
|
4
|
-
const { parseTime } = require('../restaurant_data/openinghours');
|
|
5
|
-
const { getGuestCountAtHour } = require('../reservation_data/counter');
|
|
6
|
-
|
|
7
|
-
function getInterval(data) {
|
|
8
|
-
let intervalReservatie = 15;
|
|
9
|
-
if (
|
|
10
|
-
data['general-settings'] &&
|
|
11
|
-
data['general-settings'].intervalReservatie &&
|
|
12
|
-
parseInt(data['general-settings'].intervalReservatie, 10) > 0
|
|
13
|
-
) {
|
|
14
|
-
intervalReservatie = parseInt(data['general-settings'].intervalReservatie, 10);
|
|
15
|
-
}
|
|
16
|
-
return intervalReservatie;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Retrieves meal types with shifts enabled and at least one shift defined for the specified date.
|
|
21
|
-
* @param {Object} data - The main data object.
|
|
22
|
-
* @param {string} dateStr - The date string (YYYY-MM-DD).
|
|
23
|
-
* @returns {Array} - An array of meal types with shifts.
|
|
24
|
-
*/
|
|
25
|
-
function getMealTypesWithShifts(data, dateStr) {
|
|
26
|
-
const mealTypes = ['breakfast', 'lunch', 'dinner'];
|
|
27
|
-
const mealTypesWithShifts = [];
|
|
28
|
-
|
|
29
|
-
for (const mealType of mealTypes) {
|
|
30
|
-
const mealData = getDataByDateAndMealWithExceptions(data, dateStr, mealType);
|
|
31
|
-
if (
|
|
32
|
-
mealData &&
|
|
33
|
-
mealData.shiftsEnabled &&
|
|
34
|
-
Array.isArray(mealData.shifts) &&
|
|
35
|
-
mealData.shifts.length > 0
|
|
36
|
-
) {
|
|
37
|
-
mealTypesWithShifts.push(mealType);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
return mealTypesWithShifts;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function shouldIncludeEndTime(mealType, endTime) {
|
|
45
|
-
if ((mealType === 'breakfast' && endTime === '11:00') ||
|
|
46
|
-
(mealType === 'lunch' && endTime === '16:00')) {
|
|
47
|
-
return false; // Do not include endTime for breakfast at 11:00 and lunch at 16:00
|
|
48
|
-
}
|
|
49
|
-
return true; // Include endTime for all other cases
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Calculates guest counts for each interval during a meal period or at shift times.
|
|
54
|
-
* @param {Object} data - The main data object.
|
|
55
|
-
* @param {string} dateStr - The date string (YYYY-MM-DD).
|
|
56
|
-
* @param {string} mealType - The meal type ("breakfast", "lunch", "dinner").
|
|
57
|
-
* @param {Array} reservations - An array of reservation objects.
|
|
58
|
-
* @returns {Object|null} An object mapping times to guest counts, or null if meal is not available.
|
|
59
|
-
*/
|
|
60
|
-
function getGuestCountsForMeal(data, dateStr, mealType, reservations) {
|
|
61
|
-
const mealData = getDataByDateAndMealWithExceptions(data, dateStr, mealType);
|
|
62
|
-
if (!mealData) {
|
|
63
|
-
return null;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const guestCounts = {};
|
|
67
|
-
const shiftsInfo = [];
|
|
68
|
-
|
|
69
|
-
// Get 'intervalReservatie' from general settings, default to 15 if not set or zero
|
|
70
|
-
let intervalReservatie = getInterval(data);
|
|
71
|
-
|
|
72
|
-
// Check if shifts are enabled and shifts array has valid content
|
|
73
|
-
if (
|
|
74
|
-
mealData.shiftsEnabled &&
|
|
75
|
-
Array.isArray(mealData.shifts) &&
|
|
76
|
-
mealData.shifts.length > 0
|
|
77
|
-
) {
|
|
78
|
-
// If shifts are enabled and valid, calculate guest counts at shift times
|
|
79
|
-
const shifts = mealData.shifts; // Array of shifts
|
|
80
|
-
|
|
81
|
-
for (const shift of shifts) {
|
|
82
|
-
const timeStr = shift.time; // Time of the shift in "HH:MM" format
|
|
83
|
-
|
|
84
|
-
// Get guest count at this time
|
|
85
|
-
const guestCount = getGuestCountAtHour(data, reservations, timeStr, dateStr);
|
|
86
|
-
|
|
87
|
-
// Store in guestCounts
|
|
88
|
-
guestCounts[timeStr] = mealData.maxCapacity - guestCount;
|
|
89
|
-
|
|
90
|
-
// Store shift information
|
|
91
|
-
shiftsInfo.push({
|
|
92
|
-
mealType,
|
|
93
|
-
shiftName: shift.name,
|
|
94
|
-
time: timeStr,
|
|
95
|
-
availableSeats: mealData.maxCapacity - guestCount,
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
} else {
|
|
99
|
-
// If shifts are not enabled or shifts array is empty/invalid, calculate guest counts at intervals
|
|
100
|
-
const startTime = mealData.startTime;
|
|
101
|
-
const endTime = mealData.endTime;
|
|
102
|
-
|
|
103
|
-
// Determine if endTime should be included
|
|
104
|
-
const includeEndTime = shouldIncludeEndTime(mealType, endTime);
|
|
105
|
-
|
|
106
|
-
// Convert startTime and endTime to minutes since midnight
|
|
107
|
-
let currentTime = parseTime(startTime);
|
|
108
|
-
const endTimeMinutes = parseTime(endTime);
|
|
109
|
-
|
|
110
|
-
while (includeEndTime ? currentTime <= endTimeMinutes : currentTime < endTimeMinutes) {
|
|
111
|
-
// Convert currentTime back to "HH:MM" format
|
|
112
|
-
const hours = Math.floor(currentTime / 60).toString().padStart(2, '0');
|
|
113
|
-
const minutes = (currentTime % 60).toString().padStart(2, '0');
|
|
114
|
-
const timeStr = `${hours}:${minutes}`;
|
|
115
|
-
|
|
116
|
-
// Get guest count at this time
|
|
117
|
-
const guestCount = getGuestCountAtHour(data, reservations, timeStr, dateStr);
|
|
118
|
-
|
|
119
|
-
// Store in guestCounts
|
|
120
|
-
guestCounts[timeStr] = mealData.maxCapacity - guestCount;
|
|
121
|
-
|
|
122
|
-
// Increment currentTime by 'intervalReservatie' minutes
|
|
123
|
-
currentTime += intervalReservatie;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
return { guestCounts, shiftsInfo };
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
module.exports = {
|
|
131
|
-
getGuestCountsForMeal,
|
|
132
|
-
getMealTypesWithShifts, // Exported
|
|
133
|
-
};
|
|
1
|
+
// mealTypeCount.js
|
|
2
|
+
|
|
3
|
+
const { getDataByDateAndMealWithExceptions } = require('../restaurant_data/exceptions');
|
|
4
|
+
const { parseTime } = require('../restaurant_data/openinghours');
|
|
5
|
+
const { getGuestCountAtHour } = require('../reservation_data/counter');
|
|
6
|
+
|
|
7
|
+
function getInterval(data) {
|
|
8
|
+
let intervalReservatie = 15;
|
|
9
|
+
if (
|
|
10
|
+
data['general-settings'] &&
|
|
11
|
+
data['general-settings'].intervalReservatie &&
|
|
12
|
+
parseInt(data['general-settings'].intervalReservatie, 10) > 0
|
|
13
|
+
) {
|
|
14
|
+
intervalReservatie = parseInt(data['general-settings'].intervalReservatie, 10);
|
|
15
|
+
}
|
|
16
|
+
return intervalReservatie;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Retrieves meal types with shifts enabled and at least one shift defined for the specified date.
|
|
21
|
+
* @param {Object} data - The main data object.
|
|
22
|
+
* @param {string} dateStr - The date string (YYYY-MM-DD).
|
|
23
|
+
* @returns {Array} - An array of meal types with shifts.
|
|
24
|
+
*/
|
|
25
|
+
function getMealTypesWithShifts(data, dateStr) {
|
|
26
|
+
const mealTypes = ['breakfast', 'lunch', 'dinner'];
|
|
27
|
+
const mealTypesWithShifts = [];
|
|
28
|
+
|
|
29
|
+
for (const mealType of mealTypes) {
|
|
30
|
+
const mealData = getDataByDateAndMealWithExceptions(data, dateStr, mealType);
|
|
31
|
+
if (
|
|
32
|
+
mealData &&
|
|
33
|
+
mealData.shiftsEnabled &&
|
|
34
|
+
Array.isArray(mealData.shifts) &&
|
|
35
|
+
mealData.shifts.length > 0
|
|
36
|
+
) {
|
|
37
|
+
mealTypesWithShifts.push(mealType);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return mealTypesWithShifts;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function shouldIncludeEndTime(mealType, endTime) {
|
|
45
|
+
if ((mealType === 'breakfast' && endTime === '11:00') ||
|
|
46
|
+
(mealType === 'lunch' && endTime === '16:00')) {
|
|
47
|
+
return false; // Do not include endTime for breakfast at 11:00 and lunch at 16:00
|
|
48
|
+
}
|
|
49
|
+
return true; // Include endTime for all other cases
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Calculates guest counts for each interval during a meal period or at shift times.
|
|
54
|
+
* @param {Object} data - The main data object.
|
|
55
|
+
* @param {string} dateStr - The date string (YYYY-MM-DD).
|
|
56
|
+
* @param {string} mealType - The meal type ("breakfast", "lunch", "dinner").
|
|
57
|
+
* @param {Array} reservations - An array of reservation objects.
|
|
58
|
+
* @returns {Object|null} An object mapping times to guest counts, or null if meal is not available.
|
|
59
|
+
*/
|
|
60
|
+
function getGuestCountsForMeal(data, dateStr, mealType, reservations) {
|
|
61
|
+
const mealData = getDataByDateAndMealWithExceptions(data, dateStr, mealType);
|
|
62
|
+
if (!mealData) {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const guestCounts = {};
|
|
67
|
+
const shiftsInfo = [];
|
|
68
|
+
|
|
69
|
+
// Get 'intervalReservatie' from general settings, default to 15 if not set or zero
|
|
70
|
+
let intervalReservatie = getInterval(data);
|
|
71
|
+
|
|
72
|
+
// Check if shifts are enabled and shifts array has valid content
|
|
73
|
+
if (
|
|
74
|
+
mealData.shiftsEnabled &&
|
|
75
|
+
Array.isArray(mealData.shifts) &&
|
|
76
|
+
mealData.shifts.length > 0
|
|
77
|
+
) {
|
|
78
|
+
// If shifts are enabled and valid, calculate guest counts at shift times
|
|
79
|
+
const shifts = mealData.shifts; // Array of shifts
|
|
80
|
+
|
|
81
|
+
for (const shift of shifts) {
|
|
82
|
+
const timeStr = shift.time; // Time of the shift in "HH:MM" format
|
|
83
|
+
|
|
84
|
+
// Get guest count at this time
|
|
85
|
+
const guestCount = getGuestCountAtHour(data, reservations, timeStr, dateStr);
|
|
86
|
+
|
|
87
|
+
// Store in guestCounts
|
|
88
|
+
guestCounts[timeStr] = mealData.maxCapacity - guestCount;
|
|
89
|
+
|
|
90
|
+
// Store shift information
|
|
91
|
+
shiftsInfo.push({
|
|
92
|
+
mealType,
|
|
93
|
+
shiftName: shift.name,
|
|
94
|
+
time: timeStr,
|
|
95
|
+
availableSeats: mealData.maxCapacity - guestCount,
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
} else {
|
|
99
|
+
// If shifts are not enabled or shifts array is empty/invalid, calculate guest counts at intervals
|
|
100
|
+
const startTime = mealData.startTime;
|
|
101
|
+
const endTime = mealData.endTime;
|
|
102
|
+
|
|
103
|
+
// Determine if endTime should be included
|
|
104
|
+
const includeEndTime = shouldIncludeEndTime(mealType, endTime);
|
|
105
|
+
|
|
106
|
+
// Convert startTime and endTime to minutes since midnight
|
|
107
|
+
let currentTime = parseTime(startTime);
|
|
108
|
+
const endTimeMinutes = parseTime(endTime);
|
|
109
|
+
|
|
110
|
+
while (includeEndTime ? currentTime <= endTimeMinutes : currentTime < endTimeMinutes) {
|
|
111
|
+
// Convert currentTime back to "HH:MM" format
|
|
112
|
+
const hours = Math.floor(currentTime / 60).toString().padStart(2, '0');
|
|
113
|
+
const minutes = (currentTime % 60).toString().padStart(2, '0');
|
|
114
|
+
const timeStr = `${hours}:${minutes}`;
|
|
115
|
+
|
|
116
|
+
// Get guest count at this time
|
|
117
|
+
const guestCount = getGuestCountAtHour(data, reservations, timeStr, dateStr);
|
|
118
|
+
|
|
119
|
+
// Store in guestCounts
|
|
120
|
+
guestCounts[timeStr] = mealData.maxCapacity - guestCount;
|
|
121
|
+
|
|
122
|
+
// Increment currentTime by 'intervalReservatie' minutes
|
|
123
|
+
currentTime += intervalReservatie;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return { guestCounts, shiftsInfo };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
module.exports = {
|
|
131
|
+
getGuestCountsForMeal,
|
|
132
|
+
getMealTypesWithShifts, // Exported
|
|
133
|
+
};
|
|
@@ -1,183 +1,183 @@
|
|
|
1
|
-
const { getDailyGuestCounts } = require('./dailyGuestCounts');
|
|
2
|
-
const { getMealTypesWithShifts } = require('./mealTypeCount');
|
|
3
|
-
const { getDataByDateAndMealWithExceptions } = require('../restaurant_data/exceptions');
|
|
4
|
-
const { getMealTypeByTime, parseTime: parseTimeOH, daysOfWeekEnglish } = require('../restaurant_data/openinghours');
|
|
5
|
-
const moment = require('moment-timezone');
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Parses a time string in "HH:MM" format into minutes since midnight.
|
|
9
|
-
*/
|
|
10
|
-
function parseTime(timeStr) {
|
|
11
|
-
const [hours, minutes] = timeStr.split(':').map(Number);
|
|
12
|
-
return hours * 60 + minutes;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Retrieves interval and duurReservatie from general settings
|
|
17
|
-
*/
|
|
18
|
-
function getInterval(data) {
|
|
19
|
-
let intervalReservatie = 15;
|
|
20
|
-
if (
|
|
21
|
-
data['general-settings'] &&
|
|
22
|
-
data['general-settings'].intervalReservatie &&
|
|
23
|
-
parseInt(data['general-settings'].intervalReservatie, 10) > 0
|
|
24
|
-
) {
|
|
25
|
-
intervalReservatie = parseInt(data['general-settings'].intervalReservatie, 10);
|
|
26
|
-
}
|
|
27
|
-
return intervalReservatie;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function getDuurReservatie(data) {
|
|
31
|
-
let duurReservatie = 120;
|
|
32
|
-
if (
|
|
33
|
-
data['general-settings'] &&
|
|
34
|
-
data['general-settings'].duurReservatie &&
|
|
35
|
-
parseInt(data['general-settings'].duurReservatie, 10) > 0
|
|
36
|
-
) {
|
|
37
|
-
duurReservatie = parseInt(data['general-settings'].duurReservatie, 10);
|
|
38
|
-
}
|
|
39
|
-
return duurReservatie;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Checks if a time slot belongs to a meal that has the selected giftcard enabled.
|
|
44
|
-
*/
|
|
45
|
-
function timeHasGiftcard(data, dateStr, timeStr, giftcard) {
|
|
46
|
-
if (!giftcard || (typeof giftcard === 'string' && !giftcard.trim())) {
|
|
47
|
-
return true; // No giftcard => no restriction
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const mealType = getMealTypeByTime(timeStr);
|
|
51
|
-
if (!mealType) return false;
|
|
52
|
-
|
|
53
|
-
const m = moment.tz(dateStr, 'YYYY-MM-DD', 'Europe/Brussels');
|
|
54
|
-
if (!m.isValid()) return false;
|
|
55
|
-
const englishDay = daysOfWeekEnglish[m.day()];
|
|
56
|
-
|
|
57
|
-
const ohKey = `openinghours-${mealType}`;
|
|
58
|
-
const daySettings =
|
|
59
|
-
data[ohKey] &&
|
|
60
|
-
data[ohKey].schemeSettings &&
|
|
61
|
-
data[ohKey].schemeSettings[englishDay]
|
|
62
|
-
? data[ohKey].schemeSettings[englishDay]
|
|
63
|
-
: null;
|
|
64
|
-
|
|
65
|
-
if (!daySettings) return false;
|
|
66
|
-
if (daySettings.giftcardsEnabled !== true) return false;
|
|
67
|
-
if (!Array.isArray(daySettings.giftcards) || daySettings.giftcards.length === 0) return false;
|
|
68
|
-
|
|
69
|
-
return daySettings.giftcards.includes(giftcard);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Determines if a given start time plus duurReservatie fits within the meal timeframe.
|
|
74
|
-
*/
|
|
75
|
-
function fitsWithinMeal(data, dateStr, startTimeStr, duurReservatie) {
|
|
76
|
-
// Determine the meal type based on the start time
|
|
77
|
-
const mealType = getMealTypeByTime(startTimeStr);
|
|
78
|
-
if (!mealType) return false;
|
|
79
|
-
|
|
80
|
-
// Get the meal data (with exceptions applied)
|
|
81
|
-
const mealData = getDataByDateAndMealWithExceptions(data, dateStr, mealType);
|
|
82
|
-
if (!mealData) return false;
|
|
83
|
-
|
|
84
|
-
const mealStartTime = parseTime(mealData.startTime);
|
|
85
|
-
const mealEndTime = parseTime(mealData.endTime);
|
|
86
|
-
const startTime = parseTime(startTimeStr);
|
|
87
|
-
|
|
88
|
-
// Check if startTime is after or equal to mealStartTime AND startTime + duurReservatie is within the mealEndTime
|
|
89
|
-
return startTime >= mealStartTime && startTime + duurReservatie <= mealEndTime;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
function timeblocksAvailable(data, dateStr, reservations, guests, blockedSlots = [], giftcard = null, isAdmin = false, duration = null) {
|
|
93
|
-
const duurReservatie = duration && duration > 0 ? duration : getDuurReservatie(data);
|
|
94
|
-
const intervalReservatie = getInterval(data);
|
|
95
|
-
|
|
96
|
-
// Slots needed
|
|
97
|
-
const slotsNeeded = Math.ceil(duurReservatie / intervalReservatie);
|
|
98
|
-
|
|
99
|
-
// Get guest counts and shifts info
|
|
100
|
-
const { guestCounts, shiftsInfo } = getDailyGuestCounts(data, dateStr, reservations);
|
|
101
|
-
|
|
102
|
-
const availableTimeblocks = {};
|
|
103
|
-
|
|
104
|
-
// Handle shifts first
|
|
105
|
-
const mealTypesWithShifts = getMealTypesWithShifts(data, dateStr);
|
|
106
|
-
if (mealTypesWithShifts.length > 0 && shiftsInfo && shiftsInfo.length > 0) {
|
|
107
|
-
for (const shift of shiftsInfo) {
|
|
108
|
-
const { time, availableSeats } = shift;
|
|
109
|
-
if (availableSeats >= guests && fitsWithinMeal(data, dateStr, time, duurReservatie)) {
|
|
110
|
-
// Check if time matches giftcard requirement
|
|
111
|
-
if (timeHasGiftcard(data, dateStr, time, giftcard)) {
|
|
112
|
-
availableTimeblocks[time] = { name: time };
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
// Handle non-shift times
|
|
119
|
-
if (guestCounts && Object.keys(guestCounts).length > 0) {
|
|
120
|
-
const timeSlots = Object.keys(guestCounts).sort((a, b) => parseTime(a) - parseTime(b));
|
|
121
|
-
|
|
122
|
-
for (let i = 0; i <= timeSlots.length - slotsNeeded; i++) {
|
|
123
|
-
// Check capacity for all needed slots
|
|
124
|
-
let consecutiveSlotsAvailable = true;
|
|
125
|
-
if (guestCounts[timeSlots[i]] < guests) {
|
|
126
|
-
continue;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
let previousTime = parseTime(timeSlots[i]);
|
|
130
|
-
for (let j = 1; j < slotsNeeded; j++) {
|
|
131
|
-
const currentTimeSlot = timeSlots[i + j];
|
|
132
|
-
const currentTime = parseTime(currentTimeSlot);
|
|
133
|
-
|
|
134
|
-
// Check interval and capacity
|
|
135
|
-
if ((currentTime - previousTime) !== intervalReservatie || guestCounts[currentTimeSlot] < guests) {
|
|
136
|
-
consecutiveSlotsAvailable = false;
|
|
137
|
-
break;
|
|
138
|
-
}
|
|
139
|
-
previousTime = currentTime;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
// If all consecutive slots are available, check if the full duration fits
|
|
143
|
-
if (consecutiveSlotsAvailable && fitsWithinMeal(data, dateStr, timeSlots[i], duurReservatie)) {
|
|
144
|
-
// Check if time matches giftcard requirement
|
|
145
|
-
if (timeHasGiftcard(data, dateStr, timeSlots[i], giftcard)) {
|
|
146
|
-
availableTimeblocks[timeSlots[i]] = { name: timeSlots[i] };
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
// Filter out blocked time slots (skip for admin)
|
|
153
|
-
if (!isAdmin && blockedSlots && blockedSlots.length > 0) {
|
|
154
|
-
// Check if waitlist is enabled in settings (default to true if not defined)
|
|
155
|
-
// The setting key is 'waitlistEnabled' in 'general-settings'
|
|
156
|
-
const settings = data['general-settings'] || {};
|
|
157
|
-
const waitlistEnabled = settings.waitlistEnabled !== undefined ? settings.waitlistEnabled === true : true;
|
|
158
|
-
|
|
159
|
-
for (const blockedSlot of blockedSlots) {
|
|
160
|
-
// Check if the blocked slot matches the current date
|
|
161
|
-
if (blockedSlot.date === dateStr && blockedSlot.time) {
|
|
162
|
-
if (waitlistEnabled) {
|
|
163
|
-
// If waitlist is enabled, mark it as a waitlist item instead of deleting
|
|
164
|
-
// We force it into the list even if it wasn't there (e.g. if it was full)
|
|
165
|
-
// because a manual block + waitlist implies we want to capture interest for this specific blocked time.
|
|
166
|
-
availableTimeblocks[blockedSlot.time] = {
|
|
167
|
-
name: blockedSlot.time,
|
|
168
|
-
isWaitlist: true
|
|
169
|
-
};
|
|
170
|
-
} else {
|
|
171
|
-
// Default behavior: Remove the blocked time from available timeblocks
|
|
172
|
-
delete availableTimeblocks[blockedSlot.time];
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
return availableTimeblocks;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
module.exports = {
|
|
182
|
-
timeblocksAvailable,
|
|
1
|
+
const { getDailyGuestCounts } = require('./dailyGuestCounts');
|
|
2
|
+
const { getMealTypesWithShifts } = require('./mealTypeCount');
|
|
3
|
+
const { getDataByDateAndMealWithExceptions } = require('../restaurant_data/exceptions');
|
|
4
|
+
const { getMealTypeByTime, parseTime: parseTimeOH, daysOfWeekEnglish } = require('../restaurant_data/openinghours');
|
|
5
|
+
const moment = require('moment-timezone');
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Parses a time string in "HH:MM" format into minutes since midnight.
|
|
9
|
+
*/
|
|
10
|
+
function parseTime(timeStr) {
|
|
11
|
+
const [hours, minutes] = timeStr.split(':').map(Number);
|
|
12
|
+
return hours * 60 + minutes;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Retrieves interval and duurReservatie from general settings
|
|
17
|
+
*/
|
|
18
|
+
function getInterval(data) {
|
|
19
|
+
let intervalReservatie = 15;
|
|
20
|
+
if (
|
|
21
|
+
data['general-settings'] &&
|
|
22
|
+
data['general-settings'].intervalReservatie &&
|
|
23
|
+
parseInt(data['general-settings'].intervalReservatie, 10) > 0
|
|
24
|
+
) {
|
|
25
|
+
intervalReservatie = parseInt(data['general-settings'].intervalReservatie, 10);
|
|
26
|
+
}
|
|
27
|
+
return intervalReservatie;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function getDuurReservatie(data) {
|
|
31
|
+
let duurReservatie = 120;
|
|
32
|
+
if (
|
|
33
|
+
data['general-settings'] &&
|
|
34
|
+
data['general-settings'].duurReservatie &&
|
|
35
|
+
parseInt(data['general-settings'].duurReservatie, 10) > 0
|
|
36
|
+
) {
|
|
37
|
+
duurReservatie = parseInt(data['general-settings'].duurReservatie, 10);
|
|
38
|
+
}
|
|
39
|
+
return duurReservatie;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Checks if a time slot belongs to a meal that has the selected giftcard enabled.
|
|
44
|
+
*/
|
|
45
|
+
function timeHasGiftcard(data, dateStr, timeStr, giftcard) {
|
|
46
|
+
if (!giftcard || (typeof giftcard === 'string' && !giftcard.trim())) {
|
|
47
|
+
return true; // No giftcard => no restriction
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const mealType = getMealTypeByTime(timeStr);
|
|
51
|
+
if (!mealType) return false;
|
|
52
|
+
|
|
53
|
+
const m = moment.tz(dateStr, 'YYYY-MM-DD', 'Europe/Brussels');
|
|
54
|
+
if (!m.isValid()) return false;
|
|
55
|
+
const englishDay = daysOfWeekEnglish[m.day()];
|
|
56
|
+
|
|
57
|
+
const ohKey = `openinghours-${mealType}`;
|
|
58
|
+
const daySettings =
|
|
59
|
+
data[ohKey] &&
|
|
60
|
+
data[ohKey].schemeSettings &&
|
|
61
|
+
data[ohKey].schemeSettings[englishDay]
|
|
62
|
+
? data[ohKey].schemeSettings[englishDay]
|
|
63
|
+
: null;
|
|
64
|
+
|
|
65
|
+
if (!daySettings) return false;
|
|
66
|
+
if (daySettings.giftcardsEnabled !== true) return false;
|
|
67
|
+
if (!Array.isArray(daySettings.giftcards) || daySettings.giftcards.length === 0) return false;
|
|
68
|
+
|
|
69
|
+
return daySettings.giftcards.includes(giftcard);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Determines if a given start time plus duurReservatie fits within the meal timeframe.
|
|
74
|
+
*/
|
|
75
|
+
function fitsWithinMeal(data, dateStr, startTimeStr, duurReservatie) {
|
|
76
|
+
// Determine the meal type based on the start time
|
|
77
|
+
const mealType = getMealTypeByTime(startTimeStr);
|
|
78
|
+
if (!mealType) return false;
|
|
79
|
+
|
|
80
|
+
// Get the meal data (with exceptions applied)
|
|
81
|
+
const mealData = getDataByDateAndMealWithExceptions(data, dateStr, mealType);
|
|
82
|
+
if (!mealData) return false;
|
|
83
|
+
|
|
84
|
+
const mealStartTime = parseTime(mealData.startTime);
|
|
85
|
+
const mealEndTime = parseTime(mealData.endTime);
|
|
86
|
+
const startTime = parseTime(startTimeStr);
|
|
87
|
+
|
|
88
|
+
// Check if startTime is after or equal to mealStartTime AND startTime + duurReservatie is within the mealEndTime
|
|
89
|
+
return startTime >= mealStartTime && startTime + duurReservatie <= mealEndTime;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function timeblocksAvailable(data, dateStr, reservations, guests, blockedSlots = [], giftcard = null, isAdmin = false, duration = null) {
|
|
93
|
+
const duurReservatie = duration && duration > 0 ? duration : getDuurReservatie(data);
|
|
94
|
+
const intervalReservatie = getInterval(data);
|
|
95
|
+
|
|
96
|
+
// Slots needed
|
|
97
|
+
const slotsNeeded = Math.ceil(duurReservatie / intervalReservatie);
|
|
98
|
+
|
|
99
|
+
// Get guest counts and shifts info
|
|
100
|
+
const { guestCounts, shiftsInfo } = getDailyGuestCounts(data, dateStr, reservations);
|
|
101
|
+
|
|
102
|
+
const availableTimeblocks = {};
|
|
103
|
+
|
|
104
|
+
// Handle shifts first
|
|
105
|
+
const mealTypesWithShifts = getMealTypesWithShifts(data, dateStr);
|
|
106
|
+
if (mealTypesWithShifts.length > 0 && shiftsInfo && shiftsInfo.length > 0) {
|
|
107
|
+
for (const shift of shiftsInfo) {
|
|
108
|
+
const { time, availableSeats } = shift;
|
|
109
|
+
if (availableSeats >= guests && fitsWithinMeal(data, dateStr, time, duurReservatie)) {
|
|
110
|
+
// Check if time matches giftcard requirement
|
|
111
|
+
if (timeHasGiftcard(data, dateStr, time, giftcard)) {
|
|
112
|
+
availableTimeblocks[time] = { name: time };
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Handle non-shift times
|
|
119
|
+
if (guestCounts && Object.keys(guestCounts).length > 0) {
|
|
120
|
+
const timeSlots = Object.keys(guestCounts).sort((a, b) => parseTime(a) - parseTime(b));
|
|
121
|
+
|
|
122
|
+
for (let i = 0; i <= timeSlots.length - slotsNeeded; i++) {
|
|
123
|
+
// Check capacity for all needed slots
|
|
124
|
+
let consecutiveSlotsAvailable = true;
|
|
125
|
+
if (guestCounts[timeSlots[i]] < guests) {
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
let previousTime = parseTime(timeSlots[i]);
|
|
130
|
+
for (let j = 1; j < slotsNeeded; j++) {
|
|
131
|
+
const currentTimeSlot = timeSlots[i + j];
|
|
132
|
+
const currentTime = parseTime(currentTimeSlot);
|
|
133
|
+
|
|
134
|
+
// Check interval and capacity
|
|
135
|
+
if ((currentTime - previousTime) !== intervalReservatie || guestCounts[currentTimeSlot] < guests) {
|
|
136
|
+
consecutiveSlotsAvailable = false;
|
|
137
|
+
break;
|
|
138
|
+
}
|
|
139
|
+
previousTime = currentTime;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// If all consecutive slots are available, check if the full duration fits
|
|
143
|
+
if (consecutiveSlotsAvailable && fitsWithinMeal(data, dateStr, timeSlots[i], duurReservatie)) {
|
|
144
|
+
// Check if time matches giftcard requirement
|
|
145
|
+
if (timeHasGiftcard(data, dateStr, timeSlots[i], giftcard)) {
|
|
146
|
+
availableTimeblocks[timeSlots[i]] = { name: timeSlots[i] };
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Filter out blocked time slots (skip for admin)
|
|
153
|
+
if (!isAdmin && blockedSlots && blockedSlots.length > 0) {
|
|
154
|
+
// Check if waitlist is enabled in settings (default to true if not defined)
|
|
155
|
+
// The setting key is 'waitlistEnabled' in 'general-settings'
|
|
156
|
+
const settings = data['general-settings'] || {};
|
|
157
|
+
const waitlistEnabled = settings.waitlistEnabled !== undefined ? settings.waitlistEnabled === true : true;
|
|
158
|
+
|
|
159
|
+
for (const blockedSlot of blockedSlots) {
|
|
160
|
+
// Check if the blocked slot matches the current date
|
|
161
|
+
if (blockedSlot.date === dateStr && blockedSlot.time) {
|
|
162
|
+
if (waitlistEnabled) {
|
|
163
|
+
// If waitlist is enabled, mark it as a waitlist item instead of deleting
|
|
164
|
+
// We force it into the list even if it wasn't there (e.g. if it was full)
|
|
165
|
+
// because a manual block + waitlist implies we want to capture interest for this specific blocked time.
|
|
166
|
+
availableTimeblocks[blockedSlot.time] = {
|
|
167
|
+
name: blockedSlot.time,
|
|
168
|
+
isWaitlist: true
|
|
169
|
+
};
|
|
170
|
+
} else {
|
|
171
|
+
// Default behavior: Remove the blocked time from available timeblocks
|
|
172
|
+
delete availableTimeblocks[blockedSlot.time];
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return availableTimeblocks;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
module.exports = {
|
|
182
|
+
timeblocksAvailable,
|
|
183
183
|
};
|