@mccaskie-labs/workbench-settings 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 +25 -0
- package/README.md +46 -2
- package/dist/check.d.ts +27 -0
- package/dist/check.js +176 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.js +50 -0
- package/dist/design.d.ts +20 -0
- package/dist/design.js +59 -0
- package/dist/email.d.ts +2 -0
- package/dist/email.js +46 -0
- package/dist/errors.d.ts +31 -0
- package/dist/errors.js +14 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +18 -0
- package/dist/migration.d.ts +4 -0
- package/dist/migration.js +329 -0
- package/dist/page.d.ts +58 -0
- package/dist/page.js +215 -0
- package/dist/samples.d.ts +18 -0
- package/dist/samples.js +90 -0
- package/dist/settings.d.ts +136 -0
- package/dist/settings.js +286 -0
- package/dist/styles.d.ts +34 -0
- package/dist/styles.js +83 -0
- package/dist/types.d.ts +109 -0
- package/dist/types.js +5 -0
- package/dist/words.d.ts +13 -0
- package/dist/words.js +221 -0
- package/package.json +52 -4
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
// The app's own settings tables, copied into the project's
|
|
2
|
+
// supabase/migrations as one file and its down file
|
|
3
|
+
// (`npx workbench-settings migration`).
|
|
4
|
+
//
|
|
5
|
+
// Only the service role reaches them: the Settings page is served by the
|
|
6
|
+
// app's Worker behind its sign-in, which checks the signed-in person is one
|
|
7
|
+
// of the app's admins before it calls the package. So the tables have RLS on
|
|
8
|
+
// with no policies and the functions are granted to service_role only.
|
|
9
|
+
//
|
|
10
|
+
// - One row per setting the owner changed (workbench_settings). Putting a
|
|
11
|
+
// setting back deletes its row, so the value falls back to the project's
|
|
12
|
+
// answers file. `account` is '' for a setting of the whole app; it is
|
|
13
|
+
// there for apps that host many businesses, when they need it.
|
|
14
|
+
// - Every save and put-back writes a row to workbench_settings_changes in the
|
|
15
|
+
// same transaction, kept 90 days.
|
|
16
|
+
// - A checked change waits in workbench_settings_pending: the new value, and
|
|
17
|
+
// the SHA-256 of a random salt and the six-digit code. It works for 24
|
|
18
|
+
// hours and five tries; a new request for the same setting replaces it,
|
|
19
|
+
// at most one a minute and five a day, and keeps its tries when the value
|
|
20
|
+
// is the same. The code itself is never stored.
|
|
21
|
+
// - On the test site, workbench_settings_carried keeps what the live site was
|
|
22
|
+
// last given for each setting of a tool that carries (design mode's look
|
|
23
|
+
// and layouts), so the page can say "On the test site only" until a
|
|
24
|
+
// release the owner approves carries it.
|
|
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 settingsMigration = {
|
|
29
|
+
up: `create table public.workbench_settings (
|
|
30
|
+
tool text not null check (tool ~ '^[a-z][a-z0-9-]{0,39}$'),
|
|
31
|
+
key text not null check (key ~ '^[a-z][a-z0-9_]{0,39}$'),
|
|
32
|
+
account text not null default '' check (char_length(account) <= 200),
|
|
33
|
+
value jsonb not null check (jsonb_typeof(value) in ('string', 'number', 'object') and octet_length(value::text) <= 8000),
|
|
34
|
+
changed_by text not null check (char_length(changed_by) between 1 and 200),
|
|
35
|
+
changed_at timestamptz not null default now(),
|
|
36
|
+
primary key (tool, key, account)
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
comment on table public.workbench_settings is 'Settings the app''s owner changed on the Settings page (workbench-settings). Service role only.';
|
|
40
|
+
|
|
41
|
+
create table public.workbench_settings_changes (
|
|
42
|
+
id bigint generated always as identity primary key,
|
|
43
|
+
tool text not null,
|
|
44
|
+
key text not null,
|
|
45
|
+
account text not null default '',
|
|
46
|
+
action text not null check (action in ('save', 'put_back')),
|
|
47
|
+
before jsonb check (before is null or octet_length(before::text) <= 8000),
|
|
48
|
+
after jsonb check (after is null or octet_length(after::text) <= 8000),
|
|
49
|
+
changed_by text not null check (char_length(changed_by) between 1 and 200),
|
|
50
|
+
changed_at timestamptz not null default now()
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
create index workbench_settings_changes_at on public.workbench_settings_changes (changed_at desc);
|
|
54
|
+
|
|
55
|
+
comment on table public.workbench_settings_changes is 'Every save and put-back on the Settings page, kept 90 days (workbench-settings). Service role only.';
|
|
56
|
+
|
|
57
|
+
create table public.workbench_settings_pending (
|
|
58
|
+
tool text not null,
|
|
59
|
+
key text not null,
|
|
60
|
+
account text not null default '',
|
|
61
|
+
value jsonb not null check (jsonb_typeof(value) = 'string' and octet_length(value::text) <= 2000),
|
|
62
|
+
salt text not null,
|
|
63
|
+
code_hash text not null,
|
|
64
|
+
tries smallint not null default 0 check (tries between 0 and 5),
|
|
65
|
+
requests smallint not null default 1 check (requests between 1 and 5),
|
|
66
|
+
first_requested_at timestamptz not null,
|
|
67
|
+
requested_by text not null check (char_length(requested_by) between 1 and 200),
|
|
68
|
+
requested_at timestamptz not null,
|
|
69
|
+
expires_at timestamptz not null,
|
|
70
|
+
primary key (tool, key, account)
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
comment on table public.workbench_settings_pending is 'A checked setting waiting for its emailed code: only a hash of the code is kept (workbench-settings). Service role only.';
|
|
74
|
+
|
|
75
|
+
create table public.workbench_settings_carried (
|
|
76
|
+
tool text not null check (tool ~ '^[a-z][a-z0-9-]{0,39}$'),
|
|
77
|
+
key text not null check (key ~ '^[a-z][a-z0-9_]{0,39}$'),
|
|
78
|
+
account text not null default '' check (char_length(account) <= 200),
|
|
79
|
+
value jsonb check (value is null or (jsonb_typeof(value) in ('string', 'number', 'object') and octet_length(value::text) <= 8000)),
|
|
80
|
+
carried_by text not null check (char_length(carried_by) between 1 and 200),
|
|
81
|
+
carried_at timestamptz not null,
|
|
82
|
+
primary key (tool, key, account)
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
comment on table public.workbench_settings_carried is 'On the test site: the value of each carried setting the live site was last given by an approved release (workbench-settings). Service role only.';
|
|
86
|
+
|
|
87
|
+
alter table public.workbench_settings enable row level security;
|
|
88
|
+
alter table public.workbench_settings_carried enable row level security;
|
|
89
|
+
alter table public.workbench_settings_changes enable row level security;
|
|
90
|
+
alter table public.workbench_settings_pending enable row level security;
|
|
91
|
+
|
|
92
|
+
-- Every stored value, and every waiting change without its hash.
|
|
93
|
+
create function public.workbench_settings_read(p_account text)
|
|
94
|
+
returns jsonb
|
|
95
|
+
language sql
|
|
96
|
+
stable
|
|
97
|
+
set search_path = ''
|
|
98
|
+
as $$
|
|
99
|
+
select jsonb_build_object(
|
|
100
|
+
'values', coalesce((select jsonb_agg(jsonb_build_object('tool', s.tool, 'key', s.key, 'value', s.value, 'changed_by', s.changed_by, 'changed_at', s.changed_at))
|
|
101
|
+
from public.workbench_settings s where s.account = p_account), '[]'::jsonb),
|
|
102
|
+
'pending', coalesce((select jsonb_agg(jsonb_build_object('tool', p.tool, 'key', p.key, 'value', p.value, 'expires_at', p.expires_at, 'tries', p.tries))
|
|
103
|
+
from public.workbench_settings_pending p where p.account = p_account), '[]'::jsonb),
|
|
104
|
+
'carried', coalesce((select jsonb_agg(jsonb_build_object('tool', c.tool, 'key', c.key, 'value', c.value))
|
|
105
|
+
from public.workbench_settings_carried c where c.account = p_account), '[]'::jsonb)
|
|
106
|
+
)
|
|
107
|
+
$$;
|
|
108
|
+
|
|
109
|
+
-- On the test site, after the live site took a carried tool's values: records them, one row a key.
|
|
110
|
+
create function public.workbench_settings_carried(p_tool text, p_account text, p_rows jsonb, p_by text, p_now timestamptz)
|
|
111
|
+
returns jsonb
|
|
112
|
+
language plpgsql
|
|
113
|
+
set search_path = ''
|
|
114
|
+
as $$
|
|
115
|
+
declare
|
|
116
|
+
v_row jsonb;
|
|
117
|
+
v_count integer := 0;
|
|
118
|
+
begin
|
|
119
|
+
if jsonb_typeof(p_rows) <> 'array' or jsonb_array_length(p_rows) > 100 then
|
|
120
|
+
raise exception 'p_rows must be an array of at most 100 rows' using errcode = '22023';
|
|
121
|
+
end if;
|
|
122
|
+
for v_row in select * from jsonb_array_elements(p_rows) loop
|
|
123
|
+
insert into public.workbench_settings_carried (tool, key, account, value, carried_by, carried_at)
|
|
124
|
+
values (p_tool, v_row->>'key', p_account, nullif(v_row->'value', 'null'::jsonb), p_by, p_now)
|
|
125
|
+
on conflict (tool, key, account) do update set value = excluded.value, carried_by = excluded.carried_by, carried_at = excluded.carried_at;
|
|
126
|
+
v_count := v_count + 1;
|
|
127
|
+
end loop;
|
|
128
|
+
return jsonb_build_object('carried', v_count);
|
|
129
|
+
end
|
|
130
|
+
$$;
|
|
131
|
+
|
|
132
|
+
create function public.workbench_settings_save(p_tool text, p_key text, p_account text, p_value jsonb, p_by text, p_now timestamptz)
|
|
133
|
+
returns jsonb
|
|
134
|
+
language plpgsql
|
|
135
|
+
set search_path = ''
|
|
136
|
+
as $$
|
|
137
|
+
declare
|
|
138
|
+
v_before jsonb;
|
|
139
|
+
begin
|
|
140
|
+
select s.value into v_before from public.workbench_settings s
|
|
141
|
+
where s.tool = p_tool and s.key = p_key and s.account = p_account for update;
|
|
142
|
+
insert into public.workbench_settings (tool, key, account, value, changed_by, changed_at)
|
|
143
|
+
values (p_tool, p_key, p_account, p_value, p_by, p_now)
|
|
144
|
+
on conflict (tool, key, account) do update set value = excluded.value, changed_by = excluded.changed_by, changed_at = excluded.changed_at;
|
|
145
|
+
delete from public.workbench_settings_pending p where p.tool = p_tool and p.key = p_key and p.account = p_account;
|
|
146
|
+
insert into public.workbench_settings_changes (tool, key, account, action, before, after, changed_by, changed_at)
|
|
147
|
+
values (p_tool, p_key, p_account, 'save', v_before, p_value, p_by, p_now);
|
|
148
|
+
return jsonb_build_object('before', v_before, 'after', p_value);
|
|
149
|
+
end
|
|
150
|
+
$$;
|
|
151
|
+
|
|
152
|
+
create function public.workbench_settings_put_back(p_tool text, p_key text, p_account text, p_by text, p_now timestamptz)
|
|
153
|
+
returns jsonb
|
|
154
|
+
language plpgsql
|
|
155
|
+
set search_path = ''
|
|
156
|
+
as $$
|
|
157
|
+
declare
|
|
158
|
+
v_before jsonb;
|
|
159
|
+
begin
|
|
160
|
+
delete from public.workbench_settings s
|
|
161
|
+
where s.tool = p_tool and s.key = p_key and s.account = p_account
|
|
162
|
+
returning s.value into v_before;
|
|
163
|
+
delete from public.workbench_settings_pending p where p.tool = p_tool and p.key = p_key and p.account = p_account;
|
|
164
|
+
if v_before is null then
|
|
165
|
+
return jsonb_build_object('changed', false);
|
|
166
|
+
end if;
|
|
167
|
+
insert into public.workbench_settings_changes (tool, key, account, action, before, after, changed_by, changed_at)
|
|
168
|
+
values (p_tool, p_key, p_account, 'put_back', v_before, null, p_by, p_now);
|
|
169
|
+
return jsonb_build_object('changed', true, 'before', v_before);
|
|
170
|
+
end
|
|
171
|
+
$$;
|
|
172
|
+
|
|
173
|
+
-- A checked change: keeps the value and the hash of salt and code, replacing any waiting one.
|
|
174
|
+
-- At most one code a minute and five a day per setting, and asking again for the same
|
|
175
|
+
-- value keeps its tries, so asking again never buys more guesses than five a day.
|
|
176
|
+
create function public.workbench_settings_request(p_tool text, p_key text, p_account text, p_value jsonb, p_code text, p_by text, p_now timestamptz)
|
|
177
|
+
returns jsonb
|
|
178
|
+
language plpgsql
|
|
179
|
+
set search_path = ''
|
|
180
|
+
as $$
|
|
181
|
+
declare
|
|
182
|
+
v public.workbench_settings_pending%rowtype;
|
|
183
|
+
v_salt text := gen_random_uuid()::text;
|
|
184
|
+
v_hash text;
|
|
185
|
+
v_expires timestamptz := p_now + interval '24 hours';
|
|
186
|
+
begin
|
|
187
|
+
if p_code !~ '^[0-9]{6}$' then
|
|
188
|
+
raise exception 'bad code' using errcode = '22023';
|
|
189
|
+
end if;
|
|
190
|
+
v_hash := encode(sha256(convert_to(v_salt || ':' || p_code, 'UTF8')), 'hex');
|
|
191
|
+
select * into v from public.workbench_settings_pending p
|
|
192
|
+
where p.tool = p_tool and p.key = p_key and p.account = p_account for update;
|
|
193
|
+
if not found or v.first_requested_at <= p_now - interval '24 hours' then
|
|
194
|
+
delete from public.workbench_settings_pending p where p.tool = p_tool and p.key = p_key and p.account = p_account;
|
|
195
|
+
insert into public.workbench_settings_pending (tool, key, account, value, salt, code_hash, tries, requests, first_requested_at, requested_by, requested_at, expires_at)
|
|
196
|
+
values (p_tool, p_key, p_account, p_value, v_salt, v_hash, 0, 1, p_now, p_by, p_now, v_expires);
|
|
197
|
+
return jsonb_build_object('status', 'sent', 'expires_at', v_expires);
|
|
198
|
+
end if;
|
|
199
|
+
if v.requested_at > p_now - interval '1 minute' then
|
|
200
|
+
return jsonb_build_object('status', 'too_soon');
|
|
201
|
+
end if;
|
|
202
|
+
if v.requests >= 5 then
|
|
203
|
+
return jsonb_build_object('status', 'too_many');
|
|
204
|
+
end if;
|
|
205
|
+
update public.workbench_settings_pending p
|
|
206
|
+
set value = p_value, salt = v_salt, code_hash = v_hash,
|
|
207
|
+
tries = case when p.value = p_value then p.tries else 0 end,
|
|
208
|
+
requests = p.requests + 1, requested_by = p_by, requested_at = p_now, expires_at = v_expires
|
|
209
|
+
where p.tool = p_tool and p.key = p_key and p.account = p_account;
|
|
210
|
+
return jsonb_build_object('status', 'sent', 'expires_at', v_expires);
|
|
211
|
+
end
|
|
212
|
+
$$;
|
|
213
|
+
|
|
214
|
+
-- Drops a waiting change: the owner pressed Cancel, or its email couldn't be sent.
|
|
215
|
+
create function public.workbench_settings_cancel(p_tool text, p_key text, p_account text)
|
|
216
|
+
returns jsonb
|
|
217
|
+
language sql
|
|
218
|
+
set search_path = ''
|
|
219
|
+
as $$
|
|
220
|
+
with gone as (
|
|
221
|
+
delete from public.workbench_settings_pending p
|
|
222
|
+
where p.tool = p_tool and p.key = p_key and p.account = p_account
|
|
223
|
+
returning 1
|
|
224
|
+
)
|
|
225
|
+
select jsonb_build_object('cancelled', exists (select 1 from gone))
|
|
226
|
+
$$;
|
|
227
|
+
|
|
228
|
+
-- Proves a checked change: saved on the right code, a try used on a wrong one.
|
|
229
|
+
create function public.workbench_settings_confirm(p_tool text, p_key text, p_account text, p_code text, p_by text, p_now timestamptz)
|
|
230
|
+
returns jsonb
|
|
231
|
+
language plpgsql
|
|
232
|
+
set search_path = ''
|
|
233
|
+
as $$
|
|
234
|
+
declare
|
|
235
|
+
v public.workbench_settings_pending%rowtype;
|
|
236
|
+
v_saved jsonb;
|
|
237
|
+
begin
|
|
238
|
+
select * into v from public.workbench_settings_pending p
|
|
239
|
+
where p.tool = p_tool and p.key = p_key and p.account = p_account for update;
|
|
240
|
+
if not found then
|
|
241
|
+
return jsonb_build_object('status', 'none');
|
|
242
|
+
end if;
|
|
243
|
+
if v.expires_at <= p_now then
|
|
244
|
+
delete from public.workbench_settings_pending p where p.tool = p_tool and p.key = p_key and p.account = p_account;
|
|
245
|
+
return jsonb_build_object('status', 'expired');
|
|
246
|
+
end if;
|
|
247
|
+
if v.tries >= 5 then
|
|
248
|
+
return jsonb_build_object('status', 'locked');
|
|
249
|
+
end if;
|
|
250
|
+
if p_code !~ '^[0-9]{6}$' or encode(sha256(convert_to(v.salt || ':' || p_code, 'UTF8')), 'hex') <> v.code_hash then
|
|
251
|
+
update public.workbench_settings_pending p set tries = p.tries + 1
|
|
252
|
+
where p.tool = p_tool and p.key = p_key and p.account = p_account;
|
|
253
|
+
return jsonb_build_object('status', case when v.tries + 1 >= 5 then 'locked' else 'wrong' end, 'left', 5 - (v.tries + 1));
|
|
254
|
+
end if;
|
|
255
|
+
v_saved := public.workbench_settings_save(p_tool, p_key, p_account, v.value, p_by, p_now);
|
|
256
|
+
return jsonb_build_object('status', 'saved', 'value', v.value, 'before', v_saved -> 'before');
|
|
257
|
+
end
|
|
258
|
+
$$;
|
|
259
|
+
|
|
260
|
+
create function public.workbench_settings_history(p_account text, p_limit integer)
|
|
261
|
+
returns jsonb
|
|
262
|
+
language sql
|
|
263
|
+
stable
|
|
264
|
+
set search_path = ''
|
|
265
|
+
as $$
|
|
266
|
+
select coalesce(jsonb_agg(row_to_json(c)::jsonb order by c.changed_at desc, c.id desc), '[]'::jsonb)
|
|
267
|
+
from (select h.id, h.tool, h.key, h.action, h.before, h.after, h.changed_by, h.changed_at
|
|
268
|
+
from public.workbench_settings_changes h
|
|
269
|
+
where h.account = p_account
|
|
270
|
+
order by h.changed_at desc, h.id desc
|
|
271
|
+
limit least(greatest(p_limit, 1), 200)) c
|
|
272
|
+
$$;
|
|
273
|
+
|
|
274
|
+
-- Daily: changes older than 90 days and waiting changes past their expiry.
|
|
275
|
+
create function public.workbench_settings_cleanup(p_now timestamptz)
|
|
276
|
+
returns jsonb
|
|
277
|
+
language plpgsql
|
|
278
|
+
set search_path = ''
|
|
279
|
+
as $$
|
|
280
|
+
declare
|
|
281
|
+
v_changes integer;
|
|
282
|
+
v_pending integer;
|
|
283
|
+
begin
|
|
284
|
+
delete from public.workbench_settings_changes h where h.changed_at < p_now - interval '90 days';
|
|
285
|
+
get diagnostics v_changes = row_count;
|
|
286
|
+
delete from public.workbench_settings_pending p where p.expires_at <= p_now;
|
|
287
|
+
get diagnostics v_pending = row_count;
|
|
288
|
+
return jsonb_build_object('changes', v_changes, 'pending', v_pending);
|
|
289
|
+
end
|
|
290
|
+
$$;
|
|
291
|
+
|
|
292
|
+
revoke all on table public.workbench_settings, public.workbench_settings_changes, public.workbench_settings_pending, public.workbench_settings_carried from public, anon, authenticated;
|
|
293
|
+
grant select, insert, update, delete on table public.workbench_settings, public.workbench_settings_changes, public.workbench_settings_pending, public.workbench_settings_carried to service_role;
|
|
294
|
+
|
|
295
|
+
revoke all on function public.workbench_settings_read(text) from public, anon, authenticated;
|
|
296
|
+
revoke all on function public.workbench_settings_save(text, text, text, jsonb, text, timestamptz) from public, anon, authenticated;
|
|
297
|
+
revoke all on function public.workbench_settings_put_back(text, text, text, text, timestamptz) from public, anon, authenticated;
|
|
298
|
+
revoke all on function public.workbench_settings_request(text, text, text, jsonb, text, text, timestamptz) from public, anon, authenticated;
|
|
299
|
+
revoke all on function public.workbench_settings_confirm(text, text, text, text, text, timestamptz) from public, anon, authenticated;
|
|
300
|
+
revoke all on function public.workbench_settings_cancel(text, text, text) from public, anon, authenticated;
|
|
301
|
+
revoke all on function public.workbench_settings_history(text, integer) from public, anon, authenticated;
|
|
302
|
+
revoke all on function public.workbench_settings_cleanup(timestamptz) from public, anon, authenticated;
|
|
303
|
+
revoke all on function public.workbench_settings_carried(text, text, jsonb, text, timestamptz) from public, anon, authenticated;
|
|
304
|
+
|
|
305
|
+
grant execute on function public.workbench_settings_read(text) to service_role;
|
|
306
|
+
grant execute on function public.workbench_settings_save(text, text, text, jsonb, text, timestamptz) to service_role;
|
|
307
|
+
grant execute on function public.workbench_settings_put_back(text, text, text, text, timestamptz) to service_role;
|
|
308
|
+
grant execute on function public.workbench_settings_request(text, text, text, jsonb, text, text, timestamptz) to service_role;
|
|
309
|
+
grant execute on function public.workbench_settings_confirm(text, text, text, text, text, timestamptz) to service_role;
|
|
310
|
+
grant execute on function public.workbench_settings_cancel(text, text, text) to service_role;
|
|
311
|
+
grant execute on function public.workbench_settings_history(text, integer) to service_role;
|
|
312
|
+
grant execute on function public.workbench_settings_cleanup(timestamptz) to service_role;
|
|
313
|
+
grant execute on function public.workbench_settings_carried(text, text, jsonb, text, timestamptz) to service_role;
|
|
314
|
+
`,
|
|
315
|
+
down: `drop function if exists public.workbench_settings_carried(text, text, jsonb, text, timestamptz);
|
|
316
|
+
drop function if exists public.workbench_settings_cleanup(timestamptz);
|
|
317
|
+
drop function if exists public.workbench_settings_history(text, integer);
|
|
318
|
+
drop function if exists public.workbench_settings_cancel(text, text, text);
|
|
319
|
+
drop function if exists public.workbench_settings_confirm(text, text, text, text, text, timestamptz);
|
|
320
|
+
drop function if exists public.workbench_settings_request(text, text, text, jsonb, text, text, timestamptz);
|
|
321
|
+
drop function if exists public.workbench_settings_put_back(text, text, text, text, timestamptz);
|
|
322
|
+
drop function if exists public.workbench_settings_save(text, text, text, jsonb, text, timestamptz);
|
|
323
|
+
drop function if exists public.workbench_settings_read(text);
|
|
324
|
+
drop table if exists public.workbench_settings_carried;
|
|
325
|
+
drop table if exists public.workbench_settings_pending;
|
|
326
|
+
drop table if exists public.workbench_settings_changes;
|
|
327
|
+
drop table if exists public.workbench_settings;
|
|
328
|
+
`,
|
|
329
|
+
};
|
package/dist/page.d.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { Settings } from "./settings.ts";
|
|
2
|
+
import type { SettingChange, SettingsLang, SettingState, ToolSettings } from "./types.ts";
|
|
3
|
+
import { type SettingsStrings, type SettingsWordKey } from "./words.ts";
|
|
4
|
+
/** What the last post did, to show at the top of the page and beside its setting. */
|
|
5
|
+
export interface SettingsOutcome {
|
|
6
|
+
/** 200 when it worked, 422 when the owner must fix something, 503 when the store failed. */
|
|
7
|
+
status: 200 | 422 | 503;
|
|
8
|
+
tool?: string;
|
|
9
|
+
key?: string;
|
|
10
|
+
notice?: {
|
|
11
|
+
word: SettingsWordKey;
|
|
12
|
+
values?: Record<string, string>;
|
|
13
|
+
};
|
|
14
|
+
problem?: {
|
|
15
|
+
word: SettingsWordKey;
|
|
16
|
+
values?: Record<string, string>;
|
|
17
|
+
};
|
|
18
|
+
/** What the owner typed, put back in the field after a problem. */
|
|
19
|
+
typed?: string;
|
|
20
|
+
}
|
|
21
|
+
export interface SettingsPageOptions {
|
|
22
|
+
/** Where the forms post, such as /admin/settings. */
|
|
23
|
+
endpoint: string;
|
|
24
|
+
lang?: SettingsLang;
|
|
25
|
+
/** The app's own words for the page, from its strings catalogue. */
|
|
26
|
+
t?: SettingsStrings;
|
|
27
|
+
/** The last post's outcome. */
|
|
28
|
+
outcome?: SettingsOutcome;
|
|
29
|
+
/** Recent changes to list. Leave out to hide the list. */
|
|
30
|
+
history?: readonly SettingChange[];
|
|
31
|
+
/** Formats a change's time. Default: day, month, year and time, in `timeZone`. */
|
|
32
|
+
when?: (iso: string) => string;
|
|
33
|
+
/** IANA time zone for the default `when`. Default Australia/Sydney. */
|
|
34
|
+
timeZone?: string;
|
|
35
|
+
/**
|
|
36
|
+
* Hidden fields added to every form, such as the app's own anti-forgery
|
|
37
|
+
* token. The app must still refuse a post from another site (checking its
|
|
38
|
+
* Origin, as the template's requireUser does) before calling settingsAction.
|
|
39
|
+
*/
|
|
40
|
+
fields?: Readonly<Record<string, string>>;
|
|
41
|
+
}
|
|
42
|
+
/** The page's body, for the app's admin shell. */
|
|
43
|
+
export declare function renderSettingsPage(settings: Settings, o: SettingsPageOptions): Promise<string>;
|
|
44
|
+
/**
|
|
45
|
+
* Draws the page from settings already read: the same body as
|
|
46
|
+
* renderSettingsPage, for samples and previews with made-up states.
|
|
47
|
+
*/
|
|
48
|
+
export declare function drawSettingsPage(tools: readonly ToolSettings[], states: readonly SettingState[], o: SettingsPageOptions): string;
|
|
49
|
+
/**
|
|
50
|
+
* Does what one of the page's forms asked: save, put back or confirm a code.
|
|
51
|
+
* Give it the posted fields and the signed-in admin's name or email (`by`),
|
|
52
|
+
* after the app's own sign-in and admin check. Render the page again with
|
|
53
|
+
* the outcome (renderSettingsPage's `outcome`) and its status.
|
|
54
|
+
*/
|
|
55
|
+
export declare function settingsAction(settings: Settings, fields: URLSearchParams | Readonly<Record<string, string>>, by: string, opts?: {
|
|
56
|
+
lang?: SettingsLang;
|
|
57
|
+
t?: SettingsStrings;
|
|
58
|
+
}): Promise<SettingsOutcome>;
|
package/dist/page.js
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
// The Settings page's body: one section per tool, one small form per
|
|
2
|
+
// setting (Save, and Put back once the owner has changed it), the code form
|
|
3
|
+
// while a checked change waits, and Recent changes. Plain HTML forms that
|
|
4
|
+
// post to one address, so it works with no script. The app puts the body in
|
|
5
|
+
// its own admin shell (workbench-ui's shell({ admin: true })), behind its
|
|
6
|
+
// sign-in, for its admins only. Every element carries a wb-settings-* class
|
|
7
|
+
// (SETTINGS_STYLES).
|
|
8
|
+
import { checkValue } from "./check.js";
|
|
9
|
+
import { SettingsError } from "./errors.js";
|
|
10
|
+
import { phrase, settingsWord } from "./words.js";
|
|
11
|
+
const esc = (s) => s.replace(/[&<>"']/g, (c) => `&#${c.charCodeAt(0)};`);
|
|
12
|
+
function defaultWhen(timeZone, lang) {
|
|
13
|
+
const f = new Intl.DateTimeFormat(lang === "en" ? "en-AU" : "en-PH", { day: "numeric", month: "short", year: "numeric", hour: "numeric", minute: "2-digit", timeZone });
|
|
14
|
+
return (iso) => f.format(new Date(iso));
|
|
15
|
+
}
|
|
16
|
+
const shown = (v, lang, t, def) => {
|
|
17
|
+
if (v === null || v === undefined)
|
|
18
|
+
return settingsWord("not_set", lang, {}, t);
|
|
19
|
+
if (typeof v === "object")
|
|
20
|
+
return Object.entries(v).map(([name, value]) => `${name}: ${value}`).join(", ");
|
|
21
|
+
const choice = def?.choices?.find((c) => c.value === v);
|
|
22
|
+
return choice ? phrase(choice.label, lang) : String(v);
|
|
23
|
+
};
|
|
24
|
+
function field(s, fid, value, lang, invalid, describedBy) {
|
|
25
|
+
const d = s.def;
|
|
26
|
+
const aria = `${invalid ? ' aria-invalid="true"' : ""} aria-describedby="${describedBy}"`;
|
|
27
|
+
if (d.type === "choice") {
|
|
28
|
+
const opts = (d.choices ?? []).map((c) => `<option value="${esc(c.value)}"${c.value === value ? " selected" : ""}>${esc(phrase(c.label, lang))}</option>`).join("");
|
|
29
|
+
return `<select class="wb-settings-select" id="${fid}" name="value"${aria}>${opts}</select>`;
|
|
30
|
+
}
|
|
31
|
+
const type = d.type === "email" ? "email" : d.type === "whole" ? "number" : "text";
|
|
32
|
+
const extra = d.type === "email"
|
|
33
|
+
? ' autocomplete="email" spellcheck="false" maxlength="254"'
|
|
34
|
+
: d.type === "whole"
|
|
35
|
+
? ` inputmode="numeric" step="1"${d.min !== undefined ? ` min="${d.min}"` : ""}${d.max !== undefined ? ` max="${d.max}"` : ""}`
|
|
36
|
+
: ` maxlength="${Math.min(d.max ?? 200, 500)}"`;
|
|
37
|
+
return `<input class="wb-settings-input" id="${fid}" name="value" type="${type}" value="${esc(value)}"${extra}${aria}>`;
|
|
38
|
+
}
|
|
39
|
+
function source(s, lang, t, when) {
|
|
40
|
+
if (s.from === "owner")
|
|
41
|
+
return settingsWord("from.owner", lang, { by: s.changedBy ?? "", when: s.changedAt ? when(s.changedAt) : "" }, t);
|
|
42
|
+
return settingsWord(s.from === "answers" ? "from.answers" : "from.default", lang, {}, t);
|
|
43
|
+
}
|
|
44
|
+
const carryNote = (s, lang, t) => s.carry === "test_only" ? `<p class="wb-settings-carry">${esc(settingsWord("carry.test_only", lang, {}, t))}</p>` : "";
|
|
45
|
+
/** A map is set where it is chosen (design mode): here it is shown, with Put back. */
|
|
46
|
+
function mapItem(s, o, lang, when, fid, hidden) {
|
|
47
|
+
const t = o.t;
|
|
48
|
+
const entries = s.value && typeof s.value === "object" ? Object.entries(s.value) : [];
|
|
49
|
+
const list = entries.length
|
|
50
|
+
? `<ul class="wb-settings-map" aria-labelledby="${fid}">${entries.map(([name, value]) => `<li class="wb-settings-map-item">${esc(name)}: ${esc(value)}</li>`).join("")}</ul>`
|
|
51
|
+
: `<p class="wb-settings-empty">${esc(settingsWord("map.empty", lang, {}, t))}</p>`;
|
|
52
|
+
return [
|
|
53
|
+
`<div class="wb-settings-setting"><form class="wb-settings-item" method="post" action="${esc(o.endpoint)}">`,
|
|
54
|
+
hidden,
|
|
55
|
+
`<p class="wb-settings-label" id="${fid}">${esc(phrase(s.def.label, lang))}</p>`,
|
|
56
|
+
s.def.help ? `<p class="wb-settings-help">${esc(phrase(s.def.help, lang))}</p>` : "",
|
|
57
|
+
list,
|
|
58
|
+
`<p class="wb-settings-source">${esc(source(s, lang, t, when))}</p>`,
|
|
59
|
+
carryNote(s, lang, t),
|
|
60
|
+
s.from === "owner" ? `<div class="wb-settings-actions"><button class="wb-settings-put-back" type="submit" name="action" value="put_back">${esc(settingsWord("put_back", lang, {}, t))}</button></div>` : "",
|
|
61
|
+
`</form></div>`,
|
|
62
|
+
].join("");
|
|
63
|
+
}
|
|
64
|
+
function item(s, o, lang, when) {
|
|
65
|
+
const t = o.t;
|
|
66
|
+
const fid = `wb-settings-${s.tool}-${s.key}`.replace(/_/g, "-");
|
|
67
|
+
const mine = o.outcome && o.outcome.tool === s.tool && o.outcome.key === s.key ? o.outcome : undefined;
|
|
68
|
+
const problem = mine?.problem && !mine.notice && mine.problem.word.startsWith("problem.") ? mine.problem : undefined;
|
|
69
|
+
const value = problem && mine?.typed !== undefined ? mine.typed : s.value === null ? "" : String(s.value);
|
|
70
|
+
const ids = [`${fid}-source`, ...(s.def.help ? [`${fid}-help`] : []), ...(problem ? [`${fid}-problem`] : [])].join(" ");
|
|
71
|
+
const extra = Object.entries(o.fields ?? {})
|
|
72
|
+
.map(([n, v]) => `<input type="hidden" name="${esc(n)}" value="${esc(v)}">`)
|
|
73
|
+
.join("");
|
|
74
|
+
const hidden = `${extra}<input type="hidden" name="tool" value="${esc(s.tool)}"><input type="hidden" name="key" value="${esc(s.key)}">`;
|
|
75
|
+
if (s.def.type === "map")
|
|
76
|
+
return mapItem(s, o, lang, when, fid, hidden);
|
|
77
|
+
const parts = [
|
|
78
|
+
`<form class="wb-settings-item" method="post" action="${esc(o.endpoint)}">`,
|
|
79
|
+
hidden,
|
|
80
|
+
`<label class="wb-settings-label" for="${fid}">${esc(phrase(s.def.label, lang))}</label>`,
|
|
81
|
+
s.def.help ? `<p class="wb-settings-help" id="${fid}-help">${esc(phrase(s.def.help, lang))}</p>` : "",
|
|
82
|
+
field(s, fid, value, lang, Boolean(problem), ids),
|
|
83
|
+
problem ? `<p class="wb-settings-field-problem" id="${fid}-problem" role="alert">${esc(settingsWord(problem.word, lang, problem.values, t))}</p>` : "",
|
|
84
|
+
`<p class="wb-settings-source" id="${fid}-source">${esc(source(s, lang, t, when))}</p>`,
|
|
85
|
+
carryNote(s, lang, t),
|
|
86
|
+
`<div class="wb-settings-actions">`,
|
|
87
|
+
`<button class="wb-settings-save" type="submit" name="action" value="save">${esc(settingsWord("save", lang, {}, t))}</button>`,
|
|
88
|
+
s.from === "owner" ? `<button class="wb-settings-put-back" type="submit" name="action" value="put_back" formnovalidate>${esc(settingsWord("put_back", lang, {}, t))}</button>` : "",
|
|
89
|
+
`</div></form>`,
|
|
90
|
+
];
|
|
91
|
+
if (s.pending) {
|
|
92
|
+
const cid = `${fid}-code`;
|
|
93
|
+
const codeProblem = mine?.problem && mine.problem.word.startsWith("code.") ? mine.problem : undefined;
|
|
94
|
+
parts.push(`<form class="wb-settings-pending" method="post" action="${esc(o.endpoint)}">`, hidden, `<p class="wb-settings-pending-note">${esc(settingsWord("pending.sent", lang, { value: String(s.pending.value) }, t))}</p>`, `<label class="wb-settings-label" for="${cid}">${esc(settingsWord("pending.code", lang, {}, t))}</label>`, `<input class="wb-settings-input wb-settings-code" id="${cid}" name="code" type="text" inputmode="numeric" autocomplete="one-time-code" pattern="[0-9]{6}" maxlength="6" required${codeProblem ? ` aria-invalid="true" aria-describedby="${cid}-problem"` : ""}>`, codeProblem ? `<p class="wb-settings-field-problem" id="${cid}-problem" role="alert">${esc(settingsWord(codeProblem.word, lang, codeProblem.values, t))}</p>` : "", `<div class="wb-settings-actions"><button class="wb-settings-save" type="submit" name="action" value="confirm">${esc(settingsWord("pending.confirm", lang, {}, t))}</button>`, `<button class="wb-settings-cancel" type="submit" name="action" value="cancel" formnovalidate>${esc(settingsWord("pending.cancel", lang, {}, t))}</button></div>`, `</form>`);
|
|
95
|
+
}
|
|
96
|
+
return `<div class="wb-settings-setting">${parts.join("")}</div>`;
|
|
97
|
+
}
|
|
98
|
+
function historyTable(rows, tools, lang, t, when) {
|
|
99
|
+
const head = `<h2 class="wb-settings-heading">${esc(settingsWord("history.title", lang, {}, t))}</h2>`;
|
|
100
|
+
if (!rows.length)
|
|
101
|
+
return `<section class="wb-settings-history">${head}<p class="wb-settings-empty">${esc(settingsWord("history.empty", lang, {}, t))}</p></section>`;
|
|
102
|
+
const label = (tool, key) => {
|
|
103
|
+
const d = tools.find((x) => x.tool === tool)?.settings.find((x) => x.key === key);
|
|
104
|
+
return d ? phrase(d.label, lang) : `${tool}.${key}`;
|
|
105
|
+
};
|
|
106
|
+
const defOf = (tool, key) => tools.find((x) => x.tool === tool)?.settings.find((x) => x.key === key);
|
|
107
|
+
const th = (k) => `<th class="wb-settings-th" scope="col">${esc(settingsWord(k, lang, {}, t))}</th>`;
|
|
108
|
+
const notSet = settingsWord("history.not_set", lang, {}, t);
|
|
109
|
+
const body = rows
|
|
110
|
+
.map((r) => {
|
|
111
|
+
const d = defOf(r.tool, r.key);
|
|
112
|
+
const from = r.before === null ? notSet : shown(r.before, lang, t, d);
|
|
113
|
+
const change = r.action === "put_back" ? `${settingsWord("history.put_back", lang, {}, t)} (${from})` : `${from} → ${shown(r.after, lang, t, d)}`;
|
|
114
|
+
return `<tr class="wb-settings-row"><td class="wb-settings-td">${esc(when(r.at))}</td><td class="wb-settings-td">${esc(r.by)}</td><td class="wb-settings-td">${esc(label(r.tool, r.key))}</td><td class="wb-settings-td">${esc(change)}</td></tr>`;
|
|
115
|
+
})
|
|
116
|
+
.join("");
|
|
117
|
+
return `<section class="wb-settings-history">${head}<table class="wb-settings-table"><thead><tr>${th("history.col.when")}${th("history.col.who")}${th("history.col.what")}${th("history.col.change")}</tr></thead><tbody>${body}</tbody></table></section>`;
|
|
118
|
+
}
|
|
119
|
+
/** The page's body, for the app's admin shell. */
|
|
120
|
+
export async function renderSettingsPage(settings, o) {
|
|
121
|
+
return drawSettingsPage(settings.tools, await settings.states(), o);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Draws the page from settings already read: the same body as
|
|
125
|
+
* renderSettingsPage, for samples and previews with made-up states.
|
|
126
|
+
*/
|
|
127
|
+
export function drawSettingsPage(tools, states, o) {
|
|
128
|
+
const lang = o.lang ?? "en";
|
|
129
|
+
const t = o.t;
|
|
130
|
+
const when = o.when ?? defaultWhen(o.timeZone ?? "Australia/Sydney", lang);
|
|
131
|
+
const top = o.outcome?.notice
|
|
132
|
+
? `<p class="wb-settings-notice" role="status">${esc(settingsWord(o.outcome.notice.word, lang, o.outcome.notice.values, t))}</p>`
|
|
133
|
+
: o.outcome?.status === 503 && o.outcome.problem
|
|
134
|
+
? `<p class="wb-settings-problem" role="alert">${esc(settingsWord(o.outcome.problem.word, lang, o.outcome.problem.values, t))}</p>`
|
|
135
|
+
: "";
|
|
136
|
+
const sections = tools
|
|
137
|
+
.map((tool) => {
|
|
138
|
+
const items = states.filter((s) => s.tool === tool.tool).map((s) => item(s, o, lang, when));
|
|
139
|
+
return [
|
|
140
|
+
`<section class="wb-settings-section" aria-labelledby="wb-settings-${esc(tool.tool)}">`,
|
|
141
|
+
`<h2 class="wb-settings-heading" id="wb-settings-${esc(tool.tool)}">${esc(phrase(tool.title, lang))}</h2>`,
|
|
142
|
+
tool.about ? `<p class="wb-settings-about">${esc(phrase(tool.about, lang))}</p>` : "",
|
|
143
|
+
items.join(""),
|
|
144
|
+
`</section>`,
|
|
145
|
+
].join("");
|
|
146
|
+
})
|
|
147
|
+
.join("");
|
|
148
|
+
return [
|
|
149
|
+
`<div class="wb-settings">`,
|
|
150
|
+
`<h1 class="wb-settings-title">${esc(settingsWord("title", lang, {}, t))}</h1>`,
|
|
151
|
+
`<p class="wb-settings-intro">${esc(settingsWord("intro", lang, {}, t))}</p>`,
|
|
152
|
+
top,
|
|
153
|
+
sections,
|
|
154
|
+
o.history ? historyTable(o.history, tools, lang, t, when) : "",
|
|
155
|
+
`</div>`,
|
|
156
|
+
].join("");
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Does what one of the page's forms asked: save, put back or confirm a code.
|
|
160
|
+
* Give it the posted fields and the signed-in admin's name or email (`by`),
|
|
161
|
+
* after the app's own sign-in and admin check. Render the page again with
|
|
162
|
+
* the outcome (renderSettingsPage's `outcome`) and its status.
|
|
163
|
+
*/
|
|
164
|
+
export async function settingsAction(settings, fields, by, opts = {}) {
|
|
165
|
+
const get = (k) => (fields instanceof URLSearchParams ? fields.get(k) : fields[k]) ?? "";
|
|
166
|
+
const tool = get("tool");
|
|
167
|
+
const key = get("key");
|
|
168
|
+
const action = get("action");
|
|
169
|
+
const def = settings.tools.find((x) => x.tool === tool)?.settings.find((x) => x.key === key);
|
|
170
|
+
if (!def || !["save", "put_back", "confirm", "cancel"].includes(action))
|
|
171
|
+
return { status: 422, problem: { word: "problem.text" } };
|
|
172
|
+
const at = { tool, key };
|
|
173
|
+
try {
|
|
174
|
+
if (action === "put_back") {
|
|
175
|
+
await settings.putBack(tool, key, by);
|
|
176
|
+
return { status: 200, ...at, notice: { word: "put_back.done" } };
|
|
177
|
+
}
|
|
178
|
+
if (action === "cancel") {
|
|
179
|
+
await settings.cancel(tool, key);
|
|
180
|
+
return { status: 200, ...at, notice: { word: "pending.cancelled" } };
|
|
181
|
+
}
|
|
182
|
+
if (action === "confirm") {
|
|
183
|
+
const r = await settings.confirm(tool, key, get("code"), by);
|
|
184
|
+
if (r.status === "saved")
|
|
185
|
+
return { status: 200, ...at, notice: { word: "pending.confirmed", values: { value: String(r.value) } } };
|
|
186
|
+
if (r.status === "wrong")
|
|
187
|
+
return { status: 422, ...at, problem: { word: "code.wrong", values: { left: String(r.left) } } };
|
|
188
|
+
return { status: 422, ...at, problem: { word: `code.${r.status}` } };
|
|
189
|
+
}
|
|
190
|
+
const typed = get("value");
|
|
191
|
+
const r = await settings.save(tool, key, typed, by, opts);
|
|
192
|
+
if (r.status === "pending")
|
|
193
|
+
return { status: 200, ...at, notice: { word: "pending.sent", values: { value: String(r.value) } } };
|
|
194
|
+
if (r.status === "unchanged")
|
|
195
|
+
return { status: 200, ...at, notice: { word: "unchanged" } };
|
|
196
|
+
if (r.status === "saved" && r.value === null)
|
|
197
|
+
return { status: 200, ...at, notice: { word: "put_back.done" } };
|
|
198
|
+
return { status: 200, ...at, notice: { word: "saved" } };
|
|
199
|
+
}
|
|
200
|
+
catch (e) {
|
|
201
|
+
if (e instanceof SettingsError && e.code === "bad_value") {
|
|
202
|
+
const c = checkValue(def, get("value"));
|
|
203
|
+
const problem = c.ok
|
|
204
|
+
? { word: "problem.text" }
|
|
205
|
+
: { word: c.problem, ...(c.words ? { values: { message: phrase(c.words, opts.lang ?? "en") } } : c.values ? { values: c.values } : {}) };
|
|
206
|
+
return { status: 422, ...at, typed: get("value"), problem };
|
|
207
|
+
}
|
|
208
|
+
const owned = { too_soon: "problem.too_soon", too_many: "problem.too_many", send_failed: "problem.send_failed", mailer_needed: "problem.mailer_needed" };
|
|
209
|
+
const word = e instanceof SettingsError ? owned[e.code] : undefined;
|
|
210
|
+
if (word)
|
|
211
|
+
return { status: 422, ...at, typed: get("value"), problem: { word } };
|
|
212
|
+
console.warn(JSON.stringify({ event: "settings_save_failed", tool, key, code: e instanceof SettingsError ? e.code : "unknown" }));
|
|
213
|
+
return { status: 503, ...at, problem: { word: "problem.failed" } };
|
|
214
|
+
}
|
|
215
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** One sample: a part of the tool, or one of its layouts. */
|
|
2
|
+
interface ToolSample {
|
|
3
|
+
id: string;
|
|
4
|
+
title: string;
|
|
5
|
+
layout?: string;
|
|
6
|
+
html: () => string;
|
|
7
|
+
}
|
|
8
|
+
/** Every sample of one tool (workbench-ui's ToolSamples). */
|
|
9
|
+
export interface ToolSamples {
|
|
10
|
+
format: 1;
|
|
11
|
+
package: string;
|
|
12
|
+
title: string;
|
|
13
|
+
prefix: string;
|
|
14
|
+
samples: readonly ToolSample[];
|
|
15
|
+
}
|
|
16
|
+
/** Every sample of the Settings page. */
|
|
17
|
+
export declare const SETTINGS_SAMPLES: ToolSamples;
|
|
18
|
+
export {};
|