@mccaskie-labs/workbench-roster 0.0.0-stage → 0.1.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.
- package/CHANGELOG.md +5 -0
- package/README.md +55 -2
- package/dist/cli.d.ts +10 -0
- package/dist/cli.js +124 -0
- package/dist/config.d.ts +9 -0
- package/dist/config.js +103 -0
- package/dist/csv.d.ts +9 -0
- package/dist/csv.js +35 -0
- package/dist/errors.d.ts +23 -0
- package/dist/errors.js +10 -0
- package/dist/format.d.ts +19 -0
- package/dist/format.js +31 -0
- package/dist/gaps.d.ts +23 -0
- package/dist/gaps.js +89 -0
- package/dist/ical.d.ts +9 -0
- package/dist/ical.js +57 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +21 -0
- package/dist/migration.d.ts +4 -0
- package/dist/migration.js +642 -0
- package/dist/roster.d.ts +168 -0
- package/dist/roster.js +433 -0
- package/dist/starters.d.ts +28 -0
- package/dist/starters.js +21 -0
- package/dist/styles.d.ts +34 -0
- package/dist/styles.js +99 -0
- package/dist/time.d.ts +19 -0
- package/dist/time.js +92 -0
- package/dist/types.d.ts +156 -0
- package/dist/types.js +3 -0
- package/dist/ui.d.ts +77 -0
- package/dist/ui.js +198 -0
- package/dist/words.d.ts +3 -0
- package/dist/words.js +358 -0
- package/package.json +48 -4
package/dist/index.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// @mccaskie-labs/workbench-roster: a staff roster. Staff, shifts and leave in
|
|
2
|
+
// the app's own database (rosterMigration). The owner builds a week and
|
|
3
|
+
// publishes it; each person is emailed their shifts with a calendar file and
|
|
4
|
+
// has their own page and feed; later changes are emailed to the people they
|
|
5
|
+
// touch. A gaps check compares the week with who must be on. hoursFor() is
|
|
6
|
+
// what the app passes to workbench-bookings' settings option, so nobody is
|
|
7
|
+
// booked outside a shift; exportHours() is a spreadsheet for the owner's own
|
|
8
|
+
// payroll. tick() from the app's cron; no AI and no pay.
|
|
9
|
+
export { createRoster } from "./roster.js";
|
|
10
|
+
export { checkRosterConfig, MAX_CLOSED, MAX_ROLES, MAX_RULES, rosterProblems } from "./config.js";
|
|
11
|
+
export { clashes, coverNeeds, findGaps } from "./gaps.js";
|
|
12
|
+
export { cancelIcs, feedIcs, shiftsIcs } from "./ical.js";
|
|
13
|
+
export { csvCell, hoursCsv, paidHours } from "./csv.js";
|
|
14
|
+
export { rosterMigration } from "./migration.js";
|
|
15
|
+
export { STARTER_ROSTER } from "./starters.js";
|
|
16
|
+
export { renderMyShifts, renderStaff, renderWeek, rosterAction } from "./ui.js";
|
|
17
|
+
export {} from "./format.js";
|
|
18
|
+
export { ROSTER_WORDS, ROSTER_WORD_KEYS } from "./words.js";
|
|
19
|
+
export { ROSTER_STYLES } from "./styles.js";
|
|
20
|
+
export { RosterError } from "./errors.js";
|
|
21
|
+
export { WEEKDAYS } from "./types.js";
|
|
@@ -0,0 +1,642 @@
|
|
|
1
|
+
// The app's own roster tables, copied into the project's supabase/migrations
|
|
2
|
+
// as one file and its down file (`npx workbench-roster migration`).
|
|
3
|
+
//
|
|
4
|
+
// Only the service role reaches them: the app's own server code builds the
|
|
5
|
+
// roster on the owner's pages behind its sign-in (checking the account
|
|
6
|
+
// itself), and a person's own page and feed are found by their secret's
|
|
7
|
+
// SHA-256. So the tables have RLS on with no policies and the functions are
|
|
8
|
+
// granted to service_role only. Every function takes the account, so one
|
|
9
|
+
// business never reaches another's staff.
|
|
10
|
+
//
|
|
11
|
+
// - roster_staff: the people. Name and email are personal information; the
|
|
12
|
+
// roles they can work, their bookings resource name and the app's own user
|
|
13
|
+
// id are not. An email and a resource name are each held by one active
|
|
14
|
+
// person in an account. Removing someone keeps their past shifts for the
|
|
15
|
+
// hours export and deletes their future ones.
|
|
16
|
+
// - roster_shifts: who works when, in which roster week (the package works
|
|
17
|
+
// the week out on the business's clock). Saving one takes the account's
|
|
18
|
+
// advisory lock and refuses a person's second shift at the same time
|
|
19
|
+
// (WBR01) or one while they're on leave (WBR02).
|
|
20
|
+
// - roster_leave: days a person is away. No reason is kept: a reason can be
|
|
21
|
+
// health information.
|
|
22
|
+
// - roster_weeks: draft or published, and when its shifts last changed and
|
|
23
|
+
// when the people the changes touched were last emailed.
|
|
24
|
+
// - roster_sent: for each person and week, a digest of the shifts they were
|
|
25
|
+
// last sent and those shifts' ids and times (no names), so a later send
|
|
26
|
+
// emails only the people whose shifts changed and can cancel what they lost.
|
|
27
|
+
// - roster_cleanup deletes shifts, leave and weeks retention_months after
|
|
28
|
+
// they end, and removed people once nothing of theirs is left.
|
|
29
|
+
//
|
|
30
|
+
// It creates functions, so Control asks for approval to apply it even on
|
|
31
|
+
// staging. The down file drops everything the up file made.
|
|
32
|
+
export const rosterMigration = {
|
|
33
|
+
up: `create table public.roster_staff (
|
|
34
|
+
id uuid primary key default gen_random_uuid(),
|
|
35
|
+
account text not null check (char_length(account) between 1 and 200),
|
|
36
|
+
name text not null check (char_length(name) between 1 and 100),
|
|
37
|
+
email text not null check (char_length(email) between 3 and 254 and email = lower(email)),
|
|
38
|
+
roles text[] not null default '{}' check (cardinality(roles) <= 30),
|
|
39
|
+
resource text check (resource ~ '^[a-z0-9][a-z0-9-]{0,39}$'),
|
|
40
|
+
member text check (char_length(member) between 1 and 200),
|
|
41
|
+
feed_hash text unique check (feed_hash ~ '^[0-9a-f]{64}$'),
|
|
42
|
+
active boolean not null default true,
|
|
43
|
+
removed_at timestamptz,
|
|
44
|
+
created_at timestamptz not null,
|
|
45
|
+
updated_at timestamptz not null,
|
|
46
|
+
check (active = (removed_at is null))
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
comment on table public.roster_staff is 'The people on the roster (workbench-roster): name and email are personal information; roles, bookings resource name and the app''s user id are not. Removed people are deleted once their shifts are past the project''s retention_months. Service role only.';
|
|
50
|
+
|
|
51
|
+
create unique index roster_staff_email on public.roster_staff (account, email) where active;
|
|
52
|
+
create unique index roster_staff_resource on public.roster_staff (account, resource) where active and resource is not null;
|
|
53
|
+
create index roster_staff_account on public.roster_staff (account, active, name);
|
|
54
|
+
|
|
55
|
+
create table public.roster_shifts (
|
|
56
|
+
id uuid primary key default gen_random_uuid(),
|
|
57
|
+
account text not null check (char_length(account) between 1 and 200),
|
|
58
|
+
staff_id uuid not null references public.roster_staff (id) on delete cascade,
|
|
59
|
+
week date not null,
|
|
60
|
+
starts_at timestamptz not null,
|
|
61
|
+
ends_at timestamptz not null,
|
|
62
|
+
role text check (role ~ '^[a-z0-9][a-z0-9-]{0,39}$'),
|
|
63
|
+
break_minutes integer not null default 0 check (break_minutes between 0 and 240),
|
|
64
|
+
note text check (char_length(note) between 1 and 200),
|
|
65
|
+
sequence integer not null default 0,
|
|
66
|
+
created_at timestamptz not null,
|
|
67
|
+
updated_at timestamptz not null,
|
|
68
|
+
check (ends_at - starts_at between interval '15 minutes' and interval '24 hours'),
|
|
69
|
+
check (make_interval(mins => break_minutes) < ends_at - starts_at)
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
comment on table public.roster_shifts is 'Who works when (workbench-roster): a person, a time, a role, an unpaid break and the owner''s note. No pay. Deleted retention_months after it ends. Service role only.';
|
|
73
|
+
|
|
74
|
+
create index roster_shifts_week on public.roster_shifts (account, week, starts_at);
|
|
75
|
+
create index roster_shifts_staff on public.roster_shifts (staff_id, starts_at);
|
|
76
|
+
create index roster_shifts_time on public.roster_shifts (account, starts_at);
|
|
77
|
+
create index roster_shifts_old on public.roster_shifts (ends_at);
|
|
78
|
+
|
|
79
|
+
create table public.roster_leave (
|
|
80
|
+
id uuid primary key default gen_random_uuid(),
|
|
81
|
+
account text not null check (char_length(account) between 1 and 200),
|
|
82
|
+
staff_id uuid not null references public.roster_staff (id) on delete cascade,
|
|
83
|
+
starts_at timestamptz not null,
|
|
84
|
+
ends_at timestamptz not null,
|
|
85
|
+
created_at timestamptz not null,
|
|
86
|
+
check (ends_at > starts_at and ends_at - starts_at <= interval '400 days')
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
comment on table public.roster_leave is 'Days a person is away (workbench-roster). No reason is kept. Deleted retention_months after it ends. Service role only.';
|
|
90
|
+
|
|
91
|
+
create index roster_leave_staff on public.roster_leave (staff_id, starts_at);
|
|
92
|
+
create index roster_leave_time on public.roster_leave (account, starts_at);
|
|
93
|
+
|
|
94
|
+
create table public.roster_weeks (
|
|
95
|
+
account text not null check (char_length(account) between 1 and 200),
|
|
96
|
+
week date not null,
|
|
97
|
+
status text not null default 'draft' check (status in ('draft', 'published')),
|
|
98
|
+
published_at timestamptz,
|
|
99
|
+
changed_at timestamptz,
|
|
100
|
+
sent_at timestamptz,
|
|
101
|
+
primary key (account, week),
|
|
102
|
+
check ((status = 'published') = (published_at is not null))
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
comment on table public.roster_weeks is 'Each roster week: draft or published, its last change and its last send (workbench-roster). No personal information. Service role only.';
|
|
106
|
+
|
|
107
|
+
create index roster_weeks_due on public.roster_weeks (changed_at) where status = 'published';
|
|
108
|
+
|
|
109
|
+
create table public.roster_sent (
|
|
110
|
+
account text not null,
|
|
111
|
+
week date not null,
|
|
112
|
+
staff_id uuid not null references public.roster_staff (id) on delete cascade,
|
|
113
|
+
digest text not null check (digest ~ '^[0-9a-f]{64}$'),
|
|
114
|
+
lines jsonb not null default '[]' check (jsonb_typeof(lines) = 'array' and jsonb_array_length(lines) <= 50),
|
|
115
|
+
sent_at timestamptz not null,
|
|
116
|
+
primary key (account, week, staff_id)
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
comment on table public.roster_sent is 'What each person was last sent for a week (workbench-roster): a digest and their shifts'' ids and times, no names. Deleted with the week. Service role only.';
|
|
120
|
+
|
|
121
|
+
alter table public.roster_staff enable row level security;
|
|
122
|
+
alter table public.roster_shifts enable row level security;
|
|
123
|
+
alter table public.roster_leave enable row level security;
|
|
124
|
+
alter table public.roster_weeks enable row level security;
|
|
125
|
+
alter table public.roster_sent enable row level security;
|
|
126
|
+
|
|
127
|
+
revoke all on public.roster_staff, public.roster_shifts, public.roster_leave, public.roster_weeks, public.roster_sent from anon, authenticated;
|
|
128
|
+
|
|
129
|
+
-- A person as the package reads it. The feed secret's hash never leaves.
|
|
130
|
+
create function public.roster_staff_row(s public.roster_staff)
|
|
131
|
+
returns jsonb
|
|
132
|
+
language sql
|
|
133
|
+
stable
|
|
134
|
+
security invoker
|
|
135
|
+
set search_path = ''
|
|
136
|
+
as $$
|
|
137
|
+
select jsonb_build_object('id', s.id, 'account', s.account, 'name', s.name, 'email', s.email, 'roles', to_jsonb(s.roles), 'resource', s.resource,
|
|
138
|
+
'member', s.member, 'active', s.active, 'has_link', s.feed_hash is not null, 'created_at', s.created_at);
|
|
139
|
+
$$;
|
|
140
|
+
|
|
141
|
+
-- A shift with its person's name and bookings resource.
|
|
142
|
+
create function public.roster_shift_row(s public.roster_shifts)
|
|
143
|
+
returns jsonb
|
|
144
|
+
language sql
|
|
145
|
+
stable
|
|
146
|
+
security invoker
|
|
147
|
+
set search_path = ''
|
|
148
|
+
as $$
|
|
149
|
+
select jsonb_build_object('id', s.id, 'account', s.account, 'staff', s.staff_id, 'staff_name', p.name, 'resource', p.resource, 'week', s.week,
|
|
150
|
+
'starts_at', s.starts_at, 'ends_at', s.ends_at, 'role', s.role, 'break_minutes', s.break_minutes, 'note', s.note, 'sequence', s.sequence)
|
|
151
|
+
from public.roster_staff p where p.id = s.staff_id;
|
|
152
|
+
$$;
|
|
153
|
+
|
|
154
|
+
create function public.roster_leave_row(l public.roster_leave)
|
|
155
|
+
returns jsonb
|
|
156
|
+
language sql
|
|
157
|
+
stable
|
|
158
|
+
security invoker
|
|
159
|
+
set search_path = ''
|
|
160
|
+
as $$
|
|
161
|
+
select jsonb_build_object('id', l.id, 'account', l.account, 'staff', l.staff_id, 'staff_name', p.name, 'starts_at', l.starts_at, 'ends_at', l.ends_at)
|
|
162
|
+
from public.roster_staff p where p.id = l.staff_id;
|
|
163
|
+
$$;
|
|
164
|
+
|
|
165
|
+
create function public.roster_week_row(w public.roster_weeks)
|
|
166
|
+
returns jsonb
|
|
167
|
+
language sql
|
|
168
|
+
stable
|
|
169
|
+
security invoker
|
|
170
|
+
set search_path = ''
|
|
171
|
+
as $$
|
|
172
|
+
select jsonb_build_object('week', w.week, 'status', w.status, 'published_at', w.published_at, 'changed_at', w.changed_at, 'sent_at', w.sent_at,
|
|
173
|
+
'unsent', w.status = 'published' and w.changed_at is not null and (w.sent_at is null or w.changed_at > w.sent_at));
|
|
174
|
+
$$;
|
|
175
|
+
|
|
176
|
+
-- Adds a person (p_id null) or changes one. Null when there's no such
|
|
177
|
+
-- active person. An email or resource another active person holds is
|
|
178
|
+
-- refused (unique_violation).
|
|
179
|
+
create function public.roster_staff_save(p_account text, p_id uuid, p_name text, p_email text, p_roles text[], p_resource text, p_member text, p_now timestamptz)
|
|
180
|
+
returns jsonb
|
|
181
|
+
language plpgsql
|
|
182
|
+
security invoker
|
|
183
|
+
set search_path = ''
|
|
184
|
+
as $$
|
|
185
|
+
declare
|
|
186
|
+
s public.roster_staff;
|
|
187
|
+
begin
|
|
188
|
+
if p_id is null then
|
|
189
|
+
insert into public.roster_staff (account, name, email, roles, resource, member, created_at, updated_at)
|
|
190
|
+
values (p_account, p_name, p_email, coalesce(p_roles, '{}'), p_resource, p_member, p_now, p_now)
|
|
191
|
+
returning * into s;
|
|
192
|
+
else
|
|
193
|
+
update public.roster_staff
|
|
194
|
+
set name = p_name, email = p_email, roles = coalesce(p_roles, '{}'), resource = p_resource, member = p_member, updated_at = p_now
|
|
195
|
+
where account = p_account and id = p_id and active
|
|
196
|
+
returning * into s;
|
|
197
|
+
if not found then
|
|
198
|
+
return null;
|
|
199
|
+
end if;
|
|
200
|
+
end if;
|
|
201
|
+
return public.roster_staff_row(s);
|
|
202
|
+
end;
|
|
203
|
+
$$;
|
|
204
|
+
|
|
205
|
+
-- The account's people, by name; p_all includes those removed.
|
|
206
|
+
create function public.roster_staff_list(p_account text, p_all boolean)
|
|
207
|
+
returns jsonb
|
|
208
|
+
language sql
|
|
209
|
+
stable
|
|
210
|
+
security invoker
|
|
211
|
+
set search_path = ''
|
|
212
|
+
as $$
|
|
213
|
+
select coalesce(jsonb_agg(public.roster_staff_row(s) order by s.active desc, s.name, s.id), '[]'::jsonb)
|
|
214
|
+
from public.roster_staff s where s.account = p_account and (p_all or s.active);
|
|
215
|
+
$$;
|
|
216
|
+
|
|
217
|
+
-- Takes a person off the roster: their link stops, their shifts from now on
|
|
218
|
+
-- are deleted (and those weeks marked changed), their past shifts stay for
|
|
219
|
+
-- the hours export. True when there was one.
|
|
220
|
+
create function public.roster_staff_remove(p_account text, p_id uuid, p_now timestamptz)
|
|
221
|
+
returns boolean
|
|
222
|
+
language plpgsql
|
|
223
|
+
security invoker
|
|
224
|
+
set search_path = ''
|
|
225
|
+
as $$
|
|
226
|
+
begin
|
|
227
|
+
perform pg_advisory_xact_lock(hashtext('workbench-roster:' || p_account));
|
|
228
|
+
update public.roster_staff set active = false, removed_at = p_now, feed_hash = null, updated_at = p_now
|
|
229
|
+
where account = p_account and id = p_id and active;
|
|
230
|
+
if not found then
|
|
231
|
+
return false;
|
|
232
|
+
end if;
|
|
233
|
+
with gone as (
|
|
234
|
+
delete from public.roster_shifts where staff_id = p_id and starts_at >= p_now returning week
|
|
235
|
+
)
|
|
236
|
+
insert into public.roster_weeks (account, week, changed_at)
|
|
237
|
+
select distinct p_account, week, p_now from gone
|
|
238
|
+
on conflict (account, week) do update set changed_at = excluded.changed_at;
|
|
239
|
+
delete from public.roster_leave where staff_id = p_id and ends_at > p_now;
|
|
240
|
+
return true;
|
|
241
|
+
end;
|
|
242
|
+
$$;
|
|
243
|
+
|
|
244
|
+
-- Sets a person's link secret (its SHA-256), replacing the old one. Null
|
|
245
|
+
-- when there's no such active person.
|
|
246
|
+
create function public.roster_feed_set(p_account text, p_id uuid, p_hash text, p_now timestamptz)
|
|
247
|
+
returns jsonb
|
|
248
|
+
language plpgsql
|
|
249
|
+
security invoker
|
|
250
|
+
set search_path = ''
|
|
251
|
+
as $$
|
|
252
|
+
declare
|
|
253
|
+
s public.roster_staff;
|
|
254
|
+
begin
|
|
255
|
+
update public.roster_staff set feed_hash = p_hash, updated_at = p_now where account = p_account and id = p_id and active returning * into s;
|
|
256
|
+
if not found then
|
|
257
|
+
return null;
|
|
258
|
+
end if;
|
|
259
|
+
return public.roster_staff_row(s);
|
|
260
|
+
end;
|
|
261
|
+
$$;
|
|
262
|
+
|
|
263
|
+
-- The active person a link secret belongs to, or null.
|
|
264
|
+
create function public.roster_by_hash(p_hash text)
|
|
265
|
+
returns jsonb
|
|
266
|
+
language sql
|
|
267
|
+
stable
|
|
268
|
+
security invoker
|
|
269
|
+
set search_path = ''
|
|
270
|
+
as $$
|
|
271
|
+
select public.roster_staff_row(s) from public.roster_staff s where s.feed_hash = p_hash and s.active;
|
|
272
|
+
$$;
|
|
273
|
+
|
|
274
|
+
-- Adds a shift (p_id null) or changes one, under the account's lock. Refuses
|
|
275
|
+
-- a person who isn't on the roster (WBR03), a second shift of theirs that
|
|
276
|
+
-- overlaps (WBR01) and a shift while they're on leave (WBR02). Marks the
|
|
277
|
+
-- week, and the week it moved from, changed. Null when there's no such shift.
|
|
278
|
+
create function public.roster_shift_save(p_account text, p_id uuid, p_staff uuid, p_week date, p_start timestamptz, p_end timestamptz, p_role text, p_break integer, p_note text, p_now timestamptz)
|
|
279
|
+
returns jsonb
|
|
280
|
+
language plpgsql
|
|
281
|
+
security invoker
|
|
282
|
+
set search_path = ''
|
|
283
|
+
as $$
|
|
284
|
+
declare
|
|
285
|
+
s public.roster_shifts;
|
|
286
|
+
v_old date;
|
|
287
|
+
begin
|
|
288
|
+
perform pg_advisory_xact_lock(hashtext('workbench-roster:' || p_account));
|
|
289
|
+
if not exists (select 1 from public.roster_staff where account = p_account and id = p_staff and active) then
|
|
290
|
+
raise exception using errcode = 'WBR03', message = 'roster: that person isn''t on the roster';
|
|
291
|
+
end if;
|
|
292
|
+
if exists (select 1 from public.roster_shifts where staff_id = p_staff and (p_id is null or id <> p_id) and starts_at < p_end and ends_at > p_start) then
|
|
293
|
+
raise exception using errcode = 'WBR01', message = 'roster: they already have a shift then';
|
|
294
|
+
end if;
|
|
295
|
+
if exists (select 1 from public.roster_leave where staff_id = p_staff and starts_at < p_end and ends_at > p_start) then
|
|
296
|
+
raise exception using errcode = 'WBR02', message = 'roster: they are on leave then';
|
|
297
|
+
end if;
|
|
298
|
+
if p_id is null then
|
|
299
|
+
insert into public.roster_shifts (account, staff_id, week, starts_at, ends_at, role, break_minutes, note, created_at, updated_at)
|
|
300
|
+
values (p_account, p_staff, p_week, p_start, p_end, p_role, coalesce(p_break, 0), p_note, p_now, p_now)
|
|
301
|
+
returning * into s;
|
|
302
|
+
else
|
|
303
|
+
select week into v_old from public.roster_shifts where account = p_account and id = p_id;
|
|
304
|
+
if not found then
|
|
305
|
+
return null;
|
|
306
|
+
end if;
|
|
307
|
+
update public.roster_shifts
|
|
308
|
+
set staff_id = p_staff, week = p_week, starts_at = p_start, ends_at = p_end, role = p_role, break_minutes = coalesce(p_break, 0), note = p_note,
|
|
309
|
+
sequence = sequence + 1, updated_at = p_now
|
|
310
|
+
where account = p_account and id = p_id
|
|
311
|
+
returning * into s;
|
|
312
|
+
end if;
|
|
313
|
+
insert into public.roster_weeks (account, week, changed_at)
|
|
314
|
+
select p_account, w, p_now from (select p_week as w union select v_old where v_old is not null) x
|
|
315
|
+
on conflict (account, week) do update set changed_at = excluded.changed_at;
|
|
316
|
+
return public.roster_shift_row(s);
|
|
317
|
+
end;
|
|
318
|
+
$$;
|
|
319
|
+
|
|
320
|
+
-- Deletes a shift and marks its week changed. True when there was one.
|
|
321
|
+
create function public.roster_shift_remove(p_account text, p_id uuid, p_now timestamptz)
|
|
322
|
+
returns boolean
|
|
323
|
+
language plpgsql
|
|
324
|
+
security invoker
|
|
325
|
+
set search_path = ''
|
|
326
|
+
as $$
|
|
327
|
+
declare
|
|
328
|
+
v_week date;
|
|
329
|
+
begin
|
|
330
|
+
delete from public.roster_shifts where account = p_account and id = p_id returning week into v_week;
|
|
331
|
+
if not found then
|
|
332
|
+
return false;
|
|
333
|
+
end if;
|
|
334
|
+
insert into public.roster_weeks (account, week, changed_at) values (p_account, v_week, p_now)
|
|
335
|
+
on conflict (account, week) do update set changed_at = excluded.changed_at;
|
|
336
|
+
return true;
|
|
337
|
+
end;
|
|
338
|
+
$$;
|
|
339
|
+
|
|
340
|
+
-- Shifts that start from p_from to before p_to, by start; p_staff keeps one
|
|
341
|
+
-- person's; p_published keeps those in published weeks. At most 5,000.
|
|
342
|
+
create function public.roster_shifts_list(p_account text, p_from timestamptz, p_to timestamptz, p_staff uuid, p_published boolean)
|
|
343
|
+
returns jsonb
|
|
344
|
+
language sql
|
|
345
|
+
stable
|
|
346
|
+
security invoker
|
|
347
|
+
set search_path = ''
|
|
348
|
+
as $$
|
|
349
|
+
select coalesce(jsonb_agg(public.roster_shift_row(s) order by s.starts_at, s.staff_id, s.id), '[]'::jsonb)
|
|
350
|
+
from (select x.* from public.roster_shifts x
|
|
351
|
+
where x.account = p_account and x.starts_at >= p_from and x.starts_at < p_to
|
|
352
|
+
and (p_staff is null or x.staff_id = p_staff)
|
|
353
|
+
and (not p_published or exists (select 1 from public.roster_weeks w where w.account = x.account and w.week = x.week and w.status = 'published'))
|
|
354
|
+
order by x.starts_at, x.staff_id, x.id limit 5000) s;
|
|
355
|
+
$$;
|
|
356
|
+
|
|
357
|
+
-- A week's shifts (by its week, so an overnight shift stays in it).
|
|
358
|
+
create function public.roster_week_shifts(p_account text, p_week date)
|
|
359
|
+
returns jsonb
|
|
360
|
+
language sql
|
|
361
|
+
stable
|
|
362
|
+
security invoker
|
|
363
|
+
set search_path = ''
|
|
364
|
+
as $$
|
|
365
|
+
select coalesce(jsonb_agg(public.roster_shift_row(s) order by s.starts_at, s.staff_id, s.id), '[]'::jsonb)
|
|
366
|
+
from public.roster_shifts s where s.account = p_account and s.week = p_week;
|
|
367
|
+
$$;
|
|
368
|
+
|
|
369
|
+
-- A week's state, or null when nothing has happened in it yet.
|
|
370
|
+
create function public.roster_week_get(p_account text, p_week date)
|
|
371
|
+
returns jsonb
|
|
372
|
+
language sql
|
|
373
|
+
stable
|
|
374
|
+
security invoker
|
|
375
|
+
set search_path = ''
|
|
376
|
+
as $$
|
|
377
|
+
select public.roster_week_row(w) from public.roster_weeks w where w.account = p_account and w.week = p_week;
|
|
378
|
+
$$;
|
|
379
|
+
|
|
380
|
+
-- Publishes a week (staff see it from now). Publishing again keeps the
|
|
381
|
+
-- first time.
|
|
382
|
+
create function public.roster_week_publish(p_account text, p_week date, p_now timestamptz)
|
|
383
|
+
returns jsonb
|
|
384
|
+
language plpgsql
|
|
385
|
+
security invoker
|
|
386
|
+
set search_path = ''
|
|
387
|
+
as $$
|
|
388
|
+
declare
|
|
389
|
+
w public.roster_weeks;
|
|
390
|
+
begin
|
|
391
|
+
insert into public.roster_weeks (account, week, status, published_at) values (p_account, p_week, 'published', p_now)
|
|
392
|
+
on conflict (account, week) do update set status = 'published', published_at = coalesce(public.roster_weeks.published_at, excluded.published_at)
|
|
393
|
+
returning * into w;
|
|
394
|
+
return public.roster_week_row(w);
|
|
395
|
+
end;
|
|
396
|
+
$$;
|
|
397
|
+
|
|
398
|
+
-- The changes up to p_at have been sent. A change after p_at stays unsent.
|
|
399
|
+
create function public.roster_week_sent(p_account text, p_week date, p_at timestamptz)
|
|
400
|
+
returns void
|
|
401
|
+
language sql
|
|
402
|
+
security invoker
|
|
403
|
+
set search_path = ''
|
|
404
|
+
as $$
|
|
405
|
+
update public.roster_weeks set sent_at = greatest(coalesce(sent_at, p_at), p_at) where account = p_account and week = p_week;
|
|
406
|
+
$$;
|
|
407
|
+
|
|
408
|
+
-- What each person was last sent for a week.
|
|
409
|
+
create function public.roster_sent_list(p_account text, p_week date)
|
|
410
|
+
returns jsonb
|
|
411
|
+
language sql
|
|
412
|
+
stable
|
|
413
|
+
security invoker
|
|
414
|
+
set search_path = ''
|
|
415
|
+
as $$
|
|
416
|
+
select coalesce(jsonb_agg(jsonb_build_object('staff', s.staff_id, 'digest', s.digest, 'lines', s.lines) order by s.staff_id), '[]'::jsonb)
|
|
417
|
+
from public.roster_sent s where s.account = p_account and s.week = p_week;
|
|
418
|
+
$$;
|
|
419
|
+
|
|
420
|
+
create function public.roster_sent_set(p_account text, p_week date, p_staff uuid, p_digest text, p_lines jsonb, p_now timestamptz)
|
|
421
|
+
returns void
|
|
422
|
+
language sql
|
|
423
|
+
security invoker
|
|
424
|
+
set search_path = ''
|
|
425
|
+
as $$
|
|
426
|
+
insert into public.roster_sent (account, week, staff_id, digest, lines, sent_at) values (p_account, p_week, p_staff, p_digest, p_lines, p_now)
|
|
427
|
+
on conflict (account, week, staff_id) do update set digest = excluded.digest, lines = excluded.lines, sent_at = excluded.sent_at;
|
|
428
|
+
$$;
|
|
429
|
+
|
|
430
|
+
-- Published weeks with changes not yet sent, last changed at or before
|
|
431
|
+
-- p_before (the owner has stopped editing): for tick().
|
|
432
|
+
create function public.roster_due(p_before timestamptz, p_limit integer)
|
|
433
|
+
returns jsonb
|
|
434
|
+
language sql
|
|
435
|
+
stable
|
|
436
|
+
security invoker
|
|
437
|
+
set search_path = ''
|
|
438
|
+
as $$
|
|
439
|
+
select coalesce(jsonb_agg(jsonb_build_object('account', w.account, 'week', w.week) order by w.changed_at), '[]'::jsonb)
|
|
440
|
+
from (select * from public.roster_weeks
|
|
441
|
+
where status = 'published' and changed_at is not null and (sent_at is null or changed_at > sent_at) and changed_at <= p_before
|
|
442
|
+
order by changed_at limit least(greatest(p_limit, 1), 500)) w;
|
|
443
|
+
$$;
|
|
444
|
+
|
|
445
|
+
-- Adds leave for an active person. Null when there's no such person.
|
|
446
|
+
create function public.roster_leave_add(p_account text, p_staff uuid, p_start timestamptz, p_end timestamptz, p_now timestamptz)
|
|
447
|
+
returns jsonb
|
|
448
|
+
language plpgsql
|
|
449
|
+
security invoker
|
|
450
|
+
set search_path = ''
|
|
451
|
+
as $$
|
|
452
|
+
declare
|
|
453
|
+
l public.roster_leave;
|
|
454
|
+
begin
|
|
455
|
+
if not exists (select 1 from public.roster_staff where account = p_account and id = p_staff and active) then
|
|
456
|
+
return null;
|
|
457
|
+
end if;
|
|
458
|
+
insert into public.roster_leave (account, staff_id, starts_at, ends_at, created_at) values (p_account, p_staff, p_start, p_end, p_now) returning * into l;
|
|
459
|
+
return public.roster_leave_row(l);
|
|
460
|
+
end;
|
|
461
|
+
$$;
|
|
462
|
+
|
|
463
|
+
create function public.roster_leave_remove(p_account text, p_id uuid)
|
|
464
|
+
returns boolean
|
|
465
|
+
language plpgsql
|
|
466
|
+
security invoker
|
|
467
|
+
set search_path = ''
|
|
468
|
+
as $$
|
|
469
|
+
begin
|
|
470
|
+
delete from public.roster_leave where account = p_account and id = p_id;
|
|
471
|
+
return found;
|
|
472
|
+
end;
|
|
473
|
+
$$;
|
|
474
|
+
|
|
475
|
+
-- Leave that overlaps p_from to p_to; p_staff keeps one person's.
|
|
476
|
+
create function public.roster_leave_list(p_account text, p_from timestamptz, p_to timestamptz, p_staff uuid)
|
|
477
|
+
returns jsonb
|
|
478
|
+
language sql
|
|
479
|
+
stable
|
|
480
|
+
security invoker
|
|
481
|
+
set search_path = ''
|
|
482
|
+
as $$
|
|
483
|
+
select coalesce(jsonb_agg(public.roster_leave_row(l) order by l.starts_at, l.id), '[]'::jsonb)
|
|
484
|
+
from (select * from public.roster_leave x where x.account = p_account and x.starts_at < p_to and x.ends_at > p_from and (p_staff is null or x.staff_id = p_staff)
|
|
485
|
+
order by x.starts_at limit 2000) l;
|
|
486
|
+
$$;
|
|
487
|
+
|
|
488
|
+
-- Copies shifts into an empty week (p_rows: [{ staff, start, end, role,
|
|
489
|
+
-- break, note }], times worked out by the package on the business's clock).
|
|
490
|
+
-- Refuses a week that has shifts (WBR04). Skips a person who is no longer on
|
|
491
|
+
-- the roster, is on leave then, or already has a shift then.
|
|
492
|
+
create function public.roster_shifts_copy(p_account text, p_week date, p_rows jsonb, p_now timestamptz)
|
|
493
|
+
returns jsonb
|
|
494
|
+
language plpgsql
|
|
495
|
+
security invoker
|
|
496
|
+
set search_path = ''
|
|
497
|
+
as $$
|
|
498
|
+
declare
|
|
499
|
+
r jsonb;
|
|
500
|
+
v_staff uuid;
|
|
501
|
+
v_start timestamptz;
|
|
502
|
+
v_end timestamptz;
|
|
503
|
+
v_copied integer := 0;
|
|
504
|
+
v_skipped integer := 0;
|
|
505
|
+
begin
|
|
506
|
+
perform pg_advisory_xact_lock(hashtext('workbench-roster:' || p_account));
|
|
507
|
+
if exists (select 1 from public.roster_shifts where account = p_account and week = p_week) then
|
|
508
|
+
raise exception using errcode = 'WBR04', message = 'roster: that week already has shifts';
|
|
509
|
+
end if;
|
|
510
|
+
for r in select * from jsonb_array_elements(p_rows) loop
|
|
511
|
+
v_staff := (r ->> 'staff')::uuid;
|
|
512
|
+
v_start := (r ->> 'start')::timestamptz;
|
|
513
|
+
v_end := (r ->> 'end')::timestamptz;
|
|
514
|
+
if not exists (select 1 from public.roster_staff where account = p_account and id = v_staff and active)
|
|
515
|
+
or exists (select 1 from public.roster_leave where staff_id = v_staff and starts_at < v_end and ends_at > v_start)
|
|
516
|
+
or exists (select 1 from public.roster_shifts where staff_id = v_staff and starts_at < v_end and ends_at > v_start) then
|
|
517
|
+
v_skipped := v_skipped + 1;
|
|
518
|
+
continue;
|
|
519
|
+
end if;
|
|
520
|
+
insert into public.roster_shifts (account, staff_id, week, starts_at, ends_at, role, break_minutes, note, created_at, updated_at)
|
|
521
|
+
values (p_account, v_staff, p_week, v_start, v_end, r ->> 'role', coalesce((r ->> 'break')::integer, 0), r ->> 'note', p_now, p_now);
|
|
522
|
+
v_copied := v_copied + 1;
|
|
523
|
+
end loop;
|
|
524
|
+
if v_copied > 0 then
|
|
525
|
+
insert into public.roster_weeks (account, week, changed_at) values (p_account, p_week, p_now)
|
|
526
|
+
on conflict (account, week) do update set changed_at = excluded.changed_at;
|
|
527
|
+
end if;
|
|
528
|
+
return jsonb_build_object('copied', v_copied, 'skipped', v_skipped);
|
|
529
|
+
end;
|
|
530
|
+
$$;
|
|
531
|
+
|
|
532
|
+
-- The daily clean-up: shifts and leave that ended more than p_months ago,
|
|
533
|
+
-- weeks with nothing left in them, and removed people with no shifts left.
|
|
534
|
+
create function public.roster_cleanup(p_now timestamptz, p_months integer)
|
|
535
|
+
returns jsonb
|
|
536
|
+
language plpgsql
|
|
537
|
+
security invoker
|
|
538
|
+
set search_path = ''
|
|
539
|
+
as $$
|
|
540
|
+
declare
|
|
541
|
+
v_before timestamptz := p_now - make_interval(months => p_months);
|
|
542
|
+
v_shifts integer;
|
|
543
|
+
v_leave integer;
|
|
544
|
+
v_weeks integer;
|
|
545
|
+
v_staff integer;
|
|
546
|
+
begin
|
|
547
|
+
delete from public.roster_shifts where ends_at < v_before;
|
|
548
|
+
get diagnostics v_shifts = row_count;
|
|
549
|
+
delete from public.roster_leave where ends_at < v_before;
|
|
550
|
+
get diagnostics v_leave = row_count;
|
|
551
|
+
delete from public.roster_sent s where s.week < (v_before at time zone 'UTC')::date - 7;
|
|
552
|
+
delete from public.roster_weeks w where w.week < (v_before at time zone 'UTC')::date - 7
|
|
553
|
+
and not exists (select 1 from public.roster_shifts x where x.account = w.account and x.week = w.week);
|
|
554
|
+
get diagnostics v_weeks = row_count;
|
|
555
|
+
delete from public.roster_staff p where not p.active and p.removed_at < v_before
|
|
556
|
+
and not exists (select 1 from public.roster_shifts x where x.staff_id = p.id);
|
|
557
|
+
get diagnostics v_staff = row_count;
|
|
558
|
+
return jsonb_build_object('shifts', v_shifts, 'leave', v_leave, 'weeks', v_weeks, 'staff', v_staff);
|
|
559
|
+
end;
|
|
560
|
+
$$;
|
|
561
|
+
|
|
562
|
+
revoke execute on function public.roster_staff_row(public.roster_staff) from public, anon, authenticated;
|
|
563
|
+
revoke execute on function public.roster_shift_row(public.roster_shifts) from public, anon, authenticated;
|
|
564
|
+
revoke execute on function public.roster_leave_row(public.roster_leave) from public, anon, authenticated;
|
|
565
|
+
revoke execute on function public.roster_week_row(public.roster_weeks) from public, anon, authenticated;
|
|
566
|
+
revoke execute on function public.roster_staff_save(text, uuid, text, text, text[], text, text, timestamptz) from public, anon, authenticated;
|
|
567
|
+
revoke execute on function public.roster_staff_list(text, boolean) from public, anon, authenticated;
|
|
568
|
+
revoke execute on function public.roster_staff_remove(text, uuid, timestamptz) from public, anon, authenticated;
|
|
569
|
+
revoke execute on function public.roster_feed_set(text, uuid, text, timestamptz) from public, anon, authenticated;
|
|
570
|
+
revoke execute on function public.roster_by_hash(text) from public, anon, authenticated;
|
|
571
|
+
revoke execute on function public.roster_shift_save(text, uuid, uuid, date, timestamptz, timestamptz, text, integer, text, timestamptz) from public, anon, authenticated;
|
|
572
|
+
revoke execute on function public.roster_shift_remove(text, uuid, timestamptz) from public, anon, authenticated;
|
|
573
|
+
revoke execute on function public.roster_shifts_list(text, timestamptz, timestamptz, uuid, boolean) from public, anon, authenticated;
|
|
574
|
+
revoke execute on function public.roster_week_shifts(text, date) from public, anon, authenticated;
|
|
575
|
+
revoke execute on function public.roster_week_get(text, date) from public, anon, authenticated;
|
|
576
|
+
revoke execute on function public.roster_week_publish(text, date, timestamptz) from public, anon, authenticated;
|
|
577
|
+
revoke execute on function public.roster_week_sent(text, date, timestamptz) from public, anon, authenticated;
|
|
578
|
+
revoke execute on function public.roster_sent_list(text, date) from public, anon, authenticated;
|
|
579
|
+
revoke execute on function public.roster_sent_set(text, date, uuid, text, jsonb, timestamptz) from public, anon, authenticated;
|
|
580
|
+
revoke execute on function public.roster_due(timestamptz, integer) from public, anon, authenticated;
|
|
581
|
+
revoke execute on function public.roster_leave_add(text, uuid, timestamptz, timestamptz, timestamptz) from public, anon, authenticated;
|
|
582
|
+
revoke execute on function public.roster_leave_remove(text, uuid) from public, anon, authenticated;
|
|
583
|
+
revoke execute on function public.roster_leave_list(text, timestamptz, timestamptz, uuid) from public, anon, authenticated;
|
|
584
|
+
revoke execute on function public.roster_shifts_copy(text, date, jsonb, timestamptz) from public, anon, authenticated;
|
|
585
|
+
revoke execute on function public.roster_cleanup(timestamptz, integer) from public, anon, authenticated;
|
|
586
|
+
grant execute on function public.roster_staff_row(public.roster_staff) to service_role;
|
|
587
|
+
grant execute on function public.roster_shift_row(public.roster_shifts) to service_role;
|
|
588
|
+
grant execute on function public.roster_leave_row(public.roster_leave) to service_role;
|
|
589
|
+
grant execute on function public.roster_week_row(public.roster_weeks) to service_role;
|
|
590
|
+
grant execute on function public.roster_staff_save(text, uuid, text, text, text[], text, text, timestamptz) to service_role;
|
|
591
|
+
grant execute on function public.roster_staff_list(text, boolean) to service_role;
|
|
592
|
+
grant execute on function public.roster_staff_remove(text, uuid, timestamptz) to service_role;
|
|
593
|
+
grant execute on function public.roster_feed_set(text, uuid, text, timestamptz) to service_role;
|
|
594
|
+
grant execute on function public.roster_by_hash(text) to service_role;
|
|
595
|
+
grant execute on function public.roster_shift_save(text, uuid, uuid, date, timestamptz, timestamptz, text, integer, text, timestamptz) to service_role;
|
|
596
|
+
grant execute on function public.roster_shift_remove(text, uuid, timestamptz) to service_role;
|
|
597
|
+
grant execute on function public.roster_shifts_list(text, timestamptz, timestamptz, uuid, boolean) to service_role;
|
|
598
|
+
grant execute on function public.roster_week_shifts(text, date) to service_role;
|
|
599
|
+
grant execute on function public.roster_week_get(text, date) to service_role;
|
|
600
|
+
grant execute on function public.roster_week_publish(text, date, timestamptz) to service_role;
|
|
601
|
+
grant execute on function public.roster_week_sent(text, date, timestamptz) to service_role;
|
|
602
|
+
grant execute on function public.roster_sent_list(text, date) to service_role;
|
|
603
|
+
grant execute on function public.roster_sent_set(text, date, uuid, text, jsonb, timestamptz) to service_role;
|
|
604
|
+
grant execute on function public.roster_due(timestamptz, integer) to service_role;
|
|
605
|
+
grant execute on function public.roster_leave_add(text, uuid, timestamptz, timestamptz, timestamptz) to service_role;
|
|
606
|
+
grant execute on function public.roster_leave_remove(text, uuid) to service_role;
|
|
607
|
+
grant execute on function public.roster_leave_list(text, timestamptz, timestamptz, uuid) to service_role;
|
|
608
|
+
grant execute on function public.roster_shifts_copy(text, date, jsonb, timestamptz) to service_role;
|
|
609
|
+
grant execute on function public.roster_cleanup(timestamptz, integer) to service_role;
|
|
610
|
+
grant select, insert, update, delete on public.roster_staff, public.roster_shifts, public.roster_leave, public.roster_weeks, public.roster_sent to service_role;
|
|
611
|
+
`,
|
|
612
|
+
down: `drop function public.roster_cleanup(timestamptz, integer);
|
|
613
|
+
drop function public.roster_shifts_copy(text, date, jsonb, timestamptz);
|
|
614
|
+
drop function public.roster_leave_list(text, timestamptz, timestamptz, uuid);
|
|
615
|
+
drop function public.roster_leave_remove(text, uuid);
|
|
616
|
+
drop function public.roster_leave_add(text, uuid, timestamptz, timestamptz, timestamptz);
|
|
617
|
+
drop function public.roster_due(timestamptz, integer);
|
|
618
|
+
drop function public.roster_sent_set(text, date, uuid, text, jsonb, timestamptz);
|
|
619
|
+
drop function public.roster_sent_list(text, date);
|
|
620
|
+
drop function public.roster_week_sent(text, date, timestamptz);
|
|
621
|
+
drop function public.roster_week_publish(text, date, timestamptz);
|
|
622
|
+
drop function public.roster_week_get(text, date);
|
|
623
|
+
drop function public.roster_week_shifts(text, date);
|
|
624
|
+
drop function public.roster_shifts_list(text, timestamptz, timestamptz, uuid, boolean);
|
|
625
|
+
drop function public.roster_shift_remove(text, uuid, timestamptz);
|
|
626
|
+
drop function public.roster_shift_save(text, uuid, uuid, date, timestamptz, timestamptz, text, integer, text, timestamptz);
|
|
627
|
+
drop function public.roster_by_hash(text);
|
|
628
|
+
drop function public.roster_feed_set(text, uuid, text, timestamptz);
|
|
629
|
+
drop function public.roster_staff_remove(text, uuid, timestamptz);
|
|
630
|
+
drop function public.roster_staff_list(text, boolean);
|
|
631
|
+
drop function public.roster_staff_save(text, uuid, text, text, text[], text, text, timestamptz);
|
|
632
|
+
drop function public.roster_week_row(public.roster_weeks);
|
|
633
|
+
drop function public.roster_leave_row(public.roster_leave);
|
|
634
|
+
drop function public.roster_shift_row(public.roster_shifts);
|
|
635
|
+
drop function public.roster_staff_row(public.roster_staff);
|
|
636
|
+
drop table public.roster_sent;
|
|
637
|
+
drop table public.roster_weeks;
|
|
638
|
+
drop table public.roster_leave;
|
|
639
|
+
drop table public.roster_shifts;
|
|
640
|
+
drop table public.roster_staff;
|
|
641
|
+
`,
|
|
642
|
+
};
|