@jsb188/schedule 1.1.11 → 1.1.13
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 +21 -0
- package/package.json +34 -14
- package/src/constants/schedule.ts +710 -306
- package/src/modules/Gantt.helpers.ts +11 -30
- package/src/modules/Gantt.tsx +46 -181
- package/src/types/schedule.d.ts +1827 -456
- package/src/ui/ProjectFormUI.tsx +8 -31
- package/src/ui/ScheduleSettingsUI.tsx +21 -23
- package/src/utils/autofill.ts +469 -0
- package/src/utils/availability.ts +167 -0
- package/src/utils/candidates.ts +447 -0
- package/src/utils/coverage.helpers.ts +509 -554
- package/src/utils/coverage.ts +1121 -526
- package/src/utils/cpm.helpers.ts +27 -29
- package/src/utils/cpm.ts +53 -41
- package/src/utils/cpmLeveling.helpers.ts +89 -16
- package/src/utils/cpmLeveling.ts +28 -26
- package/src/utils/cycle.ts +325 -0
- package/src/utils/demand.ts +239 -0
- package/src/utils/eligibility.ts +553 -400
- package/src/utils/ids.ts +47 -0
- package/src/utils/laborRules.ts +740 -0
- package/src/utils/localTime.ts +312 -0
- package/src/utils/offer.ts +300 -0
- package/src/utils/planCsv.ts +73 -19
- package/src/utils/predicate.ts +111 -0
- package/src/utils/request.ts +212 -114
- package/src/utils/resource.ts +412 -112
- package/src/utils/rule.ts +697 -163
- package/src/utils/rulePresets.ts +116 -0
- package/src/utils/run.ts +0 -8
- package/src/utils/scheduleText.ts +4074 -0
- package/src/utils/shiftChange.ts +279 -0
- package/src/utils/standing.ts +730 -0
- package/src/utils/template.ts +619 -0
- package/src/utils/violation.ts +184 -0
- package/src/utils/workTime.ts +341 -120
- package/src/utils/defaults.ts +0 -8
- package/src/utils/recurrence.ts +0 -616
- package/src/utils/series.ts +0 -161
- package/src/utils/slot.ts +0 -208
- package/tests/coverage.fixtures.ts +0 -111
- package/tests/coverage.test.ts +0 -503
- package/tests/cpm.bench.test.ts +0 -154
- package/tests/cpm.test.ts +0 -354
- package/tests/cpmCorrectness.test.ts +0 -327
- package/tests/cpmLeveling.test.ts +0 -471
- package/tests/eligibility.test.ts +0 -320
- package/tests/gantt-geometry.test.ts +0 -367
- package/tests/gantt-histogram.test.ts +0 -78
- package/tests/gantt-keyboard.test.ts +0 -69
- package/tests/helpers/cpmFixtures.ts +0 -101
- package/tests/projectInputs.test.ts +0 -165
- package/tests/recurrence.test.ts +0 -68
- package/tests/request.test.ts +0 -99
- package/tests/resource.test.ts +0 -95
- package/tests/rule.test.ts +0 -98
- package/tests/series.test.ts +0 -120
- package/tests/workTime.test.ts +0 -1007
- package/tests/workTimeContracts.test.ts +0 -111
- package/tsconfig.json +0 -13
- package/vitest.config.ts +0 -8
|
@@ -0,0 +1,740 @@
|
|
|
1
|
+
import { SCHEDULE_RULE_KIND_ENUMS } from '../constants/schedule.ts';
|
|
2
|
+
import type {
|
|
3
|
+
CoverageShiftChangeInput,
|
|
4
|
+
CoverageShiftInput,
|
|
5
|
+
CoverageViolationObj,
|
|
6
|
+
ScheduleCoverageViolationCode,
|
|
7
|
+
ScheduleEffectiveRule,
|
|
8
|
+
ScheduleMaxConsecutiveDaysConfigObj,
|
|
9
|
+
ScheduleMaxHoursConfigObj,
|
|
10
|
+
ScheduleMinRestConfigObj,
|
|
11
|
+
ScheduleNoticePeriodConfigObj,
|
|
12
|
+
ScheduleNoticeRulesInput,
|
|
13
|
+
SchedulePersonRulesInput,
|
|
14
|
+
ScheduleShiftLengthConfigObj,
|
|
15
|
+
} from '../types/schedule.d.ts';
|
|
16
|
+
import { compareScheduleIds } from './ids.ts';
|
|
17
|
+
import {
|
|
18
|
+
addScheduleDays,
|
|
19
|
+
getScheduleLocalDate,
|
|
20
|
+
getScheduleLocalInstant,
|
|
21
|
+
getScheduleWeekStart,
|
|
22
|
+
isScheduleDateKey,
|
|
23
|
+
toScheduleTimeMs,
|
|
24
|
+
} from './localTime.ts';
|
|
25
|
+
import {
|
|
26
|
+
getEffectiveScheduleRules,
|
|
27
|
+
getScheduleConsecutiveDaysLookback,
|
|
28
|
+
} from './rule.ts';
|
|
29
|
+
import {
|
|
30
|
+
getCoverageViolationIdentity,
|
|
31
|
+
getCoverageViolationKey,
|
|
32
|
+
} from './violation.ts';
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Person rules over one person's timeline (every live seat of theirs the
|
|
36
|
+
* caller knows, in any calendar), and NOTICE_PERIOD over a calendar's seats.
|
|
37
|
+
*
|
|
38
|
+
* - Worked minutes = duration - unpaidBreakMinutes (never below 0). A seat
|
|
39
|
+
* that crosses a period boundary contributes worked x overlap / duration to
|
|
40
|
+
* each period it touches.
|
|
41
|
+
* - A finding attaches to the BREAKING seat (the later one): the rest gap's
|
|
42
|
+
* second seat, the day that goes over in a run, the latest-starting seat of
|
|
43
|
+
* a period over its hours, the seat a rolling window ends at. It is emitted
|
|
44
|
+
* only when that seat is an anchor, so a person-rule violation is stored
|
|
45
|
+
* under the calendar of the seat that breaks the rule.
|
|
46
|
+
* - DAY and PAY_WEEK read local dates in the evaluated calendar's zone;
|
|
47
|
+
* weeks start on the organization's payWeekStart. ROLLING_DAYS is a window
|
|
48
|
+
* of `days` x 24 hours ending at each seat's end. MAX_CONSECUTIVE_DAYS
|
|
49
|
+
* reads each seat's shiftDate (its own calendar's local date), so every
|
|
50
|
+
* calendar's evaluation of a person sees the same runs.
|
|
51
|
+
* - MAX_HOURS detail.periodStart is a date key for DAY and PAY_WEEK and an ISO
|
|
52
|
+
* instant for ROLLING_DAYS. When one seat is the breaking seat of several
|
|
53
|
+
* periods of one rule (a seat across midnight or a week boundary), the
|
|
54
|
+
* worst period is kept (the earliest on a tie), so keys stay unique.
|
|
55
|
+
* - Minutes in degree and detail are rounded to two decimals (the persisted
|
|
56
|
+
* NUMERIC(14, 2)); limits are compared after rounding.
|
|
57
|
+
*/
|
|
58
|
+
|
|
59
|
+
type TimelineSeat = {
|
|
60
|
+
shift: CoverageShiftInput;
|
|
61
|
+
startMs: number;
|
|
62
|
+
endMs: number;
|
|
63
|
+
workedMinutes: number;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
type SeatAnchorCheck = (shiftId: string) => boolean;
|
|
67
|
+
|
|
68
|
+
type PeriodShare = { periodStart: string; minutes: number };
|
|
69
|
+
|
|
70
|
+
const FILE_CONSTANTS = {
|
|
71
|
+
minuteMs: 60_000,
|
|
72
|
+
dayMs: 86_400_000,
|
|
73
|
+
dayMinutes: 1440,
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/*
|
|
77
|
+
* Round minutes to two decimals, the precision violations are stored at.
|
|
78
|
+
*/
|
|
79
|
+
function roundScheduleMinutes(minutes: number): number {
|
|
80
|
+
return Math.round(minutes * 100) / 100;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/*
|
|
84
|
+
* Return a seat's worked minutes: its duration minus the unpaid break, never
|
|
85
|
+
* below 0 (0 for an unreadable or empty interval).
|
|
86
|
+
*/
|
|
87
|
+
export function getScheduleShiftWorkedMinutes(
|
|
88
|
+
shift: Pick<CoverageShiftInput, 'startAt' | 'endAt' | 'unpaidBreakMinutes'>,
|
|
89
|
+
): number {
|
|
90
|
+
const minutes =
|
|
91
|
+
(toScheduleTimeMs(shift.endAt) - toScheduleTimeMs(shift.startAt)) /
|
|
92
|
+
FILE_CONSTANTS.minuteMs;
|
|
93
|
+
if (!Number.isFinite(minutes) || minutes <= 0) return 0;
|
|
94
|
+
return Math.max(0, minutes - (shift.unpaidBreakMinutes || 0));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/*
|
|
98
|
+
* Build one person's timeline: seats deduplicated by id (the first copy
|
|
99
|
+
* wins), CANCELED seats, other people's seats and unreadable intervals
|
|
100
|
+
* dropped, sorted by start then id.
|
|
101
|
+
*/
|
|
102
|
+
function getPersonTimeline(
|
|
103
|
+
resourceId: string,
|
|
104
|
+
timeline: CoverageShiftInput[],
|
|
105
|
+
): TimelineSeat[] {
|
|
106
|
+
const seen = new Set<string>();
|
|
107
|
+
const seats: TimelineSeat[] = [];
|
|
108
|
+
for (const shift of timeline) {
|
|
109
|
+
if (seen.has(shift.id)) continue;
|
|
110
|
+
seen.add(shift.id);
|
|
111
|
+
if (shift.status === 'CANCELED' || shift.assigneeResourceId !== resourceId)
|
|
112
|
+
continue;
|
|
113
|
+
const startMs = toScheduleTimeMs(shift.startAt);
|
|
114
|
+
const endMs = toScheduleTimeMs(shift.endAt);
|
|
115
|
+
if (
|
|
116
|
+
!Number.isFinite(startMs) ||
|
|
117
|
+
!Number.isFinite(endMs) ||
|
|
118
|
+
endMs <= startMs
|
|
119
|
+
)
|
|
120
|
+
continue;
|
|
121
|
+
seats.push({
|
|
122
|
+
shift,
|
|
123
|
+
startMs,
|
|
124
|
+
endMs,
|
|
125
|
+
workedMinutes: getScheduleShiftWorkedMinutes(shift),
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
return seats.sort(
|
|
129
|
+
(a, b) =>
|
|
130
|
+
a.startMs - b.startMs || compareScheduleIds(a.shift.id, b.shift.id),
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/*
|
|
135
|
+
* Build one finding of a rule on a seat, with its identity and key.
|
|
136
|
+
*/
|
|
137
|
+
function buildRuleViolation(args: {
|
|
138
|
+
rule: ScheduleEffectiveRule;
|
|
139
|
+
code: ScheduleCoverageViolationCode | null;
|
|
140
|
+
resourceId: string | null;
|
|
141
|
+
shiftId: string;
|
|
142
|
+
startMs: number;
|
|
143
|
+
endMs: number;
|
|
144
|
+
degree: number;
|
|
145
|
+
detail: Record<string, unknown>;
|
|
146
|
+
}): CoverageViolationObj {
|
|
147
|
+
const { rule, code, resourceId, shiftId } = args;
|
|
148
|
+
const identity = getCoverageViolationIdentity({
|
|
149
|
+
kind: rule.kind,
|
|
150
|
+
ruleId: rule.ruleId,
|
|
151
|
+
resourceId,
|
|
152
|
+
shiftId,
|
|
153
|
+
code,
|
|
154
|
+
});
|
|
155
|
+
const startAt = new Date(args.startMs);
|
|
156
|
+
return {
|
|
157
|
+
key: getCoverageViolationKey({ identity, startAt }),
|
|
158
|
+
identity,
|
|
159
|
+
kind: rule.kind,
|
|
160
|
+
code,
|
|
161
|
+
ruleId: rule.ruleId,
|
|
162
|
+
severity: rule.severity,
|
|
163
|
+
resourceId,
|
|
164
|
+
shiftId,
|
|
165
|
+
startAt,
|
|
166
|
+
endAt: new Date(args.endMs),
|
|
167
|
+
degree: args.degree,
|
|
168
|
+
detail: args.detail,
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/*
|
|
173
|
+
* Build a person-rule finding whose interval is the breaking seat.
|
|
174
|
+
*/
|
|
175
|
+
function buildSeatViolation(
|
|
176
|
+
rule: ScheduleEffectiveRule,
|
|
177
|
+
resourceId: string,
|
|
178
|
+
seat: TimelineSeat,
|
|
179
|
+
degree: number,
|
|
180
|
+
detail: Record<string, unknown>,
|
|
181
|
+
): CoverageViolationObj {
|
|
182
|
+
return buildRuleViolation({
|
|
183
|
+
rule,
|
|
184
|
+
code: null,
|
|
185
|
+
resourceId,
|
|
186
|
+
shiftId: seat.shift.id,
|
|
187
|
+
startMs: seat.startMs,
|
|
188
|
+
endMs: seat.endMs,
|
|
189
|
+
degree,
|
|
190
|
+
detail,
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/*
|
|
195
|
+
* SHIFT_LENGTH: each anchor seat whose duration is below minMinutes or above
|
|
196
|
+
* maxMinutes; degree = minutes short or over.
|
|
197
|
+
*/
|
|
198
|
+
function evaluateShiftLengthRule(
|
|
199
|
+
rule: ScheduleEffectiveRule,
|
|
200
|
+
resourceId: string,
|
|
201
|
+
seats: TimelineSeat[],
|
|
202
|
+
isAnchor: SeatAnchorCheck,
|
|
203
|
+
): CoverageViolationObj[] {
|
|
204
|
+
const { minMinutes, maxMinutes } =
|
|
205
|
+
rule.config as ScheduleShiftLengthConfigObj;
|
|
206
|
+
const violations: CoverageViolationObj[] = [];
|
|
207
|
+
for (const seat of seats) {
|
|
208
|
+
if (!isAnchor(seat.shift.id)) continue;
|
|
209
|
+
const durationMinutes = roundScheduleMinutes(
|
|
210
|
+
(seat.endMs - seat.startMs) / FILE_CONSTANTS.minuteMs,
|
|
211
|
+
);
|
|
212
|
+
const degree =
|
|
213
|
+
minMinutes !== null && durationMinutes < minMinutes
|
|
214
|
+
? minMinutes - durationMinutes
|
|
215
|
+
: maxMinutes !== null && durationMinutes > maxMinutes
|
|
216
|
+
? durationMinutes - maxMinutes
|
|
217
|
+
: 0;
|
|
218
|
+
if (degree > 0)
|
|
219
|
+
violations.push(
|
|
220
|
+
buildSeatViolation(
|
|
221
|
+
rule,
|
|
222
|
+
resourceId,
|
|
223
|
+
seat,
|
|
224
|
+
roundScheduleMinutes(degree),
|
|
225
|
+
{
|
|
226
|
+
durationMinutes,
|
|
227
|
+
minMinutes,
|
|
228
|
+
maxMinutes,
|
|
229
|
+
},
|
|
230
|
+
),
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
return violations;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/*
|
|
237
|
+
* MIN_REST: the gap between each seat and the latest-ending seat before it
|
|
238
|
+
* (its consecutive predecessor). A gap in [0, minMinutes) breaks on the later
|
|
239
|
+
* seat; a negative gap is an overlap, which is ELIGIBILITY's finding.
|
|
240
|
+
*/
|
|
241
|
+
function evaluateMinRestRule(
|
|
242
|
+
rule: ScheduleEffectiveRule,
|
|
243
|
+
resourceId: string,
|
|
244
|
+
seats: TimelineSeat[],
|
|
245
|
+
isAnchor: SeatAnchorCheck,
|
|
246
|
+
): CoverageViolationObj[] {
|
|
247
|
+
const { minMinutes } = rule.config as ScheduleMinRestConfigObj;
|
|
248
|
+
const violations: CoverageViolationObj[] = [];
|
|
249
|
+
let previous: TimelineSeat | null = null;
|
|
250
|
+
for (const seat of seats) {
|
|
251
|
+
if (previous && isAnchor(seat.shift.id)) {
|
|
252
|
+
const restMinutes = roundScheduleMinutes(
|
|
253
|
+
(seat.startMs - previous.endMs) / FILE_CONSTANTS.minuteMs,
|
|
254
|
+
);
|
|
255
|
+
if (restMinutes >= 0 && restMinutes < minMinutes)
|
|
256
|
+
violations.push(
|
|
257
|
+
buildSeatViolation(
|
|
258
|
+
rule,
|
|
259
|
+
resourceId,
|
|
260
|
+
seat,
|
|
261
|
+
roundScheduleMinutes(minMinutes - restMinutes),
|
|
262
|
+
{ minMinutes, restMinutes, previousShiftId: previous.shift.id },
|
|
263
|
+
),
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
if (!previous || seat.endMs > previous.endMs) previous = seat;
|
|
267
|
+
}
|
|
268
|
+
return violations;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/*
|
|
272
|
+
* Number every date that has a seat by its position in the run of
|
|
273
|
+
* consecutive dates ending on it, counting back at most `lookback` days:
|
|
274
|
+
* date -> 1..lookback + 1, where lookback + 1 means the run may be longer.
|
|
275
|
+
*/
|
|
276
|
+
function getConsecutiveDayPositions(
|
|
277
|
+
dates: string[],
|
|
278
|
+
lookback: number,
|
|
279
|
+
): Map<string, number> {
|
|
280
|
+
const positions = new Map<string, number>();
|
|
281
|
+
let previous: string | null = null;
|
|
282
|
+
for (const date of [...new Set(dates)].sort()) {
|
|
283
|
+
const prior =
|
|
284
|
+
previous !== null && addScheduleDays(previous, 1) === date
|
|
285
|
+
? positions.get(previous)!
|
|
286
|
+
: 0;
|
|
287
|
+
positions.set(date, Math.min(prior + 1, lookback + 1));
|
|
288
|
+
previous = date;
|
|
289
|
+
}
|
|
290
|
+
return positions;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/*
|
|
294
|
+
* MAX_CONSECUTIVE_DAYS: shiftDates (each seat's own calendar-local date)
|
|
295
|
+
* with any seat form runs; every anchor seat on day p > days of its run
|
|
296
|
+
* breaks, degree = p - days. A run is counted back at most
|
|
297
|
+
* getScheduleConsecutiveDaysLookback(days) days from the seat, so the
|
|
298
|
+
* finding is the same whatever window loaded the timeline; a run that may
|
|
299
|
+
* reach further back is reported at that length with detail.truncated (it
|
|
300
|
+
* started on or before runStartDate). Seats with an unreadable shiftDate
|
|
301
|
+
* are left out.
|
|
302
|
+
*/
|
|
303
|
+
function evaluateMaxConsecutiveDaysRule(
|
|
304
|
+
rule: ScheduleEffectiveRule,
|
|
305
|
+
resourceId: string,
|
|
306
|
+
seats: TimelineSeat[],
|
|
307
|
+
isAnchor: SeatAnchorCheck,
|
|
308
|
+
): CoverageViolationObj[] {
|
|
309
|
+
const { days } = rule.config as ScheduleMaxConsecutiveDaysConfigObj;
|
|
310
|
+
const lookback = getScheduleConsecutiveDaysLookback(days);
|
|
311
|
+
const dated = seats.filter((seat) => isScheduleDateKey(seat.shift.shiftDate));
|
|
312
|
+
const positions = getConsecutiveDayPositions(
|
|
313
|
+
dated.map((seat) => seat.shift.shiftDate),
|
|
314
|
+
lookback,
|
|
315
|
+
);
|
|
316
|
+
const violations: CoverageViolationObj[] = [];
|
|
317
|
+
for (const seat of dated) {
|
|
318
|
+
const position = positions.get(seat.shift.shiftDate)!;
|
|
319
|
+
if (position <= days || !isAnchor(seat.shift.id)) continue;
|
|
320
|
+
violations.push(
|
|
321
|
+
buildSeatViolation(rule, resourceId, seat, position - days, {
|
|
322
|
+
days,
|
|
323
|
+
runStartDate: addScheduleDays(seat.shift.shiftDate, 1 - position),
|
|
324
|
+
truncated: position > lookback,
|
|
325
|
+
}),
|
|
326
|
+
);
|
|
327
|
+
}
|
|
328
|
+
return violations;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/*
|
|
332
|
+
* Return the instant a local date starts in a zone, memoized per call.
|
|
333
|
+
*/
|
|
334
|
+
function getLocalDayStartMs(
|
|
335
|
+
dateKey: string,
|
|
336
|
+
timeZone: string,
|
|
337
|
+
cache: Map<string, number>,
|
|
338
|
+
): number {
|
|
339
|
+
let ms = cache.get(dateKey);
|
|
340
|
+
if (ms === undefined) {
|
|
341
|
+
ms = getScheduleLocalInstant(dateKey, 0, timeZone).getTime();
|
|
342
|
+
cache.set(dateKey, ms);
|
|
343
|
+
}
|
|
344
|
+
return ms;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/*
|
|
348
|
+
* Split a seat's worked minutes over the local days (DAY) or pay weeks
|
|
349
|
+
* (PAY_WEEK) it overlaps, by overlap. Periods it only touches are skipped.
|
|
350
|
+
*/
|
|
351
|
+
function getSeatPeriodShares(
|
|
352
|
+
seat: TimelineSeat,
|
|
353
|
+
period: 'DAY' | 'PAY_WEEK',
|
|
354
|
+
timeZone: string,
|
|
355
|
+
payWeekStart: number,
|
|
356
|
+
cache: Map<string, number>,
|
|
357
|
+
): PeriodShare[] {
|
|
358
|
+
const step = period === 'DAY' ? 1 : 7;
|
|
359
|
+
const firstDate = getScheduleLocalDate(seat.shift.startAt, timeZone);
|
|
360
|
+
const lastDate = getScheduleLocalDate(seat.shift.endAt, timeZone);
|
|
361
|
+
const shares: PeriodShare[] = [];
|
|
362
|
+
let periodStart =
|
|
363
|
+
period === 'DAY'
|
|
364
|
+
? firstDate
|
|
365
|
+
: getScheduleWeekStart(firstDate, payWeekStart);
|
|
366
|
+
while (periodStart <= lastDate) {
|
|
367
|
+
const next = addScheduleDays(periodStart, step);
|
|
368
|
+
const overlapMs =
|
|
369
|
+
Math.min(seat.endMs, getLocalDayStartMs(next, timeZone, cache)) -
|
|
370
|
+
Math.max(seat.startMs, getLocalDayStartMs(periodStart, timeZone, cache));
|
|
371
|
+
if (overlapMs > 0)
|
|
372
|
+
shares.push({
|
|
373
|
+
periodStart,
|
|
374
|
+
minutes: (seat.workedMinutes * overlapMs) / (seat.endMs - seat.startMs),
|
|
375
|
+
});
|
|
376
|
+
periodStart = next;
|
|
377
|
+
}
|
|
378
|
+
return shares;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/*
|
|
382
|
+
* Keep, per breaking seat, the worst period finding of one rule (the
|
|
383
|
+
* earliest period on a tie): one seat may break several periods.
|
|
384
|
+
*/
|
|
385
|
+
function keepWorstFindingPerSeat(
|
|
386
|
+
violations: CoverageViolationObj[],
|
|
387
|
+
): CoverageViolationObj[] {
|
|
388
|
+
const bySeat = new Map<string, CoverageViolationObj>();
|
|
389
|
+
for (const violation of violations) {
|
|
390
|
+
const current = bySeat.get(violation.shiftId!);
|
|
391
|
+
if (!current || violation.degree > current.degree)
|
|
392
|
+
bySeat.set(violation.shiftId!, violation);
|
|
393
|
+
}
|
|
394
|
+
return [...bySeat.values()];
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/*
|
|
398
|
+
* MAX_HOURS per DAY or PAY_WEEK: total each local period's worked minutes;
|
|
399
|
+
* a period over maxMinutes breaks on its latest-starting contributing seat.
|
|
400
|
+
*/
|
|
401
|
+
function evaluateCalendarPeriodHours(
|
|
402
|
+
rule: ScheduleEffectiveRule,
|
|
403
|
+
resourceId: string,
|
|
404
|
+
seats: TimelineSeat[],
|
|
405
|
+
isAnchor: SeatAnchorCheck,
|
|
406
|
+
timeZone: string,
|
|
407
|
+
payWeekStart: number,
|
|
408
|
+
): CoverageViolationObj[] {
|
|
409
|
+
const config = rule.config as ScheduleMaxHoursConfigObj;
|
|
410
|
+
const period = config.period === 'DAY' ? 'DAY' : 'PAY_WEEK';
|
|
411
|
+
const cache = new Map<string, number>();
|
|
412
|
+
const totals = new Map<string, { minutes: number; latest: TimelineSeat }>();
|
|
413
|
+
// Seats are sorted by start then id, so the last contributor is the latest
|
|
414
|
+
for (const seat of seats)
|
|
415
|
+
for (const share of getSeatPeriodShares(
|
|
416
|
+
seat,
|
|
417
|
+
period,
|
|
418
|
+
timeZone,
|
|
419
|
+
payWeekStart,
|
|
420
|
+
cache,
|
|
421
|
+
)) {
|
|
422
|
+
const total = totals.get(share.periodStart);
|
|
423
|
+
totals.set(share.periodStart, {
|
|
424
|
+
minutes: (total?.minutes ?? 0) + share.minutes,
|
|
425
|
+
latest: seat,
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
const violations: CoverageViolationObj[] = [];
|
|
429
|
+
for (const periodStart of [...totals.keys()].sort()) {
|
|
430
|
+
const { minutes, latest } = totals.get(periodStart)!;
|
|
431
|
+
const workedMinutes = roundScheduleMinutes(minutes);
|
|
432
|
+
if (workedMinutes <= config.maxMinutes || !isAnchor(latest.shift.id))
|
|
433
|
+
continue;
|
|
434
|
+
violations.push(
|
|
435
|
+
buildSeatViolation(
|
|
436
|
+
rule,
|
|
437
|
+
resourceId,
|
|
438
|
+
latest,
|
|
439
|
+
roundScheduleMinutes(workedMinutes - config.maxMinutes),
|
|
440
|
+
{
|
|
441
|
+
period: config.period,
|
|
442
|
+
days: config.days,
|
|
443
|
+
periodStart,
|
|
444
|
+
maxMinutes: config.maxMinutes,
|
|
445
|
+
workedMinutes,
|
|
446
|
+
},
|
|
447
|
+
),
|
|
448
|
+
);
|
|
449
|
+
}
|
|
450
|
+
return keepWorstFindingPerSeat(violations);
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/*
|
|
454
|
+
* MAX_HOURS per ROLLING_DAYS: for each anchor seat, the worked minutes of
|
|
455
|
+
* every seat inside the `days` x 24 h window ending at its end (split by
|
|
456
|
+
* overlap); over maxMinutes breaks on that seat.
|
|
457
|
+
*/
|
|
458
|
+
function evaluateRollingHours(
|
|
459
|
+
rule: ScheduleEffectiveRule,
|
|
460
|
+
resourceId: string,
|
|
461
|
+
seats: TimelineSeat[],
|
|
462
|
+
isAnchor: SeatAnchorCheck,
|
|
463
|
+
): CoverageViolationObj[] {
|
|
464
|
+
const config = rule.config as ScheduleMaxHoursConfigObj;
|
|
465
|
+
const spanMs = (config.days ?? 0) * FILE_CONSTANTS.dayMs;
|
|
466
|
+
const violations: CoverageViolationObj[] = [];
|
|
467
|
+
for (const anchor of seats) {
|
|
468
|
+
if (!isAnchor(anchor.shift.id)) continue;
|
|
469
|
+
const windowStartMs = anchor.endMs - spanMs;
|
|
470
|
+
let minutes = 0;
|
|
471
|
+
for (const seat of seats) {
|
|
472
|
+
if (seat.startMs >= anchor.endMs) break;
|
|
473
|
+
const overlapMs =
|
|
474
|
+
Math.min(seat.endMs, anchor.endMs) -
|
|
475
|
+
Math.max(seat.startMs, windowStartMs);
|
|
476
|
+
if (overlapMs > 0)
|
|
477
|
+
minutes +=
|
|
478
|
+
(seat.workedMinutes * overlapMs) / (seat.endMs - seat.startMs);
|
|
479
|
+
}
|
|
480
|
+
const workedMinutes = roundScheduleMinutes(minutes);
|
|
481
|
+
if (workedMinutes > config.maxMinutes)
|
|
482
|
+
violations.push(
|
|
483
|
+
buildSeatViolation(
|
|
484
|
+
rule,
|
|
485
|
+
resourceId,
|
|
486
|
+
anchor,
|
|
487
|
+
roundScheduleMinutes(workedMinutes - config.maxMinutes),
|
|
488
|
+
{
|
|
489
|
+
period: config.period,
|
|
490
|
+
days: config.days,
|
|
491
|
+
periodStart: new Date(windowStartMs).toISOString(),
|
|
492
|
+
maxMinutes: config.maxMinutes,
|
|
493
|
+
workedMinutes,
|
|
494
|
+
},
|
|
495
|
+
),
|
|
496
|
+
);
|
|
497
|
+
}
|
|
498
|
+
return violations;
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
/*
|
|
502
|
+
* Evaluate one person rule on a timeline (kinds other than the four person
|
|
503
|
+
* timeline kinds yield nothing).
|
|
504
|
+
*/
|
|
505
|
+
function evaluatePersonRule(
|
|
506
|
+
rule: ScheduleEffectiveRule,
|
|
507
|
+
input: SchedulePersonRulesInput,
|
|
508
|
+
seats: TimelineSeat[],
|
|
509
|
+
isAnchor: SeatAnchorCheck,
|
|
510
|
+
): CoverageViolationObj[] {
|
|
511
|
+
const { resourceId, timeZone, payWeekStart } = input;
|
|
512
|
+
switch (rule.kind) {
|
|
513
|
+
case 'SHIFT_LENGTH':
|
|
514
|
+
return evaluateShiftLengthRule(rule, resourceId, seats, isAnchor);
|
|
515
|
+
case 'MIN_REST':
|
|
516
|
+
return evaluateMinRestRule(rule, resourceId, seats, isAnchor);
|
|
517
|
+
case 'MAX_CONSECUTIVE_DAYS':
|
|
518
|
+
return evaluateMaxConsecutiveDaysRule(rule, resourceId, seats, isAnchor);
|
|
519
|
+
case 'MAX_HOURS':
|
|
520
|
+
return (rule.config as ScheduleMaxHoursConfigObj).period ===
|
|
521
|
+
'ROLLING_DAYS'
|
|
522
|
+
? evaluateRollingHours(rule, resourceId, seats, isAnchor)
|
|
523
|
+
: evaluateCalendarPeriodHours(
|
|
524
|
+
rule,
|
|
525
|
+
resourceId,
|
|
526
|
+
seats,
|
|
527
|
+
isAnchor,
|
|
528
|
+
timeZone,
|
|
529
|
+
payWeekStart,
|
|
530
|
+
);
|
|
531
|
+
default:
|
|
532
|
+
return [];
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
/*
|
|
537
|
+
* Compare two nullable ids, null first.
|
|
538
|
+
*/
|
|
539
|
+
function compareNullableIds(a: string | null, b: string | null): number {
|
|
540
|
+
if (a === null || b === null) return Number(b === null) - Number(a === null);
|
|
541
|
+
return compareScheduleIds(a, b);
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
/*
|
|
545
|
+
* Sort findings by start, kind order, rule, seat, subject, then code.
|
|
546
|
+
*/
|
|
547
|
+
function sortRuleViolations(
|
|
548
|
+
violations: CoverageViolationObj[],
|
|
549
|
+
): CoverageViolationObj[] {
|
|
550
|
+
return violations.sort(
|
|
551
|
+
(a, b) =>
|
|
552
|
+
a.startAt.getTime() - b.startAt.getTime() ||
|
|
553
|
+
SCHEDULE_RULE_KIND_ENUMS.indexOf(a.kind) -
|
|
554
|
+
SCHEDULE_RULE_KIND_ENUMS.indexOf(b.kind) ||
|
|
555
|
+
compareNullableIds(a.ruleId, b.ruleId) ||
|
|
556
|
+
compareNullableIds(a.shiftId, b.shiftId) ||
|
|
557
|
+
compareNullableIds(a.resourceId, b.resourceId) ||
|
|
558
|
+
compareNullableIds(a.code, b.code),
|
|
559
|
+
);
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
/*
|
|
563
|
+
* Evaluate MAX_HOURS, MIN_REST, MAX_CONSECUTIVE_DAYS and SHIFT_LENGTH on one
|
|
564
|
+
* person's timeline. Several rules of one kind all apply, each with its own
|
|
565
|
+
* findings. Every finding: resourceId = the person, shiftId = the breaking
|
|
566
|
+
* seat, interval = that seat, code null; only anchor seats carry findings
|
|
567
|
+
* (null anchorShiftIds = every seat).
|
|
568
|
+
*/
|
|
569
|
+
export function evaluatePersonRules(
|
|
570
|
+
input: SchedulePersonRulesInput,
|
|
571
|
+
): CoverageViolationObj[] {
|
|
572
|
+
const seats = getPersonTimeline(input.resourceId, input.timeline);
|
|
573
|
+
const { anchorShiftIds } = input;
|
|
574
|
+
// A seat may carry a finding when it is an anchor (null = every seat)
|
|
575
|
+
const isAnchor: SeatAnchorCheck = (shiftId) =>
|
|
576
|
+
anchorShiftIds === null || anchorShiftIds.has(shiftId);
|
|
577
|
+
return sortRuleViolations(
|
|
578
|
+
input.rules.flatMap((rule) =>
|
|
579
|
+
evaluatePersonRule(rule, input, seats, isAnchor),
|
|
580
|
+
),
|
|
581
|
+
);
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
/*
|
|
585
|
+
* The instant a seat's notice is measured from: now for a DRAFT seat that
|
|
586
|
+
* has not started (publishing it now), publishedAt for a PUBLISHED seat;
|
|
587
|
+
* null when there is nothing to judge.
|
|
588
|
+
*/
|
|
589
|
+
function getNoticeReferenceMs(
|
|
590
|
+
seat: TimelineSeat,
|
|
591
|
+
nowMs: number,
|
|
592
|
+
): number | null {
|
|
593
|
+
if (seat.shift.status === 'DRAFT') return seat.startMs > nowMs ? nowMs : null;
|
|
594
|
+
if (seat.shift.publishedAt == null) return null;
|
|
595
|
+
const publishedMs = toScheduleTimeMs(seat.shift.publishedAt);
|
|
596
|
+
return Number.isFinite(publishedMs) ? publishedMs : null;
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
/*
|
|
600
|
+
* NOTICE_PERIOD LATE_PUBLISH: a DRAFT seat starting after now with less
|
|
601
|
+
* than `days` to go, or a PUBLISHED seat published less than `days` before
|
|
602
|
+
* it starts; degree = minutes short of the notice.
|
|
603
|
+
*/
|
|
604
|
+
function getLatePublishViolation(
|
|
605
|
+
rule: ScheduleEffectiveRule,
|
|
606
|
+
seat: TimelineSeat,
|
|
607
|
+
nowMs: number,
|
|
608
|
+
): CoverageViolationObj | null {
|
|
609
|
+
const { days } = rule.config as ScheduleNoticePeriodConfigObj;
|
|
610
|
+
const { shift } = seat;
|
|
611
|
+
const referenceMs = getNoticeReferenceMs(seat, nowMs);
|
|
612
|
+
if (referenceMs === null) return null;
|
|
613
|
+
const shortMinutes = roundScheduleMinutes(
|
|
614
|
+
days * FILE_CONSTANTS.dayMinutes -
|
|
615
|
+
(seat.startMs - referenceMs) / FILE_CONSTANTS.minuteMs,
|
|
616
|
+
);
|
|
617
|
+
if (shortMinutes <= 0) return null;
|
|
618
|
+
return buildRuleViolation({
|
|
619
|
+
rule,
|
|
620
|
+
code: 'LATE_PUBLISH',
|
|
621
|
+
resourceId: shift.assigneeResourceId,
|
|
622
|
+
shiftId: shift.id,
|
|
623
|
+
startMs: seat.startMs,
|
|
624
|
+
endMs: seat.endMs,
|
|
625
|
+
degree: shortMinutes,
|
|
626
|
+
detail: { days },
|
|
627
|
+
});
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
/*
|
|
631
|
+
* NOTICE_PERIOD LATE_CHANGE: the seat's journal rows created less than
|
|
632
|
+
* `days` before it starts, grouped by the person each affects (to, else
|
|
633
|
+
* from, else the seat's assignee); one finding per person, degree = rows.
|
|
634
|
+
*/
|
|
635
|
+
function getLateChangeViolations(
|
|
636
|
+
rule: ScheduleEffectiveRule,
|
|
637
|
+
seat: TimelineSeat,
|
|
638
|
+
changes: CoverageShiftChangeInput[],
|
|
639
|
+
): CoverageViolationObj[] {
|
|
640
|
+
const { days } = rule.config as ScheduleNoticePeriodConfigObj;
|
|
641
|
+
const sinceMs = seat.startMs - days * FILE_CONSTANTS.dayMs;
|
|
642
|
+
const byPerson = new Map<string | null, string[]>();
|
|
643
|
+
for (const change of changes) {
|
|
644
|
+
if (!(toScheduleTimeMs(change.createdAt) > sinceMs)) continue;
|
|
645
|
+
const personId =
|
|
646
|
+
change.toResourceId ??
|
|
647
|
+
change.fromResourceId ??
|
|
648
|
+
seat.shift.assigneeResourceId;
|
|
649
|
+
byPerson.set(personId, [...(byPerson.get(personId) ?? []), change.id]);
|
|
650
|
+
}
|
|
651
|
+
return [...byPerson.entries()].map(([personId, changeIds]) =>
|
|
652
|
+
buildRuleViolation({
|
|
653
|
+
rule,
|
|
654
|
+
code: 'LATE_CHANGE',
|
|
655
|
+
resourceId: personId,
|
|
656
|
+
shiftId: seat.shift.id,
|
|
657
|
+
startMs: seat.startMs,
|
|
658
|
+
endMs: seat.endMs,
|
|
659
|
+
degree: changeIds.length,
|
|
660
|
+
detail: { days, changeIds: changeIds.sort(compareScheduleIds) },
|
|
661
|
+
}),
|
|
662
|
+
);
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
/*
|
|
666
|
+
* Group journal rows by seat id.
|
|
667
|
+
*/
|
|
668
|
+
function groupShiftChangesByShift(
|
|
669
|
+
changes: CoverageShiftChangeInput[],
|
|
670
|
+
): Map<string, CoverageShiftChangeInput[]> {
|
|
671
|
+
const byShift = new Map<string, CoverageShiftChangeInput[]>();
|
|
672
|
+
for (const change of changes)
|
|
673
|
+
byShift.set(change.shiftId, [
|
|
674
|
+
...(byShift.get(change.shiftId) ?? []),
|
|
675
|
+
change,
|
|
676
|
+
]);
|
|
677
|
+
return byShift;
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
/*
|
|
681
|
+
* Return the live seats overlapping a window, deduplicated by id and sorted
|
|
682
|
+
* by start then id.
|
|
683
|
+
*/
|
|
684
|
+
function getLiveWindowSeats(
|
|
685
|
+
shifts: CoverageShiftInput[],
|
|
686
|
+
fromMs: number,
|
|
687
|
+
toMs: number,
|
|
688
|
+
): TimelineSeat[] {
|
|
689
|
+
const seen = new Set<string>();
|
|
690
|
+
const seats: TimelineSeat[] = [];
|
|
691
|
+
for (const shift of shifts) {
|
|
692
|
+
if (seen.has(shift.id) || shift.status === 'CANCELED') continue;
|
|
693
|
+
seen.add(shift.id);
|
|
694
|
+
const startMs = toScheduleTimeMs(shift.startAt);
|
|
695
|
+
const endMs = toScheduleTimeMs(shift.endAt);
|
|
696
|
+
if (!(startMs < toMs && endMs > fromMs && endMs > startMs)) continue;
|
|
697
|
+
seats.push({ shift, startMs, endMs, workedMinutes: 0 });
|
|
698
|
+
}
|
|
699
|
+
return seats.sort(
|
|
700
|
+
(a, b) =>
|
|
701
|
+
a.startMs - b.startMs || compareScheduleIds(a.shift.id, b.shift.id),
|
|
702
|
+
);
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
/*
|
|
706
|
+
* Evaluate NOTICE_PERIOD on a calendar's live seats overlapping the window:
|
|
707
|
+
* publishing late (LATE_PUBLISH) and journal rows inside the notice
|
|
708
|
+
* (LATE_CHANGE). Rules are looked up per seat with its assignee as subject
|
|
709
|
+
* (an open seat reads the calendar-or-organization rules). Interval and
|
|
710
|
+
* shiftId are the seat.
|
|
711
|
+
*/
|
|
712
|
+
export function evaluateNoticePeriodRules(
|
|
713
|
+
input: ScheduleNoticeRulesInput,
|
|
714
|
+
): CoverageViolationObj[] {
|
|
715
|
+
const nowMs = toScheduleTimeMs(input.now);
|
|
716
|
+
const seats = getLiveWindowSeats(
|
|
717
|
+
input.shifts,
|
|
718
|
+
toScheduleTimeMs(input.window.from),
|
|
719
|
+
toScheduleTimeMs(input.window.to),
|
|
720
|
+
);
|
|
721
|
+
const changesByShift = groupShiftChangesByShift(input.shiftChanges);
|
|
722
|
+
const violations: CoverageViolationObj[] = [];
|
|
723
|
+
for (const seat of seats)
|
|
724
|
+
for (const rule of getEffectiveScheduleRules(
|
|
725
|
+
input.index,
|
|
726
|
+
'NOTICE_PERIOD',
|
|
727
|
+
seat.shift.assigneeResourceId,
|
|
728
|
+
)) {
|
|
729
|
+
const latePublish = getLatePublishViolation(rule, seat, nowMs);
|
|
730
|
+
if (latePublish) violations.push(latePublish);
|
|
731
|
+
violations.push(
|
|
732
|
+
...getLateChangeViolations(
|
|
733
|
+
rule,
|
|
734
|
+
seat,
|
|
735
|
+
changesByShift.get(seat.shift.id) ?? [],
|
|
736
|
+
),
|
|
737
|
+
);
|
|
738
|
+
}
|
|
739
|
+
return sortRuleViolations(violations);
|
|
740
|
+
}
|