@managesome/eposmatic-toolkit 0.4.72 → 0.4.73

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.
@@ -94,14 +94,20 @@ export function getFirstShiftStartPosition(shifts, dayMidnightMs, dayIndex) {
94
94
  }
95
95
  /**
96
96
  * Whether a new business day may be started for a CLOSED day (mirrors the POS
97
- * `dayWindow` / the backend day guard): the closed day's `localDate` must be
98
- * BEHIND the tenant's current local date AND the previous day must have fully
99
- * elapsed — `now` is past the LAST end position of the shifts scheduled on the
100
- * closed day's weekday.
97
+ * `dayWindow` / the backend day guard). The closed day's `localDate` must be
98
+ * BEHIND the tenant's current local date and the closed day must have fully
99
+ * elapsed:
101
100
  *
102
- * Returns `null` when there is no anchor to decide with (no shift scheduled on
103
- * the closed day's weekday or on the next day's weekday) — callers apply their
104
- * legacy fallback (e.g. "a new day may start only when none is running").
101
+ * - the closed day has scheduled shifts → `now` is past the LAST end position
102
+ * on that weekday. The next weekday's schedule is irrelevant; it may be an
103
+ * off-day, and the following business day must still become startable.
104
+ * - the closed day has NO scheduled shifts (off-day) → `now` is at or past the
105
+ * NEXT calendar day's FIRST shift start (the next business day has entered —
106
+ * the same boundary `hasNextDayFirstShiftStarted` uses).
107
+ *
108
+ * Returns `null` only when there is no anchor at all (no shift scheduled on
109
+ * either the closed day's weekday or the next day's weekday) — callers apply
110
+ * their legacy fallback (e.g. "a new day may start only when none is running").
105
111
  */
