@jsb188/calendar 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.
@@ -1,38 +1,58 @@
1
1
  import type {
2
- ScheduleOccurrenceGQL,
3
2
  ScheduleResourceGQL,
3
+ ScheduleRoleGQL,
4
+ ScheduleShiftGQL,
5
+ ScheduleShiftStatus,
4
6
  ScheduleTimeOffGQL,
5
7
  ScheduleViolationGQL,
6
- ScheduleSlotGQL,
7
8
  } from '@jsb188/graphql/schedule.d';
8
9
  import { DateTime } from 'luxon';
9
10
  import type { CalendarDayObj } from '../types/calendar.d.ts';
10
11
 
11
12
  type Breach = 'BLOCK' | 'WARN' | null;
12
- type RosterPersonResource = Pick<
13
+ type RosterPerson = Pick<
13
14
  ScheduleResourceGQL,
14
- 'id' | 'accountId' | 'name' | 'kind' | 'ordinal' | 'retiredAt'
15
+ 'id' | 'name' | 'ordinal' | 'retiredAt'
15
16
  >;
17
+ type RosterRoom = Pick<ScheduleResourceGQL, 'id' | 'name' | 'ordinal'>;
18
+ type RosterRole = Pick<ScheduleRoleGQL, 'id' | 'name' | 'color'>;
19
+ type RosterShift = Pick<
20
+ ScheduleShiftGQL,
21
+ | 'id'
22
+ | 'shiftDate'
23
+ | 'startAt'
24
+ | 'endAt'
25
+ | 'roleId'
26
+ | 'locationResourceId'
27
+ | 'assigneeResourceId'
28
+ | 'unpaidBreakMinutes'
29
+ | 'label'
30
+ | 'status'
31
+ >;
32
+
33
+ // One seat drawn in a roster cell
16
34
  export interface RosterChipObj {
17
- occurrenceId: string;
18
- assignmentId: string | null;
19
- slotKey: string | null;
20
- title: string;
35
+ shiftId: string;
36
+ roleName: string | null;
37
+ roleColor: string | null;
38
+ label: string | null;
21
39
  roomId: string | null;
22
40
  roomName: string | null;
23
41
  startLabel: string;
24
42
  endLabel: string;
25
- status: string;
43
+ status: ScheduleShiftStatus;
44
+ // Ends on a later calendar day than its shiftDate
26
45
  overnight: boolean;
27
46
  breach: Breach;
28
47
  isMine: boolean;
29
- version: string;
30
48
  }
31
49
  export interface RosterCellObj {
32
50
  dateKey: string;
33
51
  chips: RosterChipObj[];
34
52
  timeOff: ScheduleTimeOffGQL | null;
53
+ // Worked minutes (duration minus unpaid break) of the live seats
35
54
  minutes: number;
55
+ // Live open seats (OPEN rows only)
36
56
  openCount: number;
37
57
  breach: Breach;
38
58
  }
@@ -41,99 +61,122 @@ export interface RosterRowObj {
41
61
  kind: 'GROUP' | 'OPEN' | 'PERSON';
42
62
  roomId: string | null;
43
63
  resourceId: string | null;
44
- accountId: string | null;
64
+ // '' = a group without a room, or a seat's assignee outside the loaded people
45
65
  name: string;
46
66
  cells: RosterCellObj[];
47
67
  weekMinutes: number;
48
68
  breach: Breach;
49
69
  }
70
+ export interface RosterDayObj {
71
+ dateKey: string;
72
+ label: string;
73
+ staffCount: number;
74
+ openCount: number;
75
+ breach: Breach;
76
+ }
50
77
  export interface RosterLayoutObj {
51
78
  rows: RosterRowObj[];
52
- days: {
53
- dateKey: string;
54
- label: string;
55
- staffCount: number;
56
- openCount: number;
57
- breach: Breach;
58
- }[];
79
+ days: RosterDayObj[];
59
80
  openTotal: number;
60
81
  breachTotal: number;
61
82
  }
