@kernhq/module-hr 0.19.0 → 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/dist/contract/capabilities.d.ts.map +1 -1
  2. package/dist/contract/capabilities.js +29 -0
  3. package/dist/contract/capabilities.js.map +1 -1
  4. package/dist/contract/index.d.ts +1 -0
  5. package/dist/contract/index.d.ts.map +1 -1
  6. package/dist/contract/index.js +1 -0
  7. package/dist/contract/index.js.map +1 -1
  8. package/dist/contract/permissions.d.ts +2 -2
  9. package/dist/contract/permissions.d.ts.map +1 -1
  10. package/dist/contract/permissions.js +12 -2
  11. package/dist/contract/permissions.js.map +1 -1
  12. package/dist/contract/rosters.d.ts +183 -0
  13. package/dist/contract/rosters.d.ts.map +1 -0
  14. package/dist/contract/rosters.js +138 -0
  15. package/dist/contract/rosters.js.map +1 -0
  16. package/dist/contract/router.d.ts +621 -0
  17. package/dist/contract/router.d.ts.map +1 -1
  18. package/dist/contract/router.js +153 -1
  19. package/dist/contract/router.js.map +1 -1
  20. package/dist/server/router.d.ts +927 -0
  21. package/dist/server/router.d.ts.map +1 -1
  22. package/dist/server/router.js +462 -1
  23. package/dist/server/router.js.map +1 -1
  24. package/dist/server/schema.d.ts +727 -1
  25. package/dist/server/schema.d.ts.map +1 -1
  26. package/dist/server/schema.js +105 -0
  27. package/dist/server/schema.js.map +1 -1
  28. package/dist/server/services/rosters.d.ts +166 -0
  29. package/dist/server/services/rosters.d.ts.map +1 -0
  30. package/dist/server/services/rosters.js +268 -0
  31. package/dist/server/services/rosters.js.map +1 -0
  32. package/migrations/0012_rosters.sql +163 -0
  33. package/migrations/meta/0012_snapshot.json +4847 -0
  34. package/migrations/meta/_journal.json +7 -0
  35. package/package.json +1 -1
  36. package/src/contract/capabilities.ts +29 -0
  37. package/src/contract/index.ts +1 -0
  38. package/src/contract/permissions.ts +13 -2
  39. package/src/contract/rosters.ts +156 -0
  40. package/src/contract/router.ts +183 -0
@@ -78,6 +78,13 @@
78
78
  "when": 1787858088123,
79
79
  "tag": "0011_privacy",
80
80
  "breakpoints": true
81
+ },
82
+ {
83
+ "idx": 12,
84
+ "version": "7",
85
+ "when": 1787864979651,
86
+ "tag": "0012_rosters",
87
+ "breakpoints": true
81
88
  }
82
89
  ]
83
90
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kernhq/module-hr",
3
- "version": "0.19.0",
3
+ "version": "0.20.0",
4
4
  "description": "Kern HR module: people, offices, org chart, calendars",
5
5
  "license": "AGPL-3.0-only",
6
6
  "type": "module",
