processfocus 0.1.0-next.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/LICENSE.md +28 -0
- package/README.md +14 -0
- package/dist/auth-FmXptohB.mjs +275 -0
- package/dist/auth-config/src/lib/authentication-config.d.ts +146 -0
- package/dist/auth-config/src/lib/custom-entities-config.d.ts +47 -0
- package/dist/auth-config/src/lib/policies-config.d.ts +52 -0
- package/dist/auth-config/src/lib/provider-types.d.ts +194 -0
- package/dist/auth.d.ts +4 -0
- package/dist/auth.mjs +2 -0
- package/dist/business-calendar/src/index.d.ts +10 -0
- package/dist/business-calendar/src/lib/calendar-config.d.ts +163 -0
- package/dist/business-calendar/src/lib/calendar-period.d.ts +88 -0
- package/dist/business-calendar/src/lib/calendar-service-impl.d.ts +19 -0
- package/dist/business-calendar/src/lib/calendar-service.d.ts +215 -0
- package/dist/business-calendar/src/lib/datetime-utils.d.ts +30 -0
- package/dist/business-calendar/src/lib/duration-calculator.d.ts +49 -0
- package/dist/business-calendar/src/lib/errors.d.ts +69 -0
- package/dist/business-calendar/src/lib/holiday-calculator.d.ts +61 -0
- package/dist/business-calendar/src/lib/holidays.d.ts +141 -0
- package/dist/business-calendar/src/lib/types.d.ts +110 -0
- package/dist/calendar.d.ts +1 -0
- package/dist/calendar.mjs +1204 -0
- package/dist/form-client-representation/src/index.d.ts +6 -0
- package/dist/form-client-representation/src/lib/as-client-representation.d.ts +31 -0
- package/dist/form-client-representation/src/lib/client-form-definition.d.ts +17 -0
- package/dist/form-client-representation/src/lib/extract-annotations.d.ts +20 -0
- package/dist/form-client-representation/src/lib/plugin-registry.d.ts +6 -0
- package/dist/form-client-representation/src/lib/types.d.ts +239 -0
- package/dist/form-client-representation/src/lib/walker.d.ts +68 -0
- package/dist/form-rule/src/index.d.ts +3 -0
- package/dist/form-rule/src/lib/evaluator.d.ts +2 -0
- package/dist/form-rule/src/lib/paths.d.ts +9 -0
- package/dist/form-rule/src/lib/types.d.ts +89 -0
- package/dist/form-schema/src/index.d.ts +8 -0
- package/dist/form-schema/src/lib/annotation-helpers.d.ts +8 -0
- package/dist/form-schema/src/lib/deferred-value.d.ts +10 -0
- package/dist/form-schema/src/lib/email.d.ts +23 -0
- package/dist/form-schema/src/lib/field-count.d.ts +32 -0
- package/dist/form-schema/src/lib/form-annotations.d.ts +25 -0
- package/dist/form-schema/src/lib/form-schema.d.ts +459 -0
- package/dist/form-schema/src/lib/phone.d.ts +4 -0
- package/dist/form-schema/src/lib/property-signature.d.ts +3 -0
- package/dist/form-submission-schema/src/index.d.ts +2 -0
- package/dist/form-submission-schema/src/lib/form-submission-schema.d.ts +49 -0
- package/dist/form-submission-schema/src/lib/schema-defaults.d.ts +16 -0
- package/dist/forms.d.ts +1 -0
- package/dist/forms.mjs +2 -0
- package/dist/frontend-manifest/src/index.d.ts +2 -0
- package/dist/frontend-manifest/src/lib/browser-plugin-artifact.d.ts +25 -0
- package/dist/frontend-manifest/src/lib/frontend-manifest.d.ts +83 -0
- package/dist/frontend-plugin-host/src/index.d.ts +2 -0
- package/dist/frontend-plugin-host/src/lib/frontend-plugin-host.d.ts +124 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.mjs +3780 -0
- package/dist/manifest.d.ts +2 -0
- package/dist/manifest.mjs +5 -0
- package/dist/paths-BkzLn-rG.mjs +282 -0
- package/dist/plugin.d.ts +3 -0
- package/dist/plugin.mjs +84 -0
- package/dist/process/src/lib/app-icons.d.ts +52 -0
- package/dist/process/src/lib/brands.d.ts +20 -0
- package/dist/process/src/lib/document-store.d.ts +30 -0
- package/dist/process/src/lib/execution-failure-notifications.d.ts +14 -0
- package/dist/process/src/lib/flow-context.d.ts +181 -0
- package/dist/process/src/lib/flow-path.d.ts +123 -0
- package/dist/process/src/lib/form-authoring.d.ts +19 -0
- package/dist/process/src/lib/form-decoration.d.ts +13 -0
- package/dist/process/src/lib/form-defaults.d.ts +5 -0
- package/dist/process/src/lib/form.d.ts +557 -0
- package/dist/process/src/lib/invitation.d.ts +35 -0
- package/dist/process/src/lib/list.d.ts +447 -0
- package/dist/process/src/lib/lookup-accessor.d.ts +68 -0
- package/dist/process/src/lib/node_step.d.ts +68 -0
- package/dist/process/src/lib/org-unit.d.ts +53 -0
- package/dist/process/src/lib/org-utils.d.ts +56 -0
- package/dist/process/src/lib/organisation-frontend-plugin-manifest.d.ts +33 -0
- package/dist/process/src/lib/organisation.d.ts +166 -0
- package/dist/process/src/lib/phase.d.ts +16 -0
- package/dist/process/src/lib/process.d.ts +196 -0
- package/dist/process/src/lib/public-form-branding.d.ts +27 -0
- package/dist/process/src/lib/role.d.ts +29 -0
- package/dist/process/src/lib/schedule.d.ts +239 -0
- package/dist/process/src/lib/sla.d.ts +84 -0
- package/dist/process/src/lib/stats-view.d.ts +68 -0
- package/dist/process/src/lib/step.d.ts +130 -0
- package/dist/process/src/lib/system-step-executor.d.ts +130 -0
- package/dist/process/src/lib/system-step.d.ts +248 -0
- package/dist/sdk/src/auth.d.ts +4 -0
- package/dist/sdk/src/calendar.d.ts +1 -0
- package/dist/sdk/src/forms.d.ts +1 -0
- package/dist/sdk/src/index.d.ts +26 -0
- package/dist/sdk/src/manifest.d.ts +2 -0
- package/dist/sdk/src/plugin.d.ts +3 -0
- package/dist/sdk/src/testing.d.ts +9 -0
- package/dist/src-C-909PVQ.mjs +498 -0
- package/dist/testing.d.ts +9 -0
- package/dist/testing.mjs +5 -0
- package/package.json +67 -0
|
@@ -0,0 +1,1204 @@
|
|
|
1
|
+
import { Cause, Context, Data, DateTime, Deferred, Duration, Effect, Exit, Layer, Option, Schema, SynchronizedRef } from "effect";
|
|
2
|
+
//#region ../business-calendar/src/lib/types.ts
|
|
3
|
+
/**
|
|
4
|
+
* Day of week (0 = Sunday, 6 = Saturday).
|
|
5
|
+
* Matches JavaScript Date.getDay() convention.
|
|
6
|
+
*/
|
|
7
|
+
const DayOfWeek = Schema.Int.pipe(Schema.between(0, 6), Schema.brand("DayOfWeek"));
|
|
8
|
+
/** Named weekday constants */
|
|
9
|
+
const Sunday = 0;
|
|
10
|
+
const Monday = 1;
|
|
11
|
+
const Tuesday = 2;
|
|
12
|
+
const Wednesday = 3;
|
|
13
|
+
const Thursday = 4;
|
|
14
|
+
const Friday = 5;
|
|
15
|
+
const Saturday = 6;
|
|
16
|
+
/** Array of weekdays (Mon-Fri) for convenience */
|
|
17
|
+
const Weekdays = [
|
|
18
|
+
1,
|
|
19
|
+
2,
|
|
20
|
+
3,
|
|
21
|
+
4,
|
|
22
|
+
5
|
|
23
|
+
];
|
|
24
|
+
/** Array of all days (Sun-Sat) for iteration */
|
|
25
|
+
const AllDays = [
|
|
26
|
+
0,
|
|
27
|
+
1,
|
|
28
|
+
2,
|
|
29
|
+
3,
|
|
30
|
+
4,
|
|
31
|
+
5,
|
|
32
|
+
6
|
|
33
|
+
];
|
|
34
|
+
/**
|
|
35
|
+
* Time of day with hour (0-23) and minute (0-59).
|
|
36
|
+
*/
|
|
37
|
+
const TimeOfDay = Schema.Struct({
|
|
38
|
+
hour: Schema.Int.pipe(Schema.between(0, 23)),
|
|
39
|
+
minute: Schema.Int.pipe(Schema.between(0, 59))
|
|
40
|
+
});
|
|
41
|
+
/**
|
|
42
|
+
* A time range within a day.
|
|
43
|
+
* Supports overnight ranges: if close < open, the range spans midnight.
|
|
44
|
+
* Example: { open: { hour: 22, minute: 0 }, close: { hour: 6, minute: 0 } }
|
|
45
|
+
* represents 10 PM to 6 AM the next day.
|
|
46
|
+
*/
|
|
47
|
+
const TimeRange = Schema.Struct({
|
|
48
|
+
open: TimeOfDay,
|
|
49
|
+
close: TimeOfDay
|
|
50
|
+
});
|
|
51
|
+
/**
|
|
52
|
+
* Schedule for a specific day of the week.
|
|
53
|
+
* Contains multiple time ranges to support split shifts (e.g., 9-12, 13-17).
|
|
54
|
+
*/
|
|
55
|
+
const DaySchedule = Schema.Struct({
|
|
56
|
+
day: DayOfWeek,
|
|
57
|
+
ranges: Schema.Array(TimeRange)
|
|
58
|
+
});
|
|
59
|
+
Schema.Array(DaySchedule);
|
|
60
|
+
/**
|
|
61
|
+
* Month number (1-12).
|
|
62
|
+
*/
|
|
63
|
+
const Month = Schema.Int.pipe(Schema.between(1, 12), Schema.brand("Month"));
|
|
64
|
+
/**
|
|
65
|
+
* Day of month (1-31).
|
|
66
|
+
* Note: validation doesn't check for valid days per month (e.g., Feb 30).
|
|
67
|
+
* That validation happens at calculation time.
|
|
68
|
+
*/
|
|
69
|
+
const DayOfMonth = Schema.Int.pipe(Schema.between(1, 31), Schema.brand("DayOfMonth"));
|
|
70
|
+
/**
|
|
71
|
+
* Nth occurrence (1-5, where 5 means "last" in some contexts).
|
|
72
|
+
*/
|
|
73
|
+
const NthOccurrence = Schema.Int.pipe(Schema.between(1, 5), Schema.brand("NthOccurrence"));
|
|
74
|
+
/**
|
|
75
|
+
* Convert TimeOfDay to minutes since midnight.
|
|
76
|
+
*/
|
|
77
|
+
const timeOfDayToMinutes = (time) => time.hour * 60 + time.minute;
|
|
78
|
+
/**
|
|
79
|
+
* Check if a TimeRange spans overnight (close time is before open time).
|
|
80
|
+
*/
|
|
81
|
+
const isOvernightRange = (range) => timeOfDayToMinutes(range.close) < timeOfDayToMinutes(range.open);
|
|
82
|
+
//#endregion
|
|
83
|
+
//#region ../business-calendar/src/lib/calendar-period.ts
|
|
84
|
+
/**
|
|
85
|
+
* A calendar period represents a date range with specific characteristics.
|
|
86
|
+
* Used for school terms, seasonal schedules, fiscal periods, etc.
|
|
87
|
+
*
|
|
88
|
+
* When `requireActivePeriod` is set on a calendar config, dates outside
|
|
89
|
+
* any active period of that type are considered non-business days.
|
|
90
|
+
*
|
|
91
|
+
* Examples:
|
|
92
|
+
* - School terms: { type: "school_term", name: "Term 1", isActive: true }
|
|
93
|
+
* - Shop hours: { type: "shop_season", name: "Christmas Extended", weeklySchedule: [...] }
|
|
94
|
+
* - Company schedule: { type: "fiscal_quarter", name: "Q1 2026", isActive: true }
|
|
95
|
+
*/
|
|
96
|
+
const CalendarPeriod = Schema.Struct({
|
|
97
|
+
/**
|
|
98
|
+
* Unique identifier for the period.
|
|
99
|
+
*/
|
|
100
|
+
id: Schema.String,
|
|
101
|
+
/**
|
|
102
|
+
* Type categorizes periods (e.g., "school_term", "shop_season", "fiscal_quarter").
|
|
103
|
+
* Used with `requireActivePeriod` to filter relevant periods.
|
|
104
|
+
*/
|
|
105
|
+
type: Schema.String,
|
|
106
|
+
/**
|
|
107
|
+
* Human-readable name (e.g., "Term 1", "Christmas Extended", "Q1 2026").
|
|
108
|
+
*/
|
|
109
|
+
name: Schema.String,
|
|
110
|
+
/**
|
|
111
|
+
* Start date of the period (inclusive).
|
|
112
|
+
* Stored as ISO 8601 date string for serialization.
|
|
113
|
+
*/
|
|
114
|
+
startDate: Schema.DateTimeUtcFromSelf,
|
|
115
|
+
/**
|
|
116
|
+
* End date of the period (inclusive).
|
|
117
|
+
* Stored as ISO 8601 date string for serialization.
|
|
118
|
+
*/
|
|
119
|
+
endDate: Schema.DateTimeUtcFromSelf,
|
|
120
|
+
/**
|
|
121
|
+
* Whether this period counts as "active" or "working".
|
|
122
|
+
* When requireActivePeriod is set, only dates within active periods
|
|
123
|
+
* of that type are considered business days.
|
|
124
|
+
*/
|
|
125
|
+
isActive: Schema.Boolean,
|
|
126
|
+
/**
|
|
127
|
+
* Optional weekly schedule override during this period.
|
|
128
|
+
* If provided, these hours replace the default calendar hours.
|
|
129
|
+
* Useful for extended hours, reduced hours, or different patterns.
|
|
130
|
+
*/
|
|
131
|
+
weeklySchedule: Schema.optional(Schema.Array(DaySchedule))
|
|
132
|
+
});
|
|
133
|
+
/**
|
|
134
|
+
* Schema for encoding CalendarPeriod to JSON (for database storage).
|
|
135
|
+
*/
|
|
136
|
+
const CalendarPeriodFromJson = Schema.Struct({
|
|
137
|
+
id: Schema.String,
|
|
138
|
+
type: Schema.String,
|
|
139
|
+
name: Schema.String,
|
|
140
|
+
startDate: Schema.DateTimeUtc,
|
|
141
|
+
endDate: Schema.DateTimeUtc,
|
|
142
|
+
isActive: Schema.Boolean,
|
|
143
|
+
weeklySchedule: Schema.optional(Schema.Array(DaySchedule))
|
|
144
|
+
});
|
|
145
|
+
//#endregion
|
|
146
|
+
//#region ../business-calendar/src/lib/holidays.ts
|
|
147
|
+
/**
|
|
148
|
+
* Fixed holiday: occurs on the same date every year.
|
|
149
|
+
* Example: Christmas (December 25), New Year's Day (January 1)
|
|
150
|
+
*/
|
|
151
|
+
const FixedHoliday = Schema.TaggedStruct("FixedHoliday", {
|
|
152
|
+
name: Schema.String,
|
|
153
|
+
month: Month,
|
|
154
|
+
day: DayOfMonth
|
|
155
|
+
});
|
|
156
|
+
/**
|
|
157
|
+
* Nth weekday holiday: occurs on the nth occurrence of a weekday in a month.
|
|
158
|
+
* Example: MLK Day (3rd Monday of January), Thanksgiving (4th Thursday of November)
|
|
159
|
+
*
|
|
160
|
+
* @property nth - 1st through 5th occurrence (5th means "last or 5th if exists")
|
|
161
|
+
* @property weekday - Day of week (0 = Sunday, 6 = Saturday)
|
|
162
|
+
* @property month - Month (1-12)
|
|
163
|
+
*/
|
|
164
|
+
const NthWeekdayHoliday = Schema.TaggedStruct("NthWeekdayHoliday", {
|
|
165
|
+
name: Schema.String,
|
|
166
|
+
month: Month,
|
|
167
|
+
weekday: DayOfWeek,
|
|
168
|
+
nth: NthOccurrence
|
|
169
|
+
});
|
|
170
|
+
/**
|
|
171
|
+
* Last weekday holiday: occurs on the last occurrence of a weekday in a month.
|
|
172
|
+
* Example: Memorial Day (last Monday of May)
|
|
173
|
+
*
|
|
174
|
+
* @property weekday - Day of week (0 = Sunday, 6 = Saturday)
|
|
175
|
+
* @property month - Month (1-12)
|
|
176
|
+
*/
|
|
177
|
+
const LastWeekdayHoliday = Schema.TaggedStruct("LastWeekdayHoliday", {
|
|
178
|
+
name: Schema.String,
|
|
179
|
+
month: Month,
|
|
180
|
+
weekday: DayOfWeek
|
|
181
|
+
});
|
|
182
|
+
/**
|
|
183
|
+
* Easter-relative holiday: calculated as days offset from Easter Sunday.
|
|
184
|
+
* Example: Good Friday (-2), Easter Monday (+1), Ash Wednesday (-46)
|
|
185
|
+
*
|
|
186
|
+
* @property daysOffset - Positive for after Easter, negative for before
|
|
187
|
+
*/
|
|
188
|
+
const EasterRelativeHoliday = Schema.TaggedStruct("EasterRelativeHoliday", {
|
|
189
|
+
name: Schema.String,
|
|
190
|
+
daysOffset: Schema.Int
|
|
191
|
+
});
|
|
192
|
+
/**
|
|
193
|
+
* Union of all holiday types.
|
|
194
|
+
*/
|
|
195
|
+
const Holiday = Schema.Union(FixedHoliday, NthWeekdayHoliday, LastWeekdayHoliday, EasterRelativeHoliday);
|
|
196
|
+
Schema.Struct({
|
|
197
|
+
date: Schema.DateTimeUtcFromSelf,
|
|
198
|
+
name: Schema.String,
|
|
199
|
+
rule: Schema.optional(Holiday)
|
|
200
|
+
});
|
|
201
|
+
const decodeMonth = Schema.decodeSync(Month);
|
|
202
|
+
const decodeDayOfMonth = Schema.decodeSync(DayOfMonth);
|
|
203
|
+
const decodeDayOfWeek = Schema.decodeSync(DayOfWeek);
|
|
204
|
+
const decodeNthOccurrence = Schema.decodeSync(NthOccurrence);
|
|
205
|
+
/**
|
|
206
|
+
* Create a fixed date holiday.
|
|
207
|
+
* @throws ParseError if month (1-12) or day (1-31) is out of range
|
|
208
|
+
*/
|
|
209
|
+
const fixedHoliday = (name, month, day) => ({
|
|
210
|
+
_tag: "FixedHoliday",
|
|
211
|
+
name,
|
|
212
|
+
month: decodeMonth(month),
|
|
213
|
+
day: decodeDayOfMonth(day)
|
|
214
|
+
});
|
|
215
|
+
/**
|
|
216
|
+
* Create an nth weekday holiday.
|
|
217
|
+
* @throws ParseError if month (1-12), weekday (0-6), or nth (1-5) is out of range
|
|
218
|
+
*/
|
|
219
|
+
const nthWeekdayHoliday = (name, month, weekday, nth) => ({
|
|
220
|
+
_tag: "NthWeekdayHoliday",
|
|
221
|
+
name,
|
|
222
|
+
month: decodeMonth(month),
|
|
223
|
+
weekday: decodeDayOfWeek(weekday),
|
|
224
|
+
nth: decodeNthOccurrence(nth)
|
|
225
|
+
});
|
|
226
|
+
/**
|
|
227
|
+
* Create a last weekday of month holiday.
|
|
228
|
+
* @throws ParseError if month (1-12) or weekday (0-6) is out of range
|
|
229
|
+
*/
|
|
230
|
+
const lastWeekdayHoliday = (name, month, weekday) => ({
|
|
231
|
+
_tag: "LastWeekdayHoliday",
|
|
232
|
+
name,
|
|
233
|
+
month: decodeMonth(month),
|
|
234
|
+
weekday: decodeDayOfWeek(weekday)
|
|
235
|
+
});
|
|
236
|
+
/**
|
|
237
|
+
* Create an Easter-relative holiday.
|
|
238
|
+
*/
|
|
239
|
+
const easterRelativeHoliday = (name, daysOffset) => ({
|
|
240
|
+
_tag: "EasterRelativeHoliday",
|
|
241
|
+
name,
|
|
242
|
+
daysOffset
|
|
243
|
+
});
|
|
244
|
+
/**
|
|
245
|
+
* Common US holidays.
|
|
246
|
+
*/
|
|
247
|
+
const US_HOLIDAYS = [
|
|
248
|
+
fixedHoliday("New Year's Day", 1, 1),
|
|
249
|
+
nthWeekdayHoliday("Martin Luther King Jr. Day", 1, 1, 3),
|
|
250
|
+
nthWeekdayHoliday("Presidents' Day", 2, 1, 3),
|
|
251
|
+
lastWeekdayHoliday("Memorial Day", 5, 1),
|
|
252
|
+
fixedHoliday("Independence Day", 7, 4),
|
|
253
|
+
nthWeekdayHoliday("Labor Day", 9, 1, 1),
|
|
254
|
+
nthWeekdayHoliday("Columbus Day", 10, 1, 2),
|
|
255
|
+
fixedHoliday("Veterans Day", 11, 11),
|
|
256
|
+
nthWeekdayHoliday("Thanksgiving", 11, 4, 4),
|
|
257
|
+
fixedHoliday("Christmas Day", 12, 25)
|
|
258
|
+
];
|
|
259
|
+
/**
|
|
260
|
+
* Common Dutch holidays.
|
|
261
|
+
*/
|
|
262
|
+
const NL_HOLIDAYS = [
|
|
263
|
+
fixedHoliday("Nieuwjaarsdag", 1, 1),
|
|
264
|
+
fixedHoliday("Koningsdag", 4, 27),
|
|
265
|
+
fixedHoliday("Bevrijdingsdag", 5, 5),
|
|
266
|
+
fixedHoliday("Eerste Kerstdag", 12, 25),
|
|
267
|
+
fixedHoliday("Tweede Kerstdag", 12, 26),
|
|
268
|
+
easterRelativeHoliday("Goede Vrijdag", -2),
|
|
269
|
+
easterRelativeHoliday("Eerste Paasdag", 0),
|
|
270
|
+
easterRelativeHoliday("Tweede Paasdag", 1),
|
|
271
|
+
easterRelativeHoliday("Hemelvaartsdag", 39),
|
|
272
|
+
easterRelativeHoliday("Eerste Pinksterdag", 49),
|
|
273
|
+
easterRelativeHoliday("Tweede Pinksterdag", 50)
|
|
274
|
+
];
|
|
275
|
+
/**
|
|
276
|
+
* Common NZ holidays.
|
|
277
|
+
*/
|
|
278
|
+
const NZ_HOLIDAYS = [
|
|
279
|
+
fixedHoliday("New Year's Day", 1, 1),
|
|
280
|
+
fixedHoliday("Day after New Year's Day", 1, 2),
|
|
281
|
+
fixedHoliday("Waitangi Day", 2, 6),
|
|
282
|
+
easterRelativeHoliday("Good Friday", -2),
|
|
283
|
+
easterRelativeHoliday("Easter Monday", 1),
|
|
284
|
+
fixedHoliday("Anzac Day", 4, 25),
|
|
285
|
+
nthWeekdayHoliday("Queen's Birthday", 6, 1, 1),
|
|
286
|
+
nthWeekdayHoliday("Labour Day", 10, 1, 4),
|
|
287
|
+
fixedHoliday("Christmas Day", 12, 25),
|
|
288
|
+
fixedHoliday("Boxing Day", 12, 26)
|
|
289
|
+
];
|
|
290
|
+
//#endregion
|
|
291
|
+
//#region ../business-calendar/src/lib/calendar-config.ts
|
|
292
|
+
/**
|
|
293
|
+
* Date exception: one-off override for a specific date.
|
|
294
|
+
* Takes precedence over holidays and periods.
|
|
295
|
+
*
|
|
296
|
+
* @property slots - Time ranges for this date, or "closed" to mark as non-working
|
|
297
|
+
* @property note - Optional human-readable note (e.g., "Early closing for staff party")
|
|
298
|
+
*/
|
|
299
|
+
const DateException = Schema.Struct({
|
|
300
|
+
date: Schema.DateTimeUtcFromSelf,
|
|
301
|
+
slots: Schema.Union(Schema.Array(TimeRange), Schema.Literal("closed")),
|
|
302
|
+
note: Schema.optional(Schema.String)
|
|
303
|
+
});
|
|
304
|
+
Schema.Struct({
|
|
305
|
+
date: Schema.DateTimeUtc,
|
|
306
|
+
slots: Schema.Union(Schema.Array(TimeRange), Schema.Literal("closed")),
|
|
307
|
+
note: Schema.optional(Schema.String)
|
|
308
|
+
});
|
|
309
|
+
/**
|
|
310
|
+
* Complete business calendar configuration.
|
|
311
|
+
* Combines weekly schedule, holidays, exceptions, and periods.
|
|
312
|
+
*
|
|
313
|
+
* Note: Timezone is stored at the Organisation/OrgUnit level, not here.
|
|
314
|
+
* The calendar service receives timezone from context.
|
|
315
|
+
*/
|
|
316
|
+
const BusinessCalendarConfig = Schema.Struct({
|
|
317
|
+
/**
|
|
318
|
+
* Default weekly schedule.
|
|
319
|
+
* Days without entries are considered non-working.
|
|
320
|
+
*/
|
|
321
|
+
weeklySchedule: Schema.Array(DaySchedule),
|
|
322
|
+
/**
|
|
323
|
+
* Holiday rules (fixed, nth weekday, Easter-relative, etc.).
|
|
324
|
+
* Holidays are computed for each year as needed.
|
|
325
|
+
*/
|
|
326
|
+
holidays: Schema.Array(Holiday),
|
|
327
|
+
/**
|
|
328
|
+
* One-off date overrides.
|
|
329
|
+
* Take precedence over holidays and periods.
|
|
330
|
+
*/
|
|
331
|
+
exceptions: Schema.Array(DateException),
|
|
332
|
+
/**
|
|
333
|
+
* Days of week that are non-working (0 = Sunday, 6 = Saturday).
|
|
334
|
+
* Typically [0, 6] for weekends.
|
|
335
|
+
*/
|
|
336
|
+
nonWorkingDays: Schema.Array(DayOfWeek),
|
|
337
|
+
/**
|
|
338
|
+
* Calendar periods loaded from database.
|
|
339
|
+
* Used for terms, seasons, fiscal periods, etc.
|
|
340
|
+
*/
|
|
341
|
+
periods: Schema.Array(CalendarPeriod),
|
|
342
|
+
/**
|
|
343
|
+
* If set, dates outside any active period of this type are non-business days.
|
|
344
|
+
* Example: "school_term" - only dates within active school terms are business days.
|
|
345
|
+
*/
|
|
346
|
+
requireActivePeriod: Schema.optional(Schema.String),
|
|
347
|
+
/**
|
|
348
|
+
* First day of the business week (0 = Sunday, 6 = Saturday).
|
|
349
|
+
* Defaults to Sunday (0).
|
|
350
|
+
*/
|
|
351
|
+
startDayOfWeek: Schema.optional(DayOfWeek),
|
|
352
|
+
/**
|
|
353
|
+
* IANA timezone identifier for the calendar (e.g., "Pacific/Auckland", "America/New_York").
|
|
354
|
+
* Schedule times are interpreted in this timezone.
|
|
355
|
+
* Defaults to "UTC" if not specified.
|
|
356
|
+
*/
|
|
357
|
+
timezone: Schema.optional(Schema.String)
|
|
358
|
+
});
|
|
359
|
+
/**
|
|
360
|
+
* Creates a standard Monday-Friday 9:00-17:00 schedule.
|
|
361
|
+
*/
|
|
362
|
+
const createStandardWeekdaySchedule = () => [
|
|
363
|
+
{
|
|
364
|
+
day: 1,
|
|
365
|
+
ranges: [{
|
|
366
|
+
open: {
|
|
367
|
+
hour: 9,
|
|
368
|
+
minute: 0
|
|
369
|
+
},
|
|
370
|
+
close: {
|
|
371
|
+
hour: 17,
|
|
372
|
+
minute: 0
|
|
373
|
+
}
|
|
374
|
+
}]
|
|
375
|
+
},
|
|
376
|
+
{
|
|
377
|
+
day: 2,
|
|
378
|
+
ranges: [{
|
|
379
|
+
open: {
|
|
380
|
+
hour: 9,
|
|
381
|
+
minute: 0
|
|
382
|
+
},
|
|
383
|
+
close: {
|
|
384
|
+
hour: 17,
|
|
385
|
+
minute: 0
|
|
386
|
+
}
|
|
387
|
+
}]
|
|
388
|
+
},
|
|
389
|
+
{
|
|
390
|
+
day: 3,
|
|
391
|
+
ranges: [{
|
|
392
|
+
open: {
|
|
393
|
+
hour: 9,
|
|
394
|
+
minute: 0
|
|
395
|
+
},
|
|
396
|
+
close: {
|
|
397
|
+
hour: 17,
|
|
398
|
+
minute: 0
|
|
399
|
+
}
|
|
400
|
+
}]
|
|
401
|
+
},
|
|
402
|
+
{
|
|
403
|
+
day: 4,
|
|
404
|
+
ranges: [{
|
|
405
|
+
open: {
|
|
406
|
+
hour: 9,
|
|
407
|
+
minute: 0
|
|
408
|
+
},
|
|
409
|
+
close: {
|
|
410
|
+
hour: 17,
|
|
411
|
+
minute: 0
|
|
412
|
+
}
|
|
413
|
+
}]
|
|
414
|
+
},
|
|
415
|
+
{
|
|
416
|
+
day: 5,
|
|
417
|
+
ranges: [{
|
|
418
|
+
open: {
|
|
419
|
+
hour: 9,
|
|
420
|
+
minute: 0
|
|
421
|
+
},
|
|
422
|
+
close: {
|
|
423
|
+
hour: 17,
|
|
424
|
+
minute: 0
|
|
425
|
+
}
|
|
426
|
+
}]
|
|
427
|
+
}
|
|
428
|
+
];
|
|
429
|
+
/**
|
|
430
|
+
* Creates a default business calendar with standard hours.
|
|
431
|
+
*/
|
|
432
|
+
const createDefaultConfig = (holidays = []) => ({
|
|
433
|
+
weeklySchedule: createStandardWeekdaySchedule(),
|
|
434
|
+
holidays,
|
|
435
|
+
exceptions: [],
|
|
436
|
+
nonWorkingDays: [6, 0],
|
|
437
|
+
periods: [],
|
|
438
|
+
startDayOfWeek: 0
|
|
439
|
+
});
|
|
440
|
+
//#endregion
|
|
441
|
+
//#region ../business-calendar/src/lib/calendar-service.ts
|
|
442
|
+
/**
|
|
443
|
+
* Error thrown when business calendar operations fail.
|
|
444
|
+
*/
|
|
445
|
+
var BusinessCalendarError = class extends Data.TaggedError("BusinessCalendarError") {};
|
|
446
|
+
/**
|
|
447
|
+
* Business Calendar Service tag.
|
|
448
|
+
*/
|
|
449
|
+
var BusinessCalendarService = class extends Context.Tag("processfocus/business-calendar/BusinessCalendarService")() {};
|
|
450
|
+
/**
|
|
451
|
+
* Business Calendar Config Service tag.
|
|
452
|
+
* Used to provide the configuration to the service implementation.
|
|
453
|
+
*/
|
|
454
|
+
var BusinessCalendarConfigService = class extends Context.Tag("processfocus/business-calendar/BusinessCalendarConfigService")() {};
|
|
455
|
+
Data.TaggedError("DateOutOfRangeError");
|
|
456
|
+
Data.TaggedError("NoBusinessHoursError");
|
|
457
|
+
Data.TaggedError("InvalidCalendarConfigError");
|
|
458
|
+
/**
|
|
459
|
+
* Error thrown when a date combination is invalid (e.g., Feb 30).
|
|
460
|
+
*/
|
|
461
|
+
var InvalidDateError = class extends Data.TaggedError("InvalidDateError") {};
|
|
462
|
+
/**
|
|
463
|
+
* Error thrown when a year is outside the supported range.
|
|
464
|
+
* Easter calculation is only valid for Gregorian calendar (year >= 1583).
|
|
465
|
+
*/
|
|
466
|
+
var InvalidYearError = class extends Data.TaggedError("InvalidYearError") {};
|
|
467
|
+
Data.TaggedError("ValidationError");
|
|
468
|
+
//#endregion
|
|
469
|
+
//#region ../business-calendar/src/lib/holiday-calculator.ts
|
|
470
|
+
/** Minimum year for Easter calculation (Gregorian calendar) */
|
|
471
|
+
const MIN_EASTER_YEAR = 1583;
|
|
472
|
+
/** Maximum year for reasonable calendar operations */
|
|
473
|
+
const MAX_YEAR = 9999;
|
|
474
|
+
/**
|
|
475
|
+
* Validate that a year is within the supported range.
|
|
476
|
+
*/
|
|
477
|
+
const validateYear = (year) => {
|
|
478
|
+
if (!Number.isInteger(year)) return Effect.fail(new InvalidYearError({
|
|
479
|
+
year,
|
|
480
|
+
message: `Year must be an integer, got ${year}`
|
|
481
|
+
}));
|
|
482
|
+
if (year < MIN_EASTER_YEAR) return Effect.fail(new InvalidYearError({
|
|
483
|
+
year,
|
|
484
|
+
message: `Year must be >= ${MIN_EASTER_YEAR} for Gregorian calendar, got ${year}`
|
|
485
|
+
}));
|
|
486
|
+
if (year > MAX_YEAR) return Effect.fail(new InvalidYearError({
|
|
487
|
+
year,
|
|
488
|
+
message: `Year must be <= ${MAX_YEAR}, got ${year}`
|
|
489
|
+
}));
|
|
490
|
+
return Effect.succeed(year);
|
|
491
|
+
};
|
|
492
|
+
/**
|
|
493
|
+
* Get the number of days in a month for a given year.
|
|
494
|
+
*/
|
|
495
|
+
const daysInMonth = (year, month) => {
|
|
496
|
+
return new Date(year, month, 0).getDate();
|
|
497
|
+
};
|
|
498
|
+
/**
|
|
499
|
+
* Validate that a date combination is valid.
|
|
500
|
+
*/
|
|
501
|
+
const validateDate = (year, month, day) => {
|
|
502
|
+
const maxDay = daysInMonth(year, month);
|
|
503
|
+
if (day > maxDay) return Effect.fail(new InvalidDateError({
|
|
504
|
+
year,
|
|
505
|
+
month,
|
|
506
|
+
day,
|
|
507
|
+
message: `Invalid date: ${year}-${month}-${day}. Month ${month} has only ${maxDay} days in year ${year}`
|
|
508
|
+
}));
|
|
509
|
+
return Effect.void;
|
|
510
|
+
};
|
|
511
|
+
/**
|
|
512
|
+
* Safely create a DateTime.Utc, wrapping potential errors.
|
|
513
|
+
*/
|
|
514
|
+
const safeDateTime = (year, month, day) => Effect.try({
|
|
515
|
+
try: () => DateTime.unsafeMake({
|
|
516
|
+
year,
|
|
517
|
+
month,
|
|
518
|
+
day,
|
|
519
|
+
hour: 0,
|
|
520
|
+
minute: 0,
|
|
521
|
+
second: 0,
|
|
522
|
+
millis: 0
|
|
523
|
+
}),
|
|
524
|
+
catch: (error) => new InvalidDateError({
|
|
525
|
+
year,
|
|
526
|
+
month,
|
|
527
|
+
day,
|
|
528
|
+
message: `Failed to create date: ${error}`
|
|
529
|
+
})
|
|
530
|
+
});
|
|
531
|
+
/**
|
|
532
|
+
* Calculate Easter Sunday for a given year using the Anonymous Gregorian algorithm.
|
|
533
|
+
* This is the "Meeus/Jones/Butcher" algorithm.
|
|
534
|
+
*
|
|
535
|
+
* @see https://en.wikipedia.org/wiki/Date_of_Easter#Anonymous_Gregorian_algorithm
|
|
536
|
+
*/
|
|
537
|
+
const calculateEasterSunday = (year) => Effect.gen(function* () {
|
|
538
|
+
yield* validateYear(year);
|
|
539
|
+
const a = year % 19;
|
|
540
|
+
const b = Math.floor(year / 100);
|
|
541
|
+
const c = year % 100;
|
|
542
|
+
const d = Math.floor(b / 4);
|
|
543
|
+
const e = b % 4;
|
|
544
|
+
const f = Math.floor((b + 8) / 25);
|
|
545
|
+
const g = Math.floor((b - f + 1) / 3);
|
|
546
|
+
const h = (19 * a + b - d - g + 15) % 30;
|
|
547
|
+
const i = Math.floor(c / 4);
|
|
548
|
+
const k = c % 4;
|
|
549
|
+
const l = (32 + 2 * e + 2 * i - h - k) % 7;
|
|
550
|
+
const m = Math.floor((a + 11 * h + 22 * l) / 451);
|
|
551
|
+
const month = Math.floor((h + l - 7 * m + 114) / 31);
|
|
552
|
+
const day = (h + l - 7 * m + 114) % 31 + 1;
|
|
553
|
+
return yield* safeDateTime(year, month, day);
|
|
554
|
+
});
|
|
555
|
+
/**
|
|
556
|
+
* Get the nth occurrence of a weekday in a month.
|
|
557
|
+
*
|
|
558
|
+
* @param year - The year
|
|
559
|
+
* @param month - The month (1-12)
|
|
560
|
+
* @param weekday - The day of week (0 = Sunday, 6 = Saturday)
|
|
561
|
+
* @param nth - Which occurrence (1-5)
|
|
562
|
+
* @returns The date, or undefined if nth occurrence doesn't exist
|
|
563
|
+
*/
|
|
564
|
+
const getNthWeekdayOfMonth = (year, month, weekday, nth) => Effect.gen(function* () {
|
|
565
|
+
yield* validateYear(year);
|
|
566
|
+
let date = yield* safeDateTime(year, month, 1);
|
|
567
|
+
let daysUntilWeekday = weekday - DateTime.toParts(date).weekDay;
|
|
568
|
+
if (daysUntilWeekday < 0) daysUntilWeekday += 7;
|
|
569
|
+
date = DateTime.add(date, { days: daysUntilWeekday });
|
|
570
|
+
date = DateTime.add(date, { days: (nth - 1) * 7 });
|
|
571
|
+
if (DateTime.toParts(date).month !== month) return;
|
|
572
|
+
return date;
|
|
573
|
+
});
|
|
574
|
+
/**
|
|
575
|
+
* Get the last occurrence of a weekday in a month.
|
|
576
|
+
*
|
|
577
|
+
* @param year - The year
|
|
578
|
+
* @param month - The month (1-12)
|
|
579
|
+
* @param weekday - The day of week (0 = Sunday, 6 = Saturday)
|
|
580
|
+
* @returns The date
|
|
581
|
+
*/
|
|
582
|
+
const getLastWeekdayOfMonth = (year, month, weekday) => Effect.gen(function* () {
|
|
583
|
+
yield* validateYear(year);
|
|
584
|
+
const nextMonth = month === 12 ? 1 : month + 1;
|
|
585
|
+
const nextYear = month === 12 ? year + 1 : year;
|
|
586
|
+
let date = yield* safeDateTime(nextYear, nextMonth, 1);
|
|
587
|
+
date = DateTime.add(date, { days: -1 });
|
|
588
|
+
let daysBack = DateTime.toParts(date).weekDay - weekday;
|
|
589
|
+
if (daysBack < 0) daysBack += 7;
|
|
590
|
+
return DateTime.add(date, { days: -daysBack });
|
|
591
|
+
});
|
|
592
|
+
/**
|
|
593
|
+
* Calculate a fixed holiday for a specific year.
|
|
594
|
+
*/
|
|
595
|
+
const calculateFixedHoliday = (holiday, year) => Effect.gen(function* () {
|
|
596
|
+
yield* validateYear(year);
|
|
597
|
+
yield* validateDate(year, holiday.month, holiday.day);
|
|
598
|
+
return {
|
|
599
|
+
date: yield* safeDateTime(year, holiday.month, holiday.day),
|
|
600
|
+
name: holiday.name,
|
|
601
|
+
rule: holiday
|
|
602
|
+
};
|
|
603
|
+
});
|
|
604
|
+
/**
|
|
605
|
+
* Calculate an nth weekday holiday for a specific year.
|
|
606
|
+
*/
|
|
607
|
+
const calculateNthWeekdayHoliday = (holiday, year) => Effect.gen(function* () {
|
|
608
|
+
const date = yield* getNthWeekdayOfMonth(year, holiday.month, holiday.weekday, holiday.nth);
|
|
609
|
+
if (!date) return void 0;
|
|
610
|
+
return {
|
|
611
|
+
date,
|
|
612
|
+
name: holiday.name,
|
|
613
|
+
rule: holiday
|
|
614
|
+
};
|
|
615
|
+
});
|
|
616
|
+
/**
|
|
617
|
+
* Calculate a last weekday holiday for a specific year.
|
|
618
|
+
*/
|
|
619
|
+
const calculateLastWeekdayHoliday = (holiday, year) => Effect.gen(function* () {
|
|
620
|
+
return {
|
|
621
|
+
date: yield* getLastWeekdayOfMonth(year, holiday.month, holiday.weekday),
|
|
622
|
+
name: holiday.name,
|
|
623
|
+
rule: holiday
|
|
624
|
+
};
|
|
625
|
+
});
|
|
626
|
+
/**
|
|
627
|
+
* Calculate an Easter-relative holiday for a specific year.
|
|
628
|
+
*/
|
|
629
|
+
const calculateEasterRelativeHoliday = (holiday, year) => Effect.gen(function* () {
|
|
630
|
+
const easter = yield* calculateEasterSunday(year);
|
|
631
|
+
return {
|
|
632
|
+
date: DateTime.add(easter, { days: holiday.daysOffset }),
|
|
633
|
+
name: holiday.name,
|
|
634
|
+
rule: holiday
|
|
635
|
+
};
|
|
636
|
+
});
|
|
637
|
+
/**
|
|
638
|
+
* Calculate a holiday for a specific year.
|
|
639
|
+
*/
|
|
640
|
+
const calculateHoliday = (holiday, year) => {
|
|
641
|
+
switch (holiday._tag) {
|
|
642
|
+
case "FixedHoliday": return calculateFixedHoliday(holiday, year);
|
|
643
|
+
case "NthWeekdayHoliday": return calculateNthWeekdayHoliday(holiday, year);
|
|
644
|
+
case "LastWeekdayHoliday": return calculateLastWeekdayHoliday(holiday, year);
|
|
645
|
+
case "EasterRelativeHoliday": return calculateEasterRelativeHoliday(holiday, year);
|
|
646
|
+
}
|
|
647
|
+
};
|
|
648
|
+
/**
|
|
649
|
+
* Calculate all holidays for a specific year.
|
|
650
|
+
* Returns an array of holiday instances sorted by date.
|
|
651
|
+
*/
|
|
652
|
+
const calculateHolidaysForYear = (holidays, year) => Effect.gen(function* () {
|
|
653
|
+
const instances = [];
|
|
654
|
+
for (const holiday of holidays) {
|
|
655
|
+
const instance = yield* calculateHoliday(holiday, year);
|
|
656
|
+
if (instance) instances.push(instance);
|
|
657
|
+
}
|
|
658
|
+
return instances.sort((a, b) => {
|
|
659
|
+
return DateTime.toEpochMillis(a.date) - DateTime.toEpochMillis(b.date);
|
|
660
|
+
});
|
|
661
|
+
});
|
|
662
|
+
/**
|
|
663
|
+
* Check if a date is a holiday.
|
|
664
|
+
* Compares only the date part (year, month, day).
|
|
665
|
+
* This is a pure function - no Effect needed.
|
|
666
|
+
*/
|
|
667
|
+
const isDateHoliday = (date, holidayInstances) => {
|
|
668
|
+
const dateParts = DateTime.toParts(date);
|
|
669
|
+
for (const instance of holidayInstances) {
|
|
670
|
+
const instanceParts = DateTime.toParts(instance.date);
|
|
671
|
+
if (dateParts.year === instanceParts.year && dateParts.month === instanceParts.month && dateParts.day === instanceParts.day) return instance;
|
|
672
|
+
}
|
|
673
|
+
};
|
|
674
|
+
//#endregion
|
|
675
|
+
//#region ../business-calendar/src/lib/calendar-service-impl.ts
|
|
676
|
+
/** Maximum number of business days to add/subtract (prevents infinite loops) */
|
|
677
|
+
const MAX_BUSINESS_DAYS = 1e4;
|
|
678
|
+
/** Maximum number of business hours to add (prevents infinite loops) */
|
|
679
|
+
const MAX_BUSINESS_HOURS = 1e5;
|
|
680
|
+
/** Maximum number of days to search for next business day */
|
|
681
|
+
const MAX_SEARCH_DAYS = 365;
|
|
682
|
+
/**
|
|
683
|
+
* Create the BusinessCalendarService implementation from a config.
|
|
684
|
+
*
|
|
685
|
+
* Prefer this at call sites that already hold a resolved config and need a
|
|
686
|
+
* service value (pass it into helpers) rather than embedding `Effect.provide`
|
|
687
|
+
* inside reusable package logic.
|
|
688
|
+
*/
|
|
689
|
+
const makeBusinessCalendarService = (config) => Effect.gen(function* () {
|
|
690
|
+
const timezone = config.timezone ? DateTime.zoneUnsafeMakeNamed(config.timezone) : DateTime.zoneUnsafeMakeNamed("UTC");
|
|
691
|
+
/**
|
|
692
|
+
* Get date/time parts in the calendar's local timezone.
|
|
693
|
+
* This is critical for correctly interpreting schedule times.
|
|
694
|
+
*/
|
|
695
|
+
const getLocalParts = (utcDateTime) => {
|
|
696
|
+
const zoned = DateTime.setZone(utcDateTime, timezone);
|
|
697
|
+
return DateTime.toParts(zoned);
|
|
698
|
+
};
|
|
699
|
+
/**
|
|
700
|
+
* Create a UTC DateTime from local date/time parts.
|
|
701
|
+
* Used when we need to return a specific time in the calendar's timezone as UTC.
|
|
702
|
+
*/
|
|
703
|
+
const makeUtcFromLocalParts = (year, month, day, hours, minutes) => {
|
|
704
|
+
const zoned = DateTime.unsafeMakeZoned({
|
|
705
|
+
year,
|
|
706
|
+
month,
|
|
707
|
+
day,
|
|
708
|
+
hours,
|
|
709
|
+
minutes,
|
|
710
|
+
seconds: 0,
|
|
711
|
+
millis: 0
|
|
712
|
+
}, {
|
|
713
|
+
timeZone: timezone,
|
|
714
|
+
adjustForTimeZone: true
|
|
715
|
+
});
|
|
716
|
+
return DateTime.toUtc(zoned);
|
|
717
|
+
};
|
|
718
|
+
/**
|
|
719
|
+
* Get holidays for a year with a success-only cache that still shares
|
|
720
|
+
* in-flight computation across concurrent callers.
|
|
721
|
+
*
|
|
722
|
+
* Unlike Effect.cachedFunction, interrupted owners drop the in-flight entry
|
|
723
|
+
* and signal abandonment to waiters (who re-enter the cache) instead of
|
|
724
|
+
* permanently memoizing Failure(interrupt) or cancelling joiners.
|
|
725
|
+
* Successful results (including intentional [] fallbacks after logged
|
|
726
|
+
* failures) are committed to the cache.
|
|
727
|
+
*/
|
|
728
|
+
const holidayCacheRef = yield* SynchronizedRef.make(/* @__PURE__ */ new Map());
|
|
729
|
+
const computeHolidaysForYear = (year) => calculateHolidaysForYear(config.holidays, year).pipe(Effect.catchAllCause((cause) => {
|
|
730
|
+
if (Cause.isInterrupted(cause) || Cause.isDie(cause)) return Effect.failCause(cause);
|
|
731
|
+
return Effect.logWarning("Failed to compute holidays for year", cause).pipe(Effect.annotateLogs({ year }), Effect.as([]));
|
|
732
|
+
}));
|
|
733
|
+
const getHolidaysForYearCached = (year) => Effect.uninterruptibleMask((restore) => Effect.gen(function* () {
|
|
734
|
+
const acquired = yield* SynchronizedRef.modifyEffect(holidayCacheRef, (map) => {
|
|
735
|
+
const existing = map.get(year);
|
|
736
|
+
if (existing !== void 0) {
|
|
737
|
+
if (existing._tag === "Done") return Effect.succeed([{
|
|
738
|
+
_tag: "hit",
|
|
739
|
+
holidays: existing.holidays
|
|
740
|
+
}, map]);
|
|
741
|
+
return Effect.succeed([{
|
|
742
|
+
_tag: "join",
|
|
743
|
+
deferred: existing.deferred
|
|
744
|
+
}, map]);
|
|
745
|
+
}
|
|
746
|
+
return Deferred.make().pipe(Effect.map((deferred) => {
|
|
747
|
+
const next = new Map(map);
|
|
748
|
+
next.set(year, {
|
|
749
|
+
_tag: "InFlight",
|
|
750
|
+
deferred
|
|
751
|
+
});
|
|
752
|
+
return [{
|
|
753
|
+
_tag: "owner",
|
|
754
|
+
deferred
|
|
755
|
+
}, next];
|
|
756
|
+
}));
|
|
757
|
+
});
|
|
758
|
+
switch (acquired._tag) {
|
|
759
|
+
case "hit": return acquired.holidays;
|
|
760
|
+
case "join": return yield* restore(Deferred.await(acquired.deferred).pipe(Effect.catchAll(() => getHolidaysForYearCached(year))));
|
|
761
|
+
case "owner": {
|
|
762
|
+
const exit = yield* Effect.exit(restore(Effect.gen(function* () {
|
|
763
|
+
yield* Effect.yieldNow();
|
|
764
|
+
return yield* computeHolidaysForYear(year);
|
|
765
|
+
})));
|
|
766
|
+
if (Exit.isSuccess(exit)) {
|
|
767
|
+
yield* Deferred.succeed(acquired.deferred, exit.value);
|
|
768
|
+
yield* SynchronizedRef.update(holidayCacheRef, (map) => {
|
|
769
|
+
const current = map.get(year);
|
|
770
|
+
if (current?._tag === "InFlight" && current.deferred === acquired.deferred) {
|
|
771
|
+
const next = new Map(map);
|
|
772
|
+
next.set(year, {
|
|
773
|
+
_tag: "Done",
|
|
774
|
+
holidays: exit.value
|
|
775
|
+
});
|
|
776
|
+
return next;
|
|
777
|
+
}
|
|
778
|
+
return map;
|
|
779
|
+
});
|
|
780
|
+
return exit.value;
|
|
781
|
+
}
|
|
782
|
+
yield* SynchronizedRef.update(holidayCacheRef, (map) => {
|
|
783
|
+
const current = map.get(year);
|
|
784
|
+
if (current?._tag === "InFlight" && current.deferred === acquired.deferred) {
|
|
785
|
+
const next = new Map(map);
|
|
786
|
+
next.delete(year);
|
|
787
|
+
return next;
|
|
788
|
+
}
|
|
789
|
+
return map;
|
|
790
|
+
});
|
|
791
|
+
yield* Deferred.fail(acquired.deferred, "abandoned");
|
|
792
|
+
return yield* Effect.failCause(exit.cause);
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
}));
|
|
796
|
+
/**
|
|
797
|
+
* Get date exception for a specific date.
|
|
798
|
+
* Compares dates in the calendar's local timezone.
|
|
799
|
+
*/
|
|
800
|
+
const getDateException = (date) => {
|
|
801
|
+
const dateParts = getLocalParts(date);
|
|
802
|
+
for (const exception of config.exceptions) {
|
|
803
|
+
const exParts = getLocalParts(exception.date);
|
|
804
|
+
if (dateParts.year === exParts.year && dateParts.month === exParts.month && dateParts.day === exParts.day) return exception;
|
|
805
|
+
}
|
|
806
|
+
};
|
|
807
|
+
/**
|
|
808
|
+
* Get periods active on a date.
|
|
809
|
+
*/
|
|
810
|
+
const getActivePeriodsForDate = (date) => {
|
|
811
|
+
const dateMillis = DateTime.toEpochMillis(date);
|
|
812
|
+
return [...config.periods].filter((period) => {
|
|
813
|
+
const startMillis = DateTime.toEpochMillis(period.startDate);
|
|
814
|
+
const endMillis = DateTime.toEpochMillis(period.endDate);
|
|
815
|
+
return period.isActive && dateMillis >= startMillis && dateMillis <= endMillis;
|
|
816
|
+
});
|
|
817
|
+
};
|
|
818
|
+
/**
|
|
819
|
+
* Check if date is within any active period of required type.
|
|
820
|
+
*/
|
|
821
|
+
const isWithinRequiredPeriod = (date) => {
|
|
822
|
+
if (!config.requireActivePeriod) return true;
|
|
823
|
+
const dateMillis = DateTime.toEpochMillis(date);
|
|
824
|
+
for (const period of config.periods) {
|
|
825
|
+
if (period.type !== config.requireActivePeriod || !period.isActive) continue;
|
|
826
|
+
const startMillis = DateTime.toEpochMillis(period.startDate);
|
|
827
|
+
const endMillis = DateTime.toEpochMillis(period.endDate);
|
|
828
|
+
if (dateMillis >= startMillis && dateMillis <= endMillis) return true;
|
|
829
|
+
}
|
|
830
|
+
return false;
|
|
831
|
+
};
|
|
832
|
+
/**
|
|
833
|
+
* Get the effective weekly schedule for a date.
|
|
834
|
+
* Priority: period override > default schedule
|
|
835
|
+
*/
|
|
836
|
+
const getEffectiveWeeklySchedule = (date) => {
|
|
837
|
+
const activePeriods = getActivePeriodsForDate(date);
|
|
838
|
+
for (const period of activePeriods) if (period.weeklySchedule) return period.weeklySchedule;
|
|
839
|
+
return config.weeklySchedule;
|
|
840
|
+
};
|
|
841
|
+
/**
|
|
842
|
+
* Get the schedule for a specific day of week.
|
|
843
|
+
*/
|
|
844
|
+
const getScheduleForDayOfWeek = (schedule, dayOfWeek) => schedule.find((s) => s.day === dayOfWeek);
|
|
845
|
+
/**
|
|
846
|
+
* Check if a time falls within a time range.
|
|
847
|
+
* Handles overnight ranges correctly.
|
|
848
|
+
*/
|
|
849
|
+
const isTimeInRange = (time, range) => {
|
|
850
|
+
const timeMinutes = timeOfDayToMinutes(time);
|
|
851
|
+
const openMinutes = timeOfDayToMinutes(range.open);
|
|
852
|
+
const closeMinutes = timeOfDayToMinutes(range.close);
|
|
853
|
+
if (isOvernightRange(range)) return timeMinutes >= openMinutes || timeMinutes < closeMinutes;
|
|
854
|
+
return timeMinutes >= openMinutes && timeMinutes < closeMinutes;
|
|
855
|
+
};
|
|
856
|
+
/**
|
|
857
|
+
* Calculate business minutes remaining in a time range from a given time.
|
|
858
|
+
*/
|
|
859
|
+
const minutesRemainingInRange = (time, range) => {
|
|
860
|
+
const timeMinutes = timeOfDayToMinutes(time);
|
|
861
|
+
const closeMinutes = timeOfDayToMinutes(range.close);
|
|
862
|
+
if (isOvernightRange(range)) {
|
|
863
|
+
if (timeMinutes >= timeOfDayToMinutes(range.open)) return 1440 - timeMinutes + closeMinutes;
|
|
864
|
+
return closeMinutes - timeMinutes;
|
|
865
|
+
}
|
|
866
|
+
return closeMinutes - timeMinutes;
|
|
867
|
+
};
|
|
868
|
+
const MINUTES_PER_DAY = 1440;
|
|
869
|
+
/**
|
|
870
|
+
* Intersection length of [aStart, aEnd) and [bStart, bEnd) in minutes.
|
|
871
|
+
*/
|
|
872
|
+
const intervalIntersectionMinutes = (aStart, aEnd, bStart, bEnd) => Math.max(0, Math.min(aEnd, bEnd) - Math.max(aStart, bStart));
|
|
873
|
+
/**
|
|
874
|
+
* Business minutes of a single range that fall within [windowStart, windowEnd)
|
|
875
|
+
* on one calendar day (minutes since local midnight). Overnight ranges contribute
|
|
876
|
+
* both the evening segment and the early-morning segment of that same day.
|
|
877
|
+
*/
|
|
878
|
+
const rangeMinutesInWindow = (range, windowStart, windowEnd) => {
|
|
879
|
+
if (windowEnd <= windowStart) return 0;
|
|
880
|
+
const openMinutes = timeOfDayToMinutes(range.open);
|
|
881
|
+
const closeMinutes = timeOfDayToMinutes(range.close);
|
|
882
|
+
if (isOvernightRange(range)) return intervalIntersectionMinutes(openMinutes, MINUTES_PER_DAY, windowStart, windowEnd) + intervalIntersectionMinutes(0, closeMinutes, windowStart, windowEnd);
|
|
883
|
+
return intervalIntersectionMinutes(openMinutes, closeMinutes, windowStart, windowEnd);
|
|
884
|
+
};
|
|
885
|
+
/**
|
|
886
|
+
* Resolve the effective time ranges for a date (exceptions before weekly schedule).
|
|
887
|
+
* Returns empty when the day has no slots. Caller must still check isBusinessDay
|
|
888
|
+
* for holidays / non-working days / required periods.
|
|
889
|
+
*/
|
|
890
|
+
const getRangesForDate = (date) => {
|
|
891
|
+
const exception = getDateException(date);
|
|
892
|
+
if (exception) {
|
|
893
|
+
if (exception.slots === "closed") return [];
|
|
894
|
+
return exception.slots;
|
|
895
|
+
}
|
|
896
|
+
const parts = getLocalParts(date);
|
|
897
|
+
const schedule = getEffectiveWeeklySchedule(date);
|
|
898
|
+
return getScheduleForDayOfWeek(schedule, parts.weekDay)?.ranges ?? [];
|
|
899
|
+
};
|
|
900
|
+
const isSameLocalDate = (a, b) => {
|
|
901
|
+
const aParts = getLocalParts(a);
|
|
902
|
+
const bParts = getLocalParts(b);
|
|
903
|
+
return aParts.year === bParts.year && aParts.month === bParts.month && aParts.day === bParts.day;
|
|
904
|
+
};
|
|
905
|
+
/**
|
|
906
|
+
* Service implementation
|
|
907
|
+
*/
|
|
908
|
+
const service = {
|
|
909
|
+
isBusinessDay: (date) => Effect.gen(function* () {
|
|
910
|
+
const parts = getLocalParts(date);
|
|
911
|
+
const exception = getDateException(date);
|
|
912
|
+
if (exception?.slots === "closed") return false;
|
|
913
|
+
if (config.nonWorkingDays.includes(parts.weekDay)) return false;
|
|
914
|
+
const holidays = yield* getHolidaysForYearCached(parts.year);
|
|
915
|
+
const civilDateForHoliday = DateTime.unsafeMake({
|
|
916
|
+
year: parts.year,
|
|
917
|
+
month: parts.month,
|
|
918
|
+
day: parts.day
|
|
919
|
+
});
|
|
920
|
+
if (isDateHoliday(civilDateForHoliday, holidays)) return false;
|
|
921
|
+
if (!isWithinRequiredPeriod(date)) return false;
|
|
922
|
+
const schedule = getEffectiveWeeklySchedule(date);
|
|
923
|
+
const daySchedule = getScheduleForDayOfWeek(schedule, parts.weekDay);
|
|
924
|
+
if (exception && typeof exception.slots !== "string") return true;
|
|
925
|
+
if (!daySchedule || daySchedule.ranges.length === 0) return false;
|
|
926
|
+
return true;
|
|
927
|
+
}),
|
|
928
|
+
isBusinessHours: (datetime) => Effect.gen(function* () {
|
|
929
|
+
if (!(yield* service.isBusinessDay(datetime))) return false;
|
|
930
|
+
const parts = getLocalParts(datetime);
|
|
931
|
+
const currentTime = {
|
|
932
|
+
hour: parts.hours,
|
|
933
|
+
minute: parts.minutes
|
|
934
|
+
};
|
|
935
|
+
const exception = getDateException(datetime);
|
|
936
|
+
if (exception && typeof exception.slots !== "string") {
|
|
937
|
+
for (const range of exception.slots) if (isTimeInRange(currentTime, range)) return true;
|
|
938
|
+
return false;
|
|
939
|
+
}
|
|
940
|
+
const schedule = getEffectiveWeeklySchedule(datetime);
|
|
941
|
+
const daySchedule = getScheduleForDayOfWeek(schedule, parts.weekDay);
|
|
942
|
+
if (!daySchedule) return false;
|
|
943
|
+
for (const range of daySchedule.ranges) if (isTimeInRange(currentTime, range)) return true;
|
|
944
|
+
return false;
|
|
945
|
+
}),
|
|
946
|
+
addBusinessDays: (date, days) => Effect.gen(function* () {
|
|
947
|
+
if (days === 0 || !Number.isFinite(days)) return date;
|
|
948
|
+
const clampedDays = Math.max(-1e4, Math.min(MAX_BUSINESS_DAYS, Math.round(days)));
|
|
949
|
+
let current = date;
|
|
950
|
+
let remaining = Math.abs(clampedDays);
|
|
951
|
+
const direction = clampedDays > 0 ? 1 : -1;
|
|
952
|
+
let iterations = 0;
|
|
953
|
+
while (remaining > 0 && iterations < MAX_BUSINESS_DAYS * 2) {
|
|
954
|
+
iterations++;
|
|
955
|
+
current = DateTime.add(current, { days: direction });
|
|
956
|
+
if (yield* service.isBusinessDay(current)) remaining--;
|
|
957
|
+
}
|
|
958
|
+
return current;
|
|
959
|
+
}),
|
|
960
|
+
addBusinessHours: (datetime, hours) => Effect.gen(function* () {
|
|
961
|
+
if (hours <= 0 || !Number.isFinite(hours)) return datetime;
|
|
962
|
+
const clampedHours = Math.min(MAX_BUSINESS_HOURS, hours);
|
|
963
|
+
let current = datetime;
|
|
964
|
+
let remainingMinutes = Math.round(clampedHours * 60);
|
|
965
|
+
let iterations = 0;
|
|
966
|
+
const maxIterations = remainingMinutes + MAX_SEARCH_DAYS * 24 * 60;
|
|
967
|
+
while (remainingMinutes > 0 && iterations < maxIterations) {
|
|
968
|
+
iterations++;
|
|
969
|
+
if (!(yield* service.isBusinessHours(current))) {
|
|
970
|
+
const nextDay = yield* service.nextBusinessDay(current);
|
|
971
|
+
const startOpt = yield* service.businessDayStart(nextDay);
|
|
972
|
+
if (Option.isNone(startOpt)) {
|
|
973
|
+
current = DateTime.add(nextDay, { days: 1 });
|
|
974
|
+
continue;
|
|
975
|
+
}
|
|
976
|
+
current = startOpt.value;
|
|
977
|
+
continue;
|
|
978
|
+
}
|
|
979
|
+
const parts = getLocalParts(current);
|
|
980
|
+
const currentTime = {
|
|
981
|
+
hour: parts.hours,
|
|
982
|
+
minute: parts.minutes
|
|
983
|
+
};
|
|
984
|
+
const exception = getDateException(current);
|
|
985
|
+
let ranges;
|
|
986
|
+
if (exception && typeof exception.slots !== "string") ranges = exception.slots;
|
|
987
|
+
else {
|
|
988
|
+
const schedule = getEffectiveWeeklySchedule(current);
|
|
989
|
+
ranges = getScheduleForDayOfWeek(schedule, parts.weekDay)?.ranges ?? [];
|
|
990
|
+
}
|
|
991
|
+
let minutesInCurrentRange = 0;
|
|
992
|
+
for (const range of ranges) if (isTimeInRange(currentTime, range)) {
|
|
993
|
+
minutesInCurrentRange = minutesRemainingInRange(currentTime, range);
|
|
994
|
+
break;
|
|
995
|
+
}
|
|
996
|
+
if (minutesInCurrentRange >= remainingMinutes) return DateTime.add(current, { minutes: remainingMinutes });
|
|
997
|
+
remainingMinutes -= minutesInCurrentRange;
|
|
998
|
+
current = DateTime.add(current, { minutes: minutesInCurrentRange });
|
|
999
|
+
const newParts = getLocalParts(current);
|
|
1000
|
+
const newTime = {
|
|
1001
|
+
hour: newParts.hours,
|
|
1002
|
+
minute: newParts.minutes
|
|
1003
|
+
};
|
|
1004
|
+
let foundNextRange = false;
|
|
1005
|
+
for (const range of ranges) {
|
|
1006
|
+
const rangeOpenMinutes = timeOfDayToMinutes(range.open);
|
|
1007
|
+
const currentMinutes = timeOfDayToMinutes(newTime);
|
|
1008
|
+
if (rangeOpenMinutes > currentMinutes && !isOvernightRange(range)) {
|
|
1009
|
+
const minutesToNextRange = rangeOpenMinutes - currentMinutes;
|
|
1010
|
+
current = DateTime.add(current, { minutes: minutesToNextRange });
|
|
1011
|
+
foundNextRange = true;
|
|
1012
|
+
break;
|
|
1013
|
+
}
|
|
1014
|
+
}
|
|
1015
|
+
if (!foundNextRange) {
|
|
1016
|
+
const nextDay = yield* service.nextBusinessDay(current);
|
|
1017
|
+
const startOpt = yield* service.businessDayStart(nextDay);
|
|
1018
|
+
if (Option.isNone(startOpt)) {
|
|
1019
|
+
current = DateTime.add(nextDay, { days: 1 });
|
|
1020
|
+
continue;
|
|
1021
|
+
}
|
|
1022
|
+
current = startOpt.value;
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
return current;
|
|
1026
|
+
}),
|
|
1027
|
+
nextBusinessDay: (date) => Effect.gen(function* () {
|
|
1028
|
+
let current = DateTime.add(date, { days: 1 });
|
|
1029
|
+
const parts = getLocalParts(current);
|
|
1030
|
+
current = makeUtcFromLocalParts(parts.year, parts.month, parts.day, 0, 0);
|
|
1031
|
+
let iterations = 0;
|
|
1032
|
+
while (iterations < MAX_SEARCH_DAYS) {
|
|
1033
|
+
iterations++;
|
|
1034
|
+
if (yield* service.isBusinessDay(current)) return current;
|
|
1035
|
+
current = DateTime.add(current, { days: 1 });
|
|
1036
|
+
}
|
|
1037
|
+
return yield* new BusinessCalendarError({ message: `No business day found within ${MAX_SEARCH_DAYS} days of ${DateTime.toUtc(date)}, possible misconfigured calendar` });
|
|
1038
|
+
}),
|
|
1039
|
+
previousBusinessDay: (date) => Effect.gen(function* () {
|
|
1040
|
+
const parts = getLocalParts(date);
|
|
1041
|
+
let current = makeUtcFromLocalParts(parts.year, parts.month, parts.day, 0, 0);
|
|
1042
|
+
current = DateTime.add(current, { days: -1 });
|
|
1043
|
+
let iterations = 0;
|
|
1044
|
+
while (iterations < MAX_SEARCH_DAYS) {
|
|
1045
|
+
iterations++;
|
|
1046
|
+
if (yield* service.isBusinessDay(current)) return current;
|
|
1047
|
+
current = DateTime.add(current, { days: -1 });
|
|
1048
|
+
}
|
|
1049
|
+
return yield* new BusinessCalendarError({ message: `No business day found within ${MAX_SEARCH_DAYS} days of ${DateTime.toUtc(date)}, possible misconfigured calendar` });
|
|
1050
|
+
}),
|
|
1051
|
+
businessDayStart: (date) => Effect.gen(function* () {
|
|
1052
|
+
if (!(yield* service.isBusinessDay(date))) return Option.none();
|
|
1053
|
+
const parts = getLocalParts(date);
|
|
1054
|
+
const exception = getDateException(date);
|
|
1055
|
+
if (exception && typeof exception.slots !== "string") {
|
|
1056
|
+
const slots = exception.slots;
|
|
1057
|
+
const firstSlot = slots[0];
|
|
1058
|
+
if (!firstSlot) return Option.none();
|
|
1059
|
+
let earliest = firstSlot.open;
|
|
1060
|
+
for (const slot of slots) if (timeOfDayToMinutes(slot.open) < timeOfDayToMinutes(earliest)) earliest = slot.open;
|
|
1061
|
+
return Option.some(makeUtcFromLocalParts(parts.year, parts.month, parts.day, earliest.hour, earliest.minute));
|
|
1062
|
+
}
|
|
1063
|
+
const schedule = getEffectiveWeeklySchedule(date);
|
|
1064
|
+
const daySchedule = getScheduleForDayOfWeek(schedule, parts.weekDay);
|
|
1065
|
+
const firstRange = daySchedule?.ranges[0];
|
|
1066
|
+
if (!daySchedule || !firstRange) return Option.none();
|
|
1067
|
+
let earliest = firstRange.open;
|
|
1068
|
+
for (const range of daySchedule.ranges) if (timeOfDayToMinutes(range.open) < timeOfDayToMinutes(earliest)) earliest = range.open;
|
|
1069
|
+
return Option.some(makeUtcFromLocalParts(parts.year, parts.month, parts.day, earliest.hour, earliest.minute));
|
|
1070
|
+
}),
|
|
1071
|
+
businessDayEnd: (date) => Effect.gen(function* () {
|
|
1072
|
+
if (!(yield* service.isBusinessDay(date))) return Option.none();
|
|
1073
|
+
const parts = getLocalParts(date);
|
|
1074
|
+
const exception = getDateException(date);
|
|
1075
|
+
if (exception && typeof exception.slots !== "string") {
|
|
1076
|
+
const slots = exception.slots;
|
|
1077
|
+
const firstSlot = slots[0];
|
|
1078
|
+
if (!firstSlot) return Option.none();
|
|
1079
|
+
let latest = firstSlot.close;
|
|
1080
|
+
for (const slot of slots) if (isOvernightRange(slot)) latest = slot.close;
|
|
1081
|
+
else if (timeOfDayToMinutes(slot.close) > timeOfDayToMinutes(latest) && !isOvernightRange({
|
|
1082
|
+
open: firstSlot.open,
|
|
1083
|
+
close: latest
|
|
1084
|
+
})) latest = slot.close;
|
|
1085
|
+
return Option.some(makeUtcFromLocalParts(parts.year, parts.month, parts.day, latest.hour, latest.minute));
|
|
1086
|
+
}
|
|
1087
|
+
const schedule = getEffectiveWeeklySchedule(date);
|
|
1088
|
+
const daySchedule = getScheduleForDayOfWeek(schedule, parts.weekDay);
|
|
1089
|
+
const firstRangeEnd = daySchedule?.ranges[0];
|
|
1090
|
+
if (!daySchedule || !firstRangeEnd) return Option.none();
|
|
1091
|
+
let latest = firstRangeEnd.close;
|
|
1092
|
+
for (const range of daySchedule.ranges) if (isOvernightRange(range)) latest = range.close;
|
|
1093
|
+
else if (timeOfDayToMinutes(range.close) > timeOfDayToMinutes(latest) && !isOvernightRange({
|
|
1094
|
+
open: firstRangeEnd.open,
|
|
1095
|
+
close: latest
|
|
1096
|
+
})) latest = range.close;
|
|
1097
|
+
return Option.some(makeUtcFromLocalParts(parts.year, parts.month, parts.day, latest.hour, latest.minute));
|
|
1098
|
+
}),
|
|
1099
|
+
businessHoursRemaining: (datetime) => Effect.gen(function* () {
|
|
1100
|
+
if (!(yield* service.isBusinessHours(datetime))) return Duration.zero;
|
|
1101
|
+
const parts = getLocalParts(datetime);
|
|
1102
|
+
const currentTime = {
|
|
1103
|
+
hour: parts.hours,
|
|
1104
|
+
minute: parts.minutes
|
|
1105
|
+
};
|
|
1106
|
+
const exception = getDateException(datetime);
|
|
1107
|
+
let ranges;
|
|
1108
|
+
if (exception && typeof exception.slots !== "string") ranges = exception.slots;
|
|
1109
|
+
else {
|
|
1110
|
+
const schedule = getEffectiveWeeklySchedule(datetime);
|
|
1111
|
+
ranges = getScheduleForDayOfWeek(schedule, parts.weekDay)?.ranges ?? [];
|
|
1112
|
+
}
|
|
1113
|
+
let totalMinutes = 0;
|
|
1114
|
+
let inCurrentRange = false;
|
|
1115
|
+
for (const range of ranges) if (isTimeInRange(currentTime, range)) {
|
|
1116
|
+
totalMinutes += minutesRemainingInRange(currentTime, range);
|
|
1117
|
+
inCurrentRange = true;
|
|
1118
|
+
} else if (inCurrentRange && timeOfDayToMinutes(range.open) > timeOfDayToMinutes(currentTime)) totalMinutes += timeOfDayToMinutes(range.close) - timeOfDayToMinutes(range.open);
|
|
1119
|
+
return Duration.minutes(totalMinutes);
|
|
1120
|
+
}),
|
|
1121
|
+
businessDaysBetween: (start, end) => Effect.gen(function* () {
|
|
1122
|
+
const startMillis = DateTime.toEpochMillis(start);
|
|
1123
|
+
const endMillis = DateTime.toEpochMillis(end);
|
|
1124
|
+
if (startMillis >= endMillis) return 0;
|
|
1125
|
+
let count = 0;
|
|
1126
|
+
let current = start;
|
|
1127
|
+
while (DateTime.toEpochMillis(current) < endMillis) {
|
|
1128
|
+
if (yield* service.isBusinessDay(current)) count++;
|
|
1129
|
+
current = DateTime.add(current, { days: 1 });
|
|
1130
|
+
}
|
|
1131
|
+
return count;
|
|
1132
|
+
}),
|
|
1133
|
+
businessHoursBetween: (start, end) => Effect.gen(function* () {
|
|
1134
|
+
if (DateTime.toEpochMillis(start) >= DateTime.toEpochMillis(end)) return Duration.zero;
|
|
1135
|
+
/**
|
|
1136
|
+
* Business minutes on a single local calendar day within
|
|
1137
|
+
* [windowStart, windowEnd) (minutes since local midnight).
|
|
1138
|
+
*/
|
|
1139
|
+
const businessMinutesOnDayInWindow = (date, windowStart, windowEnd) => Effect.gen(function* () {
|
|
1140
|
+
if (!(yield* service.isBusinessDay(date))) return 0;
|
|
1141
|
+
const ranges = getRangesForDate(date);
|
|
1142
|
+
let total = 0;
|
|
1143
|
+
for (const range of ranges) total += rangeMinutesInWindow(range, windowStart, windowEnd);
|
|
1144
|
+
return total;
|
|
1145
|
+
});
|
|
1146
|
+
const startParts = getLocalParts(start);
|
|
1147
|
+
const endParts = getLocalParts(end);
|
|
1148
|
+
const startMinuteOfDay = startParts.hours * 60 + startParts.minutes;
|
|
1149
|
+
const endMinuteOfDay = endParts.hours * 60 + endParts.minutes;
|
|
1150
|
+
if (isSameLocalDate(start, end)) {
|
|
1151
|
+
const minutes = yield* businessMinutesOnDayInWindow(start, startMinuteOfDay, endMinuteOfDay);
|
|
1152
|
+
return Duration.minutes(minutes);
|
|
1153
|
+
}
|
|
1154
|
+
let totalMinutes = 0;
|
|
1155
|
+
totalMinutes += yield* businessMinutesOnDayInWindow(start, startMinuteOfDay, MINUTES_PER_DAY);
|
|
1156
|
+
let dayCursor = makeUtcFromLocalParts(startParts.year, startParts.month, startParts.day, 12, 0);
|
|
1157
|
+
dayCursor = DateTime.add(dayCursor, { days: 1 });
|
|
1158
|
+
const endLocalNoon = makeUtcFromLocalParts(endParts.year, endParts.month, endParts.day, 12, 0);
|
|
1159
|
+
while (DateTime.toEpochMillis(dayCursor) < DateTime.toEpochMillis(endLocalNoon)) {
|
|
1160
|
+
totalMinutes += yield* businessMinutesOnDayInWindow(dayCursor, 0, MINUTES_PER_DAY);
|
|
1161
|
+
const cursorLocal = getLocalParts(dayCursor);
|
|
1162
|
+
dayCursor = DateTime.add(makeUtcFromLocalParts(cursorLocal.year, cursorLocal.month, cursorLocal.day, 12, 0), { days: 1 });
|
|
1163
|
+
}
|
|
1164
|
+
totalMinutes += yield* businessMinutesOnDayInWindow(end, 0, endMinuteOfDay);
|
|
1165
|
+
return Duration.minutes(totalMinutes);
|
|
1166
|
+
}),
|
|
1167
|
+
isHoliday: (date) => Effect.gen(function* () {
|
|
1168
|
+
const parts = getLocalParts(date);
|
|
1169
|
+
const holidays = yield* getHolidaysForYearCached(parts.year);
|
|
1170
|
+
const civilDateForHoliday = DateTime.unsafeMake({
|
|
1171
|
+
year: parts.year,
|
|
1172
|
+
month: parts.month,
|
|
1173
|
+
day: parts.day
|
|
1174
|
+
});
|
|
1175
|
+
return isDateHoliday(civilDateForHoliday, holidays) !== void 0;
|
|
1176
|
+
}),
|
|
1177
|
+
getHolidaysForYear: (year) => getHolidaysForYearCached(year),
|
|
1178
|
+
getActivePeriods: (date) => Effect.succeed(getActivePeriodsForDate(date)),
|
|
1179
|
+
getActivePeriodsByType: (date, type) => Effect.succeed(getActivePeriodsForDate(date).filter((p) => p.type === type)),
|
|
1180
|
+
isWithinPeriodType: (date, type) => Effect.succeed(getActivePeriodsForDate(date).some((p) => p.type === type)),
|
|
1181
|
+
getPeriodsForDateRange: (start, end, type) => Effect.succeed([...config.periods].filter((period) => {
|
|
1182
|
+
if (type && period.type !== type) return false;
|
|
1183
|
+
const periodStart = DateTime.toEpochMillis(period.startDate);
|
|
1184
|
+
const periodEnd = DateTime.toEpochMillis(period.endDate);
|
|
1185
|
+
const rangeStart = DateTime.toEpochMillis(start);
|
|
1186
|
+
return periodStart <= DateTime.toEpochMillis(end) && periodEnd >= rangeStart;
|
|
1187
|
+
})),
|
|
1188
|
+
getEffectiveSchedule: (date) => Effect.sync(() => {
|
|
1189
|
+
const exception = getDateException(date);
|
|
1190
|
+
if (exception) {
|
|
1191
|
+
if (exception.slots === "closed") return [];
|
|
1192
|
+
return [{
|
|
1193
|
+
day: getLocalParts(date).weekDay,
|
|
1194
|
+
ranges: [...exception.slots]
|
|
1195
|
+
}];
|
|
1196
|
+
}
|
|
1197
|
+
return [...getEffectiveWeeklySchedule(date)];
|
|
1198
|
+
})
|
|
1199
|
+
};
|
|
1200
|
+
return service;
|
|
1201
|
+
});
|
|
1202
|
+
Layer.effect(BusinessCalendarService, Effect.flatMap(BusinessCalendarConfigService, makeBusinessCalendarService));
|
|
1203
|
+
//#endregion
|
|
1204
|
+
export { AllDays, BusinessCalendarConfig, CalendarPeriod, CalendarPeriodFromJson, DateException, DayOfMonth, DayOfWeek, Friday, Holiday, Monday, Month, NL_HOLIDAYS, NZ_HOLIDAYS, NthOccurrence, Saturday, Sunday, Thursday, Tuesday, US_HOLIDAYS, Wednesday, Weekdays, createDefaultConfig, createStandardWeekdaySchedule, easterRelativeHoliday, fixedHoliday, lastWeekdayHoliday, nthWeekdayHoliday };
|