106
112
  export function canStartNewBusinessDay(shifts, dayLocalDate, gmtOffset, now = new Date()) {
107
113
  const frame = getTenantFramePosition(gmtOffset, now);
@@ -109,14 +115,19 @@ export function canStartNewBusinessDay(shifts, dayLocalDate, gmtOffset, now = ne
109
115
  const nextDayMidnightMs = dayMidnightMs + MS_IN_DAY;
110
116
  const dayIndex = ALL_DAY_INDEX[new Date(dayMidnightMs).getUTCDay()];
111
117
  const nextDayIndex = ALL_DAY_INDEX[new Date(nextDayMidnightMs).getUTCDay()];
118
+ // A day only advances once the closed date is behind the tenant's date.
119
+ if (dayLocalDate >= formatLocalDateFromMidnight(frame.dayMidnightMs))
120
+ return false;
112
121
  const lastEndPosition = getLastShiftEndPosition(shifts, dayMidnightMs, dayIndex);
113
122
  const firstNextStartPosition = getFirstShiftStartPosition(shifts, nextDayMidnightMs, nextDayIndex);
114
- // No shift anchor → the caller applies its legacy fallback.
115
- if (lastEndPosition == null || firstNextStartPosition == null)
116
- return null;
117
- const dayBehind = dayLocalDate < formatLocalDateFromMidnight(frame.dayMidnightMs);
118
- const pastLastShiftEnd = frame.nowPos > lastEndPosition;
119
- return dayBehind && pastLastShiftEnd;
123
+ // Off-day closed day: it has no window of its own to wait for, so the next
124
+ // business day's first shift start is the anchor.
125
+ if (lastEndPosition == null) {
126
+ return firstNextStartPosition == null ? null : frame.nowPos >= firstNextStartPosition;
127
+ }
128
+ // The closed day has fully elapsed once its own last shift ended. An off-day
129
+ // on the next weekday must not postpone the following business day.
130
+ return frame.nowPos > lastEndPosition;
120
131
  }
121
132
  /**
122
133
  * Normalizes a shift time-range into coverage intervals on the
@@ -82,14 +82,20 @@ export declare function getLastShiftEndPosition(shifts: IShiftWindowSource[], da
82
82
  export declare function getFirstShiftStartPosition(shifts: IShiftWindowSource[], dayMidnightMs: number, dayIndex: DAY_INDEX_ENUM): number | null;
83
83
  /**
84
84
  * Whether a new business day may be started for a CLOSED day (mirrors the POS
85
- * `dayWindow` / the backend day guard): the closed day's `localDate` must be
86
- * BEHIND the tenant's current local date AND the previous day must have fully
87
- * elapsed — `now` is past the LAST end position of the shifts scheduled on the
88
- * closed day's weekday.
85
+ * `dayWindow` / the backend day guard). The closed day's `localDate` must be
86
+ * BEHIND the tenant's current local date and the closed day must have fully
87
+ * elapsed:
89
88
  *
90
- * Returns `null` when there is no anchor to decide with (no shift scheduled on
91
- * the closed day's weekday or on the next day's weekday) — callers apply their
92
- * legacy fallback (e.g. "a new day may start only when none is running").
89
+ * - the closed day has scheduled shifts → `now` is past the LAST end position
90
+ * on that weekday. The next weekday's schedule is irrelevant; it may be an
91
+ * off-day, and the following business day must still become startable.
92
+ * - the closed day has NO scheduled shifts (off-day) → `now` is at or past the
93
+ * NEXT calendar day's FIRST shift start (the next business day has entered —
94
+ * the same boundary `hasNextDayFirstShiftStarted` uses).
95
+ *
96
+ * Returns `null` only when there is no anchor at all (no shift scheduled on
97
+ * either the closed day's weekday or the next day's weekday) — callers apply
98
+ * their legacy fallback (e.g. "a new day may start only when none is running").
93
99
  */
94
100
  export declare function canStartNewBusinessDay(shifts: IShiftWindowSource[], dayLocalDate: string, gmtOffset: number, now?: Date): boolean | null;
95
101
  /**
@@ -110,14 +110,20 @@ function getFirstShiftStartPosition(shifts, dayMidnightMs, dayIndex) {
110
110
  }
111
111
  /**
112
112
  * Whether a new business day may be started for a CLOSED day (mirrors the POS
113
- * `dayWindow` / the backend day guard): the closed day's `localDate` must be
114
- * BEHIND the tenant's current local date AND the previous day must have fully
115
- * elapsed — `now` is past the LAST end position of the shifts scheduled on the
116
- * closed day's weekday.
113
+ * `dayWindow` / the backend day guard). The closed day's `localDate` must be
114
+ * BEHIND the tenant's current local date and the closed day must have fully
115
+ * elapsed:
117
116
  *
118
- * Returns `null` when there is no anchor to decide with (no shift scheduled on
119
- * the closed day's weekday or on the next day's weekday) — callers apply their
120
- * legacy fallback (e.g. "a new day may start only when none is running").
117
+ * - the closed day has scheduled shifts → `now` is past the LAST end position
118
+ * on that weekday. The next weekday's schedule is irrelevant; it may be an
119
+ * off-day, and the following business day must still become startable.
120
+ * - the closed day has NO scheduled shifts (off-day) → `now` is at or past the
121
+ * NEXT calendar day's FIRST shift start (the next business day has entered —
122
+ * the same boundary `hasNextDayFirstShiftStarted` uses).
123
+ *
124
+ * Returns `null` only when there is no anchor at all (no shift scheduled on
125
+ * either the closed day's weekday or the next day's weekday) — callers apply
126
+ * their legacy fallback (e.g. "a new day may start only when none is running").
121
127
  */
122
128
  function canStartNewBusinessDay(shifts, dayLocalDate, gmtOffset, now = new Date()) {
123
129
  const frame = getTenantFramePosition(gmtOffset, now);
@@ -125,14 +131,19 @@ function canStartNewBusinessDay(shifts, dayLocalDate, gmtOffset, now = new Date(
125
131
  const nextDayMidnightMs = dayMidnightMs + exports.MS_IN_DAY;
126
132
  const dayIndex = constants_1.ALL_DAY_INDEX[new Date(dayMidnightMs).getUTCDay()];
127
133
  const nextDayIndex = constants_1.ALL_DAY_INDEX[new Date(nextDayMidnightMs).getUTCDay()];
134
+ // A day only advances once the closed date is behind the tenant's date.
135
+ if (dayLocalDate >= formatLocalDateFromMidnight(frame.dayMidnightMs))
136
+ return false;
128
137
  const lastEndPosition = getLastShiftEndPosition(shifts, dayMidnightMs, dayIndex);
129
138
  const firstNextStartPosition = getFirstShiftStartPosition(shifts, nextDayMidnightMs, nextDayIndex);
130
- // No shift anchor → the caller applies its legacy fallback.
131
- if (lastEndPosition == null || firstNextStartPosition == null)
132
- return null;
133
- const dayBehind = dayLocalDate < formatLocalDateFromMidnight(frame.dayMidnightMs);
134
- const pastLastShiftEnd = frame.nowPos > lastEndPosition;
135
- return dayBehind && pastLastShiftEnd;
139
+ // Off-day closed day: it has no window of its own to wait for, so the next
140
+ // business day's first shift start is the anchor.
141
+ if (lastEndPosition == null) {
142
+ return firstNextStartPosition == null ? null : frame.nowPos >= firstNextStartPosition;
143
+ }
144
+ // The closed day has fully elapsed once its own last shift ended. An off-day
145
+ // on the next weekday must not postpone the following business day.
146
+ return frame.nowPos > lastEndPosition;
136
147
  }
137
148
  /**
138
149
  * Normalizes a shift time-range into coverage intervals on the
@@ -11,9 +11,17 @@ const shift = (start_time, end_time, day_indexes = constants_1.ALL_DAY_INDEX) =>
11
11
  end_time,
12
12
  });
13
13
  const NO_MONDAY = constants_1.ALL_DAY_INDEX.filter((day) => day !== constants_1.DAY_INDEX_ENUM.MON);
14
+ const NO_SATURDAY = constants_1.ALL_DAY_INDEX.filter((day) => day !== constants_1.DAY_INDEX_ENUM.SAT);
14
15
  // Production shift sets (Auckland Thai Lemon: Day 9:00-17:00, Night 17:00-27:00:59.999).
15
16
  const R1 = [shift(32400000, 61199999), shift(61200000, 97259999)];
16
17
  const R1_NO_MONDAY = [shift(32400000, 61199999, NO_MONDAY), shift(61200000, 97259999, NO_MONDAY)];
18
+ // Reported production shape: one 08:00 → 29:59:59.999 shift, Saturday off.
19
+ // 2026-10-31 = Saturday, 2026-11-01 = Sunday.
20
+ const R_SATURDAY_OFF = [shift(8 * HOUR, 29 * HOUR + 59 * MINUTE + 59 * 1000 + 999, NO_SATURDAY)];
21
+ // Only Wednesday is scheduled — a closed Saturday and the next-day Sunday both
22
+ // have no shift anchor.
23
+ const WEDNESDAY_ONLY = [constants_1.DAY_INDEX_ENUM.WED];
24
+ const R_WEDNESDAY_ONLY = [shift(8 * HOUR, 17 * HOUR, WEDNESDAY_ONLY)];
17
25
  // 2026-09-13 = Sunday, 2026-09-14 = Monday, 2026-09-15 = Tuesday.
18
26
  const at = (y, m, d, hh, mm, ss = 0) => new Date(Date.UTC(y, m - 1, d, hh, mm, ss));
19
27
  describe('getShiftEndPosition', () => {
@@ -67,9 +75,22 @@ describe('canStartNewBusinessDay', () => {
67
75
  it('blocks while the closed date is still the current date', () => {
68
76
  expect((0, shift_window_helpers_1.canStartNewBusinessDay)(R1, '2026-09-13', 0, at(2026, 9, 13, 23, 0))).toBe(false);
69
77
  });
70
- it('returns null when there is no shift anchor (off-day next weekday / no shifts)', () => {
71
- expect((0, shift_window_helpers_1.canStartNewBusinessDay)(R1_NO_MONDAY, '2026-09-13', 0, at(2026, 9, 14, 3, 1))).toBeNull();
78
+ it('allows after the closed day window ends even when the next weekday is an off-day', () => {
79
+ // Monday has no shifts; Sunday's night window still ends at Mon 03:00:59.999.
80
+ expect((0, shift_window_helpers_1.canStartNewBusinessDay)(R1_NO_MONDAY, '2026-09-13', 0, at(2026, 9, 14, 0, 30))).toBe(false);
81
+ expect((0, shift_window_helpers_1.canStartNewBusinessDay)(R1_NO_MONDAY, '2026-09-13', 0, at(2026, 9, 14, 3, 0, 59))).toBe(false);
82
+ expect((0, shift_window_helpers_1.canStartNewBusinessDay)(R1_NO_MONDAY, '2026-09-13', 0, at(2026, 9, 14, 3, 1))).toBe(true);
83
+ });
84
+ it('anchors an off-day closed day on the next day first shift start', () => {
85
+ // 2026-10-31 is Saturday (no Saturday shift); Sunday 2026-11-01 starts 08:00.
86
+ expect((0, shift_window_helpers_1.canStartNewBusinessDay)(R_SATURDAY_OFF, '2026-10-31', 0, at(2026, 10, 31, 23, 0))).toBe(false);
87
+ expect((0, shift_window_helpers_1.canStartNewBusinessDay)(R_SATURDAY_OFF, '2026-10-31', 0, at(2026, 11, 1, 7, 59))).toBe(false);
88
+ expect((0, shift_window_helpers_1.canStartNewBusinessDay)(R_SATURDAY_OFF, '2026-10-31', 0, at(2026, 11, 1, 8, 0))).toBe(true);
89
+ expect((0, shift_window_helpers_1.canStartNewBusinessDay)(R_SATURDAY_OFF, '2026-10-31', 0, at(2026, 11, 1, 16, 28))).toBe(true);
90
+ });
91
+ it('returns null only when neither the closed day nor the next day has a shift anchor', () => {
72
92
  expect((0, shift_window_helpers_1.canStartNewBusinessDay)([], '2026-09-13', 0, at(2026, 9, 14, 3, 1))).toBeNull();
93
+ expect((0, shift_window_helpers_1.canStartNewBusinessDay)(R_WEDNESDAY_ONLY, '2026-10-31', 0, at(2026, 11, 1, 16, 28))).toBeNull();
73
94
  });
74
95
  });
75
96
  describe('getShiftCoverageIntervals / doShiftWindowsOverlap', () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@managesome/eposmatic-toolkit",
3
- "version": "0.4.72",
3
+ "version": "0.4.73",
4
4
  "description": "A Toolkit Library for Eposmatic",
5
5
  "main": "lib/index.js",
6
6
  "module": "esm/index.js",