@@ -122,6 +122,18 @@ export const hrCapabilities = defineCapabilities([
122
122
  defaultEnabled: false,
123
123
  level: 1,
124
124
  },
125
+ {
126
+ id: 'rosters',
127
+ label: 'Shift rosters',
128
+ description: 'Rotating shifts on a calendar, and who covers which office-day',
129
+ // Depends on attendance rather than on core: a roster is what the clock and the day sheet are
130
+ // judged against, and rostering a workspace that never clocks anybody in is a planning grid
131
+ // nothing reads. Off by default and level 3 — most companies work a week that repeats, and for
132
+ // them a rotation editor is a screen full of settings that change nothing.
133
+ dependsOn: ['attendance'],
134
+ defaultEnabled: false,
135
+ level: 3,
136
+ },
125
137
  {
126
138
  id: 'documents',
127
139
  label: 'Employee documents',
@@ -248,6 +260,23 @@ export const hrCapabilityProcedures: Record<string, readonly string[]> = {
248
260
  'reports.overtime',
249
261
  'reports.absence',
250
262
  ],
263
+ rosters: [
264
+ 'rosters.shifts.list',
265
+ 'rosters.shifts.create',
266
+ 'rosters.shifts.update',
267
+ 'rosters.shifts.archive',
268
+ 'rosters.patterns.list',
269
+ 'rosters.patterns.create',
270
+ 'rosters.patterns.update',
271
+ 'rosters.patterns.archive',
272
+ 'rosters.assignments',
273
+ 'rosters.assign',
274
+ 'rosters.unassign',
275
+ 'rosters.days',
276
+ 'rosters.set',
277
+ 'rosters.clear',
278
+ 'rosters.coverage',
279
+ ],
251
280
  leave_accrual: [
252
281
  'policies.list',
253
282
  'policies.get',
@@ -17,5 +17,6 @@ export * from './permissions.js'
17
17
  export * from './policies.js'
18
18
  export * from './privacy.js'
19
19
  export * from './reports.js'
20
+ export * from './rosters.js'
20
21
  export * from './router.js'
21
22
  export * from './settings.js'
@@ -252,8 +252,11 @@ export const hrPermissions = definePermissions([
252
252
  },
253
253
  {
254
254
  key: 'hr.attendance.manage',
255
- label: 'Correct punches, schedules and day sheets',
256
- description: 'Voids punches and recomputes days. Every change leaves the original visible.',
255
+ label: 'Correct punches and set schedules, rosters and day sheets',
256
+ description:
257
+ 'Voids punches and recomputes days. Every change leaves the original visible. Also edits ' +
258
+ 'shifts and rotations and puts people on them, which decides what a shift worker is ' +
259
+ 'expected to turn up for.',
257
260
  scope: 'workspace',
258
261
  defaultRoles: ['owner', 'admin'],
259
262
  dangerous: true,
@@ -262,6 +265,14 @@ export const hrPermissions = definePermissions([
262
265
  // nothing asks a question these keys could answer — a role checkbox that gates nothing teaches an
263
266
  // administrator the same lesson a dead capability switch does. They come back with the approval
264
267
  // flow that needs them.
268
+ //
269
+ // No `hr.roster.*` either, for the same reason and after weighing it. Rostering is `manage`:
270
+ // deciding what somebody was meant to work is the same authority as correcting what they did, and
271
+ // both feed the same day sheet. Reading a roster is `hr.attendance.view` for your own and
272
+ // `hr.attendance.view_team` for anybody else's — the coverage grid asks for the second, which is
273
+ // already what `attendance.days.list` costs to read a whole office. Four new keys would gate
274
+ // nothing the three existing ones do not already gate, which is how `hr.overtime.*` came to be
275
+ // deleted. They arrive if a workspace ever needs a planner who may roster and may not correct.
265
276
 
266
277
  // ---------------------------------------------------------------- reports
267
278
  {
@@ -0,0 +1,156 @@
1
+ import { Timestamp, WorkspaceId } from '@kernhq/contracts'
2
+ import { z } from 'zod'
3
+ import { IsoDate, WallClock } from './models.js'
4
+
5
+ const ws = { workspaceId: WorkspaceId }
6
+
7
+ /**
8
+ * Rosters: which shift a person works on a **date**.
9
+ *
10
+ * A `Schedule` is a week that repeats for ever, so `ScheduleWeek` is keyed by weekday name. That is
11
+ * the right shape for an office and cannot express a factory: 4-on-4-off has no weekly period at
12
+ * all, and neither does any 2- or 3-week rotation, so there is no length of `ScheduleWeek` that
13
+ * describes one. A roster is keyed by the calendar instead, and adds exactly three things a
14
+ * schedule does not have:
15
+ *
16
+ * - **A shift on a date.** The key is the day, not a modulus of seven.
17
+ * - **Coverage** — "who is on Early on Tuesday" is a question about an office-day, not about a
18
+ * person, and it cannot be asked of a set of weekly schedules without walking everybody's week.
19
+ * - **A one-day exception that survives.** A schedule change is effective-dated and rewrites every
20
+ * day after it; a roster override changes one day and leaves the rotation alone.
21
+ *
22
+ * Everything else attendance already does — grace, rounding, overnight shifts, night-shift business
23
+ * date attribution, the auto-close sweep — stays where it is. A roster is not a second computation
24
+ * of hours; it is a different answer to "what was this person meant to work today".
25
+ *
26
+ * **The rotation is computed, never stored per day.** A `RosterPattern` is a cycle of days and the
27
+ * date `days[0]` falls on; what somebody works on any date at all is arithmetic from those two.
28
+ * Generating a year of rows per person is what makes a roster impossible to change afterwards —
29
+ * moving a crew forward by a day becomes a bulk rewrite of thousands of rows with no way to say
30
+ * which of them a human had touched. Only the exceptions are rows.
31
+ */
32
+
33
+ /**
34
+ * A named shift: Early, Late, Night.
35
+ *
36
+ * Named rather than inlined into each pattern because coverage groups by it — "Early" in the
37
+ * warehouse pattern and "Early" in the picking pattern have to be the same column of the same grid.
38
+ * `graceInMinutes` / `graceOutMinutes` sit here rather than on the schedule for the same reason a
39
+ * night shift usually has a wider grace than a day one: they are properties of the shift.
40
+ */
41
+ export const RosterShift = z.object({
42
+ id: z.uuid(),
43
+ ...ws,
44
+ name: z.string().min(1).max(80),
45
+ /** A one- or two-letter code, for a grid too dense to carry a name. */
46
+ code: z.string().max(8).nullable(),
47
+ start: WallClock,
48
+ /** Earlier than `start` for a shift that ends the next morning — the same rule `ShiftSpec` uses. */
49
+ end: WallClock,
50
+ breakMinutes: z.number().int().min(0).max(480),
51
+ graceInMinutes: z.number().int().min(0).max(240),
52
+ graceOutMinutes: z.number().int().min(0).max(240),
53
+ color: z.string().max(32).nullable(),
54
+ archivedAt: Timestamp.nullable(),
55
+ })
56
+ export type RosterShift = z.infer<typeof RosterShift>
57
+
58
+ /**
59
+ * One position in a rotation: the shifts worked on that day of the cycle.
60
+ *
61
+ * An **array**, so a split shift — 06:00–10:00 and 16:00–20:00, ordinary in hospitality — is a
62
+ * cycle day with two entries rather than something the model forbids. An empty array is a rest day,
63
+ * which is a different fact from "nothing is rostered": a rest day is planned.
64
+ */
65
+ export const RosterCycleDay = z.array(z.uuid()).max(4)
66
+ export type RosterCycleDay = z.infer<typeof RosterCycleDay>
67
+
68
+ /**
69
+ * A rotation, as a cycle and the date it starts from.
70
+ *
71
+ * The cycle length is `days.length` and is not stored separately — two numbers that must agree is
72
+ * one number and a bug. `anchorDate` is the date `days[0]` applies to; every other date is
73
+ * `(dayNumber(date) - dayNumber(anchorDate) + cycleOffset) mod days.length`, which answers for
74
+ * dates before the anchor as readily as after it.
75
+ */
76
+ export const RosterPattern = z.object({
77
+ id: z.uuid(),
78
+ ...ws,
79
+ name: z.string().min(1).max(120),
80
+ /** The date `days[0]` falls on. Moving it rotates every assignment on this pattern at once. */
81
+ anchorDate: IsoDate,
82
+ days: z.array(RosterCycleDay).min(1).max(56),
83
+ archivedAt: Timestamp.nullable(),
84
+ })
85
+ export type RosterPattern = z.infer<typeof RosterPattern>
86
+
87
+ /**
88
+ * A person on a rotation, over a period.
89
+ *
90
+ * Effective-dated like every other assignment here, and at most one may be in force on a day — the
91
+ * exclusion constraint in migration 0012 is what guarantees that rather than the handler, because
92
+ * two concurrent requests cannot both win against a constraint.
93
+ *
94
+ * `cycleOffset` is what puts two crews on one pattern out of phase: crew B on a 4-on-4-off cycle
95
+ * with `cycleOffset: 4` works exactly the days crew A is off. Without it every crew needs its own
96
+ * copy of the same rotation, and a change to the rotation has to be made once per crew.
97
+ */
98
+ export const RosterAssignment = z.object({
99
+ id: z.uuid(),
100
+ ...ws,
101
+ personId: z.uuid(),
102
+ patternId: z.uuid(),
103
+ effectiveFrom: IsoDate,
104
+ effectiveTo: IsoDate.nullable(),
105
+ cycleOffset: z.number().int().min(0).max(55),
106
+ createdAt: Timestamp,
107
+ })
108
+ export type RosterAssignment = z.infer<typeof RosterAssignment>
109
+
110
+ /**
111
+ * Where a rostered day came from.
112
+ *
113
+ * `none` is not the same as a rest day: it means nothing rosters this person on this date at all,
114
+ * and a screen that renders it identically to a planned day off is telling somebody their absence
115
+ * was intended. A rest day is `pattern` with no shifts.
116
+ */
117
+ export const RosterDaySource = z.enum(['pattern', 'override', 'none'])
118
+ export type RosterDaySource = z.infer<typeof RosterDaySource>
119
+
120
+ export const RosterDay = z.object({
121
+ personId: z.uuid(),
122
+ businessDate: IsoDate,
123
+ shifts: z.array(RosterShift),
124
+ source: RosterDaySource,
125
+ /** Why this day differs from the rotation. Only ever set on an override. */
126
+ note: z.string().max(500).nullable(),
127
+ })
128
+ export type RosterDay = z.infer<typeof RosterDay>
129
+
130
+ /** Enough of a person to fill a coverage grid. The name every member may already read. */
131
+ export const RosterPerson = z.object({ personId: z.uuid(), displayName: z.string() })
132
+ export type RosterPerson = z.infer<typeof RosterPerson>
133
+
134
+ /**
135
+ * One office-day, which is the question a roster exists to answer.
136
+ *
137
+ * `off` carries the people a pattern covers on this date and does not put on a shift — the answer
138
+ * to "who could I call in", which is the second thing anybody looking at a coverage grid wants and
139
+ * the one a list of who is working cannot give.
140
+ */
141
+ export const RosterCoverageDay = z.object({
142
+ businessDate: IsoDate,
143
+ slots: z.array(z.object({ shift: RosterShift, people: z.array(RosterPerson) })),
144
+ off: z.array(RosterPerson),
145
+ })
146
+ export type RosterCoverageDay = z.infer<typeof RosterCoverageDay>
147
+
148
+ /**
149
+ * How long a roster range may be.
150
+ *
151
+ * Expansion is arithmetic and cheap; the cost is the person-days a coverage grid resolves and the
152
+ * size of what comes back. A quarter covers the longest rotation anybody plans by hand, and
153
+ * coverage is capped tighter because it multiplies by the population of an office.
154
+ */
155
+ export const MAX_ROSTER_DAYS = 186
156
+ export const MAX_COVERAGE_DAYS = 42
@@ -52,6 +52,7 @@ import {
52
52
  RegionCode,
53
53
  ResolvedCalendarDay,
54
54
  TimeZone,
55
+ WallClock,
55
56
  WorkingWeek,
56
57
  } from './models.js'
57
58
  import {
@@ -77,6 +78,14 @@ import {
77
78
  OvertimeReport,
78
79
  ReportSliceBy,
79
80
  } from './reports.js'
81
+ import {
82
+ RosterAssignment,
83
+ RosterCoverageDay,
84
+ RosterCycleDay,
85
+ RosterDay,
86
+ RosterPattern,
87
+ RosterShift,
88
+ } from './rosters.js'
80
89
 
81
90
  const ws = z.object({ workspaceId: WorkspaceId })
82
91
  const t = ['hr'] as const
@@ -974,6 +983,180 @@ export const hrContract = {
974
983
  },
975
984
  },
976
985
 
986
+ // ---------------------------------------------------------------- rosters
987
+ /**
988
+ * Who works which shift on which **date**.
989
+ *
990
+ * Separate from `attendance.schedules` rather than folded into it, because the two answer
991
+ * different questions and only one of them has a weekly period. A schedule is a week that repeats
992
+ * for ever; a rotation is a cycle of any length anchored to a date, which is the only shape
993
+ * 4-on-4-off has. Nothing here recomputes hours — grace, rounding, overnight attribution and the
994
+ * auto-close sweep all stay in attendance, and a roster only decides what the day was meant to be.
995
+ */
996
+ rosters: {
997
+ shifts: {
998
+ list: baseContract
999
+ .route({ method: 'GET', path: '/rosters/shifts', tags: t })
1000
+ .input(ws.extend({ includeArchived: z.boolean().default(false) }))
1001
+ .output(z.array(RosterShift)),
1002
+ create: baseContract
1003
+ .route({ method: 'POST', path: '/rosters/shifts', tags: t })
1004
+ .input(
1005
+ ws.extend({
1006
+ name: z.string().min(1).max(80),
1007
+ code: z.string().max(8).nullish(),
1008
+ start: WallClock,
1009
+ end: WallClock,
1010
+ breakMinutes: z.number().int().min(0).max(480).default(0),
1011
+ graceInMinutes: z.number().int().min(0).max(240).default(0),
1012
+ graceOutMinutes: z.number().int().min(0).max(240).default(0),
1013
+ color: z.string().max(32).nullish(),
1014
+ }),
1015
+ )
1016
+ .output(RosterShift),
1017
+ update: baseContract
1018
+ .route({ method: 'PATCH', path: '/rosters/shifts/{shiftId}', tags: t })
1019
+ .input(
1020
+ ws.extend({
1021
+ shiftId: z.uuid(),
1022
+ name: z.string().min(1).max(80).optional(),
1023
+ code: z.string().max(8).nullish(),
1024
+ start: WallClock.optional(),
1025
+ end: WallClock.optional(),
1026
+ breakMinutes: z.number().int().min(0).max(480).optional(),
1027
+ graceInMinutes: z.number().int().min(0).max(240).optional(),
1028
+ graceOutMinutes: z.number().int().min(0).max(240).optional(),
1029
+ color: z.string().max(32).nullish(),
1030
+ }),
1031
+ )
1032
+ .output(RosterShift),
1033
+ /**
1034
+ * Archive, never delete. Patterns and stored overrides point at a shift by id, so deleting
1035
+ * one would empty out every day it appears on — past days included.
1036
+ */
1037
+ archive: baseContract
1038
+ .route({ method: 'DELETE', path: '/rosters/shifts/{shiftId}', tags: t })
1039
+ .input(ws.extend({ shiftId: z.uuid() }))
1040
+ .output(ok),
1041
+ },
1042
+
1043
+ patterns: {
1044
+ list: baseContract
1045
+ .route({ method: 'GET', path: '/rosters/patterns', tags: t })
1046
+ .input(ws.extend({ includeArchived: z.boolean().default(false) }))
1047
+ .output(z.array(RosterPattern)),
1048
+ create: baseContract
1049
+ .route({ method: 'POST', path: '/rosters/patterns', tags: t })
1050
+ .input(
1051
+ ws.extend({
1052
+ name: z.string().min(1).max(120),
1053
+ anchorDate: IsoDate,
1054
+ days: z.array(RosterCycleDay).min(1).max(56),
1055
+ }),
1056
+ )
1057
+ .output(RosterPattern),
1058
+ /**
1059
+ * Editing a rotation moves every crew on it, on every date, at once — which is the point of a
1060
+ * rotation being computed rather than generated, and worth saying out loud on the screen that
1061
+ * does it.
1062
+ */
1063
+ update: baseContract
1064
+ .route({ method: 'PATCH', path: '/rosters/patterns/{patternId}', tags: t })
1065
+ .input(
1066
+ ws.extend({
1067
+ patternId: z.uuid(),
1068
+ name: z.string().min(1).max(120).optional(),
1069
+ anchorDate: IsoDate.optional(),
1070
+ days: z.array(RosterCycleDay).min(1).max(56).optional(),
1071
+ }),
1072
+ )
1073
+ .output(RosterPattern),
1074
+ archive: baseContract
1075
+ .route({ method: 'DELETE', path: '/rosters/patterns/{patternId}', tags: t })
1076
+ .input(ws.extend({ patternId: z.uuid() }))
1077
+ .output(ok),
1078
+ },
1079
+
1080
+ /** Who is on which rotation, and when. `attendance.schedules` has no equivalent and should. */
1081
+ assignments: baseContract
1082
+ .route({ method: 'GET', path: '/rosters/assignments', tags: t })
1083
+ .input(ws.extend({ personId: z.uuid().optional(), patternId: z.uuid().optional() }))
1084
+ .output(z.array(RosterAssignment)),
1085
+
1086
+ /**
1087
+ * Put a crew on a rotation.
1088
+ *
1089
+ * Takes a list of people because a rotation is a crew's, not a person's — assigning eleven
1090
+ * people one at a time is eleven chances to get the offset wrong. The previous assignment is
1091
+ * closed the day before, so "which rotation was she on in March" stays answerable.
1092
+ */
1093
+ assign: baseContract
1094
+ .route({ method: 'POST', path: '/rosters/assign', tags: t })
1095
+ .input(
1096
+ ws.extend({
1097
+ patternId: z.uuid(),
1098
+ personIds: z.array(z.uuid()).min(1).max(200),
1099
+ effectiveFrom: IsoDate,
1100
+ /** The last day covered, inclusive. Null leaves the assignment open. */
1101
+ effectiveTo: IsoDate.nullish(),
1102
+ /** Which position of the cycle `effectiveFrom` starts at — how two crews run out of phase. */
1103
+ cycleOffset: z.number().int().min(0).max(55).default(0),
1104
+ }),
1105
+ )
1106
+ .output(z.array(RosterAssignment)),
1107
+
1108
+ /** Take a crew off its rotation, from the day after `effectiveTo`. Nothing is deleted. */
1109
+ unassign: baseContract
1110
+ .route({ method: 'POST', path: '/rosters/unassign', tags: t })
1111
+ .input(
1112
+ ws.extend({
1113
+ personIds: z.array(z.uuid()).min(1).max(200),
1114
+ /** The last day the rotation still applies. */
1115
+ effectiveTo: IsoDate,
1116
+ }),
1117
+ )
1118
+ .output(z.object({ closed: z.number().int() })),
1119
+
1120
+ /** One person's roster over a range, rotation and overrides already resolved. */
1121
+ days: baseContract
1122
+ .route({ method: 'GET', path: '/rosters/days', tags: t })
1123
+ .input(ws.extend({ personId: z.uuid().optional(), from: IsoDate, to: IsoDate }))
1124
+ .output(z.array(RosterDay)),
1125
+
1126
+ /**
1127
+ * Change one day, leaving the rotation alone.
1128
+ *
1129
+ * An empty `shiftIds` is "off that day" and is stored — a planned rest day and a day nothing
1130
+ * rosters at all are different facts, and only one of them is somebody's decision.
1131
+ */
1132
+ set: baseContract
1133
+ .route({ method: 'POST', path: '/rosters/days', tags: t })
1134
+ .input(
1135
+ ws.extend({
1136
+ personId: z.uuid(),
1137
+ businessDate: IsoDate,
1138
+ shiftIds: RosterCycleDay,
1139
+ note: z.string().max(500).nullish(),
1140
+ }),
1141
+ )
1142
+ .output(RosterDay),
1143
+
1144
+ /** Drop the exception and let the rotation speak for that day again. */
1145
+ clear: baseContract
1146
+ .route({ method: 'DELETE', path: '/rosters/days', tags: t })
1147
+ .input(ws.extend({ personId: z.uuid(), businessDate: IsoDate }))
1148
+ .output(ok),
1149
+
1150
+ /**
1151
+ * Who is on which shift, per day — the question a roster exists for and the one a set of weekly
1152
+ * schedules cannot be asked without walking everybody's week.
1153
+ */
1154
+ coverage: baseContract
1155
+ .route({ method: 'GET', path: '/rosters/coverage', tags: t })
1156
+ .input(ws.extend({ from: IsoDate, to: IsoDate, officeId: z.uuid().optional() }))
1157
+ .output(z.array(RosterCoverageDay)),
1158
+ },
1159
+
977
1160
  // ---------------------------------------------------------------- leave
978
1161
  leave: {
979
1162
  types: {