@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.
Files changed (59) hide show
  1. package/LICENSE +21 -0
  2. package/package.json +33 -14
  3. package/src/constants/schedule.ts +710 -306
  4. package/src/types/schedule.d.ts +1827 -456
  5. package/src/ui/ScheduleSettingsUI.tsx +21 -23
  6. package/src/utils/autofill.ts +469 -0
  7. package/src/utils/availability.ts +167 -0
  8. package/src/utils/candidates.ts +447 -0
  9. package/src/utils/coverage.helpers.ts +509 -554
  10. package/src/utils/coverage.ts +1121 -526
  11. package/src/utils/cpm.helpers.ts +27 -29
  12. package/src/utils/cpm.ts +53 -41
  13. package/src/utils/cpmLeveling.helpers.ts +89 -16
  14. package/src/utils/cpmLeveling.ts +28 -26
  15. package/src/utils/cycle.ts +325 -0
  16. package/src/utils/demand.ts +239 -0
  17. package/src/utils/eligibility.ts +553 -400
  18. package/src/utils/ids.ts +47 -0
  19. package/src/utils/laborRules.ts +740 -0
  20. package/src/utils/localTime.ts +312 -0
  21. package/src/utils/offer.ts +300 -0
  22. package/src/utils/planCsv.ts +73 -19
  23. package/src/utils/predicate.ts +111 -0
  24. package/src/utils/request.ts +212 -114
  25. package/src/utils/resource.ts +412 -112
  26. package/src/utils/rule.ts +697 -163
  27. package/src/utils/rulePresets.ts +116 -0
  28. package/src/utils/run.ts +0 -8
  29. package/src/utils/scheduleText.ts +4074 -0
  30. package/src/utils/shiftChange.ts +279 -0
  31. package/src/utils/standing.ts +730 -0
  32. package/src/utils/template.ts +619 -0
  33. package/src/utils/violation.ts +184 -0
  34. package/src/utils/workTime.ts +341 -120
  35. package/src/utils/defaults.ts +0 -8
  36. package/src/utils/recurrence.ts +0 -616
  37. package/src/utils/series.ts +0 -161
  38. package/src/utils/slot.ts +0 -208
  39. package/tests/coverage.fixtures.ts +0 -111
  40. package/tests/coverage.test.ts +0 -503
  41. package/tests/cpm.bench.test.ts +0 -154
  42. package/tests/cpm.test.ts +0 -354
  43. package/tests/cpmCorrectness.test.ts +0 -327
  44. package/tests/cpmLeveling.test.ts +0 -471
  45. package/tests/eligibility.test.ts +0 -320
  46. package/tests/gantt-geometry.test.ts +0 -367
  47. package/tests/gantt-histogram.test.ts +0 -78
  48. package/tests/gantt-keyboard.test.ts +0 -69
  49. package/tests/helpers/cpmFixtures.ts +0 -101
  50. package/tests/projectInputs.test.ts +0 -165
  51. package/tests/recurrence.test.ts +0 -68
  52. package/tests/request.test.ts +0 -99
  53. package/tests/resource.test.ts +0 -95
  54. package/tests/rule.test.ts +0 -98
  55. package/tests/series.test.ts +0 -120
  56. package/tests/workTime.test.ts +0 -1007
  57. package/tests/workTimeContracts.test.ts +0 -111
  58. package/tsconfig.json +0 -13
  59. package/vitest.config.ts +0 -8
