@mccaskie-labs/workbench-bookings 0.0.0-stage → 0.2.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 +11 -0
- package/README.md +71 -2
- package/dist/bookings.d.ts +160 -0
- package/dist/bookings.js +446 -0
- package/dist/cli.d.ts +10 -0
- package/dist/cli.js +125 -0
- package/dist/config.d.ts +9 -0
- package/dist/config.js +165 -0
- package/dist/errors.d.ts +25 -0
- package/dist/errors.js +10 -0
- package/dist/format.d.ts +12 -0
- package/dist/format.js +24 -0
- package/dist/ical.d.ts +13 -0
- package/dist/ical.js +67 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +20 -0
- package/dist/migration.d.ts +4 -0
- package/dist/migration.js +441 -0
- package/dist/slots.d.ts +24 -0
- package/dist/slots.js +87 -0
- package/dist/starters.d.ts +31 -0
- package/dist/starters.js +29 -0
- package/dist/styles.d.ts +34 -0
- package/dist/styles.js +97 -0
- package/dist/time.d.ts +15 -0
- package/dist/time.js +82 -0
- package/dist/types.d.ts +130 -0
- package/dist/types.js +4 -0
- package/dist/ui.d.ts +131 -0
- package/dist/ui.js +208 -0
- package/dist/words.d.ts +4 -0
- package/dist/words.js +160 -0
- package/package.json +48 -4
|
@@ -0,0 +1,441 @@
|
|
|
1
|
+
// The app's own bookings tables (docs/capabilities/13-bookings.md, "Data
|
|
2
|
+
// stored"), copied into the project's supabase/migrations as one file and its
|
|
3
|
+
// down file (`npx workbench-bookings migration`).
|
|
4
|
+
//
|
|
5
|
+
// Only the service role reaches them: the app's public booking pages call
|
|
6
|
+
// slots(), book(), cancel() and move() server side (a customer is known by
|
|
7
|
+
// the secret in their manage link, or by the member id the app passes for a
|
|
8
|
+
// signed-in member, never by a sign-in of the package's own), and the owner's pages
|
|
9
|
+
// call list(), approve(), decline() and cancelByOwner() behind the app's own
|
|
10
|
+
// sign-in, checking the account themselves. So the tables have RLS on with no
|
|
11
|
+
// policies and the functions are granted to service_role only.
|
|
12
|
+
//
|
|
13
|
+
// - Two people can't take the last place at the same time: bookings_book and
|
|
14
|
+
// bookings_move take a per-account advisory lock and count what overlaps
|
|
15
|
+
// (with each booking's buffer) before writing.
|
|
16
|
+
// - The customer's manage link carries a random secret; only its SHA-256 is
|
|
17
|
+
// kept (token_hash), so a database copy can't cancel anyone's booking. The
|
|
18
|
+
// owner's calendar feed works the same way (booking_feeds).
|
|
19
|
+
// - Name, email, phone and note are what the customer typed on the booking
|
|
20
|
+
// form, kept in the app's own database for the booking and deleted with it
|
|
21
|
+
// retention_days (180) after it ends. Workbench never holds them.
|
|
22
|
+
// - One scan (bookings_claim) every 15 minutes finds the reminders that are
|
|
23
|
+
// due and the requests the owner didn't answer in time, under `for update
|
|
24
|
+
// skip locked` with a lease, so two overlapping cron runs email once.
|
|
25
|
+
//
|
|
26
|
+
// It creates functions, so Control asks for approval to apply it even on
|
|
27
|
+
// staging. The down file drops everything the up file made.
|
|
28
|
+
export const bookingsMigration = {
|
|
29
|
+
up: `create table public.bookings (
|
|
30
|
+
id uuid primary key default gen_random_uuid(),
|
|
31
|
+
account text not null check (char_length(account) between 1 and 200),
|
|
32
|
+
service text not null check (service ~ '^[a-z][a-z0-9-]{0,39}$'),
|
|
33
|
+
starts_at timestamptz not null,
|
|
34
|
+
ends_at timestamptz not null,
|
|
35
|
+
block_end timestamptz not null,
|
|
36
|
+
status text not null check (status in ('pending', 'confirmed', 'cancelled', 'declined')),
|
|
37
|
+
places smallint not null default 1 check (places between 1 and 100),
|
|
38
|
+
customer_name text not null check (char_length(customer_name) between 1 and 100),
|
|
39
|
+
customer_email text not null check (char_length(customer_email) between 3 and 254),
|
|
40
|
+
customer_phone text check (char_length(customer_phone) <= 40),
|
|
41
|
+
note text check (char_length(note) <= 1000),
|
|
42
|
+
member text check (char_length(member) between 1 and 200),
|
|
43
|
+
token_hash text not null unique check (token_hash ~ '^[0-9a-f]{64}$'),
|
|
44
|
+
sequence integer not null default 0 check (sequence >= 0),
|
|
45
|
+
reason text check (char_length(reason) <= 200),
|
|
46
|
+
reminders_sent smallint not null default 0 check (reminders_sent between 0 and 3),
|
|
47
|
+
next_reminder_at timestamptz,
|
|
48
|
+
hold_until timestamptz,
|
|
49
|
+
claim uuid,
|
|
50
|
+
claimed_until timestamptz,
|
|
51
|
+
created_at timestamptz not null default now(),
|
|
52
|
+
updated_at timestamptz not null default now(),
|
|
53
|
+
check (starts_at < ends_at and ends_at <= block_end),
|
|
54
|
+
check (status = 'pending' or hold_until is null)
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
comment on table public.bookings is 'Bookings customers made on the app''s booking page (workbench-bookings): service, times, status, and the customer''s name, email, optional phone and optional note as they typed them, and the signed-in member''s id when the app passed one. Deleted retention_days (default 180) after the booking ends. Service role only.';
|
|
58
|
+
|
|
59
|
+
create index bookings_account_time on public.bookings (account, starts_at) where status in ('pending', 'confirmed');
|
|
60
|
+
create index bookings_account_all on public.bookings (account, starts_at);
|
|
61
|
+
create index bookings_member on public.bookings (account, member, starts_at) where member is not null;
|
|
62
|
+
create index bookings_customer on public.bookings (account, lower(customer_email)) where status in ('pending', 'confirmed');
|
|
63
|
+
create index bookings_reminders on public.bookings (next_reminder_at) where status = 'confirmed' and next_reminder_at is not null;
|
|
64
|
+
create index bookings_holds on public.bookings (hold_until) where status = 'pending';
|
|
65
|
+
create index bookings_ended on public.bookings (block_end);
|
|
66
|
+
|
|
67
|
+
create table public.booking_feeds (
|
|
68
|
+
account text primary key check (char_length(account) between 1 and 200),
|
|
69
|
+
token_hash text not null unique check (token_hash ~ '^[0-9a-f]{64}$'),
|
|
70
|
+
created_at timestamptz not null default now()
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
comment on table public.booking_feeds is 'The SHA-256 of each account''s calendar feed secret (workbench-bookings). Service role only.';
|
|
74
|
+
|
|
75
|
+
alter table public.bookings enable row level security;
|
|
76
|
+
alter table public.booking_feeds enable row level security;
|
|
77
|
+
|
|
78
|
+
revoke all on public.bookings, public.booking_feeds from anon, authenticated;
|
|
79
|
+
|
|
80
|
+
-- A booking as the package reads it.
|
|
81
|
+
create function public.bookings_row(b public.bookings)
|
|
82
|
+
returns jsonb
|
|
83
|
+
language sql
|
|
84
|
+
immutable
|
|
85
|
+
security invoker
|
|
86
|
+
set search_path = ''
|
|
87
|
+
as $$
|
|
88
|
+
select jsonb_build_object('id', b.id, 'account', b.account, 'service', b.service, 'starts_at', b.starts_at, 'ends_at', b.ends_at,
|
|
89
|
+
'status', b.status, 'places', b.places, 'member', b.member, 'customer', jsonb_build_object('name', b.customer_name, 'email', b.customer_email, 'phone', b.customer_phone, 'note', b.note),
|
|
90
|
+
'sequence', b.sequence, 'reason', b.reason, 'created_at', b.created_at, 'updated_at', b.updated_at);
|
|
91
|
+
$$;
|
|
92
|
+
|
|
93
|
+
-- Whether p_places of p_service from p_start to p_block_end clash with what
|
|
94
|
+
-- already holds time: more than p_capacity places of the same service, or
|
|
95
|
+
-- (p_shared) any other service. p_except leaves out the booking being moved.
|
|
96
|
+
create function public.bookings_clash(p_account text, p_service text, p_start timestamptz, p_block_end timestamptz, p_capacity integer, p_shared boolean, p_places integer, p_except uuid)
|
|
97
|
+
returns boolean
|
|
98
|
+
language sql
|
|
99
|
+
stable
|
|
100
|
+
security invoker
|
|
101
|
+
set search_path = ''
|
|
102
|
+
as $$
|
|
103
|
+
select coalesce(sum(b.places) filter (where b.service = p_service), 0) + p_places > p_capacity or (p_shared and coalesce(bool_or(b.service <> p_service), false))
|
|
104
|
+
from public.bookings b
|
|
105
|
+
where b.account = p_account and b.status in ('pending', 'confirmed') and b.id is distinct from p_except
|
|
106
|
+
and b.starts_at < p_block_end and b.block_end > p_start;
|
|
107
|
+
$$;
|
|
108
|
+
|
|
109
|
+
-- Books a time if it is still free, under the account's lock. Answers the
|
|
110
|
+
-- booking, or { "refused": "taken" | "too_many" }.
|
|
111
|
+
create function public.bookings_book(p_account text, p_service text, p_start timestamptz, p_end timestamptz, p_block_end timestamptz, p_capacity integer, p_shared boolean, p_places integer,
|
|
112
|
+
p_status text, p_name text, p_email text, p_phone text, p_note text, p_member text, p_hash text, p_next_reminder timestamptz, p_hold_until timestamptz, p_max integer, p_now timestamptz)
|
|
113
|
+
returns jsonb
|
|
114
|
+
language plpgsql
|
|
115
|
+
security invoker
|
|
116
|
+
set search_path = ''
|
|
117
|
+
as $$
|
|
118
|
+
declare
|
|
119
|
+
b public.bookings;
|
|
120
|
+
begin
|
|
121
|
+
if p_status not in ('pending', 'confirmed') then
|
|
122
|
+
raise exception using errcode = '22023', message = 'bookings: a new booking is pending or confirmed';
|
|
123
|
+
end if;
|
|
124
|
+
if p_places is null or p_places < 1 or p_places > p_capacity then
|
|
125
|
+
raise exception using errcode = '22023', message = 'bookings: places is 1 to the capacity';
|
|
126
|
+
end if;
|
|
127
|
+
perform pg_advisory_xact_lock(hashtext('workbench-bookings:' || p_account));
|
|
128
|
+
if (select count(*) from public.bookings
|
|
129
|
+
where account = p_account and lower(customer_email) = lower(p_email) and status in ('pending', 'confirmed') and ends_at > p_now) >= p_max then
|
|
130
|
+
return jsonb_build_object('refused', 'too_many');
|
|
131
|
+
end if;
|
|
132
|
+
if public.bookings_clash(p_account, p_service, p_start, p_block_end, p_capacity, p_shared, p_places, null) then
|
|
133
|
+
return jsonb_build_object('refused', 'taken');
|
|
134
|
+
end if;
|
|
135
|
+
insert into public.bookings (account, service, starts_at, ends_at, block_end, status, places, customer_name, customer_email, customer_phone, note, member, token_hash,
|
|
136
|
+
next_reminder_at, hold_until, created_at, updated_at)
|
|
137
|
+
values (p_account, p_service, p_start, p_end, p_block_end, p_status, p_places, p_name, p_email, p_phone, p_note, p_member, p_hash,
|
|
138
|
+
case when p_status = 'confirmed' then p_next_reminder end, case when p_status = 'pending' then p_hold_until end, p_now, p_now)
|
|
139
|
+
returning * into b;
|
|
140
|
+
return public.bookings_row(b);
|
|
141
|
+
end;
|
|
142
|
+
$$;
|
|
143
|
+
|
|
144
|
+
-- Moves a pending or confirmed booking to a new time if it is free, under the
|
|
145
|
+
-- account's lock. Answers the booking, null when it has ended, or
|
|
146
|
+
-- { "refused": "taken" }.
|
|
147
|
+
create function public.bookings_move(p_id uuid, p_start timestamptz, p_end timestamptz, p_block_end timestamptz, p_capacity integer, p_shared boolean, p_next_reminder timestamptz, p_now timestamptz)
|
|
148
|
+
returns jsonb
|
|
149
|
+
language plpgsql
|
|
150
|
+
security invoker
|
|
151
|
+
set search_path = ''
|
|
152
|
+
as $$
|
|
153
|
+
declare
|
|
154
|
+
b public.bookings;
|
|
155
|
+
begin
|
|
156
|
+
select * into b from public.bookings where id = p_id;
|
|
157
|
+
if not found or b.status not in ('pending', 'confirmed') then
|
|
158
|
+
return null;
|
|
159
|
+
end if;
|
|
160
|
+
perform pg_advisory_xact_lock(hashtext('workbench-bookings:' || b.account));
|
|
161
|
+
if public.bookings_clash(b.account, b.service, p_start, p_block_end, p_capacity, p_shared, b.places, p_id) then
|
|
162
|
+
return jsonb_build_object('refused', 'taken');
|
|
163
|
+
end if;
|
|
164
|
+
update public.bookings
|
|
165
|
+
set starts_at = p_start, ends_at = p_end, block_end = p_block_end, sequence = sequence + 1, reminders_sent = 0,
|
|
166
|
+
next_reminder_at = case when status = 'confirmed' then p_next_reminder end, claim = null, claimed_until = null, updated_at = p_now
|
|
167
|
+
where id = p_id and status in ('pending', 'confirmed')
|
|
168
|
+
returning * into b;
|
|
169
|
+
if not found then
|
|
170
|
+
return null;
|
|
171
|
+
end if;
|
|
172
|
+
return public.bookings_row(b);
|
|
173
|
+
end;
|
|
174
|
+
$$;
|
|
175
|
+
|
|
176
|
+
-- Moves a booking from one of p_from to p_status (confirmed, cancelled,
|
|
177
|
+
-- declined) with why. Ending it frees the time and bumps its sequence, so a
|
|
178
|
+
-- calendar app takes it off. Answers the booking, or null when it wasn't in
|
|
179
|
+
-- p_from.
|
|
180
|
+
create function public.bookings_set_status(p_id uuid, p_status text, p_from text[], p_reason text, p_next_reminder timestamptz, p_now timestamptz)
|
|
181
|
+
returns jsonb
|
|
182
|
+
language plpgsql
|
|
183
|
+
security invoker
|
|
184
|
+
set search_path = ''
|
|
185
|
+
as $$
|
|
186
|
+
declare
|
|
187
|
+
b public.bookings;
|
|
188
|
+
begin
|
|
189
|
+
if p_status not in ('confirmed', 'cancelled', 'declined') then
|
|
190
|
+
raise exception using errcode = '22023', message = 'bookings: a status change is to confirmed, cancelled or declined';
|
|
191
|
+
end if;
|
|
192
|
+
update public.bookings
|
|
193
|
+
set status = p_status, reason = case when p_status = 'confirmed' then null else left(p_reason, 200) end,
|
|
194
|
+
sequence = case when p_status = 'confirmed' then sequence else sequence + 1 end,
|
|
195
|
+
next_reminder_at = case when p_status = 'confirmed' then p_next_reminder end, hold_until = null,
|
|
196
|
+
claim = null, claimed_until = null, updated_at = p_now
|
|
197
|
+
where id = p_id and status = any(p_from) and status in ('pending', 'confirmed')
|
|
198
|
+
returning * into b;
|
|
199
|
+
if not found then
|
|
200
|
+
return null;
|
|
201
|
+
end if;
|
|
202
|
+
return public.bookings_row(b);
|
|
203
|
+
end;
|
|
204
|
+
$$;
|
|
205
|
+
|
|
206
|
+
-- What holds time between p_from and p_to, for the slot finder.
|
|
207
|
+
create function public.bookings_busy(p_account text, p_from timestamptz, p_to timestamptz)
|
|
208
|
+
returns jsonb
|
|
209
|
+
language sql
|
|
210
|
+
stable
|
|
211
|
+
security invoker
|
|
212
|
+
set search_path = ''
|
|
213
|
+
as $$
|
|
214
|
+
select coalesce(jsonb_agg(jsonb_build_object('service', b.service, 'starts_at', b.starts_at, 'block_end', b.block_end, 'places', b.places) order by b.starts_at), '[]'::jsonb)
|
|
215
|
+
from public.bookings b
|
|
216
|
+
where b.account = p_account and b.status in ('pending', 'confirmed') and b.starts_at < p_to and b.block_end > p_from;
|
|
217
|
+
$$;
|
|
218
|
+
|
|
219
|
+
create function public.bookings_get(p_id uuid)
|
|
220
|
+
returns jsonb
|
|
221
|
+
language sql
|
|
222
|
+
stable
|
|
223
|
+
security invoker
|
|
224
|
+
set search_path = ''
|
|
225
|
+
as $$
|
|
226
|
+
select public.bookings_row(b) from public.bookings b where b.id = p_id;
|
|
227
|
+
$$;
|
|
228
|
+
|
|
229
|
+
create function public.bookings_by_hash(p_hash text)
|
|
230
|
+
returns jsonb
|
|
231
|
+
language sql
|
|
232
|
+
stable
|
|
233
|
+
security invoker
|
|
234
|
+
set search_path = ''
|
|
235
|
+
as $$
|
|
236
|
+
select public.bookings_row(b) from public.bookings b where b.token_hash = p_hash;
|
|
237
|
+
$$;
|
|
238
|
+
|
|
239
|
+
-- The account's bookings between p_from and p_to, every status, earliest
|
|
240
|
+
-- first. At most 1000.
|
|
241
|
+
create function public.bookings_list(p_account text, p_from timestamptz, p_to timestamptz, p_open_only boolean)
|
|
242
|
+
returns jsonb
|
|
243
|
+
language sql
|
|
244
|
+
stable
|
|
245
|
+
security invoker
|
|
246
|
+
set search_path = ''
|
|
247
|
+
as $$
|
|
248
|
+
select coalesce(jsonb_agg(public.bookings_row(b) order by b.starts_at, b.id), '[]'::jsonb)
|
|
249
|
+
from (select * from public.bookings
|
|
250
|
+
where account = p_account and starts_at < p_to and ends_at > p_from and (not p_open_only or status in ('pending', 'confirmed'))
|
|
251
|
+
order by starts_at, id limit 1000) b;
|
|
252
|
+
$$;
|
|
253
|
+
|
|
254
|
+
-- A signed-in member's bookings that end after p_from, every status, earliest
|
|
255
|
+
-- first. At most 100.
|
|
256
|
+
create function public.bookings_mine(p_account text, p_member text, p_from timestamptz)
|
|
257
|
+
returns jsonb
|
|
258
|
+
language sql
|
|
259
|
+
stable
|
|
260
|
+
security invoker
|
|
261
|
+
set search_path = ''
|
|
262
|
+
as $$
|
|
263
|
+
select coalesce(jsonb_agg(public.bookings_row(b) order by b.starts_at, b.id), '[]'::jsonb)
|
|
264
|
+
from (select * from public.bookings
|
|
265
|
+
where account = p_account and member = p_member and ends_at > p_from
|
|
266
|
+
order by starts_at, id limit 100) b;
|
|
267
|
+
$$;
|
|
268
|
+
|
|
269
|
+
-- The cron's scan: claims up to p_limit bookings with a reminder due or a
|
|
270
|
+
-- request whose hold has run out, with a lease until p_until.
|
|
271
|
+
create function public.bookings_claim(p_claim uuid, p_now timestamptz, p_until timestamptz, p_limit integer)
|
|
272
|
+
returns jsonb
|
|
273
|
+
language plpgsql
|
|
274
|
+
security invoker
|
|
275
|
+
set search_path = ''
|
|
276
|
+
as $$
|
|
277
|
+
declare
|
|
278
|
+
v jsonb;
|
|
279
|
+
begin
|
|
280
|
+
with c as (
|
|
281
|
+
select id from public.bookings
|
|
282
|
+
where ((status = 'confirmed' and next_reminder_at <= p_now) or (status = 'pending' and hold_until <= p_now))
|
|
283
|
+
and (claimed_until is null or claimed_until <= p_now)
|
|
284
|
+
order by coalesce(next_reminder_at, hold_until), id
|
|
285
|
+
limit p_limit
|
|
286
|
+
for update skip locked
|
|
287
|
+
), u as (
|
|
288
|
+
update public.bookings b set claim = p_claim, claimed_until = p_until from c where b.id = c.id returning b.*
|
|
289
|
+
)
|
|
290
|
+
select coalesce(jsonb_agg(public.bookings_row(u) || jsonb_build_object('reminders_sent', u.reminders_sent, 'next_reminder_at', u.next_reminder_at) order by u.starts_at, u.id), '[]'::jsonb)
|
|
291
|
+
into v from u;
|
|
292
|
+
return v;
|
|
293
|
+
end;
|
|
294
|
+
$$;
|
|
295
|
+
|
|
296
|
+
-- The claimed booking's reminder went (or was skipped): how many have gone,
|
|
297
|
+
-- and when the next is. False when the claim was lost.
|
|
298
|
+
create function public.bookings_reminded(p_id uuid, p_claim uuid, p_sent integer, p_next timestamptz, p_now timestamptz)
|
|
299
|
+
returns boolean
|
|
300
|
+
language plpgsql
|
|
301
|
+
security invoker
|
|
302
|
+
set search_path = ''
|
|
303
|
+
as $$
|
|
304
|
+
begin
|
|
305
|
+
update public.bookings
|
|
306
|
+
set reminders_sent = least(p_sent, 3), next_reminder_at = p_next, claim = null, claimed_until = null, updated_at = p_now
|
|
307
|
+
where id = p_id and claim = p_claim and status = 'confirmed';
|
|
308
|
+
return found;
|
|
309
|
+
end;
|
|
310
|
+
$$;
|
|
311
|
+
|
|
312
|
+
-- The claimed request's hold ran out: declined with not_answered. Answers the
|
|
313
|
+
-- booking, or null when the claim was lost or the owner answered meanwhile.
|
|
314
|
+
create function public.bookings_expire(p_id uuid, p_claim uuid, p_now timestamptz)
|
|
315
|
+
returns jsonb
|
|
316
|
+
language plpgsql
|
|
317
|
+
security invoker
|
|
318
|
+
set search_path = ''
|
|
319
|
+
as $$
|
|
320
|
+
declare
|
|
321
|
+
b public.bookings;
|
|
322
|
+
begin
|
|
323
|
+
update public.bookings
|
|
324
|
+
set status = 'declined', reason = 'not_answered', sequence = sequence + 1, hold_until = null, claim = null, claimed_until = null, updated_at = p_now
|
|
325
|
+
where id = p_id and claim = p_claim and status = 'pending'
|
|
326
|
+
returning * into b;
|
|
327
|
+
if not found then
|
|
328
|
+
return null;
|
|
329
|
+
end if;
|
|
330
|
+
return public.bookings_row(b);
|
|
331
|
+
end;
|
|
332
|
+
$$;
|
|
333
|
+
|
|
334
|
+
-- Gives back a claim the tick couldn't finish (an email failed), so the next
|
|
335
|
+
-- tick tries again.
|
|
336
|
+
create function public.bookings_release(p_id uuid, p_claim uuid)
|
|
337
|
+
returns void
|
|
338
|
+
language sql
|
|
339
|
+
security invoker
|
|
340
|
+
set search_path = ''
|
|
341
|
+
as $$
|
|
342
|
+
update public.bookings set claim = null, claimed_until = null where id = p_id and claim = p_claim;
|
|
343
|
+
$$;
|
|
344
|
+
|
|
345
|
+
-- Sets the account's calendar feed secret (its SHA-256), replacing the old
|
|
346
|
+
-- one so an old address stops working.
|
|
347
|
+
create function public.bookings_feed_set(p_account text, p_hash text, p_now timestamptz)
|
|
348
|
+
returns void
|
|
349
|
+
language sql
|
|
350
|
+
security invoker
|
|
351
|
+
set search_path = ''
|
|
352
|
+
as $$
|
|
353
|
+
insert into public.booking_feeds (account, token_hash, created_at) values (p_account, p_hash, p_now)
|
|
354
|
+
on conflict (account) do update set token_hash = excluded.token_hash, created_at = excluded.created_at;
|
|
355
|
+
$$;
|
|
356
|
+
|
|
357
|
+
-- The account whose feed secret this is, or null.
|
|
358
|
+
create function public.bookings_feed_account(p_hash text)
|
|
359
|
+
returns text
|
|
360
|
+
language sql
|
|
361
|
+
stable
|
|
362
|
+
security invoker
|
|
363
|
+
set search_path = ''
|
|
364
|
+
as $$
|
|
365
|
+
select account from public.booking_feeds where token_hash = p_hash;
|
|
366
|
+
$$;
|
|
367
|
+
|
|
368
|
+
-- The daily clean-up: bookings p_days after they ended, customer details and
|
|
369
|
+
-- all.
|
|
370
|
+
create function public.bookings_cleanup(p_now timestamptz, p_days integer)
|
|
371
|
+
returns jsonb
|
|
372
|
+
language plpgsql
|
|
373
|
+
security invoker
|
|
374
|
+
set search_path = ''
|
|
375
|
+
as $$
|
|
376
|
+
declare
|
|
377
|
+
n integer;
|
|
378
|
+
begin
|
|
379
|
+
delete from public.bookings where block_end < p_now - make_interval(days => p_days);
|
|
380
|
+
get diagnostics n = row_count;
|
|
381
|
+
return jsonb_build_object('bookings', n);
|
|
382
|
+
end;
|
|
383
|
+
$$;
|
|
384
|
+
|
|
385
|
+
revoke execute on function public.bookings_row(public.bookings) from public, anon, authenticated;
|
|
386
|
+
revoke execute on function public.bookings_clash(text, text, timestamptz, timestamptz, integer, boolean, integer, uuid) from public, anon, authenticated;
|
|
387
|
+
revoke execute on function public.bookings_book(text, text, timestamptz, timestamptz, timestamptz, integer, boolean, integer, text, text, text, text, text, text, text, timestamptz, timestamptz, integer, timestamptz) from public, anon, authenticated;
|
|
388
|
+
revoke execute on function public.bookings_move(uuid, timestamptz, timestamptz, timestamptz, integer, boolean, timestamptz, timestamptz) from public, anon, authenticated;
|
|
389
|
+
revoke execute on function public.bookings_set_status(uuid, text, text[], text, timestamptz, timestamptz) from public, anon, authenticated;
|
|
390
|
+
revoke execute on function public.bookings_busy(text, timestamptz, timestamptz) from public, anon, authenticated;
|
|
391
|
+
revoke execute on function public.bookings_get(uuid) from public, anon, authenticated;
|
|
392
|
+
revoke execute on function public.bookings_by_hash(text) from public, anon, authenticated;
|
|
393
|
+
revoke execute on function public.bookings_list(text, timestamptz, timestamptz, boolean) from public, anon, authenticated;
|
|
394
|
+
revoke execute on function public.bookings_mine(text, text, timestamptz) from public, anon, authenticated;
|
|
395
|
+
revoke execute on function public.bookings_claim(uuid, timestamptz, timestamptz, integer) from public, anon, authenticated;
|
|
396
|
+
revoke execute on function public.bookings_reminded(uuid, uuid, integer, timestamptz, timestamptz) from public, anon, authenticated;
|
|
397
|
+
revoke execute on function public.bookings_expire(uuid, uuid, timestamptz) from public, anon, authenticated;
|
|
398
|
+
revoke execute on function public.bookings_release(uuid, uuid) from public, anon, authenticated;
|
|
399
|
+
revoke execute on function public.bookings_feed_set(text, text, timestamptz) from public, anon, authenticated;
|
|
400
|
+
revoke execute on function public.bookings_feed_account(text) from public, anon, authenticated;
|
|
401
|
+
revoke execute on function public.bookings_cleanup(timestamptz, integer) from public, anon, authenticated;
|
|
402
|
+
grant execute on function public.bookings_row(public.bookings) to service_role;
|
|
403
|
+
grant execute on function public.bookings_clash(text, text, timestamptz, timestamptz, integer, boolean, integer, uuid) to service_role;
|
|
404
|
+
grant execute on function public.bookings_book(text, text, timestamptz, timestamptz, timestamptz, integer, boolean, integer, text, text, text, text, text, text, text, timestamptz, timestamptz, integer, timestamptz) to service_role;
|
|
405
|
+
grant execute on function public.bookings_move(uuid, timestamptz, timestamptz, timestamptz, integer, boolean, timestamptz, timestamptz) to service_role;
|
|
406
|
+
grant execute on function public.bookings_set_status(uuid, text, text[], text, timestamptz, timestamptz) to service_role;
|
|
407
|
+
grant execute on function public.bookings_busy(text, timestamptz, timestamptz) to service_role;
|
|
408
|
+
grant execute on function public.bookings_get(uuid) to service_role;
|
|
409
|
+
grant execute on function public.bookings_by_hash(text) to service_role;
|
|
410
|
+
grant execute on function public.bookings_list(text, timestamptz, timestamptz, boolean) to service_role;
|
|
411
|
+
grant execute on function public.bookings_mine(text, text, timestamptz) to service_role;
|
|
412
|
+
grant execute on function public.bookings_claim(uuid, timestamptz, timestamptz, integer) to service_role;
|
|
413
|
+
grant execute on function public.bookings_reminded(uuid, uuid, integer, timestamptz, timestamptz) to service_role;
|
|
414
|
+
grant execute on function public.bookings_expire(uuid, uuid, timestamptz) to service_role;
|
|
415
|
+
grant execute on function public.bookings_release(uuid, uuid) to service_role;
|
|
416
|
+
grant execute on function public.bookings_feed_set(text, text, timestamptz) to service_role;
|
|
417
|
+
grant execute on function public.bookings_feed_account(text) to service_role;
|
|
418
|
+
grant execute on function public.bookings_cleanup(timestamptz, integer) to service_role;
|
|
419
|
+
grant select, insert, update, delete on public.bookings, public.booking_feeds to service_role;
|
|
420
|
+
`,
|
|
421
|
+
down: `drop function public.bookings_cleanup(timestamptz, integer);
|
|
422
|
+
drop function public.bookings_feed_account(text);
|
|
423
|
+
drop function public.bookings_feed_set(text, text, timestamptz);
|
|
424
|
+
drop function public.bookings_release(uuid, uuid);
|
|
425
|
+
drop function public.bookings_expire(uuid, uuid, timestamptz);
|
|
426
|
+
drop function public.bookings_reminded(uuid, uuid, integer, timestamptz, timestamptz);
|
|
427
|
+
drop function public.bookings_claim(uuid, timestamptz, timestamptz, integer);
|
|
428
|
+
drop function public.bookings_mine(text, text, timestamptz);
|
|
429
|
+
drop function public.bookings_list(text, timestamptz, timestamptz, boolean);
|
|
430
|
+
drop function public.bookings_by_hash(text);
|
|
431
|
+
drop function public.bookings_get(uuid);
|
|
432
|
+
drop function public.bookings_busy(text, timestamptz, timestamptz);
|
|
433
|
+
drop function public.bookings_set_status(uuid, text, text[], text, timestamptz, timestamptz);
|
|
434
|
+
drop function public.bookings_move(uuid, timestamptz, timestamptz, timestamptz, integer, boolean, timestamptz, timestamptz);
|
|
435
|
+
drop function public.bookings_book(text, text, timestamptz, timestamptz, timestamptz, integer, boolean, integer, text, text, text, text, text, text, text, timestamptz, timestamptz, integer, timestamptz);
|
|
436
|
+
drop function public.bookings_clash(text, text, timestamptz, timestamptz, integer, boolean, integer, uuid);
|
|
437
|
+
drop function public.bookings_row(public.bookings);
|
|
438
|
+
drop table public.booking_feeds;
|
|
439
|
+
drop table public.bookings;
|
|
440
|
+
`,
|
|
441
|
+
};
|
package/dist/slots.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type BookingConfig, type BookingService, type Busy, type SlotDay, type WeekHours } from "./types.ts";
|
|
2
|
+
export interface SlotQuery {
|
|
3
|
+
service: string;
|
|
4
|
+
/** The first day (YYYY-MM-DD, local). Default today. */
|
|
5
|
+
from?: string;
|
|
6
|
+
/** How many days, 1 to 62. Default 7. */
|
|
7
|
+
days?: number;
|
|
8
|
+
/** Bookings holding time (bookings_busy). */
|
|
9
|
+
busy?: readonly Busy[];
|
|
10
|
+
/** The account's own hours, when it keeps them in the app (the settings option). */
|
|
11
|
+
hours?: WeekHours;
|
|
12
|
+
/** More closed days, such as the country's public holidays. */
|
|
13
|
+
closed?: readonly string[];
|
|
14
|
+
now: Date;
|
|
15
|
+
}
|
|
16
|
+
/** The service of a name, or unknown_service. */
|
|
17
|
+
export declare function serviceOf(config: BookingConfig, name: unknown): BookingService;
|
|
18
|
+
/** The free start times, day by day, in the business's time zone. */
|
|
19
|
+
export declare function openSlots(config: BookingConfig, q: SlotQuery): SlotDay[];
|
|
20
|
+
/** Whether `start` is a time the business offers for the service (ignoring who has booked it). */
|
|
21
|
+
export declare function offered(config: BookingConfig, service: string, start: Date, now: Date, extra?: {
|
|
22
|
+
hours?: WeekHours;
|
|
23
|
+
closed?: readonly string[];
|
|
24
|
+
}): boolean;
|
package/dist/slots.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// The free times: a pure function of bookings.json, the clock and the
|
|
2
|
+
// bookings already holding time. No database, no AI, so it is tested
|
|
3
|
+
// exhaustively and the store only has to check, under a lock, that nobody
|
|
4
|
+
// took the time in between (see migration.ts, bookings_book).
|
|
5
|
+
import { hoursOf } from "./config.js";
|
|
6
|
+
import { BookingsError } from "./errors.js";
|
|
7
|
+
import { addDays, durationMinutes, localInstant, todayIn, validDate, weekdayIndex } from "./time.js";
|
|
8
|
+
import { WEEKDAYS } from "./types.js";
|
|
9
|
+
const clock = (mins) => `${String(Math.floor(mins / 60)).padStart(2, "0")}:${String(mins % 60).padStart(2, "0")}`;
|
|
10
|
+
const toMins = (c) => Number(c.slice(0, 2)) * 60 + Number(c.slice(3, 5));
|
|
11
|
+
/** The service of a name, or unknown_service. */
|
|
12
|
+
export function serviceOf(config, name) {
|
|
13
|
+
if (typeof name !== "string" || !Object.hasOwn(config.services, name))
|
|
14
|
+
throw new BookingsError("unknown_service", `${String(name)} isn't a service in workbench/bookings.json`);
|
|
15
|
+
return config.services[name];
|
|
16
|
+
}
|
|
17
|
+
/** Whether a booking of `service` from start to blockEnd still fits next to the busy ones. Returns the places left, 0 when it doesn't. */
|
|
18
|
+
function placesLeft(config, service, start, blockEnd, busy) {
|
|
19
|
+
const cap = config.services[service]?.capacity ?? 1;
|
|
20
|
+
const shared = (config.calendar ?? "shared") === "shared";
|
|
21
|
+
let same = 0;
|
|
22
|
+
for (const b of busy) {
|
|
23
|
+
const s = Date.parse(b.starts_at);
|
|
24
|
+
const e = Date.parse(b.block_end);
|
|
25
|
+
if (!(s < blockEnd && e > start))
|
|
26
|
+
continue;
|
|
27
|
+
if (b.service === service)
|
|
28
|
+
same += b.places ?? 1;
|
|
29
|
+
else if (shared)
|
|
30
|
+
return 0;
|
|
31
|
+
}
|
|
32
|
+
return Math.max(0, cap - same);
|
|
33
|
+
}
|
|
34
|
+
/** The free start times, day by day, in the business's time zone. */
|
|
35
|
+
export function openSlots(config, q) {
|
|
36
|
+
const svc = serviceOf(config, q.service);
|
|
37
|
+
const tz = config.time_zone;
|
|
38
|
+
const today = todayIn(tz, q.now);
|
|
39
|
+
const from = q.from ?? today;
|
|
40
|
+
if (!validDate(from))
|
|
41
|
+
throw new BookingsError("bad_input", "from must be a day, YYYY-MM-DD");
|
|
42
|
+
const days = q.days ?? 7;
|
|
43
|
+
if (!Number.isInteger(days) || days < 1 || days > 62)
|
|
44
|
+
throw new BookingsError("bad_input", "days must be a whole number from 1 to 62");
|
|
45
|
+
const last = addDays(today, config.ahead_days ?? 60);
|
|
46
|
+
const earliest = q.now.getTime() + (durationMinutes(config.notice ?? "2h") ?? 0) * 60_000;
|
|
47
|
+
const closed = new Set([...(config.closed ?? []), ...(q.closed ?? [])]);
|
|
48
|
+
const hours = hoursOf(svc, q.hours ?? config.hours);
|
|
49
|
+
const step = svc.every ?? svc.minutes;
|
|
50
|
+
const busy = q.busy ?? [];
|
|
51
|
+
const out = [];
|
|
52
|
+
for (let i = 0; i < days; i++) {
|
|
53
|
+
const date = addDays(from, i);
|
|
54
|
+
if (date < today || date > last)
|
|
55
|
+
continue;
|
|
56
|
+
const day = { date, slots: [] };
|
|
57
|
+
out.push(day);
|
|
58
|
+
if (closed.has(date))
|
|
59
|
+
continue;
|
|
60
|
+
const seen = new Set();
|
|
61
|
+
for (const [a, b] of hours[WEEKDAYS[weekdayIndex(date)]] ?? []) {
|
|
62
|
+
const open = toMins(a);
|
|
63
|
+
const close = toMins(b);
|
|
64
|
+
for (let t = open; t + svc.minutes <= close; t += step) {
|
|
65
|
+
const start = localInstant(date, clock(t), tz).getTime();
|
|
66
|
+
// A start the clocks skip (daylight saving) lands on another time: offer it once, and only on its own day.
|
|
67
|
+
if (start < earliest || todayIn(tz, new Date(start)) !== date)
|
|
68
|
+
continue;
|
|
69
|
+
const end = start + svc.minutes * 60_000;
|
|
70
|
+
const left = placesLeft(config, q.service, start, end + (svc.buffer ?? 0) * 60_000, busy);
|
|
71
|
+
const iso = new Date(start).toISOString();
|
|
72
|
+
if (left === 0 || seen.has(iso))
|
|
73
|
+
continue;
|
|
74
|
+
seen.add(iso);
|
|
75
|
+
day.slots.push({ start: iso, end: new Date(end).toISOString(), left });
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
day.slots.sort((x, y) => (x.start < y.start ? -1 : 1));
|
|
79
|
+
}
|
|
80
|
+
return out;
|
|
81
|
+
}
|
|
82
|
+
/** Whether `start` is a time the business offers for the service (ignoring who has booked it). */
|
|
83
|
+
export function offered(config, service, start, now, extra = {}) {
|
|
84
|
+
const date = todayIn(config.time_zone, start);
|
|
85
|
+
const iso = start.toISOString();
|
|
86
|
+
return openSlots(config, { service, from: date, days: 1, now, ...extra }).some((d) => d.slots.some((s) => s.start === iso));
|
|
87
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export declare const STARTER_BOOKINGS: {
|
|
2
|
+
readonly format: 1;
|
|
3
|
+
readonly time_zone: "Australia/Sydney";
|
|
4
|
+
readonly hours: {
|
|
5
|
+
readonly mon: readonly [readonly ["09:00", "17:00"]];
|
|
6
|
+
readonly tue: readonly [readonly ["09:00", "17:00"]];
|
|
7
|
+
readonly wed: readonly [readonly ["09:00", "17:00"]];
|
|
8
|
+
readonly thu: readonly [readonly ["09:00", "17:00"]];
|
|
9
|
+
readonly fri: readonly [readonly ["09:00", "17:00"]];
|
|
10
|
+
};
|
|
11
|
+
readonly closed: readonly [];
|
|
12
|
+
readonly services: {
|
|
13
|
+
readonly appointment: {
|
|
14
|
+
readonly title: "Appointment";
|
|
15
|
+
readonly minutes: 30;
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
readonly calendar: "shared";
|
|
19
|
+
readonly notice: "2h";
|
|
20
|
+
readonly ahead_days: 60;
|
|
21
|
+
readonly confirm: "instant";
|
|
22
|
+
readonly reminders: readonly ["1d"];
|
|
23
|
+
readonly cancel_until: "24h";
|
|
24
|
+
readonly ask: {
|
|
25
|
+
readonly phone: "optional";
|
|
26
|
+
readonly note: false;
|
|
27
|
+
};
|
|
28
|
+
readonly owner_email: "each";
|
|
29
|
+
readonly max_per_customer: 3;
|
|
30
|
+
readonly retention_days: 180;
|
|
31
|
+
};
|
package/dist/starters.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// The bookings.json `npx workbench-bookings init` writes: one 30-minute
|
|
2
|
+
// appointment, weekdays 9 to 5 in Sydney, confirmed at once, a reminder the
|
|
3
|
+
// day before, changes online until 24 hours before. The client questions
|
|
4
|
+
// (capabilities/bookings.json, offer) set the rest.
|
|
5
|
+
export const STARTER_BOOKINGS = {
|
|
6
|
+
format: 1,
|
|
7
|
+
time_zone: "Australia/Sydney",
|
|
8
|
+
hours: {
|
|
9
|
+
mon: [["09:00", "17:00"]],
|
|
10
|
+
tue: [["09:00", "17:00"]],
|
|
11
|
+
wed: [["09:00", "17:00"]],
|
|
12
|
+
thu: [["09:00", "17:00"]],
|
|
13
|
+
fri: [["09:00", "17:00"]],
|
|
14
|
+
},
|
|
15
|
+
closed: [],
|
|
16
|
+
services: {
|
|
17
|
+
appointment: { title: "Appointment", minutes: 30 },
|
|
18
|
+
},
|
|
19
|
+
calendar: "shared",
|
|
20
|
+
notice: "2h",
|
|
21
|
+
ahead_days: 60,
|
|
22
|
+
confirm: "instant",
|
|
23
|
+
reminders: ["1d"],
|
|
24
|
+
cancel_until: "24h",
|
|
25
|
+
ask: { phone: "optional", note: false },
|
|
26
|
+
owner_email: "each",
|
|
27
|
+
max_per_customer: 3,
|
|
28
|
+
retention_days: 180,
|
|
29
|
+
};
|
package/dist/styles.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/** One CSS custom property the rules read, with its default. */
|
|
2
|
+
interface StyleVariable {
|
|
3
|
+
/** `--wb-<tool>-<what>`. */
|
|
4
|
+
name: string;
|
|
5
|
+
value: string;
|
|
6
|
+
about: string;
|
|
7
|
+
}
|
|
8
|
+
/** A rule for a class in another state, or for an element inside it. */
|
|
9
|
+
interface StyleState {
|
|
10
|
+
selector: string;
|
|
11
|
+
about: string;
|
|
12
|
+
css: string;
|
|
13
|
+
}
|
|
14
|
+
/** One class the tool puts on an element, with its default rule. */
|
|
15
|
+
interface StyleClass {
|
|
16
|
+
name: string;
|
|
17
|
+
element: string;
|
|
18
|
+
about: string;
|
|
19
|
+
css: string;
|
|
20
|
+
states?: readonly StyleState[];
|
|
21
|
+
}
|
|
22
|
+
/** Every class and variable the tool draws with. */
|
|
23
|
+
export interface ToolStyles {
|
|
24
|
+
format: 1;
|
|
25
|
+
tool: string;
|
|
26
|
+
package: string;
|
|
27
|
+
title: string;
|
|
28
|
+
prefix: string;
|
|
29
|
+
variables: readonly StyleVariable[];
|
|
30
|
+
classes: readonly StyleClass[];
|
|
31
|
+
}
|
|
32
|
+
/** The style hooks of the booking pages and the owner's list. */
|
|
33
|
+
export declare const BOOKING_STYLES: ToolStyles;
|
|
34
|
+
export {};
|