62
83
 
63
- /* Keep the most severe issue when several independent rules touch one cell. */
84
+ /*
85
+ * Keep the more severe of two issues.
86
+ */
64
87
  function worst(a: Breach, b: Breach): Breach {
65
88
  return a === 'BLOCK' || b === 'BLOCK' ? 'BLOCK' : a || b;
66
89
  }
67
90
 
68
- /* Build one people-by-day roster with stable primary-room grouping and start-date overnight chips. */
69
- export function buildRosterLayout(p: {
70
- days: CalendarDayObj[];
71
- timeZone: string;
72
- locale: string | null;
73
- resources: ScheduleResourceGQL[];
74
- rooms: ScheduleResourceGQL[];
75
- occurrences: ScheduleOccurrenceGQL[];
76
- timeOff: ScheduleTimeOffGQL[];
77
- violations: ScheduleViolationGQL[];
78
- groupByRoom: boolean;
79
- roomFilterId: string | null;
80
- mineOnly: boolean;
81
- viewerAccountId: string | null;
82
- viewerResourceId: string | null;
83
- formatTime: (at: Date, timeZone: string) => string;
84
- }): RosterLayoutObj {
85
- const resourceById = new Map<string, RosterPersonResource>(
86
- p.resources.map((resource) => [resource.id, resource]),
87
- );
88
- // Shared-calendar assignments carry display identity without exposing another organization's resource catalog.
89
- for (const occurrence of p.occurrences)
90
- for (const assignment of occurrence.assignments)
91
+ /*
92
+ * Order catalog rows by their ordinal, then by name.
93
+ */
94
+ function compareRosterOrdinal(
95
+ a: { ordinal: number; name: string },
96
+ b: { ordinal: number; name: string },
97
+ ): number {
98
+ return a.ordinal - b.ordinal || a.name.localeCompare(b.name);
99
+ }
100
+
101
+ /*
102
+ * Index the worst finding per seat and per person.
103
+ */
104
+ function getRosterBreachIndex(violations: ScheduleViolationGQL[]) {
105
+ const byShift = new Map<string, Breach>();
106
+ const byResource = new Map<string, Breach>();
107
+ for (const violation of violations) {
108
+ if (violation.shiftId)
109
+ byShift.set(
110
+ violation.shiftId,
111
+ worst(byShift.get(violation.shiftId) || null, violation.severity),
112
+ );
113
+ if (violation.resourceId)
114
+ byResource.set(
115
+ violation.resourceId,
116
+ worst(byResource.get(violation.resourceId) || null, violation.severity),
117
+ );
118
+ }
119
+ return { byShift, byResource };
120
+ }
121
+
122
+ /*
123
+ * Return a seat's worked minutes: its duration minus its unpaid break.
124
+ */
125
+ export function getRosterWorkedMinutes(
126
+ shift: Pick<RosterShift, 'startAt' | 'endAt' | 'unpaidBreakMinutes'>,
127
+ ): number {
128
+ const minutes =
129
+ (+new Date(shift.endAt) - +new Date(shift.startAt)) / 60000 -
130
+ (shift.unpaidBreakMinutes || 0);
131
+ return Math.max(0, minutes);
132
+ }
133
+
134
+ /*
135
+ * Build the day headers, with the worst finding that overlaps each day.
136
+ */
137
+ function buildRosterDays(
138
+ days: CalendarDayObj[],
139
+ timeZone: string,
140
+ locale: string | null,
141
+ violations: ScheduleViolationGQL[],
142
+ ): RosterDayObj[] {
143
+ return days.map((day) => {
144
+ let breach: Breach = null;
145
+ for (const violation of violations)
91
146
  if (
92
- assignment.status === 'ASSIGNED' &&
93
- !resourceById.has(assignment.resourceId)
147
+ +new Date(violation.startAt) < +day.endAt &&
148
+ +new Date(violation.endAt) > +day.startAt
94
149
  )
95
- resourceById.set(assignment.resourceId, {
96
- id: assignment.resourceId,
97
- accountId: assignment.accountId,
98
- name: assignment.resourceName || assignment.resourceId,
99
- kind: 'LABOR',
100
- ordinal: Number.MAX_SAFE_INTEGER,
101
- retiredAt: null,
102
- });
103
- const dayIndex = new Map(p.days.map((day, index) => [day.dateKey, index]));
104
- const rooms = [...p.rooms].sort(
105
- (a, b) => a.ordinal - b.ordinal || a.name.localeCompare(b.name),
106
- );
107
- const roomById = new Map(rooms.map((room) => [room.id, room]));
108
- const dayStaff = p.days.map(() => new Set<string>());
109
- const result: RosterLayoutObj = {
110
- rows: [],
111
- days: p.days.map((day) => ({
150
+ breach = worst(breach, violation.severity);
151
+ return {
112
152
  dateKey: day.dateKey,
113
- label: DateTime.fromJSDate(day.startAt, { zone: p.timeZone })
114
- .setLocale(p.locale || 'en')
153
+ label: DateTime.fromJSDate(day.startAt, { zone: timeZone })
154
+ .setLocale(locale || 'en')
115
155
  .toFormat('ccc d'),
116
156
  staffCount: 0,
117
157
  openCount: 0,
118
- breach: null,
119
- })),
120
- openTotal: 0,
121
- breachTotal: p.violations.length,
122
- };
123
- /* Create one row with independent cells, so mutations never alias another person's day. */
124
- const row = (
125
- kind: RosterRowObj['kind'],
126
- key: string,
127
- roomId: string | null,
128
- resource?: RosterPersonResource,
129
- ): RosterRowObj => ({
158
+ breach,
159
+ };
160
+ });
161
+ }
162
+
163
+ /*
164
+ * Create one row with its own cells, so no two rows share a cell.
165
+ */
166
+ function createRosterRow(
167
+ kind: RosterRowObj['kind'],
168
+ key: string,
169
+ roomId: string | null,
170
+ days: CalendarDayObj[],
171
+ person?: RosterPerson,
172
+ ): RosterRowObj {
173
+ return {
130
174
  key,
131
175
  kind,
132
176
  roomId,
133
- resourceId: resource?.id || null,
134
- accountId: resource?.accountId || null,
135
- name: resource?.name || '',
136
- cells: p.days.map((day) => ({
177
+ resourceId: person?.id || null,
178
+ name: person?.name || '',
179
+ cells: days.map((day) => ({
137
180
  dateKey: day.dateKey,
138
181
  chips: [],
139
182
  timeOff: null,
@@ -143,246 +186,272 @@ export function buildRosterLayout(p: {
143
186
  })),
144
187
  weekMinutes: 0,
145
188
  breach: null,
146
- });
147
- const people = new Map(
148
- [...resourceById.values()]
149
- .filter((resource) => resource.kind === 'LABOR')
150
- .map((resource) => [
151
- resource.id,
152
- row('PERSON', resource.id, null, resource),
153
- ]),
189
+ };
190
+ }
191
+
192
+ /*
193
+ * Draw one seat: role, times in the calendar's zone, room, status and findings.
194
+ */
195
+ function buildRosterChip(
196
+ shift: RosterShift,
197
+ ctx: {
198
+ timeZone: string;
199
+ roleById: Map<string, RosterRole>;
200
+ roomById: Map<string, RosterRoom>;
201
+ breachByShift: Map<string, Breach>;
202
+ viewerResourceId: string | null;
203
+ formatTime: (at: Date, timeZone: string) => string;
204
+ },
205
+ ): RosterChipObj {
206
+ const role = shift.roleId ? ctx.roleById.get(shift.roleId) : null;
207
+ const start = new Date(shift.startAt),
208
+ end = new Date(shift.endAt);
209
+ // A seat ending exactly at midnight still ends on its own day
210
+ const endDate = DateTime.fromMillis(+end - 1, {
211
+ zone: ctx.timeZone,
212
+ }).toISODate();
213
+ return {
214
+ shiftId: shift.id,
215
+ roleName: role?.name || null,
216
+ roleColor: role?.color || null,
217
+ label: shift.label || null,
218
+ roomId: shift.locationResourceId,
219
+ roomName: ctx.roomById.get(shift.locationResourceId || '')?.name || null,
220
+ startLabel: ctx.formatTime(start, ctx.timeZone),
221
+ endLabel: ctx.formatTime(end, ctx.timeZone),
222
+ status: shift.status,
223
+ overnight: endDate !== shift.shiftDate,
224
+ breach: ctx.breachByShift.get(shift.id) || null,
225
+ isMine:
226
+ !!ctx.viewerResourceId &&
227
+ shift.assigneeResourceId === ctx.viewerResourceId,
228
+ };
229
+ }
230
+
231
+ /*
232
+ * Return the room a person worked the most minutes in (ties: room ordinal, then id).
233
+ */
234
+ function getRosterPrimaryRoomId(
235
+ totals: Map<string, number> | undefined,
236
+ roomById: Map<string, RosterRoom>,
237
+ ): string | null {
238
+ const sorted = [...(totals || [])].sort(
239
+ (a, b) =>
240
+ b[1] - a[1] ||
241
+ (roomById.get(a[0])?.ordinal ?? Infinity) -
242
+ (roomById.get(b[0])?.ordinal ?? Infinity) ||
243
+ a[0].localeCompare(b[0]),
244
+ );
245
+ return sorted[0]?.[0] || null;
246
+ }
247
+
248
+ /*
249
+ * Return the time off shown in one day cell: approved before pending.
250
+ */
251
+ function getRosterDayTimeOff(
252
+ rows: ScheduleTimeOffGQL[],
253
+ day: CalendarDayObj,
254
+ ): ScheduleTimeOffGQL | null {
255
+ return (
256
+ rows
257
+ .filter(
258
+ (row) =>
259
+ +new Date(row.startAt) < +day.endAt &&
260
+ +new Date(row.endAt) > +day.startAt,
261
+ )
262
+ .sort(
263
+ (a, b) =>
264
+ Number(b.status === 'APPROVED') - Number(a.status === 'APPROVED'),
265
+ )[0] || null
266
+ );
267
+ }
268
+
269
+ /*
270
+ * Group pending and approved time off by person.
271
+ */
272
+ function getRosterTimeOffByResource(timeOff: ScheduleTimeOffGQL[]) {
273
+ const result = new Map<string, ScheduleTimeOffGQL[]>();
274
+ for (const row of timeOff)
275
+ if (row.status === 'PENDING' || row.status === 'APPROVED') {
276
+ const list = result.get(row.resourceId) || [];
277
+ list.push(row);
278
+ result.set(row.resourceId, list);
279
+ }
280
+ return result;
281
+ }
282
+
283
+ /*
284
+ * Build a read-only people-by-day roster from seats: one row per person, an
285
+ * "Open" row for seats with no assignee, each seat placed on its shiftDate
286
+ * (the calendar-local day it starts). Optionally grouped by room.
287
+ */
288
+ export function buildRosterLayout(p: {
289
+ days: CalendarDayObj[];
290
+ timeZone: string;
291
+ locale: string | null;
292
+ people: RosterPerson[];
293
+ rooms: RosterRoom[];
294
+ roles: RosterRole[];
295
+ shifts: RosterShift[];
296
+ timeOff: ScheduleTimeOffGQL[];
297
+ violations: ScheduleViolationGQL[];
298
+ groupByRoom: boolean;
299
+ roomFilterId: string | null;
300
+ viewerResourceId: string | null;
301
+ formatTime: (at: Date, timeZone: string) => string;
302
+ }): RosterLayoutObj {
303
+ const dayIndex = new Map(p.days.map((day, index) => [day.dateKey, index]));
304
+ const rooms = [...p.rooms].sort(compareRosterOrdinal);
305
+ const roomById = new Map(rooms.map((room) => [room.id, room]));
306
+ const breaches = getRosterBreachIndex(p.violations);
307
+ const chipContext = {
308
+ timeZone: p.timeZone,
309
+ roleById: new Map(p.roles.map((role) => [role.id, role])),
310
+ roomById,
311
+ breachByShift: breaches.byShift,
312
+ viewerResourceId: p.viewerResourceId,
313
+ formatTime: p.formatTime,
314
+ };
315
+ const result: RosterLayoutObj = {
316
+ rows: [],
317
+ days: buildRosterDays(p.days, p.timeZone, p.locale, p.violations),
318
+ openTotal: 0,
319
+ breachTotal: p.violations.length,
320
+ };
321
+ const personById = new Map<string, RosterPerson>(
322
+ p.people.map((person) => [person.id, person]),
323
+ );
324
+ const people = new Map<string, RosterRowObj>(
325
+ p.people.map((person) => [
326
+ person.id,
327
+ createRosterRow('PERSON', person.id, null, p.days, person),
328
+ ]),
154
329
  );
330
+ const dayStaff = p.days.map(() => new Set<string>());
155
331
  const roomMinutes = new Map<string, Map<string, number>>();
156
332
  const openRows = new Map<string, RosterRowObj>();
157
- const byOccurrence = new Map<string, Breach>();
158
- const byResource = new Map<string, Breach>();
159
- for (const violation of p.violations) {
160
- if (violation.occurrenceId)
161
- byOccurrence.set(
162
- violation.occurrenceId,
163
- worst(
164
- byOccurrence.get(violation.occurrenceId) || null,
165
- violation.severity,
166
- ),
167
- );
168
- if (violation.resourceId)
169
- byResource.set(
170
- violation.resourceId,
171
- worst(byResource.get(violation.resourceId) || null, violation.severity),
172
- );
173
- for (let i = 0; i < p.days.length; i++)
174
- if (
175
- +new Date(violation.startAt) < +p.days[i].endAt &&
176
- +new Date(violation.endAt) > +p.days[i].startAt
177
- )
178
- result.days[i].breach = worst(
179
- result.days[i].breach,
180
- violation.severity,
181
- );
182
- }
183
- for (const occurrence of p.occurrences) {
184
- if (p.roomFilterId && occurrence.resourceId !== p.roomFilterId) continue;
185
- const start = new Date(occurrence.startAt),
186
- end = new Date(occurrence.endAt);
187
- const dateKey = DateTime.fromJSDate(start, {
188
- zone: p.timeZone,
189
- }).toISODate()!;
190
- const index = dayIndex.get(dateKey);
333
+ const shifts = [...p.shifts].sort(
334
+ (a, b) =>
335
+ +new Date(a.startAt) - +new Date(b.startAt) || a.id.localeCompare(b.id),
336
+ );
337
+
338
+ for (const shift of shifts) {
339
+ if (p.roomFilterId && shift.locationResourceId !== p.roomFilterId) continue;
340
+ const index = dayIndex.get(shift.shiftDate);
191
341
  if (index === undefined) continue;
192
- const roomId = occurrence.resourceId,
193
- groupKey = p.groupByRoom ? roomId || '' : '';
194
- const active = occurrence.status !== 'CANCELED';
195
- const assignments = occurrence.assignments.filter(
196
- (assignment) => assignment.status === 'ASSIGNED',
197
- );
198
- /* Share immutable chip fields while keeping assignment identities distinct. */
199
- const chip = (
200
- assignmentId: string | null,
201
- slotKey: string | null,
202
- isMine: boolean,
203
- ): RosterChipObj => ({
204
- occurrenceId: occurrence.id,
205
- assignmentId,
206
- slotKey,
207
- title: occurrence.title,
208
- roomId,
209
- roomName: roomById.get(roomId || '')?.name || null,
210
- startLabel: p.formatTime(start, p.timeZone),
211
- endLabel: p.formatTime(end, p.timeZone),
212
- status: occurrence.status,
213
- overnight:
214
- DateTime.fromJSDate(end, { zone: p.timeZone }).toISODate() !== dateKey,
215
- breach: byOccurrence.get(occurrence.id) || null,
216
- isMine,
217
- version: occurrence.version,
218
- });
219
- for (const assignment of assignments) {
220
- const person = people.get(assignment.resourceId);
221
- if (!person) continue;
222
- const isMine =
223
- (assignment.accountId === p.viewerAccountId && !!p.viewerAccountId) ||
224
- assignment.resourceId === p.viewerResourceId;
225
- person.cells[index].chips.push(
226
- chip(assignment.id, assignment.slotKey, isMine),
227
- );
228
- person.breach = worst(
229
- person.breach,
230
- worst(
231
- byResource.get(assignment.resourceId) || null,
232
- byOccurrence.get(occurrence.id) || null,
233
- ),
234
- );
235
- if (active) {
236
- const minutes = Math.max(
237
- 0,
238
- (Math.min(+end, +p.days[p.days.length - 1].endAt) -
239
- Math.max(+start, +p.days[0].startAt)) /
240
- 60000,
241
- );
242
- person.weekMinutes += minutes;
243
- person.cells[index].minutes += minutes;
244
- dayStaff[index].add(assignment.resourceId);
245
- const totals = roomMinutes.get(assignment.resourceId) || new Map();
246
- totals.set(roomId || '', (totals.get(roomId || '') || 0) + minutes);
247
- roomMinutes.set(assignment.resourceId, totals);
248
- }
249
- }
250
- if (active && occurrence.kind === 'SHIFT' && occurrence.openCount > 0) {
342
+ const chip = buildRosterChip(shift, chipContext);
343
+ const live = shift.status !== 'CANCELED';
344
+ // A room outside the loaded rooms groups as "no room"
345
+ const roomKey = roomById.has(shift.locationResourceId || '')
346
+ ? shift.locationResourceId!
347
+ : '';
348
+
349
+ if (!shift.assigneeResourceId) {
350
+ const groupKey = p.groupByRoom ? roomKey : '';
251
351
  const open =
252
352
  openRows.get(groupKey) ||
253
- row('OPEN', `open:${groupKey}`, p.groupByRoom ? roomId : null);
353
+ createRosterRow('OPEN', `open:${groupKey}`, groupKey || null, p.days);
254
354
  openRows.set(groupKey, open);
255
- open.cells[index].openCount += occurrence.openCount;
256
- open.cells[index].chips.push(chip(null, null, false));
257
- open.cells[index].breach = worst(
258
- open.cells[index].breach,
259
- byOccurrence.get(occurrence.id) || byResource.get(roomId || '') || null,
260
- );
261
- result.days[index].openCount += occurrence.openCount;
262
- result.openTotal += occurrence.openCount;
355
+ const cell = open.cells[index];
356
+ cell.chips.push(chip);
357
+ if (live) {
358
+ cell.openCount += 1;
359
+ cell.breach = worst(cell.breach, chip.breach);
360
+ result.days[index].openCount += 1;
361
+ result.openTotal += 1;
362
+ }
363
+ continue;
263
364
  }
264
- }
265
- const timeOffByResource = new Map<string, ScheduleTimeOffGQL[]>();
266
- for (const timeOff of p.timeOff)
267
- if (['PENDING', 'APPROVED'].includes(timeOff.status)) {
268
- const list = timeOffByResource.get(timeOff.resourceId) || [];
269
- list.push(timeOff);
270
- timeOffByResource.set(timeOff.resourceId, list);
365
+
366
+ const assigneeId = shift.assigneeResourceId;
367
+ let person = people.get(assigneeId);
368
+ if (!person) {
369
+ // A seat names someone outside the loaded people (another organization's
370
+ // shared calendar, or a catalog the viewer cannot read)
371
+ const unknown = {
372
+ id: assigneeId,
373
+ name: '',
374
+ ordinal: Number.MAX_SAFE_INTEGER,
375
+ retiredAt: null,
376
+ };
377
+ personById.set(assigneeId, unknown);
378
+ person = createRosterRow('PERSON', assigneeId, null, p.days, unknown);
379
+ people.set(assigneeId, person);
380
+ }
381
+ const cell = person.cells[index];
382
+ cell.chips.push(chip);
383
+ person.breach = worst(person.breach, chip.breach);
384
+ if (live) {
385
+ const minutes = getRosterWorkedMinutes(shift);
386
+ person.weekMinutes += minutes;
387
+ cell.minutes += minutes;
388
+ dayStaff[index].add(assigneeId);
389
+ const totals = roomMinutes.get(assigneeId) || new Map<string, number>();
390
+ totals.set(roomKey, (totals.get(roomKey) || 0) + minutes);
391
+ roomMinutes.set(assigneeId, totals);
271
392
  }
393
+ }
394
+
395
+ const timeOffByResource = getRosterTimeOffByResource(p.timeOff);
396
+ const assignedAnywhere = new Set(
397
+ p.shifts.map((shift) => shift.assigneeResourceId).filter(Boolean),
398
+ );
272
399
  const visiblePeople = [...people.values()]
273
- .filter((person) => {
274
- const resource = resourceById.get(person.resourceId!)!;
275
- const hasChip = person.cells.some((cell) => cell.chips.length);
276
- if (resource.retiredAt && !hasChip) return false;
277
- if (
278
- p.mineOnly &&
279
- resource.id !== p.viewerResourceId &&
280
- resource.accountId !== p.viewerAccountId
281
- )
282
- return false;
283
- if (
284
- p.roomFilterId &&
285
- !hasChip &&
286
- p.occurrences.some((occurrence) =>
287
- occurrence.assignments.some(
288
- (assignment) => assignment.resourceId === resource.id,
289
- ),
290
- )
291
- )
400
+ .filter((row) => {
401
+ const person = personById.get(row.resourceId!)!;
402
+ const hasChip = row.cells.some((cell) => cell.chips.length);
403
+ if (person.retiredAt && !hasChip) return false;
404
+ // With a room filter, hide people whose seats this week are all elsewhere
405
+ if (p.roomFilterId && !hasChip && assignedAnywhere.has(person.id))
292
406
  return false;
293
- const totals = [...(roomMinutes.get(resource.id) || [])].sort(
294
- (a, b) =>
295
- b[1] - a[1] ||
296
- (roomById.get(a[0])?.ordinal ?? Infinity) -
297
- (roomById.get(b[0])?.ordinal ?? Infinity) ||
298
- a[0].localeCompare(b[0]),
299
- );
300
- person.roomId = totals[0]?.[0] || null;
301
- person.breach = worst(person.breach, byResource.get(resource.id) || null);
302
- person.cells.forEach((cell, i) => {
303
- cell.timeOff =
304
- (timeOffByResource.get(resource.id) || [])
305
- .filter(
306
- (timeOff) =>
307
- +new Date(timeOff.startAt) < +p.days[i].endAt &&
308
- +new Date(timeOff.endAt) > +p.days[i].startAt,
309
- )
310
- .sort(
311
- (a, b) =>
312
- Number(b.status === 'APPROVED') -
313
- Number(a.status === 'APPROVED'),
314
- )[0] || null;
315
- });
316
407
  return true;
317
408
  })
318
- .sort(
319
- (a, b) =>
320
- resourceById.get(a.resourceId!)!.ordinal -
321
- resourceById.get(b.resourceId!)!.ordinal ||
322
- a.name.localeCompare(b.name),
409
+ .sort((a, b) =>
410
+ compareRosterOrdinal(
411
+ personById.get(a.resourceId!)!,
412
+ personById.get(b.resourceId!)!,
413
+ ),
323
414
  );
415
+ for (const row of visiblePeople) {
416
+ const resourceId = row.resourceId!;
417
+ row.roomId = getRosterPrimaryRoomId(roomMinutes.get(resourceId), roomById);
418
+ row.breach = worst(row.breach, breaches.byResource.get(resourceId) || null);
419
+ const timeOff = timeOffByResource.get(resourceId) || [];
420
+ row.cells.forEach((cell, i) => {
421
+ cell.timeOff = getRosterDayTimeOff(timeOff, p.days[i]);
422
+ });
423
+ }
424
+
324
425
  if (p.groupByRoom) {
325
426
  for (const roomId of [...rooms.map((room) => room.id), '']) {
326
427
  if (p.roomFilterId && roomId !== p.roomFilterId) continue;
327
428
  const members = visiblePeople.filter(
328
- (person) => (person.roomId || '') === roomId,
429
+ (row) => (row.roomId || '') === roomId,
329
430
  );
330
431
  if (!members.length && !openRows.has(roomId)) continue;
331
- const group = row('GROUP', `group:${roomId}`, roomId || null);
432
+ const group = createRosterRow(
433
+ 'GROUP',
434
+ `group:${roomId}`,
435
+ roomId || null,
436
+ p.days,
437
+ );
332
438
  group.name = roomById.get(roomId)?.name || '';
333
- group.breach = byResource.get(roomId) || null;
439
+ group.breach = breaches.byResource.get(roomId) || null;
334
440
  result.rows.push(
335
441
  group,
336
- openRows.get(roomId) || row('OPEN', `open:${roomId}`, roomId || null),
442
+ openRows.get(roomId) ||
443
+ createRosterRow('OPEN', `open:${roomId}`, roomId || null, p.days),
337
444
  ...members,
338
445
  );
339
446
  }
340
- } else
447
+ } else {
341
448
  result.rows = [
342
- openRows.get('') || row('OPEN', 'open:', null),
449
+ openRows.get('') || createRosterRow('OPEN', 'open:', null, p.days),
343
450
  ...visiblePeople,
344
451
  ];
452
+ }
345
453
  result.days.forEach((day, i) => {
346
454
  day.staffCount = dayStaff[i].size;
347
455
  });
348
456
  return result;
349
457
  }
350
-
351
- /* Give instant picker hints; the server still decides final cross-calendar eligibility. */
352
- export function getRosterCandidateHints(p: {
353
- resource: ScheduleResourceGQL;
354
- occurrence: ScheduleOccurrenceGQL;
355
- slot: ScheduleSlotGQL | null;
356
- dayChips: RosterChipObj[];
357
- timeOff: ScheduleTimeOffGQL[];
358
- }) {
359
- const predicate = p.slot?.predicate;
360
- return {
361
- predicateMatch:
362
- !predicate ||
363
- ((predicate.roles || []).every((role) =>
364
- p.resource.config.roles.includes(role),
365
- ) &&
366
- (predicate.tags || []).every((tag) =>
367
- p.resource.config.tags.includes(tag),
368
- ) &&
369
- (predicate.qualifications || []).every((key) =>
370
- p.resource.config.qualifications.some((q) => q.key === key),
371
- )),
372
- sameDayOverlap: p.dayChips.some(
373
- (chip) => chip.occurrenceId !== p.occurrence.id,
374
- ),
375
- onTimeOff: p.timeOff.some(
376
- (row) =>
377
- row.resourceId === p.resource.id &&
378
- row.status === 'APPROVED' &&
379
- row.startAt < p.occurrence.endAt &&
380
- row.endAt > p.occurrence.startAt,
381
- ),
382
- alreadyAssigned: p.occurrence.assignments.some(
383
- (assignment) =>
384
- assignment.status === 'ASSIGNED' &&
385
- assignment.resourceId === p.resource.id,
386
- ),
387
- };
388
- }