@jsb188/schedule 1.1.12 → 1.1.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/package.json +33 -14
- package/src/constants/schedule.ts +710 -306
- package/src/types/schedule.d.ts +1827 -456
- package/src/ui/ScheduleSettingsUI.tsx +21 -23
- package/src/utils/autofill.ts +469 -0
- package/src/utils/availability.ts +167 -0
- package/src/utils/candidates.ts +447 -0
- package/src/utils/coverage.helpers.ts +509 -554
- package/src/utils/coverage.ts +1121 -526
- package/src/utils/cpm.helpers.ts +27 -29
- package/src/utils/cpm.ts +53 -41
- package/src/utils/cpmLeveling.helpers.ts +89 -16
- package/src/utils/cpmLeveling.ts +28 -26
- package/src/utils/cycle.ts +325 -0
- package/src/utils/demand.ts +239 -0
- package/src/utils/eligibility.ts +553 -400
- package/src/utils/ids.ts +47 -0
- package/src/utils/laborRules.ts +740 -0
- package/src/utils/localTime.ts +312 -0
- package/src/utils/offer.ts +300 -0
- package/src/utils/planCsv.ts +73 -19
- package/src/utils/predicate.ts +111 -0
- package/src/utils/request.ts +212 -114
- package/src/utils/resource.ts +412 -112
- package/src/utils/rule.ts +697 -163
- package/src/utils/rulePresets.ts +116 -0
- package/src/utils/run.ts +0 -8
- package/src/utils/scheduleText.ts +4074 -0
- package/src/utils/shiftChange.ts +279 -0
- package/src/utils/standing.ts +730 -0
- package/src/utils/template.ts +619 -0
- package/src/utils/violation.ts +184 -0
- package/src/utils/workTime.ts +341 -120
- package/src/utils/defaults.ts +0 -8
- package/src/utils/recurrence.ts +0 -616
- package/src/utils/series.ts +0 -161
- package/src/utils/slot.ts +0 -208
- package/tests/coverage.fixtures.ts +0 -111
- package/tests/coverage.test.ts +0 -503
- package/tests/cpm.bench.test.ts +0 -154
- package/tests/cpm.test.ts +0 -354
- package/tests/cpmCorrectness.test.ts +0 -327
- package/tests/cpmLeveling.test.ts +0 -471
- package/tests/eligibility.test.ts +0 -320
- package/tests/gantt-geometry.test.ts +0 -367
- package/tests/gantt-histogram.test.ts +0 -78
- package/tests/gantt-keyboard.test.ts +0 -69
- package/tests/helpers/cpmFixtures.ts +0 -101
- package/tests/projectInputs.test.ts +0 -165
- package/tests/recurrence.test.ts +0 -68
- package/tests/request.test.ts +0 -99
- package/tests/resource.test.ts +0 -95
- package/tests/rule.test.ts +0 -98
- package/tests/series.test.ts +0 -120
- package/tests/workTime.test.ts +0 -1007
- package/tests/workTimeContracts.test.ts +0 -111
- package/tsconfig.json +0 -13
- package/vitest.config.ts +0 -8
package/src/utils/resource.ts
CHANGED
|
@@ -1,25 +1,57 @@
|
|
|
1
1
|
import {
|
|
2
|
+
SCHEDULE_AGE_GROUPS_MAX_COUNT,
|
|
2
3
|
SCHEDULE_KEY_MAX_LENGTH,
|
|
4
|
+
SCHEDULE_LABEL_MAX_LENGTH,
|
|
5
|
+
SCHEDULE_NAME_MAX_LENGTH,
|
|
3
6
|
SCHEDULE_NOTIFY_CHANNEL_ENUMS,
|
|
7
|
+
SCHEDULE_ORGANIZATION_DEFAULT_SETTINGS,
|
|
4
8
|
SCHEDULE_RESOURCE_MAX_QUALIFICATIONS,
|
|
5
|
-
SCHEDULE_RESOURCE_MAX_ROLES,
|
|
6
9
|
SCHEDULE_RESOURCE_MAX_TAGS,
|
|
7
10
|
} from '../constants/schedule.ts';
|
|
8
11
|
import type {
|
|
9
|
-
|
|
12
|
+
ScheduleAgeGroupObj,
|
|
13
|
+
ScheduleNotifyChannelEnum,
|
|
14
|
+
ScheduleNotifyPrefsObj,
|
|
15
|
+
ScheduleOrganizationSettingsObj,
|
|
10
16
|
ScheduleQualificationObj,
|
|
11
17
|
ScheduleResourceConfigObj,
|
|
12
18
|
ScheduleResourceKindEnum,
|
|
19
|
+
ScheduleResourcePreferencesObj,
|
|
20
|
+
ScheduleRoleData,
|
|
13
21
|
} from '../types/schedule.d.ts';
|
|
22
|
+
import { normalizeScheduleIdList } from './ids.ts';
|
|
23
|
+
import { isScheduleDateKey, normalizeScheduleClockTime } from './localTime.ts';
|
|
14
24
|
|
|
15
|
-
|
|
25
|
+
/**
|
|
26
|
+
* Resources, roles and organization schedule settings, plus the small input
|
|
27
|
+
* primitives every staff-schedule validator shares (records, keys, ids,
|
|
28
|
+
* weekdays, wall-clock times, effective dates). Validators return
|
|
29
|
+
* `{ value?, error? }` with an English message and never throw.
|
|
30
|
+
*
|
|
31
|
+
* Resource config is a closed shape: `tags` on any kind; `qualifications`,
|
|
32
|
+
* `notify` and `preferences` on PERSON only. Roles a person can work live in
|
|
33
|
+
* schedule_resource_roles, never in config.
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
const FILE_CONSTANTS = {
|
|
37
|
+
configKeys: ['qualifications', 'tags', 'notify', 'preferences'],
|
|
38
|
+
maxTargetWeeklyMinutes: 10_080,
|
|
39
|
+
maxColorLength: 20,
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/*
|
|
43
|
+
* Check that a value is a plain object (not null, not an array).
|
|
44
|
+
*/
|
|
16
45
|
export function isScheduleRecord(
|
|
17
46
|
value: unknown,
|
|
18
47
|
): value is Record<string, unknown> {
|
|
19
48
|
return !!value && typeof value === 'object' && !Array.isArray(value);
|
|
20
49
|
}
|
|
21
50
|
|
|
22
|
-
/*
|
|
51
|
+
/*
|
|
52
|
+
* Normalize a bounded open key (trimmed, 1..SCHEDULE_KEY_MAX_LENGTH
|
|
53
|
+
* characters) without coercing other values into names; null when invalid.
|
|
54
|
+
*/
|
|
23
55
|
export function getScheduleKey(value: unknown): string | null {
|
|
24
56
|
return typeof value === 'string' &&
|
|
25
57
|
value.trim().length > 0 &&
|
|
@@ -28,7 +60,11 @@ export function getScheduleKey(value: unknown): string | null {
|
|
|
28
60
|
: null;
|
|
29
61
|
}
|
|
30
62
|
|
|
31
|
-
/*
|
|
63
|
+
/*
|
|
64
|
+
* Validate, deduplicate and sort a bounded list of keys (tags, qualification
|
|
65
|
+
* keys, predicate keys); null when it is not a list, too long, or holds an
|
|
66
|
+
* invalid key.
|
|
67
|
+
*/
|
|
32
68
|
export function validateScheduleKeyList(
|
|
33
69
|
value: unknown,
|
|
34
70
|
limit: number,
|
|
@@ -40,7 +76,197 @@ export function validateScheduleKeyList(
|
|
|
40
76
|
: [...new Set(keys as string[])].sort();
|
|
41
77
|
}
|
|
42
78
|
|
|
43
|
-
/*
|
|
79
|
+
/*
|
|
80
|
+
* Return the first key of an input object that is not in the allowed list,
|
|
81
|
+
* or null when every key is known.
|
|
82
|
+
*/
|
|
83
|
+
export function getUnknownScheduleKey(
|
|
84
|
+
input: Record<string, unknown>,
|
|
85
|
+
allowed: readonly string[],
|
|
86
|
+
): string | null {
|
|
87
|
+
return Object.keys(input).find((key) => !allowed.includes(key)) ?? null;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/*
|
|
91
|
+
* Read one id input (a trimmed non-empty string of at most 60 characters, or
|
|
92
|
+
* a non-negative safe integer) as a string; null when absent or invalid.
|
|
93
|
+
*/
|
|
94
|
+
export function getScheduleId(value: unknown): string | null {
|
|
95
|
+
return normalizeScheduleIdList([value], 1)?.[0] ?? null;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/*
|
|
99
|
+
* Check that a value is a weekday number: an integer 0 (Sunday) to 6
|
|
100
|
+
* (Saturday).
|
|
101
|
+
*/
|
|
102
|
+
export function isScheduleWeekday(value: unknown): value is number {
|
|
103
|
+
return (
|
|
104
|
+
Number.isInteger(value) && (value as number) >= 0 && (value as number) <= 6
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/*
|
|
109
|
+
* Validate the wall-clock bounds of a local window (`startTime`, `endTime`)
|
|
110
|
+
* and normalize them to 'HH:mm'. An end at or before the start ends the next
|
|
111
|
+
* day, so equal times are a whole day; every pair of valid times is accepted.
|
|
112
|
+
*/
|
|
113
|
+
export function validateScheduleClockTimesInput(
|
|
114
|
+
input: Record<string, unknown>,
|
|
115
|
+
): { times?: { startTime: string; endTime: string }; error?: string } {
|
|
116
|
+
const startTime =
|
|
117
|
+
typeof input.startTime === 'string'
|
|
118
|
+
? normalizeScheduleClockTime(input.startTime)
|
|
119
|
+
: null;
|
|
120
|
+
const endTime =
|
|
121
|
+
typeof input.endTime === 'string'
|
|
122
|
+
? normalizeScheduleClockTime(input.endTime)
|
|
123
|
+
: null;
|
|
124
|
+
if (!startTime || !endTime)
|
|
125
|
+
return {
|
|
126
|
+
error:
|
|
127
|
+
'Start and end times must be clock times from 00:00 to 23:59 (HH:mm).',
|
|
128
|
+
};
|
|
129
|
+
return { times: { startTime, endTime } };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/*
|
|
133
|
+
* Validate an effective date range (`effectiveFrom` required, `effectiveTo`
|
|
134
|
+
* null or on or after it; both inclusive 'YYYY-MM-DD' dates).
|
|
135
|
+
*/
|
|
136
|
+
export function validateScheduleEffectiveDatesInput(
|
|
137
|
+
input: Record<string, unknown>,
|
|
138
|
+
): {
|
|
139
|
+
dates?: { effectiveFrom: string; effectiveTo: string | null };
|
|
140
|
+
error?: string;
|
|
141
|
+
} {
|
|
142
|
+
if (!isScheduleDateKey(input.effectiveFrom))
|
|
143
|
+
return { error: 'effectiveFrom must be a date (YYYY-MM-DD).' };
|
|
144
|
+
const effectiveTo = input.effectiveTo ?? null;
|
|
145
|
+
if (effectiveTo !== null && !isScheduleDateKey(effectiveTo))
|
|
146
|
+
return { error: 'effectiveTo must be a date (YYYY-MM-DD) or null.' };
|
|
147
|
+
if (effectiveTo !== null && effectiveTo < input.effectiveFrom)
|
|
148
|
+
return { error: 'effectiveTo cannot be before effectiveFrom.' };
|
|
149
|
+
return { dates: { effectiveFrom: input.effectiveFrom, effectiveTo } };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/*
|
|
153
|
+
* Validate a PERSON's qualifications: at most
|
|
154
|
+
* SCHEDULE_RESOURCE_MAX_QUALIFICATIONS `{key, expiresAt}` entries with unique
|
|
155
|
+
* keys; expiresAt null (never expires) or a readable instant, normalized to
|
|
156
|
+
* ISO. Sorted by key.
|
|
157
|
+
*/
|
|
158
|
+
function validateScheduleQualifications(value: unknown): {
|
|
159
|
+
qualifications?: ScheduleQualificationObj[];
|
|
160
|
+
error?: string;
|
|
161
|
+
} {
|
|
162
|
+
if (value == null) return { qualifications: [] };
|
|
163
|
+
if (
|
|
164
|
+
!Array.isArray(value) ||
|
|
165
|
+
value.length > SCHEDULE_RESOURCE_MAX_QUALIFICATIONS
|
|
166
|
+
)
|
|
167
|
+
return {
|
|
168
|
+
error: `Qualifications must be a list of at most ${SCHEDULE_RESOURCE_MAX_QUALIFICATIONS} entries.`,
|
|
169
|
+
};
|
|
170
|
+
const qualifications: ScheduleQualificationObj[] = [];
|
|
171
|
+
const seen = new Set<string>();
|
|
172
|
+
for (const item of value) {
|
|
173
|
+
if (
|
|
174
|
+
!isScheduleRecord(item) ||
|
|
175
|
+
getUnknownScheduleKey(item, ['key', 'expiresAt'])
|
|
176
|
+
)
|
|
177
|
+
return { error: 'Each qualification must be {key, expiresAt}.' };
|
|
178
|
+
const key = getScheduleKey(item.key);
|
|
179
|
+
if (!key || seen.has(key))
|
|
180
|
+
return {
|
|
181
|
+
error: `Invalid or duplicate qualification key: ${String(item.key)}.`,
|
|
182
|
+
};
|
|
183
|
+
let expiresAt: string | null = null;
|
|
184
|
+
if (item.expiresAt != null) {
|
|
185
|
+
const ms =
|
|
186
|
+
typeof item.expiresAt === 'string'
|
|
187
|
+
? Date.parse(item.expiresAt)
|
|
188
|
+
: Number.NaN;
|
|
189
|
+
if (!Number.isFinite(ms))
|
|
190
|
+
return { error: `Invalid qualification expiry for ${key}.` };
|
|
191
|
+
expiresAt = new Date(ms).toISOString();
|
|
192
|
+
}
|
|
193
|
+
seen.add(key);
|
|
194
|
+
qualifications.push({ key, expiresAt });
|
|
195
|
+
}
|
|
196
|
+
return {
|
|
197
|
+
qualifications: qualifications.sort((a, b) =>
|
|
198
|
+
a.key < b.key ? -1 : a.key > b.key ? 1 : 0,
|
|
199
|
+
),
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/*
|
|
204
|
+
* Validate a PERSON's notification preferences `{channels}`: known channels,
|
|
205
|
+
* deduplicated, in SCHEDULE_NOTIFY_CHANNEL_ENUMS order ([] = opted out).
|
|
206
|
+
*/
|
|
207
|
+
function validateScheduleNotifyPrefs(value: unknown): {
|
|
208
|
+
notify?: ScheduleNotifyPrefsObj | null;
|
|
209
|
+
error?: string;
|
|
210
|
+
} {
|
|
211
|
+
if (value == null) return { notify: null };
|
|
212
|
+
if (
|
|
213
|
+
!isScheduleRecord(value) ||
|
|
214
|
+
getUnknownScheduleKey(value, ['channels']) ||
|
|
215
|
+
!Array.isArray(value.channels) ||
|
|
216
|
+
value.channels.some(
|
|
217
|
+
(channel) =>
|
|
218
|
+
!SCHEDULE_NOTIFY_CHANNEL_ENUMS.includes(
|
|
219
|
+
channel as ScheduleNotifyChannelEnum,
|
|
220
|
+
),
|
|
221
|
+
)
|
|
222
|
+
)
|
|
223
|
+
return {
|
|
224
|
+
error: `Notification preferences must be {channels} with channels from ${SCHEDULE_NOTIFY_CHANNEL_ENUMS.join(', ')}.`,
|
|
225
|
+
};
|
|
226
|
+
const channels = new Set(value.channels);
|
|
227
|
+
return {
|
|
228
|
+
notify: {
|
|
229
|
+
channels: SCHEDULE_NOTIFY_CHANNEL_ENUMS.filter((channel) =>
|
|
230
|
+
channels.has(channel),
|
|
231
|
+
),
|
|
232
|
+
},
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/*
|
|
237
|
+
* Validate a PERSON's scheduling preferences `{targetWeeklyMinutes}`: an
|
|
238
|
+
* integer 0..10080 worked minutes per pay week, or null for no preference.
|
|
239
|
+
*/
|
|
240
|
+
function validateScheduleResourcePreferences(value: unknown): {
|
|
241
|
+
preferences?: ScheduleResourcePreferencesObj | null;
|
|
242
|
+
error?: string;
|
|
243
|
+
} {
|
|
244
|
+
if (value == null) return { preferences: null };
|
|
245
|
+
if (
|
|
246
|
+
!isScheduleRecord(value) ||
|
|
247
|
+
getUnknownScheduleKey(value, ['targetWeeklyMinutes'])
|
|
248
|
+
)
|
|
249
|
+
return { error: 'Preferences must be {targetWeeklyMinutes}.' };
|
|
250
|
+
const target = value.targetWeeklyMinutes ?? null;
|
|
251
|
+
if (
|
|
252
|
+
target !== null &&
|
|
253
|
+
(!Number.isInteger(target) ||
|
|
254
|
+
(target as number) < 0 ||
|
|
255
|
+
(target as number) > FILE_CONSTANTS.maxTargetWeeklyMinutes)
|
|
256
|
+
)
|
|
257
|
+
return {
|
|
258
|
+
error: `targetWeeklyMinutes must be a whole number of minutes from 0 to ${FILE_CONSTANTS.maxTargetWeeklyMinutes}, or null.`,
|
|
259
|
+
};
|
|
260
|
+
return { preferences: { targetWeeklyMinutes: target as number | null } };
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/*
|
|
264
|
+
* Validate a resource's config for its kind and return the closed shape.
|
|
265
|
+
* `tags` are allowed on any kind; `qualifications`, `notify` and
|
|
266
|
+
* `preferences` only on PERSON (an empty list or null is accepted on any
|
|
267
|
+
* kind). Unknown keys, including the removed `roles` and `ageGroup`, are
|
|
268
|
+
* rejected.
|
|
269
|
+
*/
|
|
44
270
|
export function validateScheduleResourceConfig(
|
|
45
271
|
kind: ScheduleResourceKindEnum,
|
|
46
272
|
input: unknown,
|
|
@@ -48,88 +274,50 @@ export function validateScheduleResourceConfig(
|
|
|
48
274
|
const raw = input ?? {};
|
|
49
275
|
if (!isScheduleRecord(raw))
|
|
50
276
|
return { error: 'Resource configuration must be an object.' };
|
|
51
|
-
const unknown =
|
|
52
|
-
(key) =>
|
|
53
|
-
!['qualifications', 'roles', 'tags', 'ageGroup', 'notify'].includes(key),
|
|
54
|
-
);
|
|
277
|
+
const unknown = getUnknownScheduleKey(raw, FILE_CONSTANTS.configKeys);
|
|
55
278
|
if (unknown)
|
|
56
279
|
return { error: `Unknown resource configuration key: ${unknown}.` };
|
|
280
|
+
|
|
281
|
+
const tags =
|
|
282
|
+
raw.tags == null
|
|
283
|
+
? []
|
|
284
|
+
: validateScheduleKeyList(raw.tags, SCHEDULE_RESOURCE_MAX_TAGS);
|
|
285
|
+
if (!tags)
|
|
286
|
+
return {
|
|
287
|
+
error: `Tags must be a list of at most ${SCHEDULE_RESOURCE_MAX_TAGS} names.`,
|
|
288
|
+
};
|
|
289
|
+
const qualifications = validateScheduleQualifications(raw.qualifications);
|
|
290
|
+
if (qualifications.error) return { error: qualifications.error };
|
|
291
|
+
const notify = validateScheduleNotifyPrefs(raw.notify);
|
|
292
|
+
if (notify.error) return { error: notify.error };
|
|
293
|
+
const preferences = validateScheduleResourcePreferences(raw.preferences);
|
|
294
|
+
if (preferences.error) return { error: preferences.error };
|
|
295
|
+
|
|
57
296
|
const config: ScheduleResourceConfigObj = {
|
|
58
|
-
qualifications:
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
notify: null,
|
|
297
|
+
qualifications: qualifications.qualifications!,
|
|
298
|
+
tags,
|
|
299
|
+
notify: notify.notify!,
|
|
300
|
+
preferences: preferences.preferences!,
|
|
63
301
|
};
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
if (
|
|
73
|
-
|
|
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)] };
|
|
302
|
+
if (kind !== 'PERSON') {
|
|
303
|
+
const personOnly = config.qualifications.length
|
|
304
|
+
? 'qualifications'
|
|
305
|
+
: config.notify
|
|
306
|
+
? 'notification preferences'
|
|
307
|
+
: config.preferences
|
|
308
|
+
? 'preferences'
|
|
309
|
+
: null;
|
|
310
|
+
if (personOnly)
|
|
311
|
+
return { error: `Only PERSON resources may have ${personOnly}.` };
|
|
128
312
|
}
|
|
129
313
|
return { config };
|
|
130
314
|
}
|
|
131
315
|
|
|
132
|
-
/*
|
|
316
|
+
/*
|
|
317
|
+
* Return a qualification's expiry in epoch milliseconds: null when it never
|
|
318
|
+
* expires, and 0 when expiresAt is unreadable (treated as already expired,
|
|
319
|
+
* never as valid forever).
|
|
320
|
+
*/
|
|
133
321
|
export function getQualificationExpiryMs(
|
|
134
322
|
qualification: Pick<ScheduleQualificationObj, 'expiresAt'>,
|
|
135
323
|
): number | null {
|
|
@@ -138,45 +326,157 @@ export function getQualificationExpiryMs(
|
|
|
138
326
|
return Number.isFinite(ms) ? ms : 0;
|
|
139
327
|
}
|
|
140
328
|
|
|
141
|
-
/*
|
|
142
|
-
|
|
143
|
-
|
|
329
|
+
/*
|
|
330
|
+
* Validate a role write: `key` (a schedule key), `name` (1..
|
|
331
|
+
* SCHEDULE_LABEL_MAX_LENGTH characters), `color` (null or at most 20
|
|
332
|
+
* characters) and `requiredQualifications` (a key list, default []).
|
|
333
|
+
*/
|
|
334
|
+
export function validateScheduleRoleInput(input: unknown): {
|
|
335
|
+
role?: Pick<
|
|
336
|
+
ScheduleRoleData,
|
|
337
|
+
'key' | 'name' | 'color' | 'requiredQualifications'
|
|
338
|
+
>;
|
|
144
339
|
error?: string;
|
|
145
340
|
} {
|
|
146
|
-
if (!isScheduleRecord(input)) return { error: '
|
|
341
|
+
if (!isScheduleRecord(input)) return { error: 'A role must be an object.' };
|
|
342
|
+
const unknown = getUnknownScheduleKey(input, [
|
|
343
|
+
'key',
|
|
344
|
+
'name',
|
|
345
|
+
'color',
|
|
346
|
+
'requiredQualifications',
|
|
347
|
+
]);
|
|
348
|
+
if (unknown) return { error: `Unknown role field: ${unknown}.` };
|
|
349
|
+
|
|
147
350
|
const key = getScheduleKey(input.key);
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
351
|
+
if (!key)
|
|
352
|
+
return {
|
|
353
|
+
error: `A role needs a key of 1 to ${SCHEDULE_KEY_MAX_LENGTH} characters.`,
|
|
354
|
+
};
|
|
355
|
+
const name = typeof input.name === 'string' ? input.name.trim() : '';
|
|
356
|
+
if (!name || name.length > SCHEDULE_LABEL_MAX_LENGTH)
|
|
357
|
+
return {
|
|
358
|
+
error: `A role needs a name of 1 to ${SCHEDULE_LABEL_MAX_LENGTH} characters.`,
|
|
359
|
+
};
|
|
360
|
+
const color =
|
|
361
|
+
typeof input.color === 'string' ? input.color.trim() || null : null;
|
|
153
362
|
if (
|
|
154
|
-
typeof
|
|
155
|
-
|
|
156
|
-
quantity < 0 ||
|
|
157
|
-
Math.abs(quantity * 100 - Math.round(quantity * 100)) > 1e-7
|
|
363
|
+
(input.color != null && typeof input.color !== 'string') ||
|
|
364
|
+
(color && color.length > FILE_CONSTANTS.maxColorLength)
|
|
158
365
|
)
|
|
159
366
|
return {
|
|
160
|
-
error:
|
|
161
|
-
'Demand quantity must be non-negative with at most two decimal places.',
|
|
367
|
+
error: `A role color must be at most ${FILE_CONSTANTS.maxColorLength} characters.`,
|
|
162
368
|
};
|
|
163
|
-
const
|
|
164
|
-
input.
|
|
165
|
-
?
|
|
166
|
-
:
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
if (!
|
|
171
|
-
return {
|
|
369
|
+
const requiredQualifications =
|
|
370
|
+
input.requiredQualifications == null
|
|
371
|
+
? []
|
|
372
|
+
: validateScheduleKeyList(
|
|
373
|
+
input.requiredQualifications,
|
|
374
|
+
SCHEDULE_RESOURCE_MAX_QUALIFICATIONS,
|
|
375
|
+
);
|
|
376
|
+
if (!requiredQualifications)
|
|
377
|
+
return {
|
|
378
|
+
error: `requiredQualifications must be a list of at most ${SCHEDULE_RESOURCE_MAX_QUALIFICATIONS} qualification keys.`,
|
|
379
|
+
};
|
|
380
|
+
return { role: { key, name, color, requiredQualifications } };
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/*
|
|
384
|
+
* Validate the organization's age-group catalog: at most
|
|
385
|
+
* SCHEDULE_AGE_GROUPS_MAX_COUNT `{key, label}` entries, keys unique, labels
|
|
386
|
+
* 1..SCHEDULE_NAME_MAX_LENGTH characters. Order is kept (youngest first).
|
|
387
|
+
*/
|
|
388
|
+
function validateScheduleAgeGroups(value: unknown): {
|
|
389
|
+
ageGroups?: ScheduleAgeGroupObj[];
|
|
390
|
+
error?: string;
|
|
391
|
+
} {
|
|
392
|
+
if (!Array.isArray(value) || value.length > SCHEDULE_AGE_GROUPS_MAX_COUNT)
|
|
393
|
+
return {
|
|
394
|
+
error: `ageGroups must be a list of at most ${SCHEDULE_AGE_GROUPS_MAX_COUNT} groups.`,
|
|
395
|
+
};
|
|
396
|
+
const ageGroups: ScheduleAgeGroupObj[] = [];
|
|
397
|
+
const seen = new Set<string>();
|
|
398
|
+
for (const item of value) {
|
|
399
|
+
if (
|
|
400
|
+
!isScheduleRecord(item) ||
|
|
401
|
+
getUnknownScheduleKey(item, ['key', 'label'])
|
|
402
|
+
)
|
|
403
|
+
return { error: 'Each age group must be {key, label}.' };
|
|
404
|
+
const key = getScheduleKey(item.key);
|
|
405
|
+
if (!key || seen.has(key))
|
|
406
|
+
return {
|
|
407
|
+
error: `Invalid or duplicate age group key: ${String(item.key)}.`,
|
|
408
|
+
};
|
|
409
|
+
const label = typeof item.label === 'string' ? item.label.trim() : '';
|
|
410
|
+
if (!label || label.length > SCHEDULE_NAME_MAX_LENGTH)
|
|
411
|
+
return {
|
|
412
|
+
error: `Age group ${key} needs a label of 1 to ${SCHEDULE_NAME_MAX_LENGTH} characters.`,
|
|
413
|
+
};
|
|
414
|
+
seen.add(key);
|
|
415
|
+
ageGroups.push({ key, label });
|
|
416
|
+
}
|
|
417
|
+
return { ageGroups };
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/*
|
|
421
|
+
* Return a fresh copy of the default organization schedule settings.
|
|
422
|
+
*/
|
|
423
|
+
function getDefaultScheduleOrganizationSettings(): ScheduleOrganizationSettingsObj {
|
|
172
424
|
return {
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
quantity,
|
|
178
|
-
startAt: startAt.toISOString(),
|
|
179
|
-
endAt: endAt.toISOString(),
|
|
180
|
-
},
|
|
425
|
+
ageGroups: SCHEDULE_ORGANIZATION_DEFAULT_SETTINGS.ageGroups.map(
|
|
426
|
+
(group) => ({ ...group }),
|
|
427
|
+
),
|
|
428
|
+
payWeekStart: SCHEDULE_ORGANIZATION_DEFAULT_SETTINGS.payWeekStart,
|
|
181
429
|
};
|
|
182
430
|
}
|
|
431
|
+
|
|
432
|
+
/*
|
|
433
|
+
* Validate an organizations.settings.schedule write (set_schedule_defaults):
|
|
434
|
+
* `payWeekStart` 0..6 and `ageGroups`; a missing (or null) key takes its
|
|
435
|
+
* default from SCHEDULE_ORGANIZATION_DEFAULT_SETTINGS.
|
|
436
|
+
*/
|
|
437
|
+
export function validateScheduleOrganizationSettings(input: unknown): {
|
|
438
|
+
settings?: ScheduleOrganizationSettingsObj;
|
|
439
|
+
error?: string;
|
|
440
|
+
} {
|
|
441
|
+
const raw = input ?? {};
|
|
442
|
+
if (!isScheduleRecord(raw))
|
|
443
|
+
return { error: 'Schedule settings must be an object.' };
|
|
444
|
+
const unknown = getUnknownScheduleKey(raw, ['payWeekStart', 'ageGroups']);
|
|
445
|
+
if (unknown) return { error: `Unknown schedule setting: ${unknown}.` };
|
|
446
|
+
|
|
447
|
+
const settings = getDefaultScheduleOrganizationSettings();
|
|
448
|
+
if (raw.payWeekStart != null) {
|
|
449
|
+
if (!isScheduleWeekday(raw.payWeekStart))
|
|
450
|
+
return {
|
|
451
|
+
error:
|
|
452
|
+
'payWeekStart must be a weekday from 0 (Sunday) to 6 (Saturday).',
|
|
453
|
+
};
|
|
454
|
+
settings.payWeekStart = raw.payWeekStart;
|
|
455
|
+
}
|
|
456
|
+
if (raw.ageGroups != null) {
|
|
457
|
+
const result = validateScheduleAgeGroups(raw.ageGroups);
|
|
458
|
+
if (result.error) return { error: result.error };
|
|
459
|
+
settings.ageGroups = result.ageGroups!;
|
|
460
|
+
}
|
|
461
|
+
return { settings };
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
/*
|
|
465
|
+
* Read organizations.settings.schedule leniently (pass that object): each
|
|
466
|
+
* valid key is used and an invalid or missing one takes its default. Never
|
|
467
|
+
* throws; the write path validates.
|
|
468
|
+
*/
|
|
469
|
+
export function getScheduleOrganizationSettings(
|
|
470
|
+
settings: unknown,
|
|
471
|
+
): ScheduleOrganizationSettingsObj {
|
|
472
|
+
const raw = isScheduleRecord(settings) ? settings : {};
|
|
473
|
+
const resolved = getDefaultScheduleOrganizationSettings();
|
|
474
|
+
if (isScheduleWeekday(raw.payWeekStart))
|
|
475
|
+
resolved.payWeekStart = raw.payWeekStart;
|
|
476
|
+
const ageGroups =
|
|
477
|
+
raw.ageGroups == null
|
|
478
|
+
? null
|
|
479
|
+
: validateScheduleAgeGroups(raw.ageGroups).ageGroups;
|
|
480
|
+
if (ageGroups) resolved.ageGroups = ageGroups;
|
|
481
|
+
return resolved;
|
|
482
|
+
}
|