@@ -0,0 +1,279 @@
1
+ import {
2
+ SCHEDULE_SHIFT_EDIT_FIELDS,
3
+ SCHEDULE_SHIFT_RETIME_FIELDS,
4
+ } from '../constants/schedule.ts';
5
+ import type {
6
+ ScheduleShiftChangeEntryObj,
7
+ ScheduleShiftChangeKindEnum,
8
+ ScheduleShiftChangeRowInput,
9
+ ScheduleShiftJournalFieldEnum,
10
+ ScheduleShiftNetChangeObj,
11
+ ScheduleShiftRecipientChangeEnum,
12
+ ScheduleShiftStateObj,
13
+ ScheduleShiftValuesObj,
14
+ } from '../types/schedule.d.ts';
15
+ import { compareScheduleIds } from './ids.ts';
16
+ import { toScheduleDateKey, toScheduleTimeMs } from './localTime.ts';
17
+
18
+ /**
19
+ * The post-publish journal (schedule_shift_changes) and the change notice
20
+ * (IMPLEMENTATION §1.10).
21
+ *
22
+ * - One write to a PUBLISHED seat produces its journal rows here: CANCELED
23
+ * alone, or RETIMED, EDITED, then ASSIGNED / UNASSIGNED. previousValues /
24
+ * nextValues hold the changed fields only, serialized as
25
+ * ScheduleShiftValuesObj (ISO instants, date keys, decimal ids).
26
+ * - A coalesced SHIFTS_CHANGED notice renders the NET change of each seat:
27
+ * the first previous values seen in its window against the seat's current
28
+ * state. A change that was undone sends nothing.
29
+ */
30
+
31
+ type ScheduleShiftNetField = ScheduleShiftNetChangeObj['changedFields'][number];
32
+
33
+ const FILE_CONSTANTS = {
34
+ journalFields: [
35
+ ...SCHEDULE_SHIFT_RETIME_FIELDS,
36
+ ...SCHEDULE_SHIFT_EDIT_FIELDS,
37
+ ] as ScheduleShiftJournalFieldEnum[],
38
+ netFields: [
39
+ ...SCHEDULE_SHIFT_RETIME_FIELDS,
40
+ ...SCHEDULE_SHIFT_EDIT_FIELDS,
41
+ 'assigneeResourceId',
42
+ 'status',
43
+ ] as ScheduleShiftNetField[],
44
+ assigneeKinds: ['ASSIGNED', 'UNASSIGNED'] as ScheduleShiftChangeKindEnum[],
45
+ };
46
+
47
+ /*
48
+ * Serialize one journaled field of a seat state the way previousValues /
49
+ * nextValues store it: instants as ISO strings, shiftDate as a date key, ids
50
+ * as strings.
51
+ */
52
+ function getShiftJournalValue(
53
+ state: ScheduleShiftStateObj,
54
+ field: ScheduleShiftJournalFieldEnum,
55
+ ): string | number | null {
56
+ switch (field) {
57
+ case 'startAt':
58
+ case 'endAt':
59
+ return new Date(toScheduleTimeMs(state[field])).toISOString();
60
+ case 'shiftDate':
61
+ return toScheduleDateKey(state.shiftDate) ?? state.shiftDate;
62
+ case 'roleId':
63
+ case 'locationResourceId':
64
+ return state[field] == null ? null : String(state[field]);
65
+ default:
66
+ return state[field];
67
+ }
68
+ }
69
+
70
+ /*
71
+ * Return the previous and next values of the fields that differ between two
72
+ * seat states, or null when none of the fields changed.
73
+ */
74
+ function getChangedShiftValues(
75
+ before: ScheduleShiftStateObj,
76
+ after: ScheduleShiftStateObj,
77
+ fields: readonly ScheduleShiftJournalFieldEnum[],
78
+ ): Pick<ScheduleShiftChangeEntryObj, 'previousValues' | 'nextValues'> | null {
79
+ const previousValues: Record<string, unknown> = {};
80
+ const nextValues: Record<string, unknown> = {};
81
+ for (const field of fields) {
82
+ const previous = getShiftJournalValue(before, field);
83
+ const next = getShiftJournalValue(after, field);
84
+ if (previous === next) continue;
85
+ previousValues[field] = previous;
86
+ nextValues[field] = next;
87
+ }
88
+ return Object.keys(previousValues).length
89
+ ? {
90
+ previousValues: previousValues as ScheduleShiftValuesObj,
91
+ nextValues: nextValues as ScheduleShiftValuesObj,
92
+ }
93
+ : null;
94
+ }
95
+
96
+ /*
97
+ * Return the journal rows one write to a PUBLISHED seat produces. A move to
98
+ * CANCELED is one CANCELED row (fromResourceId = the assignee). Otherwise, in
99
+ * order: RETIMED when a retime field changed, EDITED when an edit field
100
+ * changed, then ASSIGNED (fromResourceId = the previous assignee, if any) or
101
+ * UNASSIGNED. No change returns [].
102
+ */
103
+ export function getScheduleShiftChangeEntries(
104
+ before: ScheduleShiftStateObj,
105
+ after: ScheduleShiftStateObj,
106
+ ): ScheduleShiftChangeEntryObj[] {
107
+ if (after.status === 'CANCELED' && before.status !== 'CANCELED')
108
+ return [
109
+ {
110
+ kind: 'CANCELED',
111
+ fromResourceId: before.assigneeResourceId,
112
+ toResourceId: null,
113
+ previousValues: null,
114
+ nextValues: null,
115
+ },
116
+ ];
117
+
118
+ const entries: ScheduleShiftChangeEntryObj[] = [];
119
+ for (const [kind, fields] of [
120
+ ['RETIMED', SCHEDULE_SHIFT_RETIME_FIELDS],
121
+ ['EDITED', SCHEDULE_SHIFT_EDIT_FIELDS],
122
+ ] as const) {
123
+ const values = getChangedShiftValues(before, after, fields);
124
+ if (values)
125
+ entries.push({
126
+ kind,
127
+ fromResourceId: null,
128
+ toResourceId: null,
129
+ ...values,
130
+ });
131
+ }
132
+ if (before.assigneeResourceId !== after.assigneeResourceId)
133
+ entries.push({
134
+ kind: after.assigneeResourceId ? 'ASSIGNED' : 'UNASSIGNED',
135
+ fromResourceId: before.assigneeResourceId,
136
+ toResourceId: after.assigneeResourceId,
137
+ previousValues: null,
138
+ nextValues: null,
139
+ });
140
+ return entries;
141
+ }
142
+
143
+ /*
144
+ * Return the people a journal row affects: ASSIGNED its previous and new
145
+ * assignee, UNASSIGNED its previous assignee, RETIMED / EDITED / CANCELED the
146
+ * seat's current assignee. Nulls dropped, deduplicated, sorted.
147
+ */
148
+ export function getScheduleShiftChangeAffectedResourceIds(
149
+ entry: Pick<
150
+ ScheduleShiftChangeEntryObj,
151
+ 'kind' | 'fromResourceId' | 'toResourceId'
152
+ >,
153
+ currentAssigneeResourceId: string | null,
154
+ ): string[] {
155
+ const ids =
156
+ entry.kind === 'ASSIGNED'
157
+ ? [entry.fromResourceId, entry.toResourceId]
158
+ : entry.kind === 'UNASSIGNED'
159
+ ? [entry.fromResourceId]
160
+ : [currentAssigneeResourceId];
161
+ return [
162
+ ...new Set(ids.filter((id): id is string => id != null).map(String)),
163
+ ].sort(compareScheduleIds);
164
+ }
165
+
166
+ /*
167
+ * Copy a seat state with instants as Dates and shiftDate as a date key.
168
+ */
169
+ function normalizeShiftState(
170
+ shift: ScheduleShiftStateObj,
171
+ ): ScheduleShiftStateObj {
172
+ return {
173
+ ...shift,
174
+ shiftDate: toScheduleDateKey(shift.shiftDate) ?? shift.shiftDate,
175
+ startAt: new Date(toScheduleTimeMs(shift.startAt)),
176
+ endAt: new Date(toScheduleTimeMs(shift.endAt)),
177
+ };
178
+ }
179
+
180
+ /*
181
+ * Write one journaled previous value into a seat state (instants become
182
+ * Dates).
183
+ */
184
+ function setShiftJournalValue(
185
+ state: ScheduleShiftStateObj,
186
+ field: ScheduleShiftJournalFieldEnum,
187
+ value: unknown,
188
+ ): void {
189
+ if (field === 'startAt' || field === 'endAt')
190
+ state[field] = new Date(toScheduleTimeMs(value as string));
191
+ else (state as unknown as Record<string, unknown>)[field] = value;
192
+ }
193
+
194
+ /*
195
+ * Compare one field of two seat states (instants by milliseconds).
196
+ */
197
+ function isSameShiftField(
198
+ a: ScheduleShiftStateObj,
199
+ b: ScheduleShiftStateObj,
200
+ field: ScheduleShiftNetField,
201
+ ): boolean {
202
+ return field === 'startAt' || field === 'endAt'
203
+ ? toScheduleTimeMs(a[field]) === toScheduleTimeMs(b[field])
204
+ : a[field] === b[field];
205
+ }
206
+
207
+ /*
208
+ * Return a seat's net change over its journal rows (any order; read oldest
209
+ * first by createdAt, then id; rows of other seats ignored). `before` starts
210
+ * as the current state; each journaled field takes the FIRST previous value
211
+ * seen, the assignee the first ASSIGNED / UNASSIGNED row's fromResourceId,
212
+ * and the status PUBLISHED when a CANCELED row exists. Null when before and
213
+ * after are equal (a change that was undone).
214
+ */
215
+ export function getScheduleShiftNetChange(args: {
216
+ shift: ScheduleShiftStateObj;
217
+ changes: ScheduleShiftChangeRowInput[];
218
+ }): ScheduleShiftNetChangeObj | null {
219
+ const after = normalizeShiftState(args.shift);
220
+ const before = { ...after };
221
+ const seenFields = new Set<string>();
222
+ let assigneeSeen = false;
223
+
224
+ const rows = args.changes
225
+ .filter((row) => String(row.shiftId) === String(args.shift.id))
226
+ .sort(
227
+ (a, b) =>
228
+ toScheduleTimeMs(a.createdAt) - toScheduleTimeMs(b.createdAt) ||
229
+ compareScheduleIds(a.id, b.id),
230
+ );
231
+ for (const row of rows) {
232
+ for (const field of FILE_CONSTANTS.journalFields) {
233
+ if (
234
+ seenFields.has(field) ||
235
+ !row.previousValues ||
236
+ !Object.hasOwn(row.previousValues, field)
237
+ )
238
+ continue;
239
+ seenFields.add(field);
240
+ setShiftJournalValue(before, field, row.previousValues[field]);
241
+ }
242
+ if (!assigneeSeen && FILE_CONSTANTS.assigneeKinds.includes(row.kind)) {
243
+ assigneeSeen = true;
244
+ before.assigneeResourceId = row.fromResourceId;
245
+ }
246
+ if (row.kind === 'CANCELED') before.status = 'PUBLISHED';
247
+ }
248
+
249
+ const changedFields = FILE_CONSTANTS.netFields.filter(
250
+ (field) => !isSameShiftField(before, after, field),
251
+ );
252
+ return changedFields.length
253
+ ? { shiftId: args.shift.id, before, after, changedFields }
254
+ : null;
255
+ }
256
+
257
+ /*
258
+ * Return what a seat's net change means to one person: held before and
259
+ * after = CANCELED if the status changed, else RETIMED if a retime field
260
+ * changed, else EDITED; newly held = ADDED (null if the seat is now
261
+ * canceled); no longer held = REMOVED; never held = null.
262
+ */
263
+ export function getScheduleShiftRecipientChange(
264
+ net: ScheduleShiftNetChangeObj,
265
+ resourceId: string,
266
+ ): ScheduleShiftRecipientChangeEnum | null {
267
+ const held = net.before.assigneeResourceId === resourceId;
268
+ const holds = net.after.assigneeResourceId === resourceId;
269
+ if (held && holds) {
270
+ if (net.changedFields.includes('status')) return 'CANCELED';
271
+ return net.changedFields.some((field) =>
272
+ (SCHEDULE_SHIFT_RETIME_FIELDS as readonly string[]).includes(field),
273
+ )
274
+ ? 'RETIMED'
275
+ : 'EDITED';
276
+ }
277
+ if (holds) return net.after.status === 'CANCELED' ? null : 'ADDED';
278
+ return held ? 'REMOVED' : null;
279
+ }