assignment-user-matcher 1.9.0 → 1.10.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.
Files changed (150) hide show
  1. package/README.md +106 -1
  2. package/dist/index.d.ts +1 -0
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +19 -1
  5. package/dist/matcher.class.d.ts.map +1 -1
  6. package/dist/matcher.class.js +11 -0
  7. package/dist/scheduling/constraints/availability.d.ts +44 -0
  8. package/dist/scheduling/constraints/availability.d.ts.map +1 -0
  9. package/dist/scheduling/constraints/availability.js +142 -0
  10. package/dist/scheduling/constraints/consecutive.d.ts +25 -0
  11. package/dist/scheduling/constraints/consecutive.d.ts.map +1 -0
  12. package/dist/scheduling/constraints/consecutive.js +215 -0
  13. package/dist/scheduling/constraints/constraint.d.ts +35 -0
  14. package/dist/scheduling/constraints/constraint.d.ts.map +1 -0
  15. package/dist/scheduling/constraints/constraint.js +97 -0
  16. package/dist/scheduling/constraints/contract.d.ts +21 -0
  17. package/dist/scheduling/constraints/contract.d.ts.map +1 -0
  18. package/dist/scheduling/constraints/contract.js +106 -0
  19. package/dist/scheduling/constraints/cost.d.ts +18 -0
  20. package/dist/scheduling/constraints/cost.d.ts.map +1 -0
  21. package/dist/scheduling/constraints/cost.js +65 -0
  22. package/dist/scheduling/constraints/daily-rest.d.ts +22 -0
  23. package/dist/scheduling/constraints/daily-rest.d.ts.map +1 -0
  24. package/dist/scheduling/constraints/daily-rest.js +118 -0
  25. package/dist/scheduling/constraints/duty-quota.d.ts +20 -0
  26. package/dist/scheduling/constraints/duty-quota.d.ts.map +1 -0
  27. package/dist/scheduling/constraints/duty-quota.js +95 -0
  28. package/dist/scheduling/constraints/engagement-floor.d.ts +28 -0
  29. package/dist/scheduling/constraints/engagement-floor.d.ts.map +1 -0
  30. package/dist/scheduling/constraints/engagement-floor.js +79 -0
  31. package/dist/scheduling/constraints/fairness.d.ts +34 -0
  32. package/dist/scheduling/constraints/fairness.d.ts.map +1 -0
  33. package/dist/scheduling/constraints/fairness.js +149 -0
  34. package/dist/scheduling/constraints/group-composition.d.ts +19 -0
  35. package/dist/scheduling/constraints/group-composition.d.ts.map +1 -0
  36. package/dist/scheduling/constraints/group-composition.js +92 -0
  37. package/dist/scheduling/constraints/hour-budget.d.ts +13 -0
  38. package/dist/scheduling/constraints/hour-budget.d.ts.map +1 -0
  39. package/dist/scheduling/constraints/hour-budget.js +59 -0
  40. package/dist/scheduling/constraints/in-shift-breaks.d.ts +24 -0
  41. package/dist/scheduling/constraints/in-shift-breaks.d.ts.map +1 -0
  42. package/dist/scheduling/constraints/in-shift-breaks.js +97 -0
  43. package/dist/scheduling/constraints/intervals.d.ts +5 -0
  44. package/dist/scheduling/constraints/intervals.d.ts.map +1 -0
  45. package/dist/scheduling/constraints/intervals.js +17 -0
  46. package/dist/scheduling/constraints/max-shift-duration.d.ts +7 -0
  47. package/dist/scheduling/constraints/max-shift-duration.d.ts.map +1 -0
  48. package/dist/scheduling/constraints/max-shift-duration.js +38 -0
  49. package/dist/scheduling/constraints/min-rest.d.ts +22 -0
  50. package/dist/scheduling/constraints/min-rest.d.ts.map +1 -0
  51. package/dist/scheduling/constraints/min-rest.js +60 -0
  52. package/dist/scheduling/constraints/min-staffing.d.ts +14 -0
  53. package/dist/scheduling/constraints/min-staffing.d.ts.map +1 -0
  54. package/dist/scheduling/constraints/min-staffing.js +59 -0
  55. package/dist/scheduling/constraints/night-work.d.ts +24 -0
  56. package/dist/scheduling/constraints/night-work.d.ts.map +1 -0
  57. package/dist/scheduling/constraints/night-work.js +163 -0
  58. package/dist/scheduling/constraints/no-overlap.d.ts +7 -0
  59. package/dist/scheduling/constraints/no-overlap.d.ts.map +1 -0
  60. package/dist/scheduling/constraints/no-overlap.js +36 -0
  61. package/dist/scheduling/constraints/notice.d.ts +44 -0
  62. package/dist/scheduling/constraints/notice.d.ts.map +1 -0
  63. package/dist/scheduling/constraints/notice.js +140 -0
  64. package/dist/scheduling/constraints/one-shift-per-day.d.ts +7 -0
  65. package/dist/scheduling/constraints/one-shift-per-day.d.ts.map +1 -0
  66. package/dist/scheduling/constraints/one-shift-per-day.js +35 -0
  67. package/dist/scheduling/constraints/overtime.d.ts +47 -0
  68. package/dist/scheduling/constraints/overtime.d.ts.map +1 -0
  69. package/dist/scheduling/constraints/overtime.js +149 -0
  70. package/dist/scheduling/constraints/protections.d.ts +21 -0
  71. package/dist/scheduling/constraints/protections.d.ts.map +1 -0
  72. package/dist/scheduling/constraints/protections.js +93 -0
  73. package/dist/scheduling/constraints/qualification.d.ts +18 -0
  74. package/dist/scheduling/constraints/qualification.d.ts.map +1 -0
  75. package/dist/scheduling/constraints/qualification.js +78 -0
  76. package/dist/scheduling/constraints/rest-days.d.ts +30 -0
  77. package/dist/scheduling/constraints/rest-days.d.ts.map +1 -0
  78. package/dist/scheduling/constraints/rest-days.js +133 -0
  79. package/dist/scheduling/constraints/rolling-hours.d.ts +24 -0
  80. package/dist/scheduling/constraints/rolling-hours.d.ts.map +1 -0
  81. package/dist/scheduling/constraints/rolling-hours.js +147 -0
  82. package/dist/scheduling/constraints/start-interval.d.ts +20 -0
  83. package/dist/scheduling/constraints/start-interval.d.ts.map +1 -0
  84. package/dist/scheduling/constraints/start-interval.js +53 -0
  85. package/dist/scheduling/constraints/support.d.ts +48 -0
  86. package/dist/scheduling/constraints/support.d.ts.map +1 -0
  87. package/dist/scheduling/constraints/support.js +97 -0
  88. package/dist/scheduling/constraints/time-off.d.ts +7 -0
  89. package/dist/scheduling/constraints/time-off.d.ts.map +1 -0
  90. package/dist/scheduling/constraints/time-off.js +42 -0
  91. package/dist/scheduling/constraints/weekly-rest.d.ts +22 -0
  92. package/dist/scheduling/constraints/weekly-rest.d.ts.map +1 -0
  93. package/dist/scheduling/constraints/weekly-rest.js +92 -0
  94. package/dist/scheduling/cost.d.ts +56 -0
  95. package/dist/scheduling/cost.d.ts.map +1 -0
  96. package/dist/scheduling/cost.js +120 -0
  97. package/dist/scheduling/engine/construction.d.ts +22 -0
  98. package/dist/scheduling/engine/construction.d.ts.map +1 -0
  99. package/dist/scheduling/engine/construction.js +97 -0
  100. package/dist/scheduling/engine/explain.d.ts +12 -0
  101. package/dist/scheduling/engine/explain.d.ts.map +1 -0
  102. package/dist/scheduling/engine/explain.js +24 -0
  103. package/dist/scheduling/engine/objective.d.ts +73 -0
  104. package/dist/scheduling/engine/objective.d.ts.map +1 -0
  105. package/dist/scheduling/engine/objective.js +164 -0
  106. package/dist/scheduling/engine/prng.d.ts +5 -0
  107. package/dist/scheduling/engine/prng.d.ts.map +1 -0
  108. package/dist/scheduling/engine/prng.js +24 -0
  109. package/dist/scheduling/engine/propagation.d.ts +19 -0
  110. package/dist/scheduling/engine/propagation.d.ts.map +1 -0
  111. package/dist/scheduling/engine/propagation.js +40 -0
  112. package/dist/scheduling/engine/search.d.ts +50 -0
  113. package/dist/scheduling/engine/search.d.ts.map +1 -0
  114. package/dist/scheduling/engine/search.js +127 -0
  115. package/dist/scheduling/engine/state.d.ts +28 -0
  116. package/dist/scheduling/engine/state.d.ts.map +1 -0
  117. package/dist/scheduling/engine/state.js +96 -0
  118. package/dist/scheduling/engine/timeline.d.ts +179 -0
  119. package/dist/scheduling/engine/timeline.d.ts.map +1 -0
  120. package/dist/scheduling/engine/timeline.js +416 -0
  121. package/dist/scheduling/engine/verdicts.d.ts +18 -0
  122. package/dist/scheduling/engine/verdicts.d.ts.map +1 -0
  123. package/dist/scheduling/engine/verdicts.js +74 -0
  124. package/dist/scheduling/index.d.ts +10 -0
  125. package/dist/scheduling/index.d.ts.map +1 -0
  126. package/dist/scheduling/index.js +29 -0
  127. package/dist/scheduling/model.d.ts +53 -0
  128. package/dist/scheduling/model.d.ts.map +1 -0
  129. package/dist/scheduling/model.js +379 -0
  130. package/dist/scheduling/operations.d.ts +134 -0
  131. package/dist/scheduling/operations.d.ts.map +1 -0
  132. package/dist/scheduling/operations.js +376 -0
  133. package/dist/scheduling/provenance.d.ts +33 -0
  134. package/dist/scheduling/provenance.d.ts.map +1 -0
  135. package/dist/scheduling/provenance.js +70 -0
  136. package/dist/scheduling/rules.d.ts +23 -0
  137. package/dist/scheduling/rules.d.ts.map +1 -0
  138. package/dist/scheduling/rules.js +37 -0
  139. package/dist/scheduling/scheduler.class.d.ts +24 -0
  140. package/dist/scheduling/scheduler.class.d.ts.map +1 -0
  141. package/dist/scheduling/scheduler.class.js +150 -0
  142. package/dist/scheduling/time.d.ts +132 -0
  143. package/dist/scheduling/time.d.ts.map +1 -0
  144. package/dist/scheduling/time.js +256 -0
  145. package/dist/scheduling/types.d.ts +932 -0
  146. package/dist/scheduling/types.d.ts.map +1 -0
  147. package/dist/scheduling/types.js +19 -0
  148. package/dist/types/matcher.d.ts +15 -0
  149. package/dist/types/matcher.d.ts.map +1 -1
  150. package/package.json +4 -2
@@ -0,0 +1 @@
1
+ {"version":3,"file":"no-overlap.d.ts","sourceRoot":"","sources":["../../../src/scheduling/constraints/no-overlap.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,UAAU,CAAC;AAGrD,wBAAgB,SAAS,IAAI,oBAAoB,CAsBhD"}
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ /**
3
+ * No-overlap — an employee's assignments must never overlap in time, including
4
+ * across midnight for overnight shifts. Hard by default.
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.noOverlap = void 0;
8
+ const intervals_1 = require("./intervals");
9
+ function noOverlap() {
10
+ return {
11
+ id: 'no-overlap',
12
+ hardness: 'hard',
13
+ delta(state, pair) {
14
+ const inst = state.ctx.instanceById.get(pair.shiftInstanceId);
15
+ if (!inst)
16
+ return 0;
17
+ const assigned = state.byEmployee.get(pair.employeeId);
18
+ if (!assigned)
19
+ return 0;
20
+ for (const otherId of assigned) {
21
+ if (otherId === pair.shiftInstanceId)
22
+ continue;
23
+ const other = state.ctx.instanceById.get(otherId);
24
+ if (other && (0, intervals_1.overlaps)(inst.startMinute, inst.endMinute, other.startMinute, other.endMinute))
25
+ return 1;
26
+ }
27
+ return 0;
28
+ },
29
+ explain(state, pair) {
30
+ return this.delta(state, pair) > 0
31
+ ? `shift "${pair.shiftInstanceId}" overlaps another assignment of employee "${pair.employeeId}"`
32
+ : null;
33
+ },
34
+ };
35
+ }
36
+ exports.noOverlap = noOverlap;
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Predictability of work — Directive (EU) 2019/1152 Art 10.
3
+ *
4
+ * Where a work pattern is wholly or mostly unpredictable, a worker may not be
5
+ * required to work unless *both* the work falls within predetermined reference
6
+ * hours and days, *and* they were given reasonable notice. Where either fails,
7
+ * Art 10(2) gives a **right to refuse without adverse consequences** — so the
8
+ * engine's job is not to forbid the assignment but to mark it refusable, since
9
+ * a roster built on refusable shifts is a roster that will not survive contact
10
+ * with the workforce.
11
+ *
12
+ * Art 10(3) adds that cancelling after a specified deadline entitles the worker
13
+ * to compensation. The Netherlands pays the originally-called hours outright.
14
+ * That makes a late change a *real cost*, not a soft preference, which is why
15
+ * it surfaces as a ledger entry rather than a score nudge.
16
+ *
17
+ * None of this means anything without a published roster: notice is measured
18
+ * from `published.publishedAt`, and with no publication there is nothing to give
19
+ * notice of.
20
+ */
21
+ import type { NoticeRule, SchedulingConstraint, SearchState } from '../types';
22
+ export declare const NOTICE_CITATION = "Directive (EU) 2019/1152 Art 10";
23
+ export declare function noticeRule(rule: NoticeRule): SchedulingConstraint;
24
+ /**
25
+ * Compensation owed for assignments dropped from a published roster.
26
+ *
27
+ * Computed against the published roster rather than the new one, because the
28
+ * liability attaches to what the worker was told to expect.
29
+ *
30
+ * `cancellationDeadlineMinutes` bounds the protected window before the shift's
31
+ * start: a cancellation inside it owes compensation, one before it does not.
32
+ * The cancellation instant is the run's `asOf`; without one the engine cannot
33
+ * know when the change was made, so every cancellation is treated as late —
34
+ * the conservative reading, since under-stating a wage liability is the error
35
+ * that matters.
36
+ */
37
+ export declare function cancellationLedger(state: SearchState, rule: NoticeRule | undefined): {
38
+ kind: "lateCancellationPay";
39
+ employeeId: string;
40
+ minutes: number;
41
+ reason: string;
42
+ citation: string;
43
+ }[];
44
+ //# sourceMappingURL=notice.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"notice.d.ts","sourceRoot":"","sources":["../../../src/scheduling/constraints/notice.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,KAAK,EAER,UAAU,EAEV,oBAAoB,EACpB,WAAW,EAEd,MAAM,UAAU,CAAC;AAGlB,eAAO,MAAM,eAAe,oCAAoC,CAAC;AAEjE,wBAAgB,UAAU,CAAC,IAAI,EAAE,UAAU,GAAG,oBAAoB,CAoEjE;AAwBD;;;;;;;;;;;;GAYG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,WAAW,EAAE,IAAI,EAAE,UAAU,GAAG,SAAS;;;;;;IAwBlF"}
@@ -0,0 +1,140 @@
1
+ "use strict";
2
+ /**
3
+ * Predictability of work — Directive (EU) 2019/1152 Art 10.
4
+ *
5
+ * Where a work pattern is wholly or mostly unpredictable, a worker may not be
6
+ * required to work unless *both* the work falls within predetermined reference
7
+ * hours and days, *and* they were given reasonable notice. Where either fails,
8
+ * Art 10(2) gives a **right to refuse without adverse consequences** — so the
9
+ * engine's job is not to forbid the assignment but to mark it refusable, since
10
+ * a roster built on refusable shifts is a roster that will not survive contact
11
+ * with the workforce.
12
+ *
13
+ * Art 10(3) adds that cancelling after a specified deadline entitles the worker
14
+ * to compensation. The Netherlands pays the originally-called hours outright.
15
+ * That makes a late change a *real cost*, not a soft preference, which is why
16
+ * it surfaces as a ledger entry rather than a score nudge.
17
+ *
18
+ * None of this means anything without a published roster: notice is measured
19
+ * from `published.publishedAt`, and with no publication there is nothing to give
20
+ * notice of.
21
+ */
22
+ Object.defineProperty(exports, "__esModule", { value: true });
23
+ exports.cancellationLedger = exports.noticeRule = exports.NOTICE_CITATION = void 0;
24
+ const support_1 = require("./support");
25
+ exports.NOTICE_CITATION = 'Directive (EU) 2019/1152 Art 10';
26
+ function noticeRule(rule) {
27
+ const constraint = (0, support_1.fromVerdict)({
28
+ id: 'notice',
29
+ // Medium, not hard: work outside the reference hours is refusable, not
30
+ // unlawful. Treating it as hard would make rosters infeasible for a
31
+ // right the worker may well not exercise.
32
+ hardness: 'medium',
33
+ weight: 200,
34
+ citation: exports.NOTICE_CITATION,
35
+ verdict(state, pair) {
36
+ const inst = (0, support_1.instanceOf)(state, pair);
37
+ if (!inst)
38
+ return (0, support_1.pass)('notice', 'unknown shift');
39
+ if (rule.referenceHours?.length && !insideReferenceHours(state, inst, rule)) {
40
+ return (0, support_1.fail)('notice', 'medium', `"${inst.id}" falls outside employee "${pair.employeeId}"'s reference hours, so it may be refused without detriment`, { citation: exports.NOTICE_CITATION });
41
+ }
42
+ const shortfall = noticeShortfall(state, inst, rule);
43
+ if (shortfall !== null) {
44
+ return (0, support_1.fail)('notice', 'medium', `"${inst.id}" gives ${(0, support_1.h)(shortfall.given)} notice, below the ${(0, support_1.h)(shortfall.required)} required — refusable without detriment`, {
45
+ actual: shortfall.given,
46
+ required: shortfall.required,
47
+ unit: 'minutes',
48
+ citation: exports.NOTICE_CITATION,
49
+ });
50
+ }
51
+ return (0, support_1.pass)('notice', 'within reference hours and given adequate notice', { citation: exports.NOTICE_CITATION });
52
+ },
53
+ });
54
+ // A published roster that may only change by consent (Finland) or for cause
55
+ // makes every *new* assignment a reportable change, not merely a late one.
56
+ constraint.evaluate = (state) => {
57
+ const out = [];
58
+ if (rule.changeAfterPublication === undefined || rule.changeAfterPublication === 'free')
59
+ return out;
60
+ if (state.ctx.publishedPairs.size === 0)
61
+ return out;
62
+ for (const [instanceId, employees] of state.assignments) {
63
+ for (const employeeId of employees) {
64
+ if (state.ctx.publishedPairs.has(`${employeeId}|${instanceId}`))
65
+ continue;
66
+ out.push({
67
+ constraintId: 'notice',
68
+ severity: 'medium',
69
+ employeeId,
70
+ shiftInstanceId: instanceId,
71
+ message: rule.changeAfterPublication === 'consent'
72
+ ? `"${instanceId}" was added for "${employeeId}" after publication; this needs the employee's consent`
73
+ : `"${instanceId}" was added for "${employeeId}" after publication; this needs a compelling reason on record`,
74
+ citation: exports.NOTICE_CITATION,
75
+ });
76
+ }
77
+ }
78
+ return out;
79
+ };
80
+ return constraint;
81
+ }
82
+ exports.noticeRule = noticeRule;
83
+ function insideReferenceHours(state, inst, rule) {
84
+ const clock = state.ctx.clock;
85
+ const span = inst.endMinute - inst.startMinute;
86
+ for (const window of rule.referenceHours ?? []) {
87
+ if (window.daysOfWeek.length && !window.daysOfWeek.includes(inst.weekday))
88
+ continue;
89
+ const covered = clock.minutesInClockRange({ start: inst.startMinute, end: inst.endMinute }, window);
90
+ if (covered >= span)
91
+ return true;
92
+ }
93
+ return false;
94
+ }
95
+ /** Notice actually given against the notice owed, or `null` when adequate. */
96
+ function noticeShortfall(state, inst, rule) {
97
+ if (rule.minNoticeMinutes === undefined || state.ctx.publishedAtMinute === undefined)
98
+ return null;
99
+ const given = inst.startMinute - state.ctx.publishedAtMinute;
100
+ return given < rule.minNoticeMinutes ? { given: Math.max(0, given), required: rule.minNoticeMinutes } : null;
101
+ }
102
+ /**
103
+ * Compensation owed for assignments dropped from a published roster.
104
+ *
105
+ * Computed against the published roster rather than the new one, because the
106
+ * liability attaches to what the worker was told to expect.
107
+ *
108
+ * `cancellationDeadlineMinutes` bounds the protected window before the shift's
109
+ * start: a cancellation inside it owes compensation, one before it does not.
110
+ * The cancellation instant is the run's `asOf`; without one the engine cannot
111
+ * know when the change was made, so every cancellation is treated as late —
112
+ * the conservative reading, since under-stating a wage liability is the error
113
+ * that matters.
114
+ */
115
+ function cancellationLedger(state, rule) {
116
+ if (!rule?.cancellationCompensationMinutes)
117
+ return [];
118
+ const out = [];
119
+ for (const key of state.ctx.publishedPairs) {
120
+ const [employeeId, instanceId] = key.split('|');
121
+ if (state.assignments.get(instanceId)?.has(employeeId))
122
+ continue;
123
+ const inst = state.ctx.instanceById.get(instanceId);
124
+ if (rule.cancellationDeadlineMinutes !== undefined &&
125
+ state.ctx.asOfMinute !== undefined &&
126
+ inst !== undefined &&
127
+ inst.startMinute - state.ctx.asOfMinute >= rule.cancellationDeadlineMinutes) {
128
+ continue; // cancelled with the deadline still ahead — no liability
129
+ }
130
+ out.push({
131
+ kind: 'lateCancellationPay',
132
+ employeeId,
133
+ minutes: Math.max(rule.cancellationCompensationMinutes, inst?.workingMinutes ?? 0),
134
+ reason: `published assignment "${instanceId}" was cancelled`,
135
+ citation: exports.NOTICE_CITATION,
136
+ });
137
+ }
138
+ return out;
139
+ }
140
+ exports.cancellationLedger = cancellationLedger;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * One-shift-per-day — an employee works at most one shift instance per calendar day.
3
+ * Hard by default (mirrors the legacy `<= 1` rule, which was correct).
4
+ */
5
+ import type { SchedulingConstraint } from '../types';
6
+ export declare function oneShiftPerDay(): SchedulingConstraint;
7
+ //# sourceMappingURL=one-shift-per-day.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"one-shift-per-day.d.ts","sourceRoot":"","sources":["../../../src/scheduling/constraints/one-shift-per-day.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,UAAU,CAAC;AAErD,wBAAgB,cAAc,IAAI,oBAAoB,CAsBrD"}
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ /**
3
+ * One-shift-per-day — an employee works at most one shift instance per calendar day.
4
+ * Hard by default (mirrors the legacy `<= 1` rule, which was correct).
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.oneShiftPerDay = void 0;
8
+ function oneShiftPerDay() {
9
+ return {
10
+ id: 'one-shift-per-day',
11
+ hardness: 'hard',
12
+ delta(state, pair) {
13
+ const inst = state.ctx.instanceById.get(pair.shiftInstanceId);
14
+ if (!inst)
15
+ return 0;
16
+ const assigned = state.byEmployee.get(pair.employeeId);
17
+ if (!assigned)
18
+ return 0;
19
+ for (const otherId of assigned) {
20
+ if (otherId === pair.shiftInstanceId)
21
+ continue;
22
+ const other = state.ctx.instanceById.get(otherId);
23
+ if (other && other.date === inst.date)
24
+ return 1;
25
+ }
26
+ return 0;
27
+ },
28
+ explain(state, pair) {
29
+ return this.delta(state, pair) > 0
30
+ ? `employee "${pair.employeeId}" already has a shift on ${state.ctx.instanceById.get(pair.shiftInstanceId)?.date}`
31
+ : null;
32
+ },
33
+ };
34
+ }
35
+ exports.oneShiftPerDay = oneShiftPerDay;
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Overtime — the ordinary-vs-overtime split.
3
+ *
4
+ * `rolling-hours` caps *total* working time; this rule regulates the overtime
5
+ * portion by itself, which several member states do separately: Estonia's TLS
6
+ * §44 makes overtime conditional on the employee's agreement and compensates it
7
+ * primarily in equal time off, Germany builds the 10-hour day out of 8 ordinary
8
+ * plus averaged surplus, Austria caps overtime hours in their own right.
9
+ *
10
+ * Overtime is defined against an *ordinary baseline*, never inferred: the
11
+ * caller supplies `ordinaryPerDayMinutes` / `ordinaryPerWeekMinutes`, and a
12
+ * person's `contract.weeklyMinutes` overrides the weekly figure — a part-timer
13
+ * on 20 agreed hours starts earning overtime at 20, not at full time. Both
14
+ * measures run over rolling windows anchored on the person's own assignment
15
+ * boundaries, like every other window rule in this module.
16
+ *
17
+ * Three checks, all hard:
18
+ * - consent: with `requiresConsent`, any overtime at all needs the employee's
19
+ * recorded `overtimeConsent`;
20
+ * - a cap on overtime in any rolling 24h;
21
+ * - caps on overtime over longer rolling windows, against the weekly baseline
22
+ * pro-rated to the window.
23
+ *
24
+ * Compensation in time off is not a constraint but a debt: `overtimeLedger`
25
+ * turns each employee's period overtime into a `timeOffInLieu` ledger entry.
26
+ */
27
+ import type { LedgerEntry, OvertimeRule, SchedulingConstraint, SearchState } from '../types';
28
+ export declare const OVERTIME_CITATION = "National overtime law (e.g. EE T\u00F6\u00F6lepingu seadus \u00A744, DE ArbZG \u00A73)";
29
+ /**
30
+ * Assert an overtime rule can actually be evaluated. Applied to the global rule
31
+ * at registration and to each per-person override in `buildModel` — an override
32
+ * replaces the whole family, so it must stand on its own.
33
+ */
34
+ export declare function assertValidOvertimeRule(rule: OvertimeRule, owner?: string): void;
35
+ export declare function overtime(rule: OvertimeRule): SchedulingConstraint;
36
+ /**
37
+ * Time-off-in-lieu accrued by the roster: each employee's working minutes past
38
+ * their pro-rated ordinary baseline, where the applied rule compensates
39
+ * overtime in time off.
40
+ *
41
+ * Accrual is per employee record, not per person — the debt is owed under a
42
+ * contract even when working-time *limits* aggregate across contracts. It needs
43
+ * a weekly baseline (contract minutes or `ordinaryPerWeekMinutes`); a purely
44
+ * daily split cannot be pro-rated over a period without inventing a workweek.
45
+ */
46
+ export declare function overtimeLedger(state: SearchState): LedgerEntry[];
47
+ //# sourceMappingURL=overtime.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"overtime.d.ts","sourceRoot":"","sources":["../../../src/scheduling/constraints/overtime.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH,OAAO,KAAK,EAAY,WAAW,EAAE,YAAY,EAAe,oBAAoB,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAgBpH,eAAO,MAAM,iBAAiB,2FAAuE,CAAC;AAEtG;;;;GAIG;AACH,wBAAgB,uBAAuB,CAAC,IAAI,EAAE,YAAY,EAAE,KAAK,SAAU,GAAG,IAAI,CAYjF;AAED,wBAAgB,QAAQ,CAAC,IAAI,EAAE,YAAY,GAAG,oBAAoB,CAkFjE;AAOD;;;;;;;;;GASG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,WAAW,GAAG,WAAW,EAAE,CAiBhE"}
@@ -0,0 +1,149 @@
1
+ "use strict";
2
+ /**
3
+ * Overtime — the ordinary-vs-overtime split.
4
+ *
5
+ * `rolling-hours` caps *total* working time; this rule regulates the overtime
6
+ * portion by itself, which several member states do separately: Estonia's TLS
7
+ * §44 makes overtime conditional on the employee's agreement and compensates it
8
+ * primarily in equal time off, Germany builds the 10-hour day out of 8 ordinary
9
+ * plus averaged surplus, Austria caps overtime hours in their own right.
10
+ *
11
+ * Overtime is defined against an *ordinary baseline*, never inferred: the
12
+ * caller supplies `ordinaryPerDayMinutes` / `ordinaryPerWeekMinutes`, and a
13
+ * person's `contract.weeklyMinutes` overrides the weekly figure — a part-timer
14
+ * on 20 agreed hours starts earning overtime at 20, not at full time. Both
15
+ * measures run over rolling windows anchored on the person's own assignment
16
+ * boundaries, like every other window rule in this module.
17
+ *
18
+ * Three checks, all hard:
19
+ * - consent: with `requiresConsent`, any overtime at all needs the employee's
20
+ * recorded `overtimeConsent`;
21
+ * - a cap on overtime in any rolling 24h;
22
+ * - caps on overtime over longer rolling windows, against the weekly baseline
23
+ * pro-rated to the window.
24
+ *
25
+ * Compensation in time off is not a constraint but a debt: `overtimeLedger`
26
+ * turns each employee's period overtime into a `timeOffInLieu` ledger entry.
27
+ */
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.overtimeLedger = exports.overtime = exports.assertValidOvertimeRule = exports.OVERTIME_CITATION = void 0;
30
+ const types_1 = require("../types");
31
+ const support_1 = require("./support");
32
+ exports.OVERTIME_CITATION = 'National overtime law (e.g. EE Töölepingu seadus §44, DE ArbZG §3)';
33
+ /**
34
+ * Assert an overtime rule can actually be evaluated. Applied to the global rule
35
+ * at registration and to each per-person override in `buildModel` — an override
36
+ * replaces the whole family, so it must stand on its own.
37
+ */
38
+ function assertValidOvertimeRule(rule, owner = 'rules') {
39
+ if (rule.ordinaryPerDayMinutes === undefined && rule.ordinaryPerWeekMinutes === undefined) {
40
+ throw new types_1.ScheduleValidationError(`${owner}: overtime rule needs an ordinary baseline: set ordinaryPerDayMinutes and/or ordinaryPerWeekMinutes`);
41
+ }
42
+ if (rule.maxOvertimePerDayMinutes !== undefined && rule.ordinaryPerDayMinutes === undefined) {
43
+ throw new types_1.ScheduleValidationError(`${owner}: overtime.maxOvertimePerDayMinutes requires ordinaryPerDayMinutes`);
44
+ }
45
+ if (rule.maxOvertimeInWindow?.length && rule.ordinaryPerWeekMinutes === undefined) {
46
+ throw new types_1.ScheduleValidationError(`${owner}: overtime.maxOvertimeInWindow requires ordinaryPerWeekMinutes`);
47
+ }
48
+ }
49
+ exports.assertValidOvertimeRule = assertValidOvertimeRule;
50
+ function overtime(rule) {
51
+ assertValidOvertimeRule(rule);
52
+ return (0, support_1.fromVerdict)({
53
+ id: 'overtime',
54
+ hardness: 'hard',
55
+ weight: 1,
56
+ citation: rule.citation ?? exports.OVERTIME_CITATION,
57
+ verdict(state, pair) {
58
+ const inst = (0, support_1.instanceOf)(state, pair);
59
+ if (!inst)
60
+ return (0, support_1.pass)('overtime', 'unknown shift');
61
+ // Per-person overrides replace the whole family, so a minor or an
62
+ // opted-out worker can carry a stricter (or absent) split.
63
+ const applied = (0, support_1.rulesFor)(state.ctx, pair.employeeId).overtime;
64
+ if (!applied)
65
+ return (0, support_1.pass)('overtime', 'no overtime rule applies to this employee');
66
+ const citation = applied.citation ?? exports.OVERTIME_CITATION;
67
+ const employee = state.ctx.employeeById.get(pair.employeeId);
68
+ const weeklyOrdinary = employee?.contract?.weeklyMinutes ?? applied.ordinaryPerWeekMinutes;
69
+ const timeline = (0, support_1.timelineFor)(state, pair.employeeId);
70
+ const range = (0, support_1.rangeOf)(inst);
71
+ const probe = { ...range, id: (0, support_1.entryIdOf)(pair), workingMinutes: inst.workingMinutes, tag: inst.shiftTypeTag };
72
+ return timeline.withEntry(probe, (t) => {
73
+ const dayOvertime = applied.ordinaryPerDayMinutes === undefined
74
+ ? 0
75
+ : Math.max(0, t.maxWorkingMinutesInAnyWindow(support_1.MINUTES_PER_DAY, pad(range, support_1.MINUTES_PER_DAY)) -
76
+ applied.ordinaryPerDayMinutes);
77
+ const weekSpan = 7 * support_1.MINUTES_PER_DAY;
78
+ const weekOvertime = weeklyOrdinary === undefined
79
+ ? 0
80
+ : Math.max(0, t.maxWorkingMinutesInAnyWindow(weekSpan, pad(range, weekSpan)) - weeklyOrdinary);
81
+ if (applied.requiresConsent && employee?.overtimeConsent !== true) {
82
+ const worst = Math.max(dayOvertime, weekOvertime);
83
+ if (worst > 0) {
84
+ return (0, support_1.fail)('overtime', 'hard', `employee "${pair.employeeId}" would work ${(0, support_1.h)(worst)} of overtime but has not agreed to overtime`, { actual: worst, required: 0, unit: 'minutes', citation });
85
+ }
86
+ }
87
+ if (applied.maxOvertimePerDayMinutes !== undefined && dayOvertime > applied.maxOvertimePerDayMinutes) {
88
+ return (0, support_1.fail)('overtime', 'hard', `employee "${pair.employeeId}" would work ${(0, support_1.h)(dayOvertime)} of overtime in a 24h window, over the ${(0, support_1.h)(applied.maxOvertimePerDayMinutes)} cap`, { actual: dayOvertime, required: applied.maxOvertimePerDayMinutes, unit: 'minutes', citation });
89
+ }
90
+ for (const window of applied.maxOvertimeInWindow ?? []) {
91
+ if (weeklyOrdinary === undefined)
92
+ break;
93
+ const span = window.windowDays * support_1.MINUTES_PER_DAY;
94
+ const worked = t.maxWorkingMinutesInAnyWindow(span, pad(range, span));
95
+ const ordinary = Math.round((weeklyOrdinary * window.windowDays) / 7);
96
+ const windowOvertime = Math.max(0, worked - ordinary);
97
+ if (windowOvertime > window.maxMinutes) {
98
+ const label = window.label ?? `${(0, support_1.h)(window.maxMinutes)} overtime / ${window.windowDays}d`;
99
+ return (0, support_1.fail)('overtime', 'hard', `employee "${pair.employeeId}" would work ${(0, support_1.h)(windowOvertime)} of overtime in some ${window.windowDays}-day window, over the ${label} limit`, { actual: windowOvertime, required: window.maxMinutes, unit: 'minutes', citation });
100
+ }
101
+ }
102
+ return (0, support_1.pass)('overtime', 'within every configured overtime limit', { unit: 'minutes', citation });
103
+ });
104
+ },
105
+ });
106
+ }
107
+ exports.overtime = overtime;
108
+ /** Windows overlapping `range` are the only ones the new shift can worsen. */
109
+ function pad(range, span) {
110
+ return { start: range.start - span, end: range.end + span };
111
+ }
112
+ /**
113
+ * Time-off-in-lieu accrued by the roster: each employee's working minutes past
114
+ * their pro-rated ordinary baseline, where the applied rule compensates
115
+ * overtime in time off.
116
+ *
117
+ * Accrual is per employee record, not per person — the debt is owed under a
118
+ * contract even when working-time *limits* aggregate across contracts. It needs
119
+ * a weekly baseline (contract minutes or `ordinaryPerWeekMinutes`); a purely
120
+ * daily split cannot be pro-rated over a period without inventing a workweek.
121
+ */
122
+ function overtimeLedger(state) {
123
+ const out = [];
124
+ for (const employee of state.ctx.employees) {
125
+ const applied = (0, support_1.rulesFor)(state.ctx, employee.id).overtime;
126
+ if (!applied || applied.compensation !== 'timeOff')
127
+ continue;
128
+ const overtimeMinutes = periodOvertimeMinutes(state, employee, applied);
129
+ if (overtimeMinutes <= 0)
130
+ continue;
131
+ out.push({
132
+ kind: 'timeOffInLieu',
133
+ employeeId: employee.id,
134
+ minutes: overtimeMinutes,
135
+ reason: `${(0, support_1.h)(overtimeMinutes)} of overtime to be compensated by equal time off`,
136
+ citation: applied.citation ?? exports.OVERTIME_CITATION,
137
+ });
138
+ }
139
+ return out;
140
+ }
141
+ exports.overtimeLedger = overtimeLedger;
142
+ /** Working minutes past the pro-rated weekly baseline over the whole period. */
143
+ function periodOvertimeMinutes(state, employee, rule) {
144
+ const weeklyOrdinary = employee.contract?.weeklyMinutes ?? rule.ordinaryPerWeekMinutes;
145
+ if (weeklyOrdinary === undefined)
146
+ return 0;
147
+ const baseline = Math.round((weeklyOrdinary * state.ctx.periodDays) / 7);
148
+ return Math.max(0, (state.minutesByEmployee.get(employee.id) ?? 0) - baseline);
149
+ }
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Statutory personal protections.
3
+ *
4
+ * `pregnancyNightExclusion` implements Directive 92/85/EEC Art 7: a pregnant
5
+ * worker or new mother who produces a medical certificate must not be obliged
6
+ * to perform night work. The remedy the Directive mandates is a *transfer to
7
+ * daytime work*, or leave where that is not feasible — not simply an absence.
8
+ * So the engine bars night assignment and records what is owed instead, rather
9
+ * than quietly marking the person unavailable and shrinking the roster.
10
+ *
11
+ * `minor` and `hazardousNight` mostly select stricter values, which belong in
12
+ * `Employee.rules` where they compose with every other rule. What lives here is
13
+ * only what a rule override cannot express: the categorical night bar, and the
14
+ * fallback obligation it creates.
15
+ */
16
+ import type { LedgerEntry, SchedulingConstraint, SearchState } from '../types';
17
+ export declare const PREGNANCY_CITATION = "Directive 92/85/EEC Art 7";
18
+ export declare function protections(): SchedulingConstraint;
19
+ /** Obligations owed to protected workers the roster could not accommodate. */
20
+ export declare function protectionLedger(state: SearchState): LedgerEntry[];
21
+ //# sourceMappingURL=protections.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"protections.d.ts","sourceRoot":"","sources":["../../../src/scheduling/constraints/protections.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,KAAK,EAAuB,WAAW,EAAe,oBAAoB,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAGjH,eAAO,MAAM,kBAAkB,8BAA8B,CAAC;AAE9D,wBAAgB,WAAW,IAAI,oBAAoB,CAsDlD;AAED,8EAA8E;AAC9E,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,WAAW,GAAG,WAAW,EAAE,CAiBlE"}
@@ -0,0 +1,93 @@
1
+ "use strict";
2
+ /**
3
+ * Statutory personal protections.
4
+ *
5
+ * `pregnancyNightExclusion` implements Directive 92/85/EEC Art 7: a pregnant
6
+ * worker or new mother who produces a medical certificate must not be obliged
7
+ * to perform night work. The remedy the Directive mandates is a *transfer to
8
+ * daytime work*, or leave where that is not feasible — not simply an absence.
9
+ * So the engine bars night assignment and records what is owed instead, rather
10
+ * than quietly marking the person unavailable and shrinking the roster.
11
+ *
12
+ * `minor` and `hazardousNight` mostly select stricter values, which belong in
13
+ * `Employee.rules` where they compose with every other rule. What lives here is
14
+ * only what a rule override cannot express: the categorical night bar, and the
15
+ * fallback obligation it creates.
16
+ */
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.protectionLedger = exports.protections = exports.PREGNANCY_CITATION = void 0;
19
+ const support_1 = require("./support");
20
+ exports.PREGNANCY_CITATION = 'Directive 92/85/EEC Art 7';
21
+ function protections() {
22
+ const constraint = (0, support_1.fromVerdict)({
23
+ id: 'protections',
24
+ hardness: 'hard',
25
+ weight: 1,
26
+ citation: exports.PREGNANCY_CITATION,
27
+ prune(ctx, eligibility) {
28
+ for (const [employeeId, instances] of eligibility) {
29
+ const employee = ctx.employeeById.get(employeeId);
30
+ if (!employee?.protections?.some((p) => p.kind === 'pregnancyNightExclusion'))
31
+ continue;
32
+ for (const instanceId of [...instances]) {
33
+ if (ctx.instanceById.get(instanceId)?.isNightShift)
34
+ instances.delete(instanceId);
35
+ }
36
+ }
37
+ },
38
+ verdict(state, pair) {
39
+ const inst = (0, support_1.instanceOf)(state, pair);
40
+ const employee = state.ctx.employeeById.get(pair.employeeId);
41
+ if (!inst || !employee?.protections?.length)
42
+ return (0, support_1.pass)('protections', 'no protections registered');
43
+ if (inst.isNightShift && employee.protections.some((p) => p.kind === 'pregnancyNightExclusion')) {
44
+ return (0, support_1.fail)('protections', 'hard', `employee "${pair.employeeId}" is excluded from night work on a medical certificate and cannot take "${inst.id}"`, { citation: exports.PREGNANCY_CITATION });
45
+ }
46
+ return (0, support_1.pass)('protections', 'no protection bars this shift', { citation: exports.PREGNANCY_CITATION });
47
+ },
48
+ });
49
+ // Surface the mandated alternative. A protected worker left with no daytime
50
+ // work has not been accommodated — they have been sidelined, which is the
51
+ // outcome Art 7 exists to prevent.
52
+ constraint.evaluate = (state) => {
53
+ const out = [];
54
+ for (const employee of state.ctx.employees) {
55
+ const protection = employee.protections?.find((p) => p.kind === 'pregnancyNightExclusion');
56
+ if (!protection || protection.fallback !== 'dayShift')
57
+ continue;
58
+ if ((state.byEmployee.get(employee.id)?.size ?? 0) > 0)
59
+ continue;
60
+ out.push({
61
+ constraintId: 'protections',
62
+ severity: 'medium',
63
+ employeeId: employee.id,
64
+ message: `employee "${employee.id}" is excluded from night work and was given no daytime alternative; Art 7 requires a transfer to day work or leave`,
65
+ citation: exports.PREGNANCY_CITATION,
66
+ });
67
+ }
68
+ return out;
69
+ };
70
+ return constraint;
71
+ }
72
+ exports.protections = protections;
73
+ /** Obligations owed to protected workers the roster could not accommodate. */
74
+ function protectionLedger(state) {
75
+ const out = [];
76
+ for (const employee of state.ctx.employees) {
77
+ const protection = employee.protections?.find((p) => p.kind === 'pregnancyNightExclusion');
78
+ if (!protection)
79
+ continue;
80
+ if ((state.byEmployee.get(employee.id)?.size ?? 0) > 0)
81
+ continue;
82
+ out.push({
83
+ kind: 'compensatoryRest',
84
+ employeeId: employee.id,
85
+ reason: protection.fallback === 'leave'
86
+ ? 'night-work exclusion with no daytime work available: leave or extended maternity leave is owed'
87
+ : 'night-work exclusion with no daytime work assigned: a transfer to day work is owed',
88
+ citation: exports.PREGNANCY_CITATION,
89
+ });
90
+ }
91
+ return out;
92
+ }
93
+ exports.protectionLedger = protectionLedger;
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Qualifications, valid on the day of the shift.
3
+ *
4
+ * The bug this exists to prevent: a static `Set<string>` of skills silently
5
+ * rosters someone onto a shift after their certificate has lapsed. In
6
+ * healthcare, aviation ground handling, food and security that is a live audit
7
+ * failure, and it is invisible in the roster — the person "has" the skill.
8
+ *
9
+ * So possession is a question about a *date*, not a fact about a person:
10
+ * `hasQualification(employee, tag, date)`, never `hasSkill(employee, tag)`.
11
+ * Tags on `Employee.tags` stay valid for the whole period; anything with an
12
+ * expiry belongs in `qualifications`.
13
+ */
14
+ import type { Employee, SchedulingConstraint } from '../types';
15
+ export declare function qualification(): SchedulingConstraint;
16
+ /** Whether the employee holds `tag` on `date`, by plain tag or dated qualification. */
17
+ export declare function holds(employee: Employee, tag: string, date: string, minLevel?: number): boolean;
18
+ //# sourceMappingURL=qualification.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"qualification.d.ts","sourceRoot":"","sources":["../../../src/scheduling/constraints/qualification.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,KAAK,EAAE,QAAQ,EAAe,oBAAoB,EAAE,MAAM,UAAU,CAAC;AAG5E,wBAAgB,aAAa,IAAI,oBAAoB,CA+CpD;AAED,uFAAuF;AACvF,wBAAgB,KAAK,CAAC,QAAQ,EAAE,QAAQ,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO,CAS/F"}