@mccaskie-labs/workbench-contacts 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 +76 -2
- package/dist/cli.d.ts +10 -0
- package/dist/cli.js +124 -0
- package/dist/config.d.ts +5 -0
- package/dist/config.js +59 -0
- package/dist/contacts.d.ts +128 -0
- package/dist/contacts.js +269 -0
- package/dist/csv.d.ts +9 -0
- package/dist/csv.js +42 -0
- package/dist/errors.d.ts +15 -0
- package/dist/errors.js +10 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +16 -0
- package/dist/migration.d.ts +4 -0
- package/dist/migration.js +346 -0
- package/dist/starters.d.ts +9 -0
- package/dist/starters.js +11 -0
- package/dist/styles.d.ts +34 -0
- package/dist/styles.js +84 -0
- package/dist/tidy.d.ts +17 -0
- package/dist/tidy.js +68 -0
- package/dist/types.d.ts +79 -0
- package/dist/types.js +3 -0
- package/dist/ui.d.ts +65 -0
- package/dist/ui.js +120 -0
- package/dist/words.d.ts +4 -0
- package/dist/words.js +167 -0
- package/package.json +48 -4
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
// The app's own contacts tables, copied into the project's
|
|
2
|
+
// supabase/migrations as one file and its down file
|
|
3
|
+
// (`npx workbench-contacts migration`).
|
|
4
|
+
//
|
|
5
|
+
// Only the service role reaches them: the app's own server code records
|
|
6
|
+
// people as its tools meet them, and its owner pages search, edit, export and
|
|
7
|
+
// delete behind its sign-in, checking the account themselves. So the tables
|
|
8
|
+
// have RLS on with no policies and the functions are granted to service_role
|
|
9
|
+
// only. Every function takes the account, so one business never reaches
|
|
10
|
+
// another's people.
|
|
11
|
+
//
|
|
12
|
+
// - A contact is one person in one account, found again by email, then
|
|
13
|
+
// phone, then Messenger id (each unique in the account). record() takes
|
|
14
|
+
// an advisory lock on the account, so two records of a new person at once
|
|
15
|
+
// make one contact, not two.
|
|
16
|
+
// - A detail is only ever added to a contact, never overwritten by a later
|
|
17
|
+
// record: what the owner corrected stays corrected. A detail that already
|
|
18
|
+
// belongs to someone else in the account is left off, never merged.
|
|
19
|
+
// - contact_events holds what pointed at the person: the kind of tool and the
|
|
20
|
+
// app's own row id (booking 7f…). Never a message, a review's words, an
|
|
21
|
+
// amount or a card. Deleted with the contact.
|
|
22
|
+
// - Marketing consent is unknown until the person says. Yes needs the words
|
|
23
|
+
// they agreed to (proof of consent); a later no always wins over an earlier
|
|
24
|
+
// yes, and only the person's own later yes undoes a no.
|
|
25
|
+
// - contacts_cleanup deletes a person who hasn't been back in
|
|
26
|
+
// retention_months; forget deletes one on request, with their events.
|
|
27
|
+
//
|
|
28
|
+
// It creates functions, so Control asks for approval to apply it even on
|
|
29
|
+
// staging. The down file drops everything the up file made.
|
|
30
|
+
export const contactsMigration = {
|
|
31
|
+
up: `create table public.contacts (
|
|
32
|
+
id uuid primary key default gen_random_uuid(),
|
|
33
|
+
account text not null check (char_length(account) between 1 and 200),
|
|
34
|
+
name text check (char_length(name) between 1 and 200),
|
|
35
|
+
email text check (char_length(email) between 3 and 254 and email = lower(email)),
|
|
36
|
+
phone text check (phone ~ '^\\+[1-9][0-9]{6,14}$'),
|
|
37
|
+
messenger text check (messenger ~ '^[0-9]{5,30}$'),
|
|
38
|
+
marketing text not null default 'unknown' check (marketing in ('unknown', 'yes', 'no')),
|
|
39
|
+
marketing_at timestamptz,
|
|
40
|
+
marketing_source text check (char_length(marketing_source) between 1 and 40),
|
|
41
|
+
marketing_wording text check (char_length(marketing_wording) between 1 and 500),
|
|
42
|
+
note text check (char_length(note) between 1 and 1000),
|
|
43
|
+
first_seen_at timestamptz not null,
|
|
44
|
+
last_seen_at timestamptz not null,
|
|
45
|
+
created_at timestamptz not null default now(),
|
|
46
|
+
updated_at timestamptz not null default now(),
|
|
47
|
+
check (email is not null or phone is not null or messenger is not null),
|
|
48
|
+
check ((marketing = 'unknown') = (marketing_at is null)),
|
|
49
|
+
check (marketing <> 'yes' or marketing_wording is not null)
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
comment on table public.contacts is 'The business''s customer list (workbench-contacts): name, email, phone, Messenger id, marketing consent with the words agreed to, and the owner''s own note. Personal information: deleted on request (contacts_forget) and when the person hasn''t been back in the project''s retention_months. Service role only.';
|
|
53
|
+
|
|
54
|
+
create unique index contacts_email on public.contacts (account, email) where email is not null;
|
|
55
|
+
create unique index contacts_phone on public.contacts (account, phone) where phone is not null;
|
|
56
|
+
create unique index contacts_messenger on public.contacts (account, messenger) where messenger is not null;
|
|
57
|
+
create index contacts_seen on public.contacts (account, last_seen_at desc, id);
|
|
58
|
+
create index contacts_old on public.contacts (last_seen_at);
|
|
59
|
+
|
|
60
|
+
create table public.contact_events (
|
|
61
|
+
contact_id uuid not null references public.contacts (id) on delete cascade,
|
|
62
|
+
kind text not null check (kind in ('form', 'booking', 'payment', 'quote', 'review', 'message', 'manual')),
|
|
63
|
+
ref_type text not null check (char_length(ref_type) between 1 and 60),
|
|
64
|
+
ref_id text not null check (char_length(ref_id) between 1 and 200),
|
|
65
|
+
at timestamptz not null,
|
|
66
|
+
primary key (contact_id, kind, ref_type, ref_id)
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
comment on table public.contact_events is 'What pointed at a contact (workbench-contacts): the kind of tool and the app''s own row id. Never a message, a review''s words, an amount or a card. Deleted with the contact. Service role only.';
|
|
70
|
+
|
|
71
|
+
create index contact_events_at on public.contact_events (contact_id, at desc);
|
|
72
|
+
|
|
73
|
+
alter table public.contacts enable row level security;
|
|
74
|
+
alter table public.contact_events enable row level security;
|
|
75
|
+
|
|
76
|
+
revoke all on public.contacts, public.contact_events from anon, authenticated;
|
|
77
|
+
|
|
78
|
+
-- A contact as the package reads it, with how many of each kind point at it.
|
|
79
|
+
create function public.contacts_row(c public.contacts)
|
|
80
|
+
returns jsonb
|
|
81
|
+
language sql
|
|
82
|
+
stable
|
|
83
|
+
security invoker
|
|
84
|
+
set search_path = ''
|
|
85
|
+
as $$
|
|
86
|
+
select jsonb_build_object('id', c.id, 'account', c.account, 'name', c.name, 'email', c.email, 'phone', c.phone, 'messenger', c.messenger,
|
|
87
|
+
'marketing', c.marketing, 'marketing_at', c.marketing_at, 'marketing_source', c.marketing_source, 'marketing_wording', c.marketing_wording,
|
|
88
|
+
'note', c.note, 'first_seen_at', c.first_seen_at, 'last_seen_at', c.last_seen_at,
|
|
89
|
+
'counts', coalesce((select jsonb_object_agg(e.kind, e.n) from (select kind, count(*)::integer as n from public.contact_events where contact_id = c.id group by kind) e), '{}'::jsonb));
|
|
90
|
+
$$;
|
|
91
|
+
|
|
92
|
+
-- One tool met a person: find them (email, then phone, then Messenger id),
|
|
93
|
+
-- or add them; add the details they lacked; note the tool's row; and take
|
|
94
|
+
-- their yes or no to marketing if they gave one.
|
|
95
|
+
create function public.contacts_record(p_account text, p_name text, p_email text, p_phone text, p_messenger text, p_kind text, p_ref_type text, p_ref_id text,
|
|
96
|
+
p_at timestamptz, p_marketing text, p_wording text, p_now timestamptz)
|
|
97
|
+
returns jsonb
|
|
98
|
+
language plpgsql
|
|
99
|
+
security invoker
|
|
100
|
+
set search_path = ''
|
|
101
|
+
as $$
|
|
102
|
+
declare
|
|
103
|
+
c public.contacts;
|
|
104
|
+
v_created boolean := false;
|
|
105
|
+
begin
|
|
106
|
+
if p_email is null and p_phone is null and p_messenger is null then
|
|
107
|
+
raise exception using errcode = '22023', message = 'contacts: an email, phone or Messenger id is needed to find a person again';
|
|
108
|
+
end if;
|
|
109
|
+
if p_marketing is not null and p_marketing not in ('yes', 'no') then
|
|
110
|
+
raise exception using errcode = '22023', message = 'contacts: marketing is yes or no';
|
|
111
|
+
end if;
|
|
112
|
+
perform pg_advisory_xact_lock(hashtext('workbench-contacts:' || p_account));
|
|
113
|
+
if p_email is not null then
|
|
114
|
+
select * into c from public.contacts where account = p_account and email = p_email;
|
|
115
|
+
end if;
|
|
116
|
+
if c.id is null and p_phone is not null then
|
|
117
|
+
select * into c from public.contacts where account = p_account and phone = p_phone;
|
|
118
|
+
end if;
|
|
119
|
+
if c.id is null and p_messenger is not null then
|
|
120
|
+
select * into c from public.contacts where account = p_account and messenger = p_messenger;
|
|
121
|
+
end if;
|
|
122
|
+
if c.id is null then
|
|
123
|
+
insert into public.contacts (account, name, email, phone, messenger, first_seen_at, last_seen_at, created_at, updated_at)
|
|
124
|
+
values (p_account, p_name, p_email, p_phone, p_messenger, p_at, p_at, p_now, p_now)
|
|
125
|
+
returning * into c;
|
|
126
|
+
v_created := true;
|
|
127
|
+
else
|
|
128
|
+
update public.contacts o
|
|
129
|
+
set name = coalesce(o.name, p_name),
|
|
130
|
+
email = coalesce(o.email, case when not exists (select 1 from public.contacts x where x.account = p_account and x.email = p_email) then p_email end),
|
|
131
|
+
phone = coalesce(o.phone, case when not exists (select 1 from public.contacts x where x.account = p_account and x.phone = p_phone) then p_phone end),
|
|
132
|
+
messenger = coalesce(o.messenger, case when not exists (select 1 from public.contacts x where x.account = p_account and x.messenger = p_messenger) then p_messenger end),
|
|
133
|
+
first_seen_at = least(o.first_seen_at, p_at),
|
|
134
|
+
last_seen_at = greatest(o.last_seen_at, p_at),
|
|
135
|
+
updated_at = p_now
|
|
136
|
+
where o.id = c.id
|
|
137
|
+
returning * into c;
|
|
138
|
+
end if;
|
|
139
|
+
if p_marketing is not null and (c.marketing_at is null or c.marketing_at <= p_at) then
|
|
140
|
+
update public.contacts
|
|
141
|
+
set marketing = p_marketing, marketing_at = p_at, marketing_source = p_kind,
|
|
142
|
+
marketing_wording = case when p_marketing = 'yes' then p_wording else marketing_wording end, updated_at = p_now
|
|
143
|
+
where id = c.id
|
|
144
|
+
returning * into c;
|
|
145
|
+
end if;
|
|
146
|
+
insert into public.contact_events (contact_id, kind, ref_type, ref_id, at) values (c.id, p_kind, p_ref_type, p_ref_id, p_at) on conflict do nothing;
|
|
147
|
+
return jsonb_build_object('contact', public.contacts_row(c), 'created', v_created);
|
|
148
|
+
end;
|
|
149
|
+
$$;
|
|
150
|
+
|
|
151
|
+
-- One contact with what pointed at them, the latest first (at most 200).
|
|
152
|
+
create function public.contacts_get(p_account text, p_id uuid)
|
|
153
|
+
returns jsonb
|
|
154
|
+
language sql
|
|
155
|
+
stable
|
|
156
|
+
security invoker
|
|
157
|
+
set search_path = ''
|
|
158
|
+
as $$
|
|
159
|
+
select public.contacts_row(c) || jsonb_build_object('events', coalesce((
|
|
160
|
+
select jsonb_agg(jsonb_build_object('kind', e.kind, 'type', e.ref_type, 'id', e.ref_id, 'at', e.at) order by e.at desc, e.kind, e.ref_id)
|
|
161
|
+
from (select * from public.contact_events where contact_id = c.id order by at desc, kind, ref_id limit 200) e), '[]'::jsonb))
|
|
162
|
+
from public.contacts c
|
|
163
|
+
where c.account = p_account and c.id = p_id;
|
|
164
|
+
$$;
|
|
165
|
+
|
|
166
|
+
-- The account's contacts, the latest seen first. p_pattern is an ILIKE
|
|
167
|
+
-- pattern for the name or email (wildcards escaped by the package), p_digits
|
|
168
|
+
-- a run of digits for the phone. p_marketing keeps only yes.
|
|
169
|
+
create function public.contacts_search(p_account text, p_pattern text, p_digits text, p_marketing boolean, p_limit integer, p_offset integer)
|
|
170
|
+
returns jsonb
|
|
171
|
+
language sql
|
|
172
|
+
stable
|
|
173
|
+
security invoker
|
|
174
|
+
set search_path = ''
|
|
175
|
+
as $$
|
|
176
|
+
with m as (
|
|
177
|
+
select * from public.contacts c
|
|
178
|
+
where c.account = p_account
|
|
179
|
+
and (not p_marketing or c.marketing = 'yes')
|
|
180
|
+
and (p_pattern is null
|
|
181
|
+
or c.name ilike p_pattern escape '\\'
|
|
182
|
+
or c.email ilike p_pattern escape '\\'
|
|
183
|
+
or (p_digits is not null and c.phone like '%' || p_digits || '%')
|
|
184
|
+
or (p_digits is not null and c.messenger = p_digits))
|
|
185
|
+
)
|
|
186
|
+
select jsonb_build_object('total', (select count(*) from m),
|
|
187
|
+
'contacts', coalesce((select jsonb_agg(public.contacts_row(p) order by p.last_seen_at desc, p.id)
|
|
188
|
+
from (select * from m order by last_seen_at desc, id limit least(greatest(p_limit, 1), 200) offset greatest(p_offset, 0)) p), '[]'::jsonb));
|
|
189
|
+
$$;
|
|
190
|
+
|
|
191
|
+
-- The owner's edit: each key in p_set (name, email, phone, messenger, note)
|
|
192
|
+
-- replaces that detail; a JSON null clears it. Null when there's no such
|
|
193
|
+
-- contact. A detail another contact holds is refused (unique_violation).
|
|
194
|
+
create function public.contacts_update(p_account text, p_id uuid, p_set jsonb, p_now timestamptz)
|
|
195
|
+
returns jsonb
|
|
196
|
+
language plpgsql
|
|
197
|
+
security invoker
|
|
198
|
+
set search_path = ''
|
|
199
|
+
as $$
|
|
200
|
+
declare
|
|
201
|
+
c public.contacts;
|
|
202
|
+
begin
|
|
203
|
+
update public.contacts
|
|
204
|
+
set name = case when p_set ? 'name' then p_set ->> 'name' else name end,
|
|
205
|
+
email = case when p_set ? 'email' then p_set ->> 'email' else email end,
|
|
206
|
+
phone = case when p_set ? 'phone' then p_set ->> 'phone' else phone end,
|
|
207
|
+
messenger = case when p_set ? 'messenger' then p_set ->> 'messenger' else messenger end,
|
|
208
|
+
note = case when p_set ? 'note' then p_set ->> 'note' else note end,
|
|
209
|
+
updated_at = p_now
|
|
210
|
+
where account = p_account and id = p_id
|
|
211
|
+
returning * into c;
|
|
212
|
+
if not found then
|
|
213
|
+
return null;
|
|
214
|
+
end if;
|
|
215
|
+
return public.contacts_row(c);
|
|
216
|
+
end;
|
|
217
|
+
$$;
|
|
218
|
+
|
|
219
|
+
-- A yes or no to marketing for one contact. A yes needs the words agreed to.
|
|
220
|
+
-- Ignored when a later answer is already kept.
|
|
221
|
+
create function public.contacts_marketing(p_account text, p_id uuid, p_value text, p_source text, p_wording text, p_at timestamptz, p_now timestamptz)
|
|
222
|
+
returns jsonb
|
|
223
|
+
language plpgsql
|
|
224
|
+
security invoker
|
|
225
|
+
set search_path = ''
|
|
226
|
+
as $$
|
|
227
|
+
declare
|
|
228
|
+
c public.contacts;
|
|
229
|
+
begin
|
|
230
|
+
if p_value not in ('yes', 'no') then
|
|
231
|
+
raise exception using errcode = '22023', message = 'contacts: marketing is yes or no';
|
|
232
|
+
end if;
|
|
233
|
+
select * into c from public.contacts where account = p_account and id = p_id for update;
|
|
234
|
+
if not found then
|
|
235
|
+
return null;
|
|
236
|
+
end if;
|
|
237
|
+
if c.marketing_at is null or c.marketing_at <= p_at then
|
|
238
|
+
update public.contacts
|
|
239
|
+
set marketing = p_value, marketing_at = p_at, marketing_source = p_source,
|
|
240
|
+
marketing_wording = case when p_value = 'yes' then p_wording else marketing_wording end, updated_at = p_now
|
|
241
|
+
where id = p_id
|
|
242
|
+
returning * into c;
|
|
243
|
+
end if;
|
|
244
|
+
return public.contacts_row(c);
|
|
245
|
+
end;
|
|
246
|
+
$$;
|
|
247
|
+
|
|
248
|
+
-- An unsubscribe link or a STOP reply: every contact in the account with this
|
|
249
|
+
-- email, phone or Messenger id says no to marketing. How many changed.
|
|
250
|
+
create function public.contacts_opt_out(p_account text, p_email text, p_phone text, p_messenger text, p_source text, p_at timestamptz, p_now timestamptz)
|
|
251
|
+
returns integer
|
|
252
|
+
language plpgsql
|
|
253
|
+
security invoker
|
|
254
|
+
set search_path = ''
|
|
255
|
+
as $$
|
|
256
|
+
declare
|
|
257
|
+
n integer;
|
|
258
|
+
begin
|
|
259
|
+
update public.contacts
|
|
260
|
+
set marketing = 'no', marketing_at = greatest(coalesce(marketing_at, p_at), p_at), marketing_source = p_source, updated_at = p_now
|
|
261
|
+
where account = p_account and marketing <> 'no'
|
|
262
|
+
and ((p_email is not null and email = p_email) or (p_phone is not null and phone = p_phone) or (p_messenger is not null and messenger = p_messenger));
|
|
263
|
+
get diagnostics n = row_count;
|
|
264
|
+
return n;
|
|
265
|
+
end;
|
|
266
|
+
$$;
|
|
267
|
+
|
|
268
|
+
-- Deletes one contact and what pointed at them (a person's request, or the
|
|
269
|
+
-- owner's). True when there was one.
|
|
270
|
+
create function public.contacts_forget(p_account text, p_id uuid)
|
|
271
|
+
returns boolean
|
|
272
|
+
language plpgsql
|
|
273
|
+
security invoker
|
|
274
|
+
set search_path = ''
|
|
275
|
+
as $$
|
|
276
|
+
begin
|
|
277
|
+
delete from public.contacts where account = p_account and id = p_id;
|
|
278
|
+
return found;
|
|
279
|
+
end;
|
|
280
|
+
$$;
|
|
281
|
+
|
|
282
|
+
-- Every contact in the account for the owner's spreadsheet, the latest seen
|
|
283
|
+
-- first; p_marketing keeps only yes. At most 50,000.
|
|
284
|
+
create function public.contacts_export(p_account text, p_marketing boolean)
|
|
285
|
+
returns jsonb
|
|
286
|
+
language sql
|
|
287
|
+
stable
|
|
288
|
+
security invoker
|
|
289
|
+
set search_path = ''
|
|
290
|
+
as $$
|
|
291
|
+
select coalesce(jsonb_agg(public.contacts_row(c) order by c.last_seen_at desc, c.id), '[]'::jsonb)
|
|
292
|
+
from (select * from public.contacts where account = p_account and (not p_marketing or marketing = 'yes') order by last_seen_at desc, id limit 50000) c;
|
|
293
|
+
$$;
|
|
294
|
+
|
|
295
|
+
-- The daily clean-up: people who haven't been back in p_months.
|
|
296
|
+
create function public.contacts_cleanup(p_now timestamptz, p_months integer)
|
|
297
|
+
returns integer
|
|
298
|
+
language plpgsql
|
|
299
|
+
security invoker
|
|
300
|
+
set search_path = ''
|
|
301
|
+
as $$
|
|
302
|
+
declare
|
|
303
|
+
n integer;
|
|
304
|
+
begin
|
|
305
|
+
delete from public.contacts where last_seen_at < p_now - make_interval(months => p_months);
|
|
306
|
+
get diagnostics n = row_count;
|
|
307
|
+
return n;
|
|
308
|
+
end;
|
|
309
|
+
$$;
|
|
310
|
+
|
|
311
|
+
revoke execute on function public.contacts_row(public.contacts) from public, anon, authenticated;
|
|
312
|
+
revoke execute on function public.contacts_record(text, text, text, text, text, text, text, text, timestamptz, text, text, timestamptz) from public, anon, authenticated;
|
|
313
|
+
revoke execute on function public.contacts_get(text, uuid) from public, anon, authenticated;
|
|
314
|
+
revoke execute on function public.contacts_search(text, text, text, boolean, integer, integer) from public, anon, authenticated;
|
|
315
|
+
revoke execute on function public.contacts_update(text, uuid, jsonb, timestamptz) from public, anon, authenticated;
|
|
316
|
+
revoke execute on function public.contacts_marketing(text, uuid, text, text, text, timestamptz, timestamptz) from public, anon, authenticated;
|
|
317
|
+
revoke execute on function public.contacts_opt_out(text, text, text, text, text, timestamptz, timestamptz) from public, anon, authenticated;
|
|
318
|
+
revoke execute on function public.contacts_forget(text, uuid) from public, anon, authenticated;
|
|
319
|
+
revoke execute on function public.contacts_export(text, boolean) from public, anon, authenticated;
|
|
320
|
+
revoke execute on function public.contacts_cleanup(timestamptz, integer) from public, anon, authenticated;
|
|
321
|
+
grant execute on function public.contacts_row(public.contacts) to service_role;
|
|
322
|
+
grant execute on function public.contacts_record(text, text, text, text, text, text, text, text, timestamptz, text, text, timestamptz) to service_role;
|
|
323
|
+
grant execute on function public.contacts_get(text, uuid) to service_role;
|
|
324
|
+
grant execute on function public.contacts_search(text, text, text, boolean, integer, integer) to service_role;
|
|
325
|
+
grant execute on function public.contacts_update(text, uuid, jsonb, timestamptz) to service_role;
|
|
326
|
+
grant execute on function public.contacts_marketing(text, uuid, text, text, text, timestamptz, timestamptz) to service_role;
|
|
327
|
+
grant execute on function public.contacts_opt_out(text, text, text, text, text, timestamptz, timestamptz) to service_role;
|
|
328
|
+
grant execute on function public.contacts_forget(text, uuid) to service_role;
|
|
329
|
+
grant execute on function public.contacts_export(text, boolean) to service_role;
|
|
330
|
+
grant execute on function public.contacts_cleanup(timestamptz, integer) to service_role;
|
|
331
|
+
grant select, insert, update, delete on public.contacts, public.contact_events to service_role;
|
|
332
|
+
`,
|
|
333
|
+
down: `drop function public.contacts_cleanup(timestamptz, integer);
|
|
334
|
+
drop function public.contacts_export(text, boolean);
|
|
335
|
+
drop function public.contacts_forget(text, uuid);
|
|
336
|
+
drop function public.contacts_opt_out(text, text, text, text, text, timestamptz, timestamptz);
|
|
337
|
+
drop function public.contacts_marketing(text, uuid, text, text, text, timestamptz, timestamptz);
|
|
338
|
+
drop function public.contacts_update(text, uuid, jsonb, timestamptz);
|
|
339
|
+
drop function public.contacts_search(text, text, text, boolean, integer, integer);
|
|
340
|
+
drop function public.contacts_get(text, uuid);
|
|
341
|
+
drop function public.contacts_record(text, text, text, text, text, text, text, text, timestamptz, text, text, timestamptz);
|
|
342
|
+
drop function public.contacts_row(public.contacts);
|
|
343
|
+
drop table public.contact_events;
|
|
344
|
+
drop table public.contacts;
|
|
345
|
+
`,
|
|
346
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare const STARTER_CONTACTS: {
|
|
2
|
+
readonly format: 1;
|
|
3
|
+
readonly sources: readonly ["form", "booking", "payment", "quote", "review", "message"];
|
|
4
|
+
readonly keep: readonly ["name", "email", "phone"];
|
|
5
|
+
readonly retention_months: 24;
|
|
6
|
+
readonly marketing: "ask";
|
|
7
|
+
readonly time_zone: "Australia/Sydney";
|
|
8
|
+
readonly notes: true;
|
|
9
|
+
};
|
package/dist/starters.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// What `npx workbench-contacts init` writes: workbench/contacts.json with
|
|
2
|
+
// Workbench's recommended answers, before the client's own.
|
|
3
|
+
export const STARTER_CONTACTS = {
|
|
4
|
+
format: 1,
|
|
5
|
+
sources: ["form", "booking", "payment", "quote", "review", "message"],
|
|
6
|
+
keep: ["name", "email", "phone"],
|
|
7
|
+
retention_months: 24,
|
|
8
|
+
marketing: "ask",
|
|
9
|
+
time_zone: "Australia/Sydney",
|
|
10
|
+
notes: true,
|
|
11
|
+
};
|
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 customer list (renderContactList) and a customer's page (renderContact). */
|
|
33
|
+
export declare const CONTACT_STYLES: ToolStyles;
|
|
34
|
+
export {};
|
package/dist/styles.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// The look the customer list and a customer's page start with
|
|
2
|
+
// (docs/capabilities-manifest.md §10). Every element renderContactList and
|
|
3
|
+
// renderContact draw carries one of these classes, and every class has a
|
|
4
|
+
// default rule here, so the pages look finished out of the box and a project
|
|
5
|
+
// restyles them from its own CSS by the same names. `npx workbench-ui styles`
|
|
6
|
+
// writes these into the project's public/styles/workbench.css inside
|
|
7
|
+
// `@layer workbench`. The names are public: renaming one is a breaking change.
|
|
8
|
+
const v = (name) => `var(--wb-contact-${name})`;
|
|
9
|
+
const focus = { selector: ":focus-visible", about: "Has focus", css: `outline:none;box-shadow:0 0 0 3px ${v("focus")}` };
|
|
10
|
+
const button = `display:inline-flex;align-items:center;justify-content:center;min-height:2.75rem;padding:0 1rem;font:inherit;font-weight:600;border-radius:${v("radius")};cursor:pointer;text-decoration:none`;
|
|
11
|
+
const input = `box-sizing:border-box;width:100%;min-height:2.75rem;padding:.5rem .75rem;font:inherit;color:${v("text")};background:${v("sunk")};border:1px solid ${v("line")};border-radius:${v("radius")}`;
|
|
12
|
+
/** The style hooks of the customer list (renderContactList) and a customer's page (renderContact). */
|
|
13
|
+
export const CONTACT_STYLES = {
|
|
14
|
+
format: 1,
|
|
15
|
+
tool: "contact",
|
|
16
|
+
package: "@mccaskie-labs/workbench-contacts",
|
|
17
|
+
title: "Customer records",
|
|
18
|
+
prefix: "wb-contact-",
|
|
19
|
+
variables: [
|
|
20
|
+
{ name: "--wb-contact-text", value: "#e8e8ec", about: "Text: names, details, labels" },
|
|
21
|
+
{ name: "--wb-contact-muted", value: "#9a9aa6", about: "Quieter text: when last in touch, hints, the empty list" },
|
|
22
|
+
{ name: "--wb-contact-surface", value: "#1b1b1f", about: "Background of each customer and of the page's panels" },
|
|
23
|
+
{ name: "--wb-contact-sunk", value: "#141417", about: "Background of the list and of inputs" },
|
|
24
|
+
{ name: "--wb-contact-line", value: "#33333b", about: "Borders" },
|
|
25
|
+
{ name: "--wb-contact-accent", value: "#4c8dff", about: "Main buttons and links: Find, Save" },
|
|
26
|
+
{ name: "--wb-contact-on-accent", value: "#ffffff", about: "Text on a main button" },
|
|
27
|
+
{ name: "--wb-contact-yes", value: "#3fb27f", about: "The said-yes-to-news badge" },
|
|
28
|
+
{ name: "--wb-contact-danger", value: "#e5484d", about: "Delete customer" },
|
|
29
|
+
{ name: "--wb-contact-focus", value: "rgba(76,141,255,.4)", about: "The ring round a control with focus" },
|
|
30
|
+
{ name: "--wb-contact-radius", value: "8px", about: "Corner rounding" },
|
|
31
|
+
],
|
|
32
|
+
classes: [
|
|
33
|
+
{ name: "wb-contact-list", element: "section", about: "The whole customer list", css: `display:grid;gap:.75rem;color:${v("text")};background:${v("sunk")};padding:.75rem;border-radius:${v("radius")}` },
|
|
34
|
+
{ name: "wb-contact-list-title", element: "h2", about: "\"Customers\", with the count", css: "margin:0;font-size:1.15rem;font-weight:600" },
|
|
35
|
+
{ name: "wb-contact-count", element: "span", about: "How many match", css: `color:${v("muted")};font-weight:400` },
|
|
36
|
+
{ name: "wb-contact-search", element: "form", about: "The search box and its button", css: "display:flex;flex-wrap:wrap;gap:.5rem;align-items:end" },
|
|
37
|
+
{ name: "wb-contact-search-label", element: "label", about: "\"Find a customer\"", css: "flex:1 1 100%;font-size:.9rem;font-weight:600" },
|
|
38
|
+
{ name: "wb-contact-query", element: "input", about: "What to search for: part of a name or email, or a phone's digits", css: `${input};flex:1 1 12rem;width:auto`, states: [focus] },
|
|
39
|
+
{ name: "wb-contact-find", element: "button", about: "Find", css: `${button};color:${v("on-accent")};background:${v("accent")};border:1px solid ${v("accent")}`, states: [focus] },
|
|
40
|
+
{ name: "wb-contact-exports", element: "div", about: "The download links", css: "display:flex;flex-wrap:wrap;gap:.5rem" },
|
|
41
|
+
{ name: "wb-contact-export", element: "a", about: "Download the list, or the mailing list", css: `${button};color:${v("text")};background:transparent;border:1px solid ${v("line")}`, states: [focus] },
|
|
42
|
+
{ name: "wb-contact-empty", element: "p", about: "No customers yet, nobody matches, or no history yet", css: `margin:0;color:${v("muted")}` },
|
|
43
|
+
{ name: "wb-contact-items", element: "ul", about: "The customers", css: "list-style:none;margin:0;padding:0;display:grid;gap:.5rem" },
|
|
44
|
+
{ name: "wb-contact-item", element: "li", about: "One customer's place in the list", css: "margin:0" },
|
|
45
|
+
{
|
|
46
|
+
name: "wb-contact-link",
|
|
47
|
+
element: "a",
|
|
48
|
+
about: "One customer: opens their page",
|
|
49
|
+
css: `display:grid;gap:.15rem;min-height:2.75rem;padding:.65rem .75rem;color:${v("text")};background:${v("surface")};border:1px solid ${v("line")};border-radius:${v("radius")};text-decoration:none`,
|
|
50
|
+
states: [focus, { selector: ":hover", about: "Under the pointer", css: `border-color:${v("accent")}` }],
|
|
51
|
+
},
|
|
52
|
+
{ name: "wb-contact-name", element: "span", about: "The customer's name (or their email or phone when there is no name)", css: "font-weight:600;overflow-wrap:anywhere" },
|
|
53
|
+
{ name: "wb-contact-detail", element: "span", about: "Email and phone", css: `color:${v("muted")};font-size:.9rem;overflow-wrap:anywhere` },
|
|
54
|
+
{ name: "wb-contact-seen", element: "span", about: "When they were last in touch", css: `color:${v("muted")};font-size:.85rem` },
|
|
55
|
+
{ name: "wb-contact-badge", element: "span", about: "Said yes to news and offers", css: `justify-self:start;padding:.1rem .5rem;font-size:.8rem;font-weight:600;color:${v("yes")};border:1px solid ${v("yes")};border-radius:999px` },
|
|
56
|
+
{ name: "wb-contact-more", element: "a", about: "Show more", css: `${button};justify-self:center;color:${v("accent")};background:transparent;border:1px solid transparent`, states: [focus] },
|
|
57
|
+
{ name: "wb-contact", element: "article", about: "One customer's page", css: `display:grid;gap:1rem;color:${v("text")};background:${v("sunk")};padding:.75rem;border-radius:${v("radius")}` },
|
|
58
|
+
{ name: "wb-contact-title", element: "h2", about: "The customer's name", css: "margin:0;font-size:1.25rem;font-weight:600;overflow-wrap:anywhere" },
|
|
59
|
+
{ name: "wb-contact-done", element: "p", about: "What just happened: Saved", css: `margin:0;padding:.5rem .75rem;background:${v("surface")};border-left:4px solid ${v("yes")};border-radius:${v("radius")}` },
|
|
60
|
+
{ name: "wb-contact-facts", element: "dl", about: "News and offers, first and last in touch, and how many of each", css: "display:grid;gap:.5rem;margin:0" },
|
|
61
|
+
{ name: "wb-contact-fact", element: "div", about: "One fact: its term and value", css: "display:grid;gap:.1rem" },
|
|
62
|
+
{ name: "wb-contact-term", element: "dt", about: "What the fact is", css: `color:${v("muted")};font-size:.85rem` },
|
|
63
|
+
{ name: "wb-contact-value", element: "dd", about: "The fact", css: "margin:0;overflow-wrap:anywhere" },
|
|
64
|
+
{ name: "wb-contact-form", element: "form", about: "The details to correct, and Stop news and offers", css: `display:grid;gap:.75rem;padding:.75rem;background:${v("surface")};border:1px solid ${v("line")};border-radius:${v("radius")}` },
|
|
65
|
+
{ name: "wb-contact-field", element: "div", about: "One detail: its label and input", css: "display:grid;gap:.25rem" },
|
|
66
|
+
{ name: "wb-contact-label", element: "label", about: "A detail's name", css: "font-size:.9rem;font-weight:600" },
|
|
67
|
+
{ name: "wb-contact-input", element: "input", about: "A detail to correct", css: input, states: [focus] },
|
|
68
|
+
{ name: "wb-contact-note", element: "textarea", about: "The owner's own note", css: `${input};min-height:5rem;resize:vertical;line-height:1.45`, states: [focus] },
|
|
69
|
+
{ name: "wb-contact-save", element: "button", about: "Save", css: `${button};justify-self:start;color:${v("on-accent")};background:${v("accent")};border:1px solid ${v("accent")}`, states: [focus] },
|
|
70
|
+
{ name: "wb-contact-stop", element: "button", about: "Stop news and offers", css: `${button};justify-self:start;color:${v("text")};background:transparent;border:1px solid ${v("line")}`, states: [focus] },
|
|
71
|
+
{ name: "wb-contact-download", element: "a", about: "Download their details (for their request to see what is kept)", css: `${button};justify-self:start;color:${v("text")};background:transparent;border:1px solid ${v("line")}`, states: [focus] },
|
|
72
|
+
{ name: "wb-contact-history", element: "section", about: "What pointed at them: forms, bookings, payments", css: "display:grid;gap:.5rem" },
|
|
73
|
+
{ name: "wb-contact-heading", element: "h3", about: "\"History\"", css: "margin:0;font-size:1rem;font-weight:600" },
|
|
74
|
+
{ name: "wb-contact-events", element: "ul", about: "The history", css: "list-style:none;margin:0;padding:0;display:grid;gap:.25rem" },
|
|
75
|
+
{ name: "wb-contact-event", element: "li", about: "One booking, payment or form, with when", css: `display:flex;flex-wrap:wrap;gap:.5rem;justify-content:space-between;padding:.4rem 0;border-bottom:1px solid ${v("line")}` },
|
|
76
|
+
{ name: "wb-contact-kind", element: "span", about: "Booking, Payment, Form", css: "font-weight:600" },
|
|
77
|
+
{ name: "wb-contact-when", element: "time", about: "When it happened", css: `color:${v("muted")};font-size:.9rem` },
|
|
78
|
+
{ name: "wb-contact-forget-form", element: "form", about: "Delete customer, with its tick box", css: `display:grid;gap:.5rem;padding:.75rem;border:1px solid ${v("danger")};border-radius:${v("radius")}` },
|
|
79
|
+
{ name: "wb-contact-hint", element: "p", about: "When to delete a customer", css: `margin:0;color:${v("muted")};font-size:.9rem` },
|
|
80
|
+
{ name: "wb-contact-confirm", element: "label", about: "Yes, delete this customer", css: "display:flex;gap:.5rem;align-items:center;min-height:2.75rem" },
|
|
81
|
+
{ name: "wb-contact-check", element: "input", about: "The tick box Delete needs", css: `width:1.25rem;height:1.25rem;accent-color:${v("danger")}`, states: [focus] },
|
|
82
|
+
{ name: "wb-contact-forget", element: "button", about: "Delete customer", css: `${button};justify-self:start;color:${v("on-accent")};background:${v("danger")};border:1px solid ${v("danger")}`, states: [focus] },
|
|
83
|
+
],
|
|
84
|
+
};
|
package/dist/tidy.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** The phone shape of the project's country pack (workbench-country's pack.phone). */
|
|
2
|
+
export interface PhoneCountry {
|
|
3
|
+
callingCode: string;
|
|
4
|
+
trunkPrefix?: string;
|
|
5
|
+
}
|
|
6
|
+
/** A name with control characters gone and spaces tidied, up to 200 characters; null if there is nothing. */
|
|
7
|
+
export declare function tidyName(v: unknown): string | null | undefined;
|
|
8
|
+
/** An email in lower case, or undefined when it isn't one. */
|
|
9
|
+
export declare function tidyEmail(v: unknown): string | null | undefined;
|
|
10
|
+
/**
|
|
11
|
+
* A phone as E.164 (+639171234567). "+63 917 123 4567", "0063 917…" and
|
|
12
|
+
* "63 917…" are read anywhere; a national number ("0917 123 4567",
|
|
13
|
+
* "0412 345 678") needs the project's country.
|
|
14
|
+
*/
|
|
15
|
+
export declare function tidyPhone(v: unknown, country?: PhoneCountry): string | null | undefined;
|
|
16
|
+
/** A Messenger page-scoped id: digits, as Meta sends it. */
|
|
17
|
+
export declare function tidyMessenger(v: unknown): string | null | undefined;
|
package/dist/tidy.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// How each detail is kept, so the same person is found again however they
|
|
2
|
+
// typed it: an email in lower case, a phone as +<country code><number>, a
|
|
3
|
+
// name with its spaces tidied. A detail that can't be read is dropped and
|
|
4
|
+
// named, never guessed.
|
|
5
|
+
const EMAIL = /^[^\s@<>()[\]\\,;:"]{1,64}@[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)+$/;
|
|
6
|
+
const CONTROL = /[\u0000-\u001f\u007f-\u009f\u200b-\u200f\u2028-\u202e\u2060-\u206f\ufeff]/g;
|
|
7
|
+
const blank = (v) => v === undefined || v === null || (typeof v === "string" && v.trim() === "");
|
|
8
|
+
/** A name with control characters gone and spaces tidied, up to 200 characters; null if there is nothing. */
|
|
9
|
+
export function tidyName(v) {
|
|
10
|
+
if (blank(v))
|
|
11
|
+
return null;
|
|
12
|
+
if (typeof v !== "string")
|
|
13
|
+
return undefined;
|
|
14
|
+
const s = v.normalize("NFC").replace(CONTROL, "").replace(/\s+/g, " ").trim();
|
|
15
|
+
return s && s.length <= 200 ? s : s ? undefined : null;
|
|
16
|
+
}
|
|
17
|
+
/** An email in lower case, or undefined when it isn't one. */
|
|
18
|
+
export function tidyEmail(v) {
|
|
19
|
+
if (blank(v))
|
|
20
|
+
return null;
|
|
21
|
+
if (typeof v !== "string")
|
|
22
|
+
return undefined;
|
|
23
|
+
const s = v.replace(CONTROL, "").trim().toLowerCase();
|
|
24
|
+
return s.length <= 254 && EMAIL.test(s) ? s : undefined;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* A phone as E.164 (+639171234567). "+63 917 123 4567", "0063 917…" and
|
|
28
|
+
* "63 917…" are read anywhere; a national number ("0917 123 4567",
|
|
29
|
+
* "0412 345 678") needs the project's country.
|
|
30
|
+
*/
|
|
31
|
+
export function tidyPhone(v, country) {
|
|
32
|
+
if (blank(v))
|
|
33
|
+
return null;
|
|
34
|
+
if (typeof v !== "string")
|
|
35
|
+
return undefined;
|
|
36
|
+
const raw = v.replace(CONTROL, "").trim();
|
|
37
|
+
if (!/^[+\d(][\d\s().-]*$/.test(raw))
|
|
38
|
+
return undefined;
|
|
39
|
+
let digits = raw.replace(/\D/g, "");
|
|
40
|
+
let international = raw.startsWith("+");
|
|
41
|
+
if (!international && digits.startsWith("00")) {
|
|
42
|
+
digits = digits.slice(2);
|
|
43
|
+
international = true;
|
|
44
|
+
}
|
|
45
|
+
if (!international && country) {
|
|
46
|
+
const trunk = country.trunkPrefix ?? "";
|
|
47
|
+
if (trunk && digits.startsWith(trunk))
|
|
48
|
+
digits = country.callingCode + digits.slice(trunk.length);
|
|
49
|
+
else if (!digits.startsWith(country.callingCode))
|
|
50
|
+
digits = country.callingCode + digits;
|
|
51
|
+
// A trunk zero typed after the country code: +61 0412 …
|
|
52
|
+
}
|
|
53
|
+
else if (!international)
|
|
54
|
+
return undefined;
|
|
55
|
+
if (country?.trunkPrefix && digits.startsWith(country.callingCode + country.trunkPrefix)) {
|
|
56
|
+
digits = country.callingCode + digits.slice(country.callingCode.length + country.trunkPrefix.length);
|
|
57
|
+
}
|
|
58
|
+
return /^[1-9]\d{6,14}$/.test(digits) ? `+${digits}` : undefined;
|
|
59
|
+
}
|
|
60
|
+
/** A Messenger page-scoped id: digits, as Meta sends it. */
|
|
61
|
+
export function tidyMessenger(v) {
|
|
62
|
+
if (blank(v))
|
|
63
|
+
return null;
|
|
64
|
+
if (typeof v !== "string" && typeof v !== "number")
|
|
65
|
+
return undefined;
|
|
66
|
+
const s = String(v).trim();
|
|
67
|
+
return /^\d{5,30}$/.test(s) ? s : undefined;
|
|
68
|
+
}
|