@happychef/algorithm 1.0.1 → 1.0.3
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.
|
@@ -28,9 +28,11 @@ function parseDateTimeInTimeZone(dateStr, timeStr, timeZone) {
|
|
|
28
28
|
* @param {string} dateStr - The date string in "YYYY-MM-DD" format.
|
|
29
29
|
* @param {Array} reservations - An array of reservation objects.
|
|
30
30
|
* @param {number} guests - The number of guests for the reservation.
|
|
31
|
+
* @param {Array} blockedSlots - Optional array of blocked time slots to exclude.
|
|
32
|
+
* @param {string|null} giftcard - Optional giftcard to filter times by meal.
|
|
31
33
|
* @returns {Object} - Returns a pruned object of available time blocks or shifts, or an empty object if out of range.
|
|
32
34
|
*/
|
|
33
|
-
function getAvailableTimeblocks(data, dateStr, reservations, guests) {
|
|
35
|
+
function getAvailableTimeblocks(data, dateStr, reservations, guests, blockedSlots = [], giftcard = null) {
|
|
34
36
|
// Get 'uurOpVoorhand' from general settings
|
|
35
37
|
let uurOpVoorhand = 4;
|
|
36
38
|
if (
|
|
@@ -83,7 +85,7 @@ function getAvailableTimeblocks(data, dateStr, reservations, guests) {
|
|
|
83
85
|
currentTimeInTimeZone.toDateString() === targetDateInTimeZone.toDateString();
|
|
84
86
|
|
|
85
87
|
// Get available time blocks or shifts
|
|
86
|
-
const availableTimeblocks = timeblocksAvailable(data, dateStr, reservations, guests);
|
|
88
|
+
const availableTimeblocks = timeblocksAvailable(data, dateStr, reservations, guests, blockedSlots, giftcard);
|
|
87
89
|
|
|
88
90
|
// If the date is today and uurOpVoorhand is greater than zero, prune time blocks
|
|
89
91
|
if (isToday && uurOpVoorhand >= 0) {
|
package/isDateAvailable.js
CHANGED
|
@@ -57,16 +57,18 @@ function isDateWithinAllowedRange(data, dateStr) {
|
|
|
57
57
|
* @param {string} dateStr - The date string in "YYYY-MM-DD" format.
|
|
58
58
|
* @param {Array} reservations - An array of reservation objects.
|
|
59
59
|
* @param {number} guests - The number of guests for the reservation.
|
|
60
|
+
* @param {Array} blockedSlots - Optional array of blocked time slots to exclude.
|
|
61
|
+
* @param {string|null} giftcard - Optional giftcard to filter times by meal.
|
|
60
62
|
* @returns {boolean} - Returns true if the date has at least one available timeblock, false otherwise.
|
|
61
63
|
*/
|
|
62
|
-
function isDateAvailable(data, dateStr, reservations, guests) {
|
|
64
|
+
function isDateAvailable(data, dateStr, reservations, guests, blockedSlots = [], giftcard = null) {
|
|
63
65
|
// Check if date is within allowed range
|
|
64
66
|
if (!isDateWithinAllowedRange(data, dateStr)) {
|
|
65
67
|
return false;
|
|
66
68
|
}
|
|
67
69
|
|
|
68
70
|
// Get available timeblocks using the existing logic
|
|
69
|
-
const availableTimeblocks = getAvailableTimeblocks(data, dateStr, reservations, guests);
|
|
71
|
+
const availableTimeblocks = getAvailableTimeblocks(data, dateStr, reservations, guests, blockedSlots, giftcard);
|
|
70
72
|
|
|
71
73
|
// Return true only if we have at least one available timeblock
|
|
72
74
|
return Object.keys(availableTimeblocks).length > 0;
|
package/isTimeAvailable.js
CHANGED
|
@@ -7,11 +7,13 @@ const { timeblocksAvailable } = require('./processing/timeblocksAvailable');
|
|
|
7
7
|
* @param {string} timeStr - The time string in "HH:MM" format.
|
|
8
8
|
* @param {Array} reservations - An array of existing reservation objects.
|
|
9
9
|
* @param {number} guests - The number of guests for the new reservation request.
|
|
10
|
+
* @param {Array} blockedSlots - Optional array of blocked time slots to exclude.
|
|
11
|
+
* @param {string|null} giftcard - Optional giftcard to filter times by meal.
|
|
10
12
|
* @returns {boolean} - Returns true if the time is available, false otherwise.
|
|
11
13
|
*/
|
|
12
|
-
function isTimeAvailable(data, dateStr, timeStr, reservations, guests) {
|
|
14
|
+
function isTimeAvailable(data, dateStr, timeStr, reservations, guests, blockedSlots = [], giftcard = null) {
|
|
13
15
|
// Get all available timeblocks for the specified date and guest count
|
|
14
|
-
const availableTimeblocks = timeblocksAvailable(data, dateStr, reservations, guests);
|
|
16
|
+
const availableTimeblocks = timeblocksAvailable(data, dateStr, reservations, guests, blockedSlots, giftcard);
|
|
15
17
|
|
|
16
18
|
// Check if the specific timeStr is one of the available keys
|
|
17
19
|
return Object.prototype.hasOwnProperty.call(availableTimeblocks, timeStr);
|
package/package.json
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
const { getDailyGuestCounts } = require('./dailyGuestCounts');
|
|
2
2
|
const { getMealTypesWithShifts } = require('./mealTypeCount');
|
|
3
3
|
const { getDataByDateAndMealWithExceptions } = require('../restaurant_data/exceptions');
|
|
4
|
-
const { getMealTypeByTime, parseTime: parseTimeOH } = require('../restaurant_data/openinghours');
|
|
4
|
+
const { getMealTypeByTime, parseTime: parseTimeOH, daysOfWeekEnglish } = require('../restaurant_data/openinghours');
|
|
5
|
+
const moment = require('moment-timezone');
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* Parses a time string in "HH:MM" format into minutes since midnight.
|
|
@@ -38,6 +39,36 @@ function getDuurReservatie(data) {
|
|
|
38
39
|
return duurReservatie;
|
|
39
40
|
}
|
|
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
|
+
|
|
41
72
|
/**
|
|
42
73
|
* Determines if a given start time plus duurReservatie fits within the meal timeframe.
|
|
43
74
|
*/
|
|
@@ -58,7 +89,7 @@ function fitsWithinMeal(data, dateStr, startTimeStr, duurReservatie) {
|
|
|
58
89
|
return startTime >= mealStartTime && startTime + duurReservatie <= mealEndTime;
|
|
59
90
|
}
|
|
60
91
|
|
|
61
|
-
function timeblocksAvailable(data, dateStr, reservations, guests) {
|
|
92
|
+
function timeblocksAvailable(data, dateStr, reservations, guests, blockedSlots = [], giftcard = null) {
|
|
62
93
|
const duurReservatie = getDuurReservatie(data);
|
|
63
94
|
const intervalReservatie = getInterval(data);
|
|
64
95
|
|
|
@@ -76,7 +107,10 @@ function timeblocksAvailable(data, dateStr, reservations, guests) {
|
|
|
76
107
|
for (const shift of shiftsInfo) {
|
|
77
108
|
const { time, availableSeats } = shift;
|
|
78
109
|
if (availableSeats >= guests && fitsWithinMeal(data, dateStr, time, duurReservatie)) {
|
|
79
|
-
|
|
110
|
+
// Check if time matches giftcard requirement
|
|
111
|
+
if (timeHasGiftcard(data, dateStr, time, giftcard)) {
|
|
112
|
+
availableTimeblocks[time] = { name: time };
|
|
113
|
+
}
|
|
80
114
|
}
|
|
81
115
|
}
|
|
82
116
|
}
|
|
@@ -107,7 +141,21 @@ function timeblocksAvailable(data, dateStr, reservations, guests) {
|
|
|
107
141
|
|
|
108
142
|
// If all consecutive slots are available, check if the full duration fits
|
|
109
143
|
if (consecutiveSlotsAvailable && fitsWithinMeal(data, dateStr, timeSlots[i], duurReservatie)) {
|
|
110
|
-
|
|
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
|
|
153
|
+
if (blockedSlots && blockedSlots.length > 0) {
|
|
154
|
+
for (const blockedSlot of blockedSlots) {
|
|
155
|
+
// Check if the blocked slot matches the current date
|
|
156
|
+
if (blockedSlot.date === dateStr && blockedSlot.time) {
|
|
157
|
+
// Remove the blocked time from available timeblocks
|
|
158
|
+
delete availableTimeblocks[blockedSlot.time];
|
|
111
159
|
}
|
|
112
160
|
}
|
|
113
161
|
}
|