@jsb188/schedule 1.1.11
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/package.json +61 -0
- package/src/constants/schedule.ts +549 -0
- package/src/css.d.ts +7 -0
- package/src/libs/gantt-geometry.ts +771 -0
- package/src/libs/gantt-histogram.ts +124 -0
- package/src/libs/gantt-keyboard.ts +113 -0
- package/src/libs/gantt-state.ts +57 -0
- package/src/libs/gantt-url.ts +63 -0
- package/src/modules/Gantt.helpers.ts +138 -0
- package/src/modules/Gantt.tsx +719 -0
- package/src/types/schedule.d.ts +1149 -0
- package/src/ui/ActivityFormUI.tsx +133 -0
- package/src/ui/GanttEditorsUI.tsx +102 -0
- package/src/ui/GanttHistogramUI.tsx +236 -0
- package/src/ui/GanttUI.tsx +792 -0
- package/src/ui/ProjectFormUI.tsx +273 -0
- package/src/ui/ProjectMobileListUI.tsx +80 -0
- package/src/ui/ProjectToolbarUI.tsx +54 -0
- package/src/ui/RelationshipListUI.tsx +176 -0
- package/src/ui/ScheduleRunUI.tsx +173 -0
- package/src/ui/ScheduleSettingsUI.tsx +420 -0
- package/src/utils/coverage.helpers.ts +679 -0
- package/src/utils/coverage.ts +630 -0
- package/src/utils/cpm.helpers.ts +674 -0
- package/src/utils/cpm.ts +907 -0
- package/src/utils/cpmLeveling.helpers.ts +564 -0
- package/src/utils/cpmLeveling.ts +367 -0
- package/src/utils/defaults.ts +8 -0
- package/src/utils/eligibility.ts +457 -0
- package/src/utils/planCsv.ts +344 -0
- package/src/utils/predecessorText.ts +96 -0
- package/src/utils/recurrence.ts +616 -0
- package/src/utils/request.ts +153 -0
- package/src/utils/resource.ts +182 -0
- package/src/utils/rule.ts +216 -0
- package/src/utils/run.ts +43 -0
- package/src/utils/series.ts +161 -0
- package/src/utils/slot.ts +208 -0
- package/src/utils/workTime.ts +1382 -0
- package/tests/coverage.fixtures.ts +111 -0
- package/tests/coverage.test.ts +503 -0
- package/tests/cpm.bench.test.ts +154 -0
- package/tests/cpm.test.ts +354 -0
- package/tests/cpmCorrectness.test.ts +327 -0
- package/tests/cpmLeveling.test.ts +471 -0
- package/tests/eligibility.test.ts +320 -0
- package/tests/gantt-geometry.test.ts +367 -0
- package/tests/gantt-histogram.test.ts +78 -0
- package/tests/gantt-keyboard.test.ts +69 -0
- package/tests/helpers/cpmFixtures.ts +101 -0
- package/tests/projectInputs.test.ts +165 -0
- package/tests/recurrence.test.ts +68 -0
- package/tests/request.test.ts +99 -0
- package/tests/resource.test.ts +95 -0
- package/tests/rule.test.ts +98 -0
- package/tests/series.test.ts +120 -0
- package/tests/workTime.test.ts +1007 -0
- package/tests/workTimeContracts.test.ts +111 -0
- package/tsconfig.json +13 -0
- package/vitest.config.ts +8 -0
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { SCHEDULE_CALENDAR_DEFAULT_SETTINGS } from '../constants/schedule.ts';
|
|
2
|
+
import type {
|
|
3
|
+
ScheduleRequestKindEnum,
|
|
4
|
+
ScheduleRequestPolicyObj,
|
|
5
|
+
ScheduleRequestStatusEnum,
|
|
6
|
+
} from '../types/schedule.d.ts';
|
|
7
|
+
|
|
8
|
+
export type ScheduleRequestActor =
|
|
9
|
+
| 'REQUESTER'
|
|
10
|
+
| 'COUNTERPARTY'
|
|
11
|
+
| 'MANAGER'
|
|
12
|
+
| 'SYSTEM';
|
|
13
|
+
export type ScheduleRequestTransition =
|
|
14
|
+
| 'CREATE'
|
|
15
|
+
| 'AUTO_APPROVE'
|
|
16
|
+
| 'CONSENT'
|
|
17
|
+
| 'REFUSE_CONSENT'
|
|
18
|
+
| 'WITHDRAW'
|
|
19
|
+
| 'APPROVE'
|
|
20
|
+
| 'DECLINE'
|
|
21
|
+
| 'EXPIRE'
|
|
22
|
+
| 'CASCADE_CANCEL';
|
|
23
|
+
export type ScheduleRequestVerb =
|
|
24
|
+
| 'approve'
|
|
25
|
+
| 'decline'
|
|
26
|
+
| 'consent'
|
|
27
|
+
| 'refuse'
|
|
28
|
+
| 'withdraw';
|
|
29
|
+
|
|
30
|
+
const FILE_CONSTANTS = {
|
|
31
|
+
live: ['PENDING', 'ACCEPTED_BY_COUNTERPARTY'] as ScheduleRequestStatusEnum[],
|
|
32
|
+
verbs: {
|
|
33
|
+
approve: 'APPROVE',
|
|
34
|
+
decline: 'DECLINE',
|
|
35
|
+
consent: 'CONSENT',
|
|
36
|
+
refuse: 'REFUSE_CONSENT',
|
|
37
|
+
withdraw: 'WITHDRAW',
|
|
38
|
+
} as Record<ScheduleRequestVerb, ScheduleRequestTransition>,
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/* Return the live states an authorized actor may leave through this transition. */
|
|
42
|
+
export function getScheduleRequestTransitionSources(
|
|
43
|
+
kind: ScheduleRequestKindEnum,
|
|
44
|
+
transition: ScheduleRequestTransition,
|
|
45
|
+
actor: ScheduleRequestActor,
|
|
46
|
+
_policy: ScheduleRequestPolicyObj,
|
|
47
|
+
): ScheduleRequestStatusEnum[] {
|
|
48
|
+
if (
|
|
49
|
+
actor === 'SYSTEM' &&
|
|
50
|
+
(transition === 'EXPIRE' || transition === 'CASCADE_CANCEL')
|
|
51
|
+
)
|
|
52
|
+
return [...FILE_CONSTANTS.live];
|
|
53
|
+
if (actor === 'REQUESTER' && transition === 'WITHDRAW')
|
|
54
|
+
return [...FILE_CONSTANTS.live];
|
|
55
|
+
if (actor === 'COUNTERPARTY' && kind === 'SWAP') {
|
|
56
|
+
if (transition === 'CONSENT') return ['PENDING'];
|
|
57
|
+
if (transition === 'REFUSE_CONSENT') return [...FILE_CONSTANTS.live];
|
|
58
|
+
}
|
|
59
|
+
if (actor === 'MANAGER') {
|
|
60
|
+
if (transition === 'DECLINE') return [...FILE_CONSTANTS.live];
|
|
61
|
+
if (transition === 'APPROVE')
|
|
62
|
+
return kind === 'SWAP'
|
|
63
|
+
? ['ACCEPTED_BY_COUNTERPARTY']
|
|
64
|
+
: ['PENDING'];
|
|
65
|
+
}
|
|
66
|
+
return [];
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/* Auto-approve only a clean claim or drop made by the worker under an enabled policy. */
|
|
70
|
+
export function getScheduleRequestInitialStatus(
|
|
71
|
+
kind: ScheduleRequestKindEnum,
|
|
72
|
+
policy: ScheduleRequestPolicyObj,
|
|
73
|
+
verdictClean: boolean,
|
|
74
|
+
onBehalf: boolean,
|
|
75
|
+
): 'PENDING' | 'APPROVED' {
|
|
76
|
+
return verdictClean &&
|
|
77
|
+
!onBehalf &&
|
|
78
|
+
((kind === 'CLAIM' && policy.autoApproveClaims) ||
|
|
79
|
+
(kind === 'DROP' && policy.autoApproveDrops))
|
|
80
|
+
? 'APPROVED'
|
|
81
|
+
: 'PENDING';
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/* Bound a request's lifetime by its subject shift, including policies with no hour cap. */
|
|
85
|
+
export function getScheduleRequestExpiry(
|
|
86
|
+
policy: ScheduleRequestPolicyObj,
|
|
87
|
+
now: Date,
|
|
88
|
+
subjectStartAt: Date,
|
|
89
|
+
): Date {
|
|
90
|
+
return new Date(
|
|
91
|
+
policy.requestExpiryHours > 0
|
|
92
|
+
? Math.min(
|
|
93
|
+
now.getTime() + policy.requestExpiryHours * 3_600_000,
|
|
94
|
+
subjectStartAt.getTime(),
|
|
95
|
+
)
|
|
96
|
+
: subjectStartAt.getTime(),
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/* Resolve known request policy fields and reject invalid persisted or edited settings. */
|
|
101
|
+
export function getScheduleRequestPolicy(calendar: {
|
|
102
|
+
settings?: unknown;
|
|
103
|
+
}): ScheduleRequestPolicyObj {
|
|
104
|
+
const settings =
|
|
105
|
+
calendar.settings && typeof calendar.settings === 'object'
|
|
106
|
+
? (calendar.settings as Record<string, unknown>)
|
|
107
|
+
: {};
|
|
108
|
+
const raw =
|
|
109
|
+
settings.requests && typeof settings.requests === 'object'
|
|
110
|
+
? (settings.requests as Record<string, unknown>)
|
|
111
|
+
: {};
|
|
112
|
+
const policy: ScheduleRequestPolicyObj = {
|
|
113
|
+
...SCHEDULE_CALENDAR_DEFAULT_SETTINGS.requests,
|
|
114
|
+
};
|
|
115
|
+
for (const key of [
|
|
116
|
+
'autoApproveClaims',
|
|
117
|
+
'autoApproveDrops',
|
|
118
|
+
] as const) {
|
|
119
|
+
if (raw[key] !== undefined) {
|
|
120
|
+
if (typeof raw[key] !== 'boolean') throw new Error('20150 POLICY');
|
|
121
|
+
policy[key] = raw[key];
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
if (raw.requireCounterpartyConsent !== undefined && raw.requireCounterpartyConsent !== true) throw new Error('20150 POLICY');
|
|
125
|
+
for (const key of ['minNoticeHours', 'requestExpiryHours'] as const) {
|
|
126
|
+
if (raw[key] !== undefined) {
|
|
127
|
+
if (
|
|
128
|
+
typeof raw[key] !== 'number' ||
|
|
129
|
+
!Number.isFinite(raw[key]) ||
|
|
130
|
+
raw[key] < 0 ||
|
|
131
|
+
raw[key] > 8760
|
|
132
|
+
)
|
|
133
|
+
throw new Error('20150 POLICY');
|
|
134
|
+
policy[key] = raw[key];
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return policy;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/* Keep client actions aligned with the server's conditional transition predicates. */
|
|
141
|
+
export function canActOnScheduleRequest(
|
|
142
|
+
verb: ScheduleRequestVerb,
|
|
143
|
+
request: { kind: ScheduleRequestKindEnum; status: ScheduleRequestStatusEnum },
|
|
144
|
+
actor: ScheduleRequestActor,
|
|
145
|
+
policy: ScheduleRequestPolicyObj,
|
|
146
|
+
): boolean {
|
|
147
|
+
return getScheduleRequestTransitionSources(
|
|
148
|
+
request.kind,
|
|
149
|
+
FILE_CONSTANTS.verbs[verb],
|
|
150
|
+
actor,
|
|
151
|
+
policy,
|
|
152
|
+
).includes(request.status);
|
|
153
|
+
}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SCHEDULE_KEY_MAX_LENGTH,
|
|
3
|
+
SCHEDULE_NOTIFY_CHANNEL_ENUMS,
|
|
4
|
+
SCHEDULE_RESOURCE_MAX_QUALIFICATIONS,
|
|
5
|
+
SCHEDULE_RESOURCE_MAX_ROLES,
|
|
6
|
+
SCHEDULE_RESOURCE_MAX_TAGS,
|
|
7
|
+
} from '../constants/schedule.ts';
|
|
8
|
+
import type {
|
|
9
|
+
CoverageDemandInput,
|
|
10
|
+
ScheduleQualificationObj,
|
|
11
|
+
ScheduleResourceConfigObj,
|
|
12
|
+
ScheduleResourceKindEnum,
|
|
13
|
+
} from '../types/schedule.d.ts';
|
|
14
|
+
|
|
15
|
+
/* Validate a closed record before reading optional configuration fields. */
|
|
16
|
+
export function isScheduleRecord(
|
|
17
|
+
value: unknown,
|
|
18
|
+
): value is Record<string, unknown> {
|
|
19
|
+
return !!value && typeof value === 'object' && !Array.isArray(value);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/* Normalize a bounded open key without coercing invalid values into names. */
|
|
23
|
+
export function getScheduleKey(value: unknown): string | null {
|
|
24
|
+
return typeof value === 'string' &&
|
|
25
|
+
value.trim().length > 0 &&
|
|
26
|
+
value.trim().length <= SCHEDULE_KEY_MAX_LENGTH
|
|
27
|
+
? value.trim()
|
|
28
|
+
: null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/* Validate, deduplicate and sort a bounded list of resource or predicate keys. */
|
|
32
|
+
export function validateScheduleKeyList(
|
|
33
|
+
value: unknown,
|
|
34
|
+
limit: number,
|
|
35
|
+
): string[] | null {
|
|
36
|
+
if (!Array.isArray(value) || value.length > limit) return null;
|
|
37
|
+
const keys = value.map(getScheduleKey);
|
|
38
|
+
return keys.some((key) => key === null)
|
|
39
|
+
? null
|
|
40
|
+
: [...new Set(keys as string[])].sort();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/* Validate the closed resource configuration used by eligibility and notifications. */
|
|
44
|
+
export function validateScheduleResourceConfig(
|
|
45
|
+
kind: ScheduleResourceKindEnum,
|
|
46
|
+
input: unknown,
|
|
47
|
+
): { config?: ScheduleResourceConfigObj; error?: string } {
|
|
48
|
+
const raw = input ?? {};
|
|
49
|
+
if (!isScheduleRecord(raw))
|
|
50
|
+
return { error: 'Resource configuration must be an object.' };
|
|
51
|
+
const unknown = Object.keys(raw).find(
|
|
52
|
+
(key) =>
|
|
53
|
+
!['qualifications', 'roles', 'tags', 'ageGroup', 'notify'].includes(key),
|
|
54
|
+
);
|
|
55
|
+
if (unknown)
|
|
56
|
+
return { error: `Unknown resource configuration key: ${unknown}.` };
|
|
57
|
+
const config: ScheduleResourceConfigObj = {
|
|
58
|
+
qualifications: [],
|
|
59
|
+
roles: [],
|
|
60
|
+
tags: [],
|
|
61
|
+
ageGroup: null,
|
|
62
|
+
notify: null,
|
|
63
|
+
};
|
|
64
|
+
for (const field of ['roles', 'tags'] as const) {
|
|
65
|
+
if (raw[field] === undefined) continue;
|
|
66
|
+
const keys = validateScheduleKeyList(
|
|
67
|
+
raw[field],
|
|
68
|
+
field === 'roles'
|
|
69
|
+
? SCHEDULE_RESOURCE_MAX_ROLES
|
|
70
|
+
: SCHEDULE_RESOURCE_MAX_TAGS,
|
|
71
|
+
);
|
|
72
|
+
if (!keys) return { error: `Invalid resource ${field}.` };
|
|
73
|
+
if (field === 'roles' && kind !== 'LABOR' && keys.length)
|
|
74
|
+
return { error: 'Only LABOR resources may have roles.' };
|
|
75
|
+
config[field] = keys;
|
|
76
|
+
}
|
|
77
|
+
if (raw.qualifications !== undefined) {
|
|
78
|
+
if (
|
|
79
|
+
!Array.isArray(raw.qualifications) ||
|
|
80
|
+
raw.qualifications.length > SCHEDULE_RESOURCE_MAX_QUALIFICATIONS
|
|
81
|
+
)
|
|
82
|
+
return { error: 'Invalid resource qualifications.' };
|
|
83
|
+
if (kind !== 'LABOR' && raw.qualifications.length)
|
|
84
|
+
return { error: 'Only LABOR resources may have qualifications.' };
|
|
85
|
+
const seen = new Set<string>();
|
|
86
|
+
for (const item of raw.qualifications) {
|
|
87
|
+
if (
|
|
88
|
+
!isScheduleRecord(item) ||
|
|
89
|
+
Object.keys(item).some((key) => !['key', 'expiresAt'].includes(key))
|
|
90
|
+
)
|
|
91
|
+
return { error: 'Invalid qualification configuration.' };
|
|
92
|
+
const key = getScheduleKey(item.key);
|
|
93
|
+
if (!key || seen.has(key))
|
|
94
|
+
return {
|
|
95
|
+
error: `Invalid or duplicate qualification key: ${String(item.key)}.`,
|
|
96
|
+
};
|
|
97
|
+
let expiresAt: string | null = null;
|
|
98
|
+
if (item.expiresAt != null) {
|
|
99
|
+
const ms =
|
|
100
|
+
typeof item.expiresAt === 'string' ? Date.parse(item.expiresAt) : NaN;
|
|
101
|
+
if (!Number.isFinite(ms))
|
|
102
|
+
return { error: `Invalid qualification expiry for ${key}.` };
|
|
103
|
+
expiresAt = new Date(ms).toISOString();
|
|
104
|
+
}
|
|
105
|
+
seen.add(key);
|
|
106
|
+
config.qualifications.push({ key, expiresAt });
|
|
107
|
+
}
|
|
108
|
+
config.qualifications.sort((a, b) => a.key.localeCompare(b.key));
|
|
109
|
+
}
|
|
110
|
+
if (raw.ageGroup != null) {
|
|
111
|
+
const key = getScheduleKey(raw.ageGroup);
|
|
112
|
+
if (kind !== 'SPACE' || !key)
|
|
113
|
+
return { error: 'Only SPACE resources may have a valid age group.' };
|
|
114
|
+
config.ageGroup = key;
|
|
115
|
+
}
|
|
116
|
+
if (raw.notify != null) {
|
|
117
|
+
if (
|
|
118
|
+
kind !== 'LABOR' ||
|
|
119
|
+
!isScheduleRecord(raw.notify) ||
|
|
120
|
+
Object.keys(raw.notify).some((key) => key !== 'channels') ||
|
|
121
|
+
!Array.isArray(raw.notify.channels) ||
|
|
122
|
+
raw.notify.channels.some(
|
|
123
|
+
(channel) => !SCHEDULE_NOTIFY_CHANNEL_ENUMS.includes(channel),
|
|
124
|
+
)
|
|
125
|
+
)
|
|
126
|
+
return { error: 'Invalid resource notification preferences.' };
|
|
127
|
+
config.notify = { channels: [...new Set(raw.notify.channels)] };
|
|
128
|
+
}
|
|
129
|
+
return { config };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/* Treat malformed expiry data as expired rather than granting indefinite validity. */
|
|
133
|
+
export function getQualificationExpiryMs(
|
|
134
|
+
qualification: Pick<ScheduleQualificationObj, 'expiresAt'>,
|
|
135
|
+
): number | null {
|
|
136
|
+
if (qualification.expiresAt == null) return null;
|
|
137
|
+
const ms = Date.parse(qualification.expiresAt);
|
|
138
|
+
return Number.isFinite(ms) ? ms : 0;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/* Validate a demand interval and normalize its room, age-group key and timestamps. */
|
|
142
|
+
export function validateScheduleDemandRow(input: unknown): {
|
|
143
|
+
demand?: Omit<CoverageDemandInput, 'id'> & { id?: string };
|
|
144
|
+
error?: string;
|
|
145
|
+
} {
|
|
146
|
+
if (!isScheduleRecord(input)) return { error: 'Demand must be an object.' };
|
|
147
|
+
const key = getScheduleKey(input.key);
|
|
148
|
+
const resourceId =
|
|
149
|
+
typeof input.resourceId === 'string' ? input.resourceId.trim() : '';
|
|
150
|
+
const quantity = input.quantity;
|
|
151
|
+
if (!key || !resourceId)
|
|
152
|
+
return { error: 'Demand requires a room and a valid key.' };
|
|
153
|
+
if (
|
|
154
|
+
typeof quantity !== 'number' ||
|
|
155
|
+
!Number.isFinite(quantity) ||
|
|
156
|
+
quantity < 0 ||
|
|
157
|
+
Math.abs(quantity * 100 - Math.round(quantity * 100)) > 1e-7
|
|
158
|
+
)
|
|
159
|
+
return {
|
|
160
|
+
error:
|
|
161
|
+
'Demand quantity must be non-negative with at most two decimal places.',
|
|
162
|
+
};
|
|
163
|
+
const startAt =
|
|
164
|
+
input.startAt instanceof Date
|
|
165
|
+
? input.startAt
|
|
166
|
+
: new Date(input.startAt as string);
|
|
167
|
+
const endAt =
|
|
168
|
+
input.endAt instanceof Date ? input.endAt : new Date(input.endAt as string);
|
|
169
|
+
const span = endAt.getTime() - startAt.getTime();
|
|
170
|
+
if (!Number.isFinite(span) || span <= 0 || span > 31 * 86_400_000)
|
|
171
|
+
return { error: 'Demand must span more than zero and at most 31 days.' };
|
|
172
|
+
return {
|
|
173
|
+
demand: {
|
|
174
|
+
...(typeof input.id === 'string' ? { id: input.id } : {}),
|
|
175
|
+
resourceId,
|
|
176
|
+
key,
|
|
177
|
+
quantity,
|
|
178
|
+
startAt: startAt.toISOString(),
|
|
179
|
+
endAt: endAt.toISOString(),
|
|
180
|
+
},
|
|
181
|
+
};
|
|
182
|
+
}
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SCHEDULE_ACTOR_WINDOW_MAX_DAYS,
|
|
3
|
+
SCHEDULE_IMPLICIT_RULE_KINDS,
|
|
4
|
+
SCHEDULE_RULE_KIND_ENUMS,
|
|
5
|
+
SCHEDULE_RULE_MAX_MIN_ADULTS,
|
|
6
|
+
SCHEDULE_RULE_MAX_RATIO,
|
|
7
|
+
SCHEDULE_RULE_SUBJECT_BY_KIND,
|
|
8
|
+
} from '../constants/schedule.ts';
|
|
9
|
+
import type {
|
|
10
|
+
CoverageDiagnosticObj,
|
|
11
|
+
CoverageResourceInput,
|
|
12
|
+
CoverageRuleInput,
|
|
13
|
+
ScheduleActorWindowConfigObj,
|
|
14
|
+
ScheduleRuleConfigObj,
|
|
15
|
+
ScheduleRuleKindEnum,
|
|
16
|
+
ScheduleRuleSeverityEnum,
|
|
17
|
+
} from '../types/schedule.d.ts';
|
|
18
|
+
import { getScheduleKey, isScheduleRecord } from './resource.ts';
|
|
19
|
+
import { validateScheduleSlotPredicate } from './slot.ts';
|
|
20
|
+
|
|
21
|
+
export interface EffectiveRuleIndex {
|
|
22
|
+
byKind: Map<
|
|
23
|
+
ScheduleRuleKindEnum,
|
|
24
|
+
{
|
|
25
|
+
fallback: CoverageRuleInput | null;
|
|
26
|
+
bySubject: Map<string, CoverageRuleInput>;
|
|
27
|
+
}
|
|
28
|
+
>;
|
|
29
|
+
}
|
|
30
|
+
export interface EffectiveRule {
|
|
31
|
+
rule: CoverageRuleInput | null;
|
|
32
|
+
ruleId: string | null;
|
|
33
|
+
severity: ScheduleRuleSeverityEnum;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/* Check a bounded integer without coercing configuration strings or booleans. */
|
|
37
|
+
function isIntegerWithin(value: unknown, maximum: number): value is number {
|
|
38
|
+
return (
|
|
39
|
+
typeof value === 'number' &&
|
|
40
|
+
Number.isInteger(value) &&
|
|
41
|
+
value > 0 &&
|
|
42
|
+
value <= maximum
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/* Reject unknown fields and normalize the configuration appropriate to one rule kind. */
|
|
47
|
+
export function validateScheduleRuleConfig(
|
|
48
|
+
kind: ScheduleRuleKindEnum,
|
|
49
|
+
input: unknown,
|
|
50
|
+
): { config?: ScheduleRuleConfigObj; error?: string } {
|
|
51
|
+
const raw = input ?? {};
|
|
52
|
+
if (!isScheduleRecord(raw))
|
|
53
|
+
return { error: 'Rule configuration must be an object.' };
|
|
54
|
+
if (!SCHEDULE_RULE_KIND_ENUMS.includes(kind))
|
|
55
|
+
return { error: `Unknown rule kind: ${kind}.` };
|
|
56
|
+
const allowed =
|
|
57
|
+
kind === 'DERIVED_COUNT'
|
|
58
|
+
? ['ratios', 'predicate']
|
|
59
|
+
: kind === 'OPERATING_HOURS'
|
|
60
|
+
? ['minAdults', 'predicate']
|
|
61
|
+
: kind === 'ACTOR_WINDOW_AGGREGATE'
|
|
62
|
+
? ['windowDays', 'maxMinutes', 'minRestMinutes']
|
|
63
|
+
: [];
|
|
64
|
+
const unknown = Object.keys(raw).find((key) => !allowed.includes(key));
|
|
65
|
+
if (unknown)
|
|
66
|
+
return { error: `Unknown ${kind} configuration key: ${unknown}.` };
|
|
67
|
+
if (kind === 'DERIVED_COUNT' || kind === 'OPERATING_HOURS') {
|
|
68
|
+
const { predicate, error } = validateScheduleSlotPredicate(raw.predicate);
|
|
69
|
+
if (error) return { error };
|
|
70
|
+
if (kind === 'OPERATING_HOURS')
|
|
71
|
+
return isIntegerWithin(raw.minAdults, SCHEDULE_RULE_MAX_MIN_ADULTS)
|
|
72
|
+
? { config: { minAdults: raw.minAdults, predicate: predicate ?? null } }
|
|
73
|
+
: { error: 'Operating hours require minAdults between 1 and 99.' };
|
|
74
|
+
if (!isScheduleRecord(raw.ratios) || !Object.keys(raw.ratios).length)
|
|
75
|
+
return { error: 'Derived count requires at least one ratio.' };
|
|
76
|
+
const entries: Array<[string, number]> = [];
|
|
77
|
+
const seen = new Set<string>();
|
|
78
|
+
for (const [rawKey, value] of Object.entries(raw.ratios)) {
|
|
79
|
+
const key = getScheduleKey(rawKey);
|
|
80
|
+
if (
|
|
81
|
+
!key ||
|
|
82
|
+
seen.has(key) ||
|
|
83
|
+
!isIntegerWithin(value, SCHEDULE_RULE_MAX_RATIO)
|
|
84
|
+
)
|
|
85
|
+
return { error: `Invalid or duplicate ratio: ${rawKey}.` };
|
|
86
|
+
seen.add(key);
|
|
87
|
+
entries.push([key, value]);
|
|
88
|
+
}
|
|
89
|
+
return {
|
|
90
|
+
config: {
|
|
91
|
+
ratios: Object.fromEntries(
|
|
92
|
+
entries.sort(([a], [b]) => a.localeCompare(b)),
|
|
93
|
+
),
|
|
94
|
+
predicate: predicate ?? null,
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
if (kind === 'ACTOR_WINDOW_AGGREGATE') {
|
|
99
|
+
const hasHours = raw.windowDays != null || raw.maxMinutes != null;
|
|
100
|
+
const hasRest = raw.minRestMinutes != null;
|
|
101
|
+
if (
|
|
102
|
+
(!hasHours && !hasRest) ||
|
|
103
|
+
(hasHours &&
|
|
104
|
+
(!isIntegerWithin(raw.windowDays, SCHEDULE_ACTOR_WINDOW_MAX_DAYS) ||
|
|
105
|
+
!isIntegerWithin(raw.maxMinutes, 44_640))) ||
|
|
106
|
+
(hasRest && !isIntegerWithin(raw.minRestMinutes, 1440))
|
|
107
|
+
)
|
|
108
|
+
return {
|
|
109
|
+
error:
|
|
110
|
+
'Actor limits require windowDays with maxMinutes and/or minRestMinutes in their permitted ranges.',
|
|
111
|
+
};
|
|
112
|
+
return {
|
|
113
|
+
config: {
|
|
114
|
+
windowDays: (raw.windowDays as number) ?? null,
|
|
115
|
+
maxMinutes: (raw.maxMinutes as number) ?? null,
|
|
116
|
+
minRestMinutes: (raw.minRestMinutes as number) ?? null,
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
return { config: {} };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/* Compare identifiers numerically when both are integer ids, then lexically. */
|
|
124
|
+
export function compareScheduleIds(a: string, b: string): number {
|
|
125
|
+
if (/^\d+$/.test(a) && /^\d+$/.test(b))
|
|
126
|
+
return BigInt(a) < BigInt(b) ? -1 : BigInt(a) > BigInt(b) ? 1 : 0;
|
|
127
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/* Resolve one winner per subject using org, calendar and resource scope precedence. */
|
|
131
|
+
export function resolveEffectiveScheduleRules(
|
|
132
|
+
rules: CoverageRuleInput[],
|
|
133
|
+
calendarId: string,
|
|
134
|
+
resourcesById: Map<string, CoverageResourceInput>,
|
|
135
|
+
): { index: EffectiveRuleIndex; diagnostics: CoverageDiagnosticObj[] } {
|
|
136
|
+
const index: EffectiveRuleIndex = {
|
|
137
|
+
byKind: new Map(
|
|
138
|
+
SCHEDULE_RULE_KIND_ENUMS.map((kind) => [
|
|
139
|
+
kind,
|
|
140
|
+
{ fallback: null, bySubject: new Map() },
|
|
141
|
+
]),
|
|
142
|
+
),
|
|
143
|
+
};
|
|
144
|
+
const diagnostics: CoverageDiagnosticObj[] = [];
|
|
145
|
+
for (const rule of [...rules].sort((a, b) =>
|
|
146
|
+
compareScheduleIds(a.id, b.id),
|
|
147
|
+
)) {
|
|
148
|
+
if (rule.calendarId !== null && rule.calendarId !== calendarId) continue;
|
|
149
|
+
const validated = validateScheduleRuleConfig(rule.kind, rule.config);
|
|
150
|
+
const resource = rule.resourceId
|
|
151
|
+
? resourcesById.get(rule.resourceId)
|
|
152
|
+
: null;
|
|
153
|
+
const code = validated.error
|
|
154
|
+
? 'RULE_CONFIG_INVALID'
|
|
155
|
+
: rule.resourceId && !resource
|
|
156
|
+
? 'UNKNOWN_RESOURCE'
|
|
157
|
+
: resource &&
|
|
158
|
+
resource.kind !==
|
|
159
|
+
(SCHEDULE_RULE_SUBJECT_BY_KIND[rule.kind] === 'ROOM'
|
|
160
|
+
? 'SPACE'
|
|
161
|
+
: 'LABOR')
|
|
162
|
+
? 'RULE_SUBJECT_KIND_MISMATCH'
|
|
163
|
+
: null;
|
|
164
|
+
if (code) {
|
|
165
|
+
diagnostics.push({
|
|
166
|
+
code,
|
|
167
|
+
ruleId: rule.id,
|
|
168
|
+
resourceId: rule.resourceId,
|
|
169
|
+
occurrenceId: null,
|
|
170
|
+
demandId: null,
|
|
171
|
+
detail: validated.error ? { error: validated.error } : {},
|
|
172
|
+
});
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
const group = index.byKind.get(rule.kind)!;
|
|
176
|
+
const current = rule.resourceId
|
|
177
|
+
? group.bySubject.get(rule.resourceId)
|
|
178
|
+
: group.fallback;
|
|
179
|
+
if (current && (current.calendarId !== null || rule.calendarId === null))
|
|
180
|
+
continue;
|
|
181
|
+
const normalized = { ...rule, config: validated.config! };
|
|
182
|
+
if (rule.resourceId) group.bySubject.set(rule.resourceId, normalized);
|
|
183
|
+
else group.fallback = normalized;
|
|
184
|
+
}
|
|
185
|
+
return { index, diagnostics };
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/* Get the effective row or the implicit BLOCK default for a subject. */
|
|
189
|
+
export function getEffectiveRule(
|
|
190
|
+
index: EffectiveRuleIndex,
|
|
191
|
+
kind: ScheduleRuleKindEnum,
|
|
192
|
+
subjectId: string | null,
|
|
193
|
+
): EffectiveRule | null {
|
|
194
|
+
const group = index.byKind.get(kind);
|
|
195
|
+
const rule = subjectId
|
|
196
|
+
? (group?.bySubject.get(subjectId) ??
|
|
197
|
+
(kind === 'OPERATING_HOURS' ? null : group?.fallback))
|
|
198
|
+
: group?.fallback;
|
|
199
|
+
if (rule) return { rule, ruleId: rule.id, severity: rule.severity };
|
|
200
|
+
return (SCHEDULE_IMPLICIT_RULE_KINDS as readonly string[]).includes(kind)
|
|
201
|
+
? { rule: null, ruleId: null, severity: 'BLOCK' }
|
|
202
|
+
: null;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/* Bound the external assignment history needed by active actor window rules. */
|
|
206
|
+
export function getMaxActorWindowDays(rules: CoverageRuleInput[]): number {
|
|
207
|
+
let days = 0;
|
|
208
|
+
for (const rule of rules) {
|
|
209
|
+
if (rule.kind !== 'ACTOR_WINDOW_AGGREGATE') continue;
|
|
210
|
+
const validated = validateScheduleRuleConfig(rule.kind, rule.config);
|
|
211
|
+
if (!validated.config) continue;
|
|
212
|
+
const config = validated.config as ScheduleActorWindowConfigObj;
|
|
213
|
+
days = Math.max(days, config.windowDays ?? (config.minRestMinutes ? 1 : 0));
|
|
214
|
+
}
|
|
215
|
+
return days;
|
|
216
|
+
}
|
package/src/utils/run.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/* Select the newest usable run after resolving newer fragments of each sequence. */
|
|
2
|
+
export function getLatestScheduleRun<
|
|
3
|
+
T extends {
|
|
4
|
+
sequence: string | number;
|
|
5
|
+
status: string;
|
|
6
|
+
version?: string | number;
|
|
7
|
+
revision?: string | number;
|
|
8
|
+
updatedAt?: string;
|
|
9
|
+
},
|
|
10
|
+
>(rows: Array<T | null | undefined>): T | null {
|
|
11
|
+
const bySequence = new Map<string, T>();
|
|
12
|
+
for (const row of rows) {
|
|
13
|
+
if (!row) continue;
|
|
14
|
+
const key = String(BigInt(row.sequence));
|
|
15
|
+
const current = bySequence.get(key);
|
|
16
|
+
const version = BigInt(row.version ?? row.revision ?? 0);
|
|
17
|
+
const currentVersion = BigInt(current?.version ?? current?.revision ?? 0);
|
|
18
|
+
if (
|
|
19
|
+
!current ||
|
|
20
|
+
version > currentVersion ||
|
|
21
|
+
(version === currentVersion &&
|
|
22
|
+
(row.updatedAt || '') > (current.updatedAt || ''))
|
|
23
|
+
)
|
|
24
|
+
bySequence.set(key, row);
|
|
25
|
+
}
|
|
26
|
+
let latest: T | null = null;
|
|
27
|
+
for (const row of bySequence.values()) {
|
|
28
|
+
if (
|
|
29
|
+
row.status !== 'SUPERSEDED' &&
|
|
30
|
+
(!latest || BigInt(row.sequence) > BigInt(latest.sequence))
|
|
31
|
+
)
|
|
32
|
+
latest = row;
|
|
33
|
+
}
|
|
34
|
+
return latest;
|
|
35
|
+
}
|
|
36
|
+
/* Identify the durable job currently waiting or executing. */
|
|
37
|
+
export function getActiveScheduleRun<T extends { status: string }>(
|
|
38
|
+
run: T | null,
|
|
39
|
+
): T | null {
|
|
40
|
+
return run && (run.status === 'QUEUED' || run.status === 'RUNNING')
|
|
41
|
+
? run
|
|
42
|
+
: null;
|
|
43
|
+
}
|