assignment-user-matcher 1.8.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.
- package/README.md +240 -12
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +23 -1
- package/dist/matcher.class.d.ts +44 -2
- package/dist/matcher.class.d.ts.map +1 -1
- package/dist/matcher.class.js +393 -4
- package/dist/queries/pagination.d.ts +5 -1
- package/dist/queries/pagination.d.ts.map +1 -1
- package/dist/queries/pagination.js +14 -3
- package/dist/schedule/policy.d.ts +35 -0
- package/dist/schedule/policy.d.ts.map +1 -0
- package/dist/schedule/policy.js +62 -0
- package/dist/scheduling/constraints/availability.d.ts +44 -0
- package/dist/scheduling/constraints/availability.d.ts.map +1 -0
- package/dist/scheduling/constraints/availability.js +142 -0
- package/dist/scheduling/constraints/consecutive.d.ts +25 -0
- package/dist/scheduling/constraints/consecutive.d.ts.map +1 -0
- package/dist/scheduling/constraints/consecutive.js +215 -0
- package/dist/scheduling/constraints/constraint.d.ts +35 -0
- package/dist/scheduling/constraints/constraint.d.ts.map +1 -0
- package/dist/scheduling/constraints/constraint.js +97 -0
- package/dist/scheduling/constraints/contract.d.ts +21 -0
- package/dist/scheduling/constraints/contract.d.ts.map +1 -0
- package/dist/scheduling/constraints/contract.js +106 -0
- package/dist/scheduling/constraints/cost.d.ts +18 -0
- package/dist/scheduling/constraints/cost.d.ts.map +1 -0
- package/dist/scheduling/constraints/cost.js +65 -0
- package/dist/scheduling/constraints/daily-rest.d.ts +22 -0
- package/dist/scheduling/constraints/daily-rest.d.ts.map +1 -0
- package/dist/scheduling/constraints/daily-rest.js +118 -0
- package/dist/scheduling/constraints/duty-quota.d.ts +20 -0
- package/dist/scheduling/constraints/duty-quota.d.ts.map +1 -0
- package/dist/scheduling/constraints/duty-quota.js +95 -0
- package/dist/scheduling/constraints/engagement-floor.d.ts +28 -0
- package/dist/scheduling/constraints/engagement-floor.d.ts.map +1 -0
- package/dist/scheduling/constraints/engagement-floor.js +79 -0
- package/dist/scheduling/constraints/fairness.d.ts +34 -0
- package/dist/scheduling/constraints/fairness.d.ts.map +1 -0
- package/dist/scheduling/constraints/fairness.js +149 -0
- package/dist/scheduling/constraints/group-composition.d.ts +19 -0
- package/dist/scheduling/constraints/group-composition.d.ts.map +1 -0
- package/dist/scheduling/constraints/group-composition.js +92 -0
- package/dist/scheduling/constraints/hour-budget.d.ts +13 -0
- package/dist/scheduling/constraints/hour-budget.d.ts.map +1 -0
- package/dist/scheduling/constraints/hour-budget.js +59 -0
- package/dist/scheduling/constraints/in-shift-breaks.d.ts +24 -0
- package/dist/scheduling/constraints/in-shift-breaks.d.ts.map +1 -0
- package/dist/scheduling/constraints/in-shift-breaks.js +97 -0
- package/dist/scheduling/constraints/intervals.d.ts +5 -0
- package/dist/scheduling/constraints/intervals.d.ts.map +1 -0
- package/dist/scheduling/constraints/intervals.js +17 -0
- package/dist/scheduling/constraints/max-shift-duration.d.ts +7 -0
- package/dist/scheduling/constraints/max-shift-duration.d.ts.map +1 -0
- package/dist/scheduling/constraints/max-shift-duration.js +38 -0
- package/dist/scheduling/constraints/min-rest.d.ts +22 -0
- package/dist/scheduling/constraints/min-rest.d.ts.map +1 -0
- package/dist/scheduling/constraints/min-rest.js +60 -0
- package/dist/scheduling/constraints/min-staffing.d.ts +14 -0
- package/dist/scheduling/constraints/min-staffing.d.ts.map +1 -0
- package/dist/scheduling/constraints/min-staffing.js +59 -0
- package/dist/scheduling/constraints/night-work.d.ts +24 -0
- package/dist/scheduling/constraints/night-work.d.ts.map +1 -0
- package/dist/scheduling/constraints/night-work.js +163 -0
- package/dist/scheduling/constraints/no-overlap.d.ts +7 -0
- package/dist/scheduling/constraints/no-overlap.d.ts.map +1 -0
- package/dist/scheduling/constraints/no-overlap.js +36 -0
- package/dist/scheduling/constraints/notice.d.ts +44 -0
- package/dist/scheduling/constraints/notice.d.ts.map +1 -0
- package/dist/scheduling/constraints/notice.js +140 -0
- package/dist/scheduling/constraints/one-shift-per-day.d.ts +7 -0
- package/dist/scheduling/constraints/one-shift-per-day.d.ts.map +1 -0
- package/dist/scheduling/constraints/one-shift-per-day.js +35 -0
- package/dist/scheduling/constraints/overtime.d.ts +47 -0
- package/dist/scheduling/constraints/overtime.d.ts.map +1 -0
- package/dist/scheduling/constraints/overtime.js +149 -0
- package/dist/scheduling/constraints/protections.d.ts +21 -0
- package/dist/scheduling/constraints/protections.d.ts.map +1 -0
- package/dist/scheduling/constraints/protections.js +93 -0
- package/dist/scheduling/constraints/qualification.d.ts +18 -0
- package/dist/scheduling/constraints/qualification.d.ts.map +1 -0
- package/dist/scheduling/constraints/qualification.js +78 -0
- package/dist/scheduling/constraints/rest-days.d.ts +30 -0
- package/dist/scheduling/constraints/rest-days.d.ts.map +1 -0
- package/dist/scheduling/constraints/rest-days.js +133 -0
- package/dist/scheduling/constraints/rolling-hours.d.ts +24 -0
- package/dist/scheduling/constraints/rolling-hours.d.ts.map +1 -0
- package/dist/scheduling/constraints/rolling-hours.js +147 -0
- package/dist/scheduling/constraints/start-interval.d.ts +20 -0
- package/dist/scheduling/constraints/start-interval.d.ts.map +1 -0
- package/dist/scheduling/constraints/start-interval.js +53 -0
- package/dist/scheduling/constraints/support.d.ts +48 -0
- package/dist/scheduling/constraints/support.d.ts.map +1 -0
- package/dist/scheduling/constraints/support.js +97 -0
- package/dist/scheduling/constraints/time-off.d.ts +7 -0
- package/dist/scheduling/constraints/time-off.d.ts.map +1 -0
- package/dist/scheduling/constraints/time-off.js +42 -0
- package/dist/scheduling/constraints/weekly-rest.d.ts +22 -0
- package/dist/scheduling/constraints/weekly-rest.d.ts.map +1 -0
- package/dist/scheduling/constraints/weekly-rest.js +92 -0
- package/dist/scheduling/cost.d.ts +56 -0
- package/dist/scheduling/cost.d.ts.map +1 -0
- package/dist/scheduling/cost.js +120 -0
- package/dist/scheduling/engine/construction.d.ts +22 -0
- package/dist/scheduling/engine/construction.d.ts.map +1 -0
- package/dist/scheduling/engine/construction.js +97 -0
- package/dist/scheduling/engine/explain.d.ts +12 -0
- package/dist/scheduling/engine/explain.d.ts.map +1 -0
- package/dist/scheduling/engine/explain.js +24 -0
- package/dist/scheduling/engine/objective.d.ts +73 -0
- package/dist/scheduling/engine/objective.d.ts.map +1 -0
- package/dist/scheduling/engine/objective.js +164 -0
- package/dist/scheduling/engine/prng.d.ts +5 -0
- package/dist/scheduling/engine/prng.d.ts.map +1 -0
- package/dist/scheduling/engine/prng.js +24 -0
- package/dist/scheduling/engine/propagation.d.ts +19 -0
- package/dist/scheduling/engine/propagation.d.ts.map +1 -0
- package/dist/scheduling/engine/propagation.js +40 -0
- package/dist/scheduling/engine/search.d.ts +50 -0
- package/dist/scheduling/engine/search.d.ts.map +1 -0
- package/dist/scheduling/engine/search.js +127 -0
- package/dist/scheduling/engine/state.d.ts +28 -0
- package/dist/scheduling/engine/state.d.ts.map +1 -0
- package/dist/scheduling/engine/state.js +96 -0
- package/dist/scheduling/engine/timeline.d.ts +179 -0
- package/dist/scheduling/engine/timeline.d.ts.map +1 -0
- package/dist/scheduling/engine/timeline.js +416 -0
- package/dist/scheduling/engine/verdicts.d.ts +18 -0
- package/dist/scheduling/engine/verdicts.d.ts.map +1 -0
- package/dist/scheduling/engine/verdicts.js +74 -0
- package/dist/scheduling/index.d.ts +10 -0
- package/dist/scheduling/index.d.ts.map +1 -0
- package/dist/scheduling/index.js +29 -0
- package/dist/scheduling/model.d.ts +53 -0
- package/dist/scheduling/model.d.ts.map +1 -0
- package/dist/scheduling/model.js +379 -0
- package/dist/scheduling/operations.d.ts +134 -0
- package/dist/scheduling/operations.d.ts.map +1 -0
- package/dist/scheduling/operations.js +376 -0
- package/dist/scheduling/provenance.d.ts +33 -0
- package/dist/scheduling/provenance.d.ts.map +1 -0
- package/dist/scheduling/provenance.js +70 -0
- package/dist/scheduling/rules.d.ts +23 -0
- package/dist/scheduling/rules.d.ts.map +1 -0
- package/dist/scheduling/rules.js +37 -0
- package/dist/scheduling/scheduler.class.d.ts +24 -0
- package/dist/scheduling/scheduler.class.d.ts.map +1 -0
- package/dist/scheduling/scheduler.class.js +150 -0
- package/dist/scheduling/time.d.ts +132 -0
- package/dist/scheduling/time.d.ts.map +1 -0
- package/dist/scheduling/time.js +256 -0
- package/dist/scheduling/types.d.ts +932 -0
- package/dist/scheduling/types.d.ts.map +1 -0
- package/dist/scheduling/types.js +19 -0
- package/dist/scoring/match-score.d.ts.map +1 -1
- package/dist/scoring/match-score.js +1 -3
- package/dist/types/matcher.d.ts +195 -1
- package/dist/types/matcher.d.ts.map +1 -1
- package/dist/utils/keys.d.ts +3 -0
- package/dist/utils/keys.d.ts.map +1 -1
- package/dist/utils/keys.js +15 -0
- package/dist/validation/assignment-lint.d.ts +24 -0
- package/dist/validation/assignment-lint.d.ts.map +1 -0
- package/dist/validation/assignment-lint.js +132 -0
- package/package.json +4 -2
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Schedule policy — pure logic.
|
|
4
|
+
*
|
|
5
|
+
* An assignment carries an optional `schedule` policy describing its offer
|
|
6
|
+
* window: `notBefore` holds it out of the queue entirely, `notAfter` expires
|
|
7
|
+
* it while still un-accepted. Everything here is deliberately side-effect
|
|
8
|
+
* free: `AssignmentMatcher` owns the Redis writes, this module owns the
|
|
9
|
+
* decisions.
|
|
10
|
+
*
|
|
11
|
+
* Escalation owns the response clock and SLA owns the post-accept contract;
|
|
12
|
+
* schedule owns the offer window. Acceptance ends its authority.
|
|
13
|
+
*/
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.isHeld = exports.scheduleFromJson = exports.normalizeSchedulePolicy = void 0;
|
|
16
|
+
function epochMs(value) {
|
|
17
|
+
const n = Number(value);
|
|
18
|
+
return Number.isFinite(n) && n > 0 ? Math.floor(n) : undefined;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Resolve an assignment's schedule policy, or `null` when it has none (the
|
|
22
|
+
* overwhelming majority of assignments — callers use the null to stay on
|
|
23
|
+
* the untouched legacy path). A window with `notAfter <= notBefore` can
|
|
24
|
+
* never be satisfied, so the whole policy is treated as absent.
|
|
25
|
+
*/
|
|
26
|
+
function normalizeSchedulePolicy(policy) {
|
|
27
|
+
if (!policy || typeof policy !== 'object')
|
|
28
|
+
return null;
|
|
29
|
+
const notBefore = epochMs(policy.notBefore);
|
|
30
|
+
const notAfter = epochMs(policy.notAfter);
|
|
31
|
+
if (notBefore === undefined && notAfter === undefined)
|
|
32
|
+
return null;
|
|
33
|
+
if (notBefore !== undefined && notAfter !== undefined && notAfter <= notBefore)
|
|
34
|
+
return null;
|
|
35
|
+
return {
|
|
36
|
+
notBefore,
|
|
37
|
+
notAfter,
|
|
38
|
+
onMiss: policy.onMiss === 'drop' ? 'drop' : 'park',
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
exports.normalizeSchedulePolicy = normalizeSchedulePolicy;
|
|
42
|
+
/**
|
|
43
|
+
* Same question, answered from the stored JSON without paying for a parse on
|
|
44
|
+
* the accept hot path. Assignments without a schedule — effectively all of
|
|
45
|
+
* them in a typical deployment — cost one `indexOf` and nothing else.
|
|
46
|
+
*/
|
|
47
|
+
function scheduleFromJson(json) {
|
|
48
|
+
if (!json || json.indexOf('"schedule"') === -1)
|
|
49
|
+
return null;
|
|
50
|
+
try {
|
|
51
|
+
return normalizeSchedulePolicy(JSON.parse(json).schedule);
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
exports.scheduleFromJson = scheduleFromJson;
|
|
58
|
+
/** Whether the assignment must stay in the scheduled store at `now`. */
|
|
59
|
+
function isHeld(policy, now) {
|
|
60
|
+
return policy.notBefore !== undefined && policy.notBefore > now;
|
|
61
|
+
}
|
|
62
|
+
exports.isHeld = isHeld;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Availability and preferences.
|
|
3
|
+
*
|
|
4
|
+
* Four kinds, deliberately distinct rather than a single boolean:
|
|
5
|
+
*
|
|
6
|
+
* - `unavailable` — a hard blackout, like time off.
|
|
7
|
+
* - `available` — an allow-list. Once a person declares *any* availability
|
|
8
|
+
* window, anything outside every window is ineligible. This is the
|
|
9
|
+
* part-time and student pattern, and it inverts the default.
|
|
10
|
+
* - `preferred` / `avoid` — soft, weighted. A preference model that only has
|
|
11
|
+
* "can't work" cannot express "would rather not", so every unwanted shift
|
|
12
|
+
* becomes a refusal and the roster loses the flexibility it needs.
|
|
13
|
+
*
|
|
14
|
+
* The soft half matters for more than morale: pro-rata fairness in shift quality
|
|
15
|
+
* is a live discrimination question under the part-time and fixed-term
|
|
16
|
+
* directives, and it can only be traded off if it is scored rather than binary.
|
|
17
|
+
*/
|
|
18
|
+
import type { AvailabilityRule, SchedulingConstraint, SearchState, ShiftInstance } from '../types';
|
|
19
|
+
export declare function availability(): SchedulingConstraint;
|
|
20
|
+
/** Minutes of `range` covered by one availability rule's clock window. */
|
|
21
|
+
declare function overlapsRule(clock: {
|
|
22
|
+
minutesInClockRange: (r: {
|
|
23
|
+
start: number;
|
|
24
|
+
end: number;
|
|
25
|
+
}, c: {
|
|
26
|
+
from: string;
|
|
27
|
+
to: string;
|
|
28
|
+
}) => number;
|
|
29
|
+
}, range: {
|
|
30
|
+
start: number;
|
|
31
|
+
end: number;
|
|
32
|
+
}, rule: AvailabilityRule): number;
|
|
33
|
+
/**
|
|
34
|
+
* Weighted preference term over the whole roster, for the soft objective:
|
|
35
|
+
* every assigned pair contributes its `avoid` weights and is credited its
|
|
36
|
+
* `preferred` weights. This is what makes `preferred`/`avoid` *scored* rather
|
|
37
|
+
* than advisory — without it, the solver would treat a roster of avoided
|
|
38
|
+
* shifts and a roster of preferred ones as equal.
|
|
39
|
+
*/
|
|
40
|
+
export declare function preferencePenalty(state: SearchState): number;
|
|
41
|
+
/** Soft preference score for a pair: negative is better. Used by candidate ranking. */
|
|
42
|
+
export declare function preferenceScore(clock: Parameters<typeof overlapsRule>[0], rules: AvailabilityRule[] | undefined, inst: ShiftInstance): number;
|
|
43
|
+
export {};
|
|
44
|
+
//# sourceMappingURL=availability.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"availability.d.ts","sourceRoot":"","sources":["../../../src/scheduling/constraints/availability.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAe,oBAAoB,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAIhH,wBAAgB,YAAY,IAAI,oBAAoB,CAiFnD;AAED,0EAA0E;AAC1E,iBAAS,YAAY,CACjB,KAAK,EAAE;IAAE,mBAAmB,EAAE,CAAC,CAAC,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,GAAG,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,EAAE,EAAE,MAAM,CAAA;KAAE,KAAK,MAAM,CAAA;CAAE,EAC9G,KAAK,EAAE;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,EACrC,IAAI,EAAE,gBAAgB,GACvB,MAAM,CAGR;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,WAAW,GAAG,MAAM,CAW5D;AAED,uFAAuF;AACvF,wBAAgB,eAAe,CAC3B,KAAK,EAAE,UAAU,CAAC,OAAO,YAAY,CAAC,CAAC,CAAC,CAAC,EACzC,KAAK,EAAE,gBAAgB,EAAE,GAAG,SAAS,EACrC,IAAI,EAAE,aAAa,GACpB,MAAM,CAWR"}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Availability and preferences.
|
|
4
|
+
*
|
|
5
|
+
* Four kinds, deliberately distinct rather than a single boolean:
|
|
6
|
+
*
|
|
7
|
+
* - `unavailable` — a hard blackout, like time off.
|
|
8
|
+
* - `available` — an allow-list. Once a person declares *any* availability
|
|
9
|
+
* window, anything outside every window is ineligible. This is the
|
|
10
|
+
* part-time and student pattern, and it inverts the default.
|
|
11
|
+
* - `preferred` / `avoid` — soft, weighted. A preference model that only has
|
|
12
|
+
* "can't work" cannot express "would rather not", so every unwanted shift
|
|
13
|
+
* becomes a refusal and the roster loses the flexibility it needs.
|
|
14
|
+
*
|
|
15
|
+
* The soft half matters for more than morale: pro-rata fairness in shift quality
|
|
16
|
+
* is a live discrimination question under the part-time and fixed-term
|
|
17
|
+
* directives, and it can only be traded off if it is scored rather than binary.
|
|
18
|
+
*/
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.preferenceScore = exports.preferencePenalty = exports.availability = void 0;
|
|
21
|
+
const model_1 = require("../model");
|
|
22
|
+
const support_1 = require("./support");
|
|
23
|
+
function availability() {
|
|
24
|
+
const constraint = (0, support_1.fromVerdict)({
|
|
25
|
+
id: 'availability',
|
|
26
|
+
hardness: 'hard',
|
|
27
|
+
weight: 1,
|
|
28
|
+
verdict(state, pair) {
|
|
29
|
+
const inst = (0, support_1.instanceOf)(state, pair);
|
|
30
|
+
const employee = state.ctx.employeeById.get(pair.employeeId);
|
|
31
|
+
if (!inst || !employee?.availability?.length)
|
|
32
|
+
return (0, support_1.pass)('availability', 'no availability rules');
|
|
33
|
+
const clock = state.ctx.clock;
|
|
34
|
+
const range = { start: inst.startMinute, end: inst.endMinute };
|
|
35
|
+
const span = range.end - range.start;
|
|
36
|
+
const matching = employee.availability.filter((rule) => (0, model_1.availabilityApplies)(rule, inst));
|
|
37
|
+
for (const rule of matching.filter((r) => r.kind === 'unavailable')) {
|
|
38
|
+
if (overlapsRule(clock, range, rule) > 0) {
|
|
39
|
+
return (0, support_1.fail)('availability', 'hard', `employee "${pair.employeeId}" is unavailable during "${inst.id}"`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
// Declaring any 'available' window inverts the default: everything
|
|
43
|
+
// outside every declared window is ineligible, including days no
|
|
44
|
+
// window mentions — a Monday-only declaration bars Tuesdays too.
|
|
45
|
+
const allowLists = employee.availability.filter((r) => r.kind === 'available');
|
|
46
|
+
if (allowLists.length > 0) {
|
|
47
|
+
const covered = matching
|
|
48
|
+
.filter((r) => r.kind === 'available')
|
|
49
|
+
.reduce((max, rule) => Math.max(max, overlapsRule(clock, range, rule)), 0);
|
|
50
|
+
if (covered < span) {
|
|
51
|
+
return (0, support_1.fail)('availability', 'hard', `"${inst.id}" falls outside employee "${pair.employeeId}"'s declared availability`, { actual: covered, required: span, unit: 'minutes' });
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const avoided = matching.find((r) => r.kind === 'avoid' && overlapsRule(clock, range, r) > 0);
|
|
55
|
+
if (avoided) {
|
|
56
|
+
return (0, support_1.fail)('availability', 'soft', `employee "${pair.employeeId}" prefers to avoid "${inst.id}"`, {
|
|
57
|
+
actual: avoided.weight ?? 1,
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
const preferred = matching.find((r) => r.kind === 'preferred' && overlapsRule(clock, range, r) > 0);
|
|
61
|
+
return (0, support_1.pass)('availability', preferred ? `matches a preferred window` : 'no availability objection');
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
// A soft breach must not make the pair ineligible, and it must not leak
|
|
65
|
+
// into the hard score bucket (the SPI levels by constraint, not by
|
|
66
|
+
// verdict), so `delta` reports hard breaches only. The soft half is scored
|
|
67
|
+
// by `preferencePenalty`, which the objective adds at the soft level.
|
|
68
|
+
const innerDelta = constraint.delta;
|
|
69
|
+
constraint.delta = (state, pair) => {
|
|
70
|
+
const v = constraint.verdict(state, pair);
|
|
71
|
+
if (v.pass)
|
|
72
|
+
return 0;
|
|
73
|
+
return v.severity === 'hard' ? innerDelta(state, pair) : 0;
|
|
74
|
+
};
|
|
75
|
+
constraint.evaluate = (state) => {
|
|
76
|
+
const out = [];
|
|
77
|
+
for (const [instanceId, employees] of state.assignments) {
|
|
78
|
+
for (const employeeId of employees) {
|
|
79
|
+
const v = constraint.verdict(state, { employeeId, shiftInstanceId: instanceId });
|
|
80
|
+
if (v.pass || v.severity === 'hard')
|
|
81
|
+
continue;
|
|
82
|
+
out.push({
|
|
83
|
+
constraintId: 'availability',
|
|
84
|
+
severity: v.severity,
|
|
85
|
+
employeeId,
|
|
86
|
+
shiftInstanceId: instanceId,
|
|
87
|
+
message: v.message,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return out;
|
|
92
|
+
};
|
|
93
|
+
return constraint;
|
|
94
|
+
}
|
|
95
|
+
exports.availability = availability;
|
|
96
|
+
/** Minutes of `range` covered by one availability rule's clock window. */
|
|
97
|
+
function overlapsRule(clock, range, rule) {
|
|
98
|
+
if (rule.from === undefined || rule.to === undefined)
|
|
99
|
+
return range.end - range.start;
|
|
100
|
+
return clock.minutesInClockRange(range, { from: rule.from, to: rule.to });
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Weighted preference term over the whole roster, for the soft objective:
|
|
104
|
+
* every assigned pair contributes its `avoid` weights and is credited its
|
|
105
|
+
* `preferred` weights. This is what makes `preferred`/`avoid` *scored* rather
|
|
106
|
+
* than advisory — without it, the solver would treat a roster of avoided
|
|
107
|
+
* shifts and a roster of preferred ones as equal.
|
|
108
|
+
*/
|
|
109
|
+
function preferencePenalty(state) {
|
|
110
|
+
let penalty = 0;
|
|
111
|
+
for (const [instanceId, employees] of state.assignments) {
|
|
112
|
+
const inst = state.ctx.instanceById.get(instanceId);
|
|
113
|
+
if (!inst)
|
|
114
|
+
continue;
|
|
115
|
+
for (const employeeId of employees) {
|
|
116
|
+
const rules = state.ctx.employeeById.get(employeeId)?.availability;
|
|
117
|
+
if (rules?.length)
|
|
118
|
+
penalty += preferenceScore(state.ctx.clock, rules, inst);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return penalty;
|
|
122
|
+
}
|
|
123
|
+
exports.preferencePenalty = preferencePenalty;
|
|
124
|
+
/** Soft preference score for a pair: negative is better. Used by candidate ranking. */
|
|
125
|
+
function preferenceScore(clock, rules, inst) {
|
|
126
|
+
if (!rules?.length)
|
|
127
|
+
return 0;
|
|
128
|
+
const range = { start: inst.startMinute, end: inst.endMinute };
|
|
129
|
+
let score = 0;
|
|
130
|
+
for (const rule of rules) {
|
|
131
|
+
if (!(0, model_1.availabilityApplies)(rule, inst))
|
|
132
|
+
continue;
|
|
133
|
+
if (overlapsRule(clock, range, rule) <= 0)
|
|
134
|
+
continue;
|
|
135
|
+
if (rule.kind === 'preferred')
|
|
136
|
+
score -= rule.weight ?? 1;
|
|
137
|
+
if (rule.kind === 'avoid')
|
|
138
|
+
score += rule.weight ?? 1;
|
|
139
|
+
}
|
|
140
|
+
return score;
|
|
141
|
+
}
|
|
142
|
+
exports.preferenceScore = preferenceScore;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sequence rules — limits on runs rather than on totals.
|
|
3
|
+
*
|
|
4
|
+
* These are the rules a per-assignment check structurally cannot see, because
|
|
5
|
+
* the breach lives in the *pattern*, not in any one shift:
|
|
6
|
+
*
|
|
7
|
+
* - **Consecutive working days.** Portugal caps a shift worker at six.
|
|
8
|
+
* - **Consecutive night shifts, and the rest owed when the run ends.** Finland
|
|
9
|
+
* allows five and then requires 24h off; the Netherlands allows seven (eight
|
|
10
|
+
* by collective agreement) and then requires 46h. Only NL and FI legislate a
|
|
11
|
+
* cap at all — elsewhere it is collective-agreement territory, which is
|
|
12
|
+
* exactly why it has to be configuration rather than a built-in number.
|
|
13
|
+
* - **Forbidden successions.** The "quick return" — a late shift followed by
|
|
14
|
+
* an early one — is the single most-requested constraint in nurse rostering
|
|
15
|
+
* and is invisible to a rest rule whenever the gap technically clears the
|
|
16
|
+
* minimum.
|
|
17
|
+
*/
|
|
18
|
+
import type { ConsecutiveRule, SchedulingConstraint } from '../types';
|
|
19
|
+
export declare const CONSECUTIVE_CITATION = "National working-time law / collective agreement";
|
|
20
|
+
/** Every sequence constraint the rule configures. */
|
|
21
|
+
export declare function consecutiveConstraints(rule: ConsecutiveRule): SchedulingConstraint[];
|
|
22
|
+
export declare function consecutiveDays(maxDays: number): SchedulingConstraint;
|
|
23
|
+
export declare function consecutiveNights(rule: ConsecutiveRule): SchedulingConstraint;
|
|
24
|
+
export declare function shiftSuccession(forbidden: NonNullable<ConsecutiveRule['forbiddenSuccessions']>): SchedulingConstraint;
|
|
25
|
+
//# sourceMappingURL=consecutive.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"consecutive.d.ts","sourceRoot":"","sources":["../../../src/scheduling/constraints/consecutive.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,KAAK,EAAE,eAAe,EAAe,oBAAoB,EAA8B,MAAM,UAAU,CAAC;AAI/G,eAAO,MAAM,oBAAoB,qDAAqD,CAAC;AAEvF,qDAAqD;AACrD,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,eAAe,GAAG,oBAAoB,EAAE,CAQpF;AAED,wBAAgB,eAAe,CAAC,OAAO,EAAE,MAAM,GAAG,oBAAoB,CAiCrE;AAED,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,eAAe,GAAG,oBAAoB,CAqE7E;AAED,wBAAgB,eAAe,CAAC,SAAS,EAAE,WAAW,CAAC,eAAe,CAAC,sBAAsB,CAAC,CAAC,GAAG,oBAAoB,CA6DrH"}
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Sequence rules — limits on runs rather than on totals.
|
|
4
|
+
*
|
|
5
|
+
* These are the rules a per-assignment check structurally cannot see, because
|
|
6
|
+
* the breach lives in the *pattern*, not in any one shift:
|
|
7
|
+
*
|
|
8
|
+
* - **Consecutive working days.** Portugal caps a shift worker at six.
|
|
9
|
+
* - **Consecutive night shifts, and the rest owed when the run ends.** Finland
|
|
10
|
+
* allows five and then requires 24h off; the Netherlands allows seven (eight
|
|
11
|
+
* by collective agreement) and then requires 46h. Only NL and FI legislate a
|
|
12
|
+
* cap at all — elsewhere it is collective-agreement territory, which is
|
|
13
|
+
* exactly why it has to be configuration rather than a built-in number.
|
|
14
|
+
* - **Forbidden successions.** The "quick return" — a late shift followed by
|
|
15
|
+
* an early one — is the single most-requested constraint in nurse rostering
|
|
16
|
+
* and is invisible to a rest rule whenever the gap technically clears the
|
|
17
|
+
* minimum.
|
|
18
|
+
*/
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.shiftSuccession = exports.consecutiveNights = exports.consecutiveDays = exports.consecutiveConstraints = exports.CONSECUTIVE_CITATION = void 0;
|
|
21
|
+
const support_1 = require("./support");
|
|
22
|
+
exports.CONSECUTIVE_CITATION = 'National working-time law / collective agreement';
|
|
23
|
+
/** Every sequence constraint the rule configures. */
|
|
24
|
+
function consecutiveConstraints(rule) {
|
|
25
|
+
const out = [];
|
|
26
|
+
if (rule.maxWorkingDays !== undefined)
|
|
27
|
+
out.push(consecutiveDays(rule.maxWorkingDays));
|
|
28
|
+
if (rule.maxNightShifts !== undefined || rule.restAfterNightBlockMinutes !== undefined) {
|
|
29
|
+
out.push(consecutiveNights(rule));
|
|
30
|
+
}
|
|
31
|
+
if (rule.forbiddenSuccessions?.length)
|
|
32
|
+
out.push(shiftSuccession(rule.forbiddenSuccessions));
|
|
33
|
+
return out;
|
|
34
|
+
}
|
|
35
|
+
exports.consecutiveConstraints = consecutiveConstraints;
|
|
36
|
+
function consecutiveDays(maxDays) {
|
|
37
|
+
return (0, support_1.fromVerdict)({
|
|
38
|
+
id: 'consecutive-days',
|
|
39
|
+
hardness: 'hard',
|
|
40
|
+
weight: 1,
|
|
41
|
+
citation: exports.CONSECUTIVE_CITATION,
|
|
42
|
+
verdict(state, pair) {
|
|
43
|
+
const inst = (0, support_1.instanceOf)(state, pair);
|
|
44
|
+
if (!inst)
|
|
45
|
+
return (0, support_1.pass)('consecutive-days', 'unknown shift');
|
|
46
|
+
const timeline = (0, support_1.timelineFor)(state, pair.employeeId);
|
|
47
|
+
const probe = { ...(0, support_1.rangeOf)(inst), id: (0, support_1.entryIdOf)(pair), workingMinutes: inst.workingMinutes };
|
|
48
|
+
const clock = state.ctx.clock;
|
|
49
|
+
return timeline.withEntry(probe, (t) => {
|
|
50
|
+
const run = t.longestConsecutiveDays((minute) => clock.dayIndexOfMinute(minute));
|
|
51
|
+
if (run > maxDays) {
|
|
52
|
+
return (0, support_1.fail)('consecutive-days', 'hard', `employee "${pair.employeeId}" would work ${run} consecutive days, over the ${maxDays}-day maximum`, { actual: run, required: maxDays, unit: 'days', citation: exports.CONSECUTIVE_CITATION });
|
|
53
|
+
}
|
|
54
|
+
return (0, support_1.pass)('consecutive-days', `${run} consecutive days, within the ${maxDays}-day maximum`, {
|
|
55
|
+
actual: run,
|
|
56
|
+
required: maxDays,
|
|
57
|
+
unit: 'days',
|
|
58
|
+
citation: exports.CONSECUTIVE_CITATION,
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
exports.consecutiveDays = consecutiveDays;
|
|
65
|
+
function consecutiveNights(rule) {
|
|
66
|
+
return (0, support_1.fromVerdict)({
|
|
67
|
+
id: 'consecutive-nights',
|
|
68
|
+
hardness: 'hard',
|
|
69
|
+
weight: 1,
|
|
70
|
+
citation: exports.CONSECUTIVE_CITATION,
|
|
71
|
+
verdict(state, pair) {
|
|
72
|
+
const inst = (0, support_1.instanceOf)(state, pair);
|
|
73
|
+
if (!inst)
|
|
74
|
+
return (0, support_1.pass)('consecutive-nights', 'unknown shift');
|
|
75
|
+
const timeline = (0, support_1.timelineFor)(state, pair.employeeId);
|
|
76
|
+
const probe = {
|
|
77
|
+
...(0, support_1.rangeOf)(inst),
|
|
78
|
+
id: (0, support_1.entryIdOf)(pair),
|
|
79
|
+
workingMinutes: inst.workingMinutes,
|
|
80
|
+
tag: inst.shiftTypeTag,
|
|
81
|
+
};
|
|
82
|
+
return timeline.withEntry(probe, (t) => {
|
|
83
|
+
const nights = nightRuns(state, t);
|
|
84
|
+
if (rule.maxNightShifts !== undefined) {
|
|
85
|
+
const longest = Math.max(0, ...nights.map((run) => run.length));
|
|
86
|
+
if (longest > rule.maxNightShifts) {
|
|
87
|
+
return (0, support_1.fail)('consecutive-nights', 'hard', `employee "${pair.employeeId}" would work ${longest} consecutive night shifts, over the ${rule.maxNightShifts} maximum`, {
|
|
88
|
+
actual: longest,
|
|
89
|
+
required: rule.maxNightShifts,
|
|
90
|
+
unit: 'count',
|
|
91
|
+
citation: exports.CONSECUTIVE_CITATION,
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
// The rest owed once a night block ends. Only completed runs can
|
|
96
|
+
// breach it: a run still in progress has no "after" yet.
|
|
97
|
+
if (rule.restAfterNightBlockMinutes !== undefined) {
|
|
98
|
+
for (const run of nights) {
|
|
99
|
+
if (rule.maxNightShifts !== undefined && run.length < rule.maxNightShifts)
|
|
100
|
+
continue;
|
|
101
|
+
const endOfRun = run[run.length - 1].end;
|
|
102
|
+
const next = t.all().find((e) => e.start >= endOfRun && !run.includes(e));
|
|
103
|
+
if (!next)
|
|
104
|
+
continue;
|
|
105
|
+
const gap = next.start - endOfRun;
|
|
106
|
+
if (gap < rule.restAfterNightBlockMinutes) {
|
|
107
|
+
return (0, support_1.fail)('consecutive-nights', 'hard', `employee "${pair.employeeId}" would get ${(0, support_1.h)(gap)} after a block of ${run.length} night shifts, below the ${(0, support_1.h)(rule.restAfterNightBlockMinutes)} required`, {
|
|
108
|
+
actual: gap,
|
|
109
|
+
required: rule.restAfterNightBlockMinutes,
|
|
110
|
+
unit: 'minutes',
|
|
111
|
+
citation: exports.CONSECUTIVE_CITATION,
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return (0, support_1.pass)('consecutive-nights', 'night-shift run and post-block rest within limits', {
|
|
117
|
+
citation: exports.CONSECUTIVE_CITATION,
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
},
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
exports.consecutiveNights = consecutiveNights;
|
|
124
|
+
function shiftSuccession(forbidden) {
|
|
125
|
+
return (0, support_1.fromVerdict)({
|
|
126
|
+
id: 'shift-succession',
|
|
127
|
+
hardness: 'hard',
|
|
128
|
+
weight: 1,
|
|
129
|
+
citation: exports.CONSECUTIVE_CITATION,
|
|
130
|
+
verdict(state, pair) {
|
|
131
|
+
const inst = (0, support_1.instanceOf)(state, pair);
|
|
132
|
+
if (!inst)
|
|
133
|
+
return (0, support_1.pass)('shift-succession', 'unknown shift');
|
|
134
|
+
const timeline = (0, support_1.timelineFor)(state, pair.employeeId);
|
|
135
|
+
const range = (0, support_1.rangeOf)(inst);
|
|
136
|
+
const ownId = (0, support_1.entryIdOf)(pair);
|
|
137
|
+
const clock = state.ctx.clock;
|
|
138
|
+
const entries = timeline.all().filter((e) => e.id !== ownId);
|
|
139
|
+
// Nearest neighbour on each side, and only when it is close enough
|
|
140
|
+
// to be a *succession*. "Night followed by early" means the next
|
|
141
|
+
// day, not the next shift whenever it happens to fall: without a
|
|
142
|
+
// bound, a night four days earlier is still the nearest preceding
|
|
143
|
+
// shift and every later early would breach.
|
|
144
|
+
const ownDay = clock.dayIndexOfMinute(range.start);
|
|
145
|
+
const adjacent = (entry) => Math.abs(clock.dayIndexOfMinute(entry.start) - ownDay) <= 1;
|
|
146
|
+
const before = [...entries].reverse().find((e) => e.end <= range.start && adjacent(e));
|
|
147
|
+
const after = entries.find((e) => e.start >= range.end && adjacent(e));
|
|
148
|
+
for (const succession of forbidden) {
|
|
149
|
+
const check = (fromTag, toTag, gap) => {
|
|
150
|
+
if (fromTag !== succession.fromTag || toTag !== succession.toTag)
|
|
151
|
+
return null;
|
|
152
|
+
if (succession.minGapMinutes !== undefined && gap >= succession.minGapMinutes)
|
|
153
|
+
return null;
|
|
154
|
+
const detail = succession.minGapMinutes === undefined
|
|
155
|
+
? 'is not allowed'
|
|
156
|
+
: `needs at least ${(0, support_1.h)(succession.minGapMinutes)}, got ${(0, support_1.h)(gap)}`;
|
|
157
|
+
return (0, support_1.fail)('shift-succession', 'hard', `employee "${pair.employeeId}": a "${succession.fromTag}" shift followed by a "${succession.toTag}" shift ${detail}`, {
|
|
158
|
+
actual: gap,
|
|
159
|
+
required: succession.minGapMinutes,
|
|
160
|
+
unit: 'minutes',
|
|
161
|
+
citation: exports.CONSECUTIVE_CITATION,
|
|
162
|
+
});
|
|
163
|
+
};
|
|
164
|
+
if (before) {
|
|
165
|
+
const breach = check(before.tag, inst.shiftTypeTag, range.start - before.end);
|
|
166
|
+
if (breach)
|
|
167
|
+
return breach;
|
|
168
|
+
}
|
|
169
|
+
if (after) {
|
|
170
|
+
const breach = check(inst.shiftTypeTag, after.tag, after.start - range.end);
|
|
171
|
+
if (breach)
|
|
172
|
+
return breach;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return (0, support_1.pass)('shift-succession', 'shift-type succession allowed', { citation: exports.CONSECUTIVE_CITATION });
|
|
176
|
+
},
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
exports.shiftSuccession = shiftSuccession;
|
|
180
|
+
/**
|
|
181
|
+
* Runs of night shifts on consecutive days.
|
|
182
|
+
*
|
|
183
|
+
* "Consecutive" is by calendar day, not by adjacency in the entry list: a night
|
|
184
|
+
* shift, a day off, then another night shift is two runs of one, even though
|
|
185
|
+
* they are neighbours on the timeline.
|
|
186
|
+
*/
|
|
187
|
+
function nightRuns(state, timeline) {
|
|
188
|
+
const clock = state.ctx.clock;
|
|
189
|
+
const nights = timeline.all().filter((e) => isNight(state, e));
|
|
190
|
+
const runs = [];
|
|
191
|
+
let current = [];
|
|
192
|
+
let previousDay;
|
|
193
|
+
for (const entry of nights) {
|
|
194
|
+
const day = clock.dayIndexOfMinute(entry.start);
|
|
195
|
+
if (previousDay !== undefined && day === previousDay + 1)
|
|
196
|
+
current.push(entry);
|
|
197
|
+
else {
|
|
198
|
+
if (current.length)
|
|
199
|
+
runs.push(current);
|
|
200
|
+
current = [entry];
|
|
201
|
+
}
|
|
202
|
+
previousDay = day;
|
|
203
|
+
}
|
|
204
|
+
if (current.length)
|
|
205
|
+
runs.push(current);
|
|
206
|
+
return runs;
|
|
207
|
+
}
|
|
208
|
+
function isNight(state, entry) {
|
|
209
|
+
const instanceId = entry.id.includes('@@') ? entry.id.slice(entry.id.indexOf('@@') + 2) : entry.id;
|
|
210
|
+
const inst = state.ctx.instanceById.get(instanceId);
|
|
211
|
+
if (inst)
|
|
212
|
+
return inst.isNightShift;
|
|
213
|
+
// Historical entries carry only their tag.
|
|
214
|
+
return entry.tag === 'night';
|
|
215
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Constraint registry — pure logic.
|
|
3
|
+
*
|
|
4
|
+
* Built-ins are the legacy rules re-derived correctly; the registry lets
|
|
5
|
+
* callers override hardness/weights or add customs without engine surgery.
|
|
6
|
+
* Adding a built-in = new file here plus one line in `createDefaultConstraints`.
|
|
7
|
+
*
|
|
8
|
+
* `one-shift-per-day` is opt-in rather than always-on: split shifts are lawful
|
|
9
|
+
* and common (Directive 2003/88/EC Art 17(4)(b) names them explicitly), as is
|
|
10
|
+
* an on-call duty attached to the same day as a rostered shift. `no-overlap`
|
|
11
|
+
* plus `min-rest` already prevent the physically impossible cases, so forcing
|
|
12
|
+
* one shift per calendar day is a policy choice, not a safety net.
|
|
13
|
+
*/
|
|
14
|
+
import type { ObjectiveWeights, SchedulingConstraint, WorkingTimeRules } from '../types';
|
|
15
|
+
export interface DefaultConstraintOptions {
|
|
16
|
+
/** Minimum rest between consecutive assignments, in minutes. */
|
|
17
|
+
minRestMinutes: number;
|
|
18
|
+
/** Whether to enforce at most one shift per calendar day. Defaults to false. */
|
|
19
|
+
oneShiftPerDay?: boolean;
|
|
20
|
+
/** The labour-law layer. Each family present here registers its constraint. */
|
|
21
|
+
rules?: WorkingTimeRules;
|
|
22
|
+
/** Optional soft-objective terms; a positive cost weight registers the cost term. */
|
|
23
|
+
objectives?: ObjectiveWeights;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* The built-in constraints in evaluation order.
|
|
27
|
+
*
|
|
28
|
+
* The always-on set is the physical/contractual core. Everything driven by
|
|
29
|
+
* `WorkingTimeRules` registers only when its rule family is present, so a
|
|
30
|
+
* caller who supplies no rules pays for nothing, and a sectoral regime that
|
|
31
|
+
* *replaces* a Working Time Directive rule (Art 14/20/21) omits it rather than
|
|
32
|
+
* widening its bounds.
|
|
33
|
+
*/
|
|
34
|
+
export declare function createDefaultConstraints(options: DefaultConstraintOptions): SchedulingConstraint[];
|
|
35
|
+
//# sourceMappingURL=constraint.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constraint.d.ts","sourceRoot":"","sources":["../../../src/scheduling/constraints/constraint.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,oBAAoB,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AA4BzF,MAAM,WAAW,wBAAwB;IACrC,gEAAgE;IAChE,cAAc,EAAE,MAAM,CAAC;IACvB,gFAAgF;IAChF,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,+EAA+E;IAC/E,KAAK,CAAC,EAAE,gBAAgB,CAAC;IACzB,qFAAqF;IACrF,UAAU,CAAC,EAAE,gBAAgB,CAAC;CACjC;AAED;;;;;;;;GAQG;AACH,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,wBAAwB,GAAG,oBAAoB,EAAE,CAkClG"}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Constraint registry — pure logic.
|
|
4
|
+
*
|
|
5
|
+
* Built-ins are the legacy rules re-derived correctly; the registry lets
|
|
6
|
+
* callers override hardness/weights or add customs without engine surgery.
|
|
7
|
+
* Adding a built-in = new file here plus one line in `createDefaultConstraints`.
|
|
8
|
+
*
|
|
9
|
+
* `one-shift-per-day` is opt-in rather than always-on: split shifts are lawful
|
|
10
|
+
* and common (Directive 2003/88/EC Art 17(4)(b) names them explicitly), as is
|
|
11
|
+
* an on-call duty attached to the same day as a rostered shift. `no-overlap`
|
|
12
|
+
* plus `min-rest` already prevent the physically impossible cases, so forcing
|
|
13
|
+
* one shift per calendar day is a policy choice, not a safety net.
|
|
14
|
+
*/
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.createDefaultConstraints = void 0;
|
|
17
|
+
const one_shift_per_day_1 = require("./one-shift-per-day");
|
|
18
|
+
const no_overlap_1 = require("./no-overlap");
|
|
19
|
+
const min_staffing_1 = require("./min-staffing");
|
|
20
|
+
const time_off_1 = require("./time-off");
|
|
21
|
+
const hour_budget_1 = require("./hour-budget");
|
|
22
|
+
const max_shift_duration_1 = require("./max-shift-duration");
|
|
23
|
+
const min_rest_1 = require("./min-rest");
|
|
24
|
+
const daily_rest_1 = require("./daily-rest");
|
|
25
|
+
const weekly_rest_1 = require("./weekly-rest");
|
|
26
|
+
const rolling_hours_1 = require("./rolling-hours");
|
|
27
|
+
const night_work_1 = require("./night-work");
|
|
28
|
+
const consecutive_1 = require("./consecutive");
|
|
29
|
+
const start_interval_1 = require("./start-interval");
|
|
30
|
+
const in_shift_breaks_1 = require("./in-shift-breaks");
|
|
31
|
+
const rest_days_1 = require("./rest-days");
|
|
32
|
+
const engagement_floor_1 = require("./engagement-floor");
|
|
33
|
+
const notice_1 = require("./notice");
|
|
34
|
+
const fairness_1 = require("./fairness");
|
|
35
|
+
const availability_1 = require("./availability");
|
|
36
|
+
const qualification_1 = require("./qualification");
|
|
37
|
+
const group_composition_1 = require("./group-composition");
|
|
38
|
+
const protections_1 = require("./protections");
|
|
39
|
+
const contract_1 = require("./contract");
|
|
40
|
+
const overtime_1 = require("./overtime");
|
|
41
|
+
const duty_quota_1 = require("./duty-quota");
|
|
42
|
+
const cost_1 = require("./cost");
|
|
43
|
+
/**
|
|
44
|
+
* The built-in constraints in evaluation order.
|
|
45
|
+
*
|
|
46
|
+
* The always-on set is the physical/contractual core. Everything driven by
|
|
47
|
+
* `WorkingTimeRules` registers only when its rule family is present, so a
|
|
48
|
+
* caller who supplies no rules pays for nothing, and a sectoral regime that
|
|
49
|
+
* *replaces* a Working Time Directive rule (Art 14/20/21) omits it rather than
|
|
50
|
+
* widening its bounds.
|
|
51
|
+
*/
|
|
52
|
+
function createDefaultConstraints(options) {
|
|
53
|
+
const constraints = [];
|
|
54
|
+
const rules = options.rules ?? {};
|
|
55
|
+
if (options.oneShiftPerDay)
|
|
56
|
+
constraints.push((0, one_shift_per_day_1.oneShiftPerDay)());
|
|
57
|
+
constraints.push((0, no_overlap_1.noOverlap)(), (0, min_staffing_1.minStaffing)(), (0, time_off_1.timeOff)(), (0, hour_budget_1.hourBudget)(), (0, max_shift_duration_1.maxShiftDuration)());
|
|
58
|
+
// Rest: the rule-driven form supersedes the simple gap check when configured.
|
|
59
|
+
if (rules.dailyRest)
|
|
60
|
+
constraints.push((0, daily_rest_1.dailyRest)(rules.dailyRest));
|
|
61
|
+
else
|
|
62
|
+
constraints.push((0, min_rest_1.minRest)(options.minRestMinutes));
|
|
63
|
+
if (rules.weeklyRest)
|
|
64
|
+
constraints.push((0, weekly_rest_1.weeklyRest)(rules.weeklyRest));
|
|
65
|
+
if (rules.workingTime)
|
|
66
|
+
constraints.push((0, rolling_hours_1.rollingHours)(rules.workingTime));
|
|
67
|
+
if (rules.overtime)
|
|
68
|
+
constraints.push((0, overtime_1.overtime)(rules.overtime));
|
|
69
|
+
if (rules.dutyQuotas?.length)
|
|
70
|
+
constraints.push((0, duty_quota_1.dutyQuotas)(rules.dutyQuotas));
|
|
71
|
+
if (rules.nightWork)
|
|
72
|
+
constraints.push((0, night_work_1.nightWork)(rules.nightWork));
|
|
73
|
+
if (rules.consecutive)
|
|
74
|
+
constraints.push(...(0, consecutive_1.consecutiveConstraints)(rules.consecutive));
|
|
75
|
+
if (rules.minimumStartInterval)
|
|
76
|
+
constraints.push((0, start_interval_1.startInterval)(rules.minimumStartInterval));
|
|
77
|
+
if (rules.breaks?.length)
|
|
78
|
+
constraints.push((0, in_shift_breaks_1.inShiftBreaks)(rules.breaks));
|
|
79
|
+
if (rules.restDays)
|
|
80
|
+
constraints.push((0, rest_days_1.restDays)(rules.restDays));
|
|
81
|
+
if (rules.engagement)
|
|
82
|
+
constraints.push((0, engagement_floor_1.engagementFloor)(rules.engagement));
|
|
83
|
+
if (rules.notice)
|
|
84
|
+
constraints.push((0, notice_1.noticeRule)(rules.notice));
|
|
85
|
+
if (rules.fairness?.length)
|
|
86
|
+
constraints.push((0, fairness_1.fairness)(rules.fairness));
|
|
87
|
+
// Always-on person-scoped rules: they no-op unless the employee carries the
|
|
88
|
+
// relevant data, so they cost nothing for simple rosters.
|
|
89
|
+
constraints.push((0, availability_1.availability)(), (0, qualification_1.qualification)(), (0, group_composition_1.groupComposition)(), (0, protections_1.protections)(), (0, contract_1.contractLimits)());
|
|
90
|
+
// The cost term is score-only: its verdict always passes, so it steers the
|
|
91
|
+
// search without ever appearing as a violation.
|
|
92
|
+
const costWeight = options.objectives?.costWeightPerEuro;
|
|
93
|
+
if (costWeight !== undefined && costWeight > 0)
|
|
94
|
+
constraints.push((0, cost_1.costObjective)(costWeight));
|
|
95
|
+
return constraints;
|
|
96
|
+
}
|
|
97
|
+
exports.createDefaultConstraints = createDefaultConstraints;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Contract limits.
|
|
3
|
+
*
|
|
4
|
+
* Two shapes:
|
|
5
|
+
*
|
|
6
|
+
* - **Hours contracts** carry period bounds in minutes, which sit alongside
|
|
7
|
+
* the statutory limits rather than replacing them — a contract may be
|
|
8
|
+
* stricter than the law but never looser.
|
|
9
|
+
* - **Day-count contracts** have no hours limit at all. The French
|
|
10
|
+
* *forfait jours* fixes 218 working *days* a year for autonomous staff, and
|
|
11
|
+
* the daily and weekly hour caps do not apply to them — but the 11h daily
|
|
12
|
+
* and 35h weekly rest still do. Modelling that as a very large hours budget
|
|
13
|
+
* would be wrong in both directions, so it gets its own counter.
|
|
14
|
+
*
|
|
15
|
+
* A contract end date also bounds eligibility: a fixed-term worker cannot be
|
|
16
|
+
* rostered past their last day, which is easy to miss when the roster period
|
|
17
|
+
* outruns the contract.
|
|
18
|
+
*/
|
|
19
|
+
import type { SchedulingConstraint } from '../types';
|
|
20
|
+
export declare function contractLimits(): SchedulingConstraint;
|
|
21
|
+
//# sourceMappingURL=contract.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"contract.d.ts","sourceRoot":"","sources":["../../../src/scheduling/constraints/contract.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,KAAK,EAAoC,oBAAoB,EAAE,MAAM,UAAU,CAAC;AAGvF,wBAAgB,cAAc,IAAI,oBAAoB,CAmGrD"}
|