@mccaskie-labs/workbench-nudges 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 +65 -2
- package/dist/cli.d.ts +12 -0
- package/dist/cli.js +146 -0
- package/dist/config.d.ts +9 -0
- package/dist/config.js +140 -0
- package/dist/errors.d.ts +17 -0
- package/dist/errors.js +10 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +15 -0
- package/dist/migration.d.ts +4 -0
- package/dist/migration.js +378 -0
- package/dist/nudges.d.ts +172 -0
- package/dist/nudges.js +371 -0
- package/dist/starters.d.ts +80 -0
- package/dist/starters.js +61 -0
- package/dist/styles.d.ts +34 -0
- package/dist/styles.js +70 -0
- package/dist/time.d.ts +18 -0
- package/dist/time.js +100 -0
- package/dist/types.d.ts +136 -0
- package/dist/types.js +3 -0
- package/dist/ui.d.ts +39 -0
- package/dist/ui.js +83 -0
- package/dist/words.d.ts +4 -0
- package/dist/words.js +121 -0
- package/package.json +48 -4
|
@@ -0,0 +1,378 @@
|
|
|
1
|
+
// The app's own nudges tables (docs/capabilities/09-follow-up-nudges.md,
|
|
2
|
+
// "Data stored"), copied into the project's supabase/migrations as one file
|
|
3
|
+
// and its down file (`npx workbench-nudges migration`).
|
|
4
|
+
//
|
|
5
|
+
// Only the service role reaches them: the app's cron calls tick() and the
|
|
6
|
+
// app's own pages call due(), snooze() and dismiss() server side, behind its
|
|
7
|
+
// sign-in, checking the account themselves. So the tables have RLS on with no
|
|
8
|
+
// policies and the functions are granted to service_role only.
|
|
9
|
+
//
|
|
10
|
+
// - A nudge is a row with a due time. One scan (nudges_claim) every 15
|
|
11
|
+
// minutes finds the rows that are due and claims them with a lease, under
|
|
12
|
+
// `for update skip locked`, so two cron runs that overlap never draft the
|
|
13
|
+
// same nudge twice; only the holder of the claim can write the result.
|
|
14
|
+
// - One nudge a rule is active per subject at a time (a unique index):
|
|
15
|
+
// starting it again starts it over.
|
|
16
|
+
// - The subject is the app's own row (enquiry 7f…), never a person's words.
|
|
17
|
+
// `facts` holds what start() was given (a date, a package id); a name, if a
|
|
18
|
+
// guide needs one, comes from the app's refresh when the draft is made.
|
|
19
|
+
// - A draft is kept only while its nudge is due: deleted when the nudge
|
|
20
|
+
// moves on or ends, and after draft_days (30) at most.
|
|
21
|
+
// - nudge_digests is one row a day per account that got a digest, so the
|
|
22
|
+
// digest goes once a day even when ticks overlap.
|
|
23
|
+
//
|
|
24
|
+
// It creates functions, so Control asks for approval to apply it even on
|
|
25
|
+
// staging. The down file drops everything the up file made.
|
|
26
|
+
export const nudgesMigration = {
|
|
27
|
+
up: `create table public.nudges (
|
|
28
|
+
id uuid primary key default gen_random_uuid(),
|
|
29
|
+
rule text not null check (rule ~ '^[a-z][a-z0-9-]{0,39}$'),
|
|
30
|
+
account text not null check (char_length(account) between 1 and 200),
|
|
31
|
+
subject_type text not null check (char_length(subject_type) between 1 and 60),
|
|
32
|
+
subject_id text not null check (char_length(subject_id) between 1 and 200),
|
|
33
|
+
step smallint not null default 1 check (step between 1 and 3),
|
|
34
|
+
due_at timestamptz not null,
|
|
35
|
+
status text not null default 'waiting' check (status in ('waiting', 'due', 'done', 'snoozed', 'dismissed', 'stopped')),
|
|
36
|
+
email text not null default 'daily_digest' check (email in ('daily_digest', 'each', 'none')),
|
|
37
|
+
facts jsonb check (facts is null or (jsonb_typeof(facts) = 'object' and octet_length(facts::text) <= 4000)),
|
|
38
|
+
stop_reason text check (char_length(stop_reason) <= 200),
|
|
39
|
+
reason text check (reason in ('no_reply', 'date_unavailable', 'not_on_plan', 'draft_failed', 'no_ai')),
|
|
40
|
+
made_due_at timestamptz,
|
|
41
|
+
claim uuid,
|
|
42
|
+
claimed_until timestamptz,
|
|
43
|
+
started_at timestamptz not null default now(),
|
|
44
|
+
ended_at timestamptz,
|
|
45
|
+
updated_at timestamptz not null default now(),
|
|
46
|
+
check ((status in ('done', 'dismissed', 'stopped')) = (ended_at is not null)),
|
|
47
|
+
check (status <> 'due' or made_due_at is not null)
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
comment on table public.nudges is 'Follow-up reminders for the business (workbench-nudges): rule, account, subject (an id in the app''s own tables), step, due time and status. Never a customer''s words. Deleted 90 days after they end. Service role only.';
|
|
51
|
+
|
|
52
|
+
create unique index nudges_active on public.nudges (rule, subject_type, subject_id) where status in ('waiting', 'due', 'snoozed');
|
|
53
|
+
create index nudges_scan on public.nudges (due_at) where status in ('waiting', 'due', 'snoozed');
|
|
54
|
+
create index nudges_subject on public.nudges (subject_type, subject_id) where status in ('waiting', 'due', 'snoozed');
|
|
55
|
+
create index nudges_account_due on public.nudges (account, made_due_at) where status = 'due';
|
|
56
|
+
create index nudges_ended on public.nudges (ended_at) where ended_at is not null;
|
|
57
|
+
|
|
58
|
+
create table public.nudge_drafts (
|
|
59
|
+
nudge_id uuid primary key references public.nudges (id) on delete cascade,
|
|
60
|
+
step smallint not null check (step between 1 and 3),
|
|
61
|
+
draft jsonb not null check (jsonb_typeof(draft) = 'object' and octet_length(draft::text) <= 20000),
|
|
62
|
+
request_id text check (char_length(request_id) <= 200),
|
|
63
|
+
created_at timestamptz not null default now()
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
comment on table public.nudge_drafts is 'The latest drafted follow-up of a due nudge (workbench-nudges): text and request id; may hold the customer''s first name. Deleted when the nudge moves on or ends, and after 30 days at most. Service role only.';
|
|
67
|
+
|
|
68
|
+
create index nudge_drafts_created on public.nudge_drafts (created_at);
|
|
69
|
+
|
|
70
|
+
create table public.nudge_digests (
|
|
71
|
+
account text not null check (char_length(account) between 1 and 200),
|
|
72
|
+
day date not null,
|
|
73
|
+
due integer not null check (due >= 0),
|
|
74
|
+
sent_at timestamptz not null default now(),
|
|
75
|
+
primary key (account, day)
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
comment on table public.nudge_digests is 'One row a day per account that was sent the daily digest (workbench-nudges), so it goes once. A count only. Deleted after 60 days. Service role only.';
|
|
79
|
+
|
|
80
|
+
alter table public.nudges enable row level security;
|
|
81
|
+
alter table public.nudge_drafts enable row level security;
|
|
82
|
+
alter table public.nudge_digests enable row level security;
|
|
83
|
+
|
|
84
|
+
revoke all on public.nudges, public.nudge_drafts, public.nudge_digests from anon, authenticated;
|
|
85
|
+
|
|
86
|
+
-- A nudge as the package reads it.
|
|
87
|
+
create function public.nudges_row(n public.nudges)
|
|
88
|
+
returns jsonb
|
|
89
|
+
language sql
|
|
90
|
+
immutable
|
|
91
|
+
security invoker
|
|
92
|
+
set search_path = ''
|
|
93
|
+
as $$
|
|
94
|
+
select jsonb_build_object('id', n.id, 'rule', n.rule, 'account', n.account, 'subject', jsonb_build_object('type', n.subject_type, 'id', n.subject_id),
|
|
95
|
+
'step', n.step, 'due_at', n.due_at, 'status', n.status, 'stop_reason', n.stop_reason, 'reason', n.reason, 'made_due_at', n.made_due_at,
|
|
96
|
+
'started_at', n.started_at, 'ended_at', n.ended_at);
|
|
97
|
+
$$;
|
|
98
|
+
|
|
99
|
+
-- Starts a rule for a subject, or starts it over if it is already active.
|
|
100
|
+
create function public.nudges_start(p_rule text, p_account text, p_type text, p_id text, p_due timestamptz, p_email text, p_facts jsonb, p_now timestamptz)
|
|
101
|
+
returns jsonb
|
|
102
|
+
language plpgsql
|
|
103
|
+
security invoker
|
|
104
|
+
set search_path = ''
|
|
105
|
+
as $$
|
|
106
|
+
declare
|
|
107
|
+
n public.nudges;
|
|
108
|
+
begin
|
|
109
|
+
perform pg_advisory_xact_lock(hashtext('workbench-nudges:' || p_rule || ':' || p_type || ':' || p_id));
|
|
110
|
+
update public.nudges
|
|
111
|
+
set account = p_account, step = 1, due_at = p_due, status = 'waiting', email = p_email, facts = p_facts, stop_reason = null, reason = null,
|
|
112
|
+
made_due_at = null, claim = null, claimed_until = null, started_at = p_now, updated_at = p_now
|
|
113
|
+
where rule = p_rule and subject_type = p_type and subject_id = p_id and status in ('waiting', 'due', 'snoozed')
|
|
114
|
+
returning * into n;
|
|
115
|
+
if found then
|
|
116
|
+
delete from public.nudge_drafts where nudge_id = n.id;
|
|
117
|
+
return public.nudges_row(n);
|
|
118
|
+
end if;
|
|
119
|
+
insert into public.nudges (rule, account, subject_type, subject_id, step, due_at, status, email, facts, started_at, updated_at)
|
|
120
|
+
values (p_rule, p_account, p_type, p_id, 1, p_due, 'waiting', p_email, p_facts, p_now, p_now)
|
|
121
|
+
returning * into n;
|
|
122
|
+
return public.nudges_row(n);
|
|
123
|
+
end;
|
|
124
|
+
$$;
|
|
125
|
+
|
|
126
|
+
-- The subject's active nudges (every rule), for touch().
|
|
127
|
+
create function public.nudges_active_for(p_type text, p_id text)
|
|
128
|
+
returns jsonb
|
|
129
|
+
language sql
|
|
130
|
+
stable
|
|
131
|
+
security invoker
|
|
132
|
+
set search_path = ''
|
|
133
|
+
as $$
|
|
134
|
+
select coalesce(jsonb_agg(public.nudges_row(n) order by n.started_at, n.id), '[]'::jsonb)
|
|
135
|
+
from public.nudges n
|
|
136
|
+
where n.subject_type = p_type and n.subject_id = p_id and n.status in ('waiting', 'due', 'snoozed');
|
|
137
|
+
$$;
|
|
138
|
+
|
|
139
|
+
-- Moves an active nudge to a step and a due time (touch restarting or
|
|
140
|
+
-- advancing it, snooze), if it is still in one of p_from. Its draft goes.
|
|
141
|
+
create function public.nudges_schedule(p_nudge uuid, p_step integer, p_due timestamptz, p_status text, p_from text[], p_now timestamptz)
|
|
142
|
+
returns boolean
|
|
143
|
+
language plpgsql
|
|
144
|
+
security invoker
|
|
145
|
+
set search_path = ''
|
|
146
|
+
as $$
|
|
147
|
+
begin
|
|
148
|
+
if p_status not in ('waiting', 'snoozed') then
|
|
149
|
+
raise exception using errcode = '22023', message = 'nudges: a schedule is waiting or snoozed';
|
|
150
|
+
end if;
|
|
151
|
+
update public.nudges
|
|
152
|
+
set step = p_step, due_at = p_due, status = p_status, reason = null, made_due_at = null, claim = null, claimed_until = null, updated_at = p_now
|
|
153
|
+
where id = p_nudge and status = any(p_from) and status in ('waiting', 'due', 'snoozed');
|
|
154
|
+
if not found then
|
|
155
|
+
return false;
|
|
156
|
+
end if;
|
|
157
|
+
delete from public.nudge_drafts where nudge_id = p_nudge;
|
|
158
|
+
return true;
|
|
159
|
+
end;
|
|
160
|
+
$$;
|
|
161
|
+
|
|
162
|
+
-- Ends an active nudge (done, dismissed or stopped) with why, and deletes its
|
|
163
|
+
-- draft. With p_claim, only the tick that holds the claim can end it.
|
|
164
|
+
create function public.nudges_end(p_nudge uuid, p_status text, p_reason text, p_claim uuid, p_now timestamptz)
|
|
165
|
+
returns boolean
|
|
166
|
+
language plpgsql
|
|
167
|
+
security invoker
|
|
168
|
+
set search_path = ''
|
|
169
|
+
as $$
|
|
170
|
+
begin
|
|
171
|
+
if p_status not in ('done', 'dismissed', 'stopped') then
|
|
172
|
+
raise exception using errcode = '22023', message = 'nudges: an end is done, dismissed or stopped';
|
|
173
|
+
end if;
|
|
174
|
+
update public.nudges
|
|
175
|
+
set status = p_status, stop_reason = left(p_reason, 200), ended_at = p_now, claim = null, claimed_until = null, updated_at = p_now
|
|
176
|
+
where id = p_nudge and status in ('waiting', 'due', 'snoozed') and (p_claim is null or claim = p_claim);
|
|
177
|
+
if not found then
|
|
178
|
+
return false;
|
|
179
|
+
end if;
|
|
180
|
+
delete from public.nudge_drafts where nudge_id = p_nudge;
|
|
181
|
+
return true;
|
|
182
|
+
end;
|
|
183
|
+
$$;
|
|
184
|
+
|
|
185
|
+
-- The cron's scan: claims up to p_limit active nudges whose time has come
|
|
186
|
+
-- (a waiting or snoozed step comes due; a due one moves to its next step or
|
|
187
|
+
-- ends), with a lease until p_until. Rows another tick holds are skipped.
|
|
188
|
+
create function public.nudges_claim(p_claim uuid, p_now timestamptz, p_until timestamptz, p_limit integer)
|
|
189
|
+
returns jsonb
|
|
190
|
+
language plpgsql
|
|
191
|
+
security invoker
|
|
192
|
+
set search_path = ''
|
|
193
|
+
as $$
|
|
194
|
+
declare
|
|
195
|
+
v jsonb;
|
|
196
|
+
begin
|
|
197
|
+
with c as (
|
|
198
|
+
select id from public.nudges
|
|
199
|
+
where status in ('waiting', 'due', 'snoozed') and due_at <= p_now and (claimed_until is null or claimed_until <= p_now)
|
|
200
|
+
order by due_at, id
|
|
201
|
+
limit p_limit
|
|
202
|
+
for update skip locked
|
|
203
|
+
), u as (
|
|
204
|
+
update public.nudges n set claim = p_claim, claimed_until = p_until from c where n.id = c.id returning n.*
|
|
205
|
+
)
|
|
206
|
+
select coalesce(jsonb_agg(public.nudges_row(u) || jsonb_build_object('facts', u.facts, 'email', u.email) order by u.due_at, u.id), '[]'::jsonb) into v from u;
|
|
207
|
+
return v;
|
|
208
|
+
end;
|
|
209
|
+
$$;
|
|
210
|
+
|
|
211
|
+
-- The claimed nudge's step is due now: its draft (or none, with why), and
|
|
212
|
+
-- when the next step (or the end) comes. False when the claim was lost.
|
|
213
|
+
create function public.nudges_made_due(p_nudge uuid, p_claim uuid, p_step integer, p_next timestamptz, p_reason text, p_draft jsonb, p_now timestamptz)
|
|
214
|
+
returns boolean
|
|
215
|
+
language plpgsql
|
|
216
|
+
security invoker
|
|
217
|
+
set search_path = ''
|
|
218
|
+
as $$
|
|
219
|
+
begin
|
|
220
|
+
update public.nudges
|
|
221
|
+
set status = 'due', step = p_step, due_at = p_next, reason = p_reason, made_due_at = p_now, claim = null, claimed_until = null, updated_at = p_now
|
|
222
|
+
where id = p_nudge and claim = p_claim and status in ('waiting', 'due', 'snoozed');
|
|
223
|
+
if not found then
|
|
224
|
+
return false;
|
|
225
|
+
end if;
|
|
226
|
+
if p_draft is null then
|
|
227
|
+
delete from public.nudge_drafts where nudge_id = p_nudge;
|
|
228
|
+
else
|
|
229
|
+
insert into public.nudge_drafts (nudge_id, step, draft, request_id, created_at)
|
|
230
|
+
values (p_nudge, p_step, p_draft, left(p_draft #>> '{workbench,request_id}', 200), p_now)
|
|
231
|
+
on conflict (nudge_id) do update set step = excluded.step, draft = excluded.draft, request_id = excluded.request_id, created_at = excluded.created_at;
|
|
232
|
+
end if;
|
|
233
|
+
return true;
|
|
234
|
+
end;
|
|
235
|
+
$$;
|
|
236
|
+
|
|
237
|
+
-- Gives back a claim the tick couldn't finish (the refresh failed), so the
|
|
238
|
+
-- next tick tries again.
|
|
239
|
+
create function public.nudges_release(p_nudge uuid, p_claim uuid)
|
|
240
|
+
returns void
|
|
241
|
+
language sql
|
|
242
|
+
security invoker
|
|
243
|
+
set search_path = ''
|
|
244
|
+
as $$
|
|
245
|
+
update public.nudges set claim = null, claimed_until = null where id = p_nudge and claim = p_claim;
|
|
246
|
+
$$;
|
|
247
|
+
|
|
248
|
+
create function public.nudges_get(p_nudge uuid)
|
|
249
|
+
returns jsonb
|
|
250
|
+
language sql
|
|
251
|
+
stable
|
|
252
|
+
security invoker
|
|
253
|
+
set search_path = ''
|
|
254
|
+
as $$
|
|
255
|
+
select public.nudges_row(n) from public.nudges n where n.id = p_nudge;
|
|
256
|
+
$$;
|
|
257
|
+
|
|
258
|
+
-- The account's due nudges with their drafts, for its Waiting for you list:
|
|
259
|
+
-- the longest waiting first. At most 500.
|
|
260
|
+
create function public.nudges_due(p_account text)
|
|
261
|
+
returns jsonb
|
|
262
|
+
language sql
|
|
263
|
+
stable
|
|
264
|
+
security invoker
|
|
265
|
+
set search_path = ''
|
|
266
|
+
as $$
|
|
267
|
+
select coalesce(jsonb_agg(public.nudges_row(n) || jsonb_build_object('draft', d.draft) order by n.made_due_at, n.id), '[]'::jsonb)
|
|
268
|
+
from (select * from public.nudges where account = p_account and status = 'due' order by made_due_at, id limit 500) n
|
|
269
|
+
left join public.nudge_drafts d on d.nudge_id = n.id and d.step = n.step;
|
|
270
|
+
$$;
|
|
271
|
+
|
|
272
|
+
-- Accounts with due nudges for the daily digest, and how many each has.
|
|
273
|
+
create function public.nudges_digest_counts()
|
|
274
|
+
returns jsonb
|
|
275
|
+
language sql
|
|
276
|
+
stable
|
|
277
|
+
security invoker
|
|
278
|
+
set search_path = ''
|
|
279
|
+
as $$
|
|
280
|
+
select coalesce(jsonb_agg(jsonb_build_object('account', account, 'due', due) order by account), '[]'::jsonb)
|
|
281
|
+
from (select account, count(*)::integer as due from public.nudges where status = 'due' and email = 'daily_digest' group by account) c;
|
|
282
|
+
$$;
|
|
283
|
+
|
|
284
|
+
-- Claims the account's digest for the day: true once, false after.
|
|
285
|
+
create function public.nudges_digest_claim(p_account text, p_day date, p_due integer, p_now timestamptz)
|
|
286
|
+
returns boolean
|
|
287
|
+
language plpgsql
|
|
288
|
+
security invoker
|
|
289
|
+
set search_path = ''
|
|
290
|
+
as $$
|
|
291
|
+
begin
|
|
292
|
+
insert into public.nudge_digests (account, day, due, sent_at) values (p_account, p_day, p_due, p_now) on conflict do nothing;
|
|
293
|
+
return found;
|
|
294
|
+
end;
|
|
295
|
+
$$;
|
|
296
|
+
|
|
297
|
+
-- The digest didn't go (the email failed): the next tick tries again.
|
|
298
|
+
create function public.nudges_digest_release(p_account text, p_day date)
|
|
299
|
+
returns void
|
|
300
|
+
language sql
|
|
301
|
+
security invoker
|
|
302
|
+
set search_path = ''
|
|
303
|
+
as $$
|
|
304
|
+
delete from public.nudge_digests where account = p_account and day = p_day;
|
|
305
|
+
$$;
|
|
306
|
+
|
|
307
|
+
-- The daily clean-up: ended nudges p_days after they ended, drafts after
|
|
308
|
+
-- p_draft_days, digests after 60 days.
|
|
309
|
+
create function public.nudges_cleanup(p_now timestamptz, p_days integer, p_draft_days integer)
|
|
310
|
+
returns jsonb
|
|
311
|
+
language plpgsql
|
|
312
|
+
security invoker
|
|
313
|
+
set search_path = ''
|
|
314
|
+
as $$
|
|
315
|
+
declare
|
|
316
|
+
n_nudges integer;
|
|
317
|
+
n_drafts integer;
|
|
318
|
+
n_digests integer;
|
|
319
|
+
begin
|
|
320
|
+
delete from public.nudge_drafts where created_at < p_now - make_interval(days => p_draft_days);
|
|
321
|
+
get diagnostics n_drafts = row_count;
|
|
322
|
+
delete from public.nudges where ended_at < p_now - make_interval(days => p_days);
|
|
323
|
+
get diagnostics n_nudges = row_count;
|
|
324
|
+
delete from public.nudge_digests where day < (p_now - interval '60 days')::date;
|
|
325
|
+
get diagnostics n_digests = row_count;
|
|
326
|
+
return jsonb_build_object('nudges', n_nudges, 'drafts', n_drafts, 'digests', n_digests);
|
|
327
|
+
end;
|
|
328
|
+
$$;
|
|
329
|
+
|
|
330
|
+
revoke execute on function public.nudges_row(public.nudges) from public, anon, authenticated;
|
|
331
|
+
revoke execute on function public.nudges_start(text, text, text, text, timestamptz, text, jsonb, timestamptz) from public, anon, authenticated;
|
|
332
|
+
revoke execute on function public.nudges_active_for(text, text) from public, anon, authenticated;
|
|
333
|
+
revoke execute on function public.nudges_schedule(uuid, integer, timestamptz, text, text[], timestamptz) from public, anon, authenticated;
|
|
334
|
+
revoke execute on function public.nudges_end(uuid, text, text, uuid, timestamptz) from public, anon, authenticated;
|
|
335
|
+
revoke execute on function public.nudges_claim(uuid, timestamptz, timestamptz, integer) from public, anon, authenticated;
|
|
336
|
+
revoke execute on function public.nudges_made_due(uuid, uuid, integer, timestamptz, text, jsonb, timestamptz) from public, anon, authenticated;
|
|
337
|
+
revoke execute on function public.nudges_release(uuid, uuid) from public, anon, authenticated;
|
|
338
|
+
revoke execute on function public.nudges_get(uuid) from public, anon, authenticated;
|
|
339
|
+
revoke execute on function public.nudges_due(text) from public, anon, authenticated;
|
|
340
|
+
revoke execute on function public.nudges_digest_counts() from public, anon, authenticated;
|
|
341
|
+
revoke execute on function public.nudges_digest_claim(text, date, integer, timestamptz) from public, anon, authenticated;
|
|
342
|
+
revoke execute on function public.nudges_digest_release(text, date) from public, anon, authenticated;
|
|
343
|
+
revoke execute on function public.nudges_cleanup(timestamptz, integer, integer) from public, anon, authenticated;
|
|
344
|
+
grant execute on function public.nudges_row(public.nudges) to service_role;
|
|
345
|
+
grant execute on function public.nudges_start(text, text, text, text, timestamptz, text, jsonb, timestamptz) to service_role;
|
|
346
|
+
grant execute on function public.nudges_active_for(text, text) to service_role;
|
|
347
|
+
grant execute on function public.nudges_schedule(uuid, integer, timestamptz, text, text[], timestamptz) to service_role;
|
|
348
|
+
grant execute on function public.nudges_end(uuid, text, text, uuid, timestamptz) to service_role;
|
|
349
|
+
grant execute on function public.nudges_claim(uuid, timestamptz, timestamptz, integer) to service_role;
|
|
350
|
+
grant execute on function public.nudges_made_due(uuid, uuid, integer, timestamptz, text, jsonb, timestamptz) to service_role;
|
|
351
|
+
grant execute on function public.nudges_release(uuid, uuid) to service_role;
|
|
352
|
+
grant execute on function public.nudges_get(uuid) to service_role;
|
|
353
|
+
grant execute on function public.nudges_due(text) to service_role;
|
|
354
|
+
grant execute on function public.nudges_digest_counts() to service_role;
|
|
355
|
+
grant execute on function public.nudges_digest_claim(text, date, integer, timestamptz) to service_role;
|
|
356
|
+
grant execute on function public.nudges_digest_release(text, date) to service_role;
|
|
357
|
+
grant execute on function public.nudges_cleanup(timestamptz, integer, integer) to service_role;
|
|
358
|
+
grant select, insert, update, delete on public.nudges, public.nudge_drafts, public.nudge_digests to service_role;
|
|
359
|
+
`,
|
|
360
|
+
down: `drop function public.nudges_cleanup(timestamptz, integer, integer);
|
|
361
|
+
drop function public.nudges_digest_release(text, date);
|
|
362
|
+
drop function public.nudges_digest_claim(text, date, integer, timestamptz);
|
|
363
|
+
drop function public.nudges_digest_counts();
|
|
364
|
+
drop function public.nudges_due(text);
|
|
365
|
+
drop function public.nudges_get(uuid);
|
|
366
|
+
drop function public.nudges_release(uuid, uuid);
|
|
367
|
+
drop function public.nudges_made_due(uuid, uuid, integer, timestamptz, text, jsonb, timestamptz);
|
|
368
|
+
drop function public.nudges_claim(uuid, timestamptz, timestamptz, integer);
|
|
369
|
+
drop function public.nudges_end(uuid, text, text, uuid, timestamptz);
|
|
370
|
+
drop function public.nudges_schedule(uuid, integer, timestamptz, text, text[], timestamptz);
|
|
371
|
+
drop function public.nudges_active_for(text, text);
|
|
372
|
+
drop function public.nudges_start(text, text, text, text, timestamptz, text, jsonb, timestamptz);
|
|
373
|
+
drop function public.nudges_row(public.nudges);
|
|
374
|
+
drop table public.nudge_digests;
|
|
375
|
+
drop table public.nudge_drafts;
|
|
376
|
+
drop table public.nudges;
|
|
377
|
+
`,
|
|
378
|
+
};
|
package/dist/nudges.d.ts
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import type { DueNudge, Lang, Nudge, NudgeConfig, NudgeDraft, Subject, TickResult } from "./types.ts";
|
|
2
|
+
type DbError = {
|
|
3
|
+
message?: string;
|
|
4
|
+
code?: string;
|
|
5
|
+
details?: string;
|
|
6
|
+
} | null;
|
|
7
|
+
/** The part of a Supabase client nudges need. Any supabase-js client fits; use the service client. */
|
|
8
|
+
export interface NudgesClient {
|
|
9
|
+
rpc(fn: string, args: Record<string, unknown>): PromiseLike<{
|
|
10
|
+
data: unknown;
|
|
11
|
+
error: DbError;
|
|
12
|
+
}>;
|
|
13
|
+
}
|
|
14
|
+
/** A link for the draft: a short link to make (capability 3, needs createAi({ links })), or a finished https address. */
|
|
15
|
+
export type NudgeLink = {
|
|
16
|
+
kind: string;
|
|
17
|
+
page: Readonly<Record<string, unknown>>;
|
|
18
|
+
image?: {
|
|
19
|
+
template: string;
|
|
20
|
+
data: unknown;
|
|
21
|
+
} | {
|
|
22
|
+
url: string;
|
|
23
|
+
width?: number;
|
|
24
|
+
height?: number;
|
|
25
|
+
};
|
|
26
|
+
} | {
|
|
27
|
+
url: string;
|
|
28
|
+
};
|
|
29
|
+
/** The part of workbench-ai's createAi(...) nudges use: the reply drafter. */
|
|
30
|
+
export interface NudgesAi {
|
|
31
|
+
reply(req: {
|
|
32
|
+
guide: string;
|
|
33
|
+
facts: Readonly<Record<string, unknown>>;
|
|
34
|
+
language?: Lang | "match";
|
|
35
|
+
note?: string;
|
|
36
|
+
link?: NudgeLink | false;
|
|
37
|
+
subject?: {
|
|
38
|
+
type: string;
|
|
39
|
+
id: string;
|
|
40
|
+
};
|
|
41
|
+
account?: string;
|
|
42
|
+
}): Promise<NudgeDraft>;
|
|
43
|
+
}
|
|
44
|
+
/** The part of workbench-email's createMailer(...) nudges use. */
|
|
45
|
+
export interface NudgesMailer {
|
|
46
|
+
send(mail: {
|
|
47
|
+
to: string | string[];
|
|
48
|
+
subject: string;
|
|
49
|
+
text: string;
|
|
50
|
+
html?: string;
|
|
51
|
+
template: string;
|
|
52
|
+
idempotencyKey?: string;
|
|
53
|
+
}): Promise<unknown>;
|
|
54
|
+
}
|
|
55
|
+
/** The part of workbench-plans' createPlans(...) nudges use. */
|
|
56
|
+
export interface NudgesPlans {
|
|
57
|
+
can(account: string, feature: string): Promise<{
|
|
58
|
+
ok: boolean;
|
|
59
|
+
reason?: string;
|
|
60
|
+
}>;
|
|
61
|
+
use(account: string, meter: string, req?: {
|
|
62
|
+
subject?: string;
|
|
63
|
+
idempotencyKey?: string;
|
|
64
|
+
}): Promise<{
|
|
65
|
+
ok: boolean;
|
|
66
|
+
reason?: string;
|
|
67
|
+
useId?: string;
|
|
68
|
+
}>;
|
|
69
|
+
release(useId: string): Promise<void>;
|
|
70
|
+
}
|
|
71
|
+
/** The nudge a refresh is asked about. */
|
|
72
|
+
export interface NudgeContext {
|
|
73
|
+
id: string;
|
|
74
|
+
rule: string;
|
|
75
|
+
account: string;
|
|
76
|
+
subject: Subject;
|
|
77
|
+
/** The step about to come due. */
|
|
78
|
+
step: number;
|
|
79
|
+
/** What start() was given. */
|
|
80
|
+
facts: Readonly<Record<string, unknown>>;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* The latest facts for a nudge about to come due. `facts` are laid over
|
|
84
|
+
* start()'s (a re-quote: `available`, `alternatives`, a name the guide uses);
|
|
85
|
+
* `stop` ends the nudge with that reason instead of drafting.
|
|
86
|
+
*/
|
|
87
|
+
export interface NudgeRefresh {
|
|
88
|
+
facts?: Readonly<Record<string, unknown>>;
|
|
89
|
+
language?: Lang;
|
|
90
|
+
link?: NudgeLink | false;
|
|
91
|
+
stop?: string;
|
|
92
|
+
}
|
|
93
|
+
/** Someone told about an account's due nudges. */
|
|
94
|
+
export interface NudgeOwner {
|
|
95
|
+
email: string;
|
|
96
|
+
language?: Lang;
|
|
97
|
+
}
|
|
98
|
+
export interface NudgesOptions {
|
|
99
|
+
/** The service client. */
|
|
100
|
+
db: NudgesClient;
|
|
101
|
+
/** createAi(...) with the project's reply guides. Without it nudges still come due, with no draft. */
|
|
102
|
+
ai?: NudgesAi;
|
|
103
|
+
/** createMailer(...). Without it no email goes; the app's list still shows them. */
|
|
104
|
+
mailer?: NudgesMailer;
|
|
105
|
+
/** workbench/nudges.json, or NUDGE_RULES from src/nudges.generated.ts. Checked here. */
|
|
106
|
+
rules: unknown;
|
|
107
|
+
/** createPlans(...): a draft needs the account's plan to have the `nudges` feature (and counts against plans.meter, if set). */
|
|
108
|
+
plans?: NudgesPlans;
|
|
109
|
+
/** The latest facts when a step comes due. Without it, start()'s facts are used as they are. null ends the nudge (subject_gone). */
|
|
110
|
+
refresh?: (n: NudgeContext) => Promise<NudgeRefresh | null>;
|
|
111
|
+
/** Who gets the account's emails. Needed for any email. */
|
|
112
|
+
owners?: (account: string) => Promise<readonly NudgeOwner[]>;
|
|
113
|
+
/** The app's Waiting for you page for an account, linked from the emails. */
|
|
114
|
+
listUrl?: (account: string) => string;
|
|
115
|
+
/** How many nudges one tick handles at most. Default 200. */
|
|
116
|
+
batch?: number;
|
|
117
|
+
/** The clock, for tests. */
|
|
118
|
+
now?: () => Date;
|
|
119
|
+
}
|
|
120
|
+
export interface StartRequest {
|
|
121
|
+
rule: string;
|
|
122
|
+
account: string;
|
|
123
|
+
subject: Subject;
|
|
124
|
+
/** Facts for the drafts that won't change (the date, the package's id). No customer's words; up to 4 KB. */
|
|
125
|
+
facts?: unknown;
|
|
126
|
+
/** Default now. */
|
|
127
|
+
startedAt?: Date;
|
|
128
|
+
}
|
|
129
|
+
/** The touch events every app has; a rule may name its own too (do_not_contact). */
|
|
130
|
+
export type TouchKind = "they_replied" | "we_replied" | "booked" | "closed";
|
|
131
|
+
export interface Nudges {
|
|
132
|
+
/** Starts a rule for a subject (a quote was sent), or starts it over. Refused with not_allowed, writing nothing, when the account's plan doesn't have nudges. */
|
|
133
|
+
start(req: StartRequest): Promise<Nudge>;
|
|
134
|
+
/** Something happened to the subject: an event in a rule's stops_on ends it, one in restarts_on starts it over, and we_replied (the owner sent the follow-up) moves it to its next step. */
|
|
135
|
+
touch(subject: Subject, kind: TouchKind | (string & {})): Promise<void>;
|
|
136
|
+
/** Hides it until `until`; it comes due again then, with a fresh draft. */
|
|
137
|
+
snooze(nudgeId: string, until: Date): Promise<void>;
|
|
138
|
+
/** The owner doesn't want it: ended, its draft deleted. */
|
|
139
|
+
dismiss(nudgeId: string, reason?: string): Promise<void>;
|
|
140
|
+
/** The account's due nudges, the longest waiting first, each with its draft: for the app's Waiting for you list. */
|
|
141
|
+
due(account: string): Promise<DueNudge[]>;
|
|
142
|
+
/** One nudge, or null. */
|
|
143
|
+
get(nudgeId: string): Promise<Nudge | null>;
|
|
144
|
+
/** The cron's scan, every 15 minutes: comes due, drafts, emails. Safe to run twice at once. */
|
|
145
|
+
tick(): Promise<TickResult>;
|
|
146
|
+
/** The daily clean-up: ended nudges after retention_days, drafts after draft_days, digests after 60 days. */
|
|
147
|
+
cleanup(): Promise<{
|
|
148
|
+
nudges: number;
|
|
149
|
+
drafts: number;
|
|
150
|
+
digests: number;
|
|
151
|
+
}>;
|
|
152
|
+
readonly rules: NudgeConfig;
|
|
153
|
+
}
|
|
154
|
+
export declare function createNudges(opts: NudgesOptions): Nudges;
|
|
155
|
+
/** What workbench-social's createOutreach(...) gives nudges: one contact. */
|
|
156
|
+
export interface OutreachLike {
|
|
157
|
+
get(contactId: string): Promise<{
|
|
158
|
+
status: string;
|
|
159
|
+
business: string | null;
|
|
160
|
+
detail: string | null;
|
|
161
|
+
contact_name: string | null;
|
|
162
|
+
} | null>;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* The refresh for outreach contacts (subject type `outreach_contact`, rule
|
|
166
|
+
* supplier-no-reply with workbench-social's outreach-follow-up guide): a
|
|
167
|
+
* contact still waiting on a reply (contacted) gets its business and detail
|
|
168
|
+
* as facts; one that replied, said no or said stop ends the nudge with its
|
|
169
|
+
* status. Compose it with the app's own refresh for other subjects.
|
|
170
|
+
*/
|
|
171
|
+
export declare function outreachRefresh(outreach: OutreachLike): (n: NudgeContext) => Promise<NudgeRefresh | null>;
|
|
172
|
+
export {};
|