ekwo-os 0.25.2 → 0.26.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/README.md +8 -1
- package/dist/assets/expected-objects.json +324 -4
- package/dist/assets/migrations/20261107100000_a_control_account_reads_contact_by_contact.sql +269 -0
- package/dist/assets/migrations/20261108120000_an_electronic_invoice_arrives_by_the_same_door.sql +150 -0
- package/dist/assets/migrations/20261108152128_a_bank_account_keeps_the_profile_of_its_csv_statements.sql +26 -0
- package/dist/assets/migrations/20261110124117_the_socle_policies_ask_once.sql +177 -0
- package/dist/assets/migrations/20261115070000_a_financial_year_opens_on_any_month.sql +86 -0
- package/dist/assets/migrations/20261115080000_the_portfolio_reads_what_is_overdue.sql +119 -0
- package/dist/assets/migrations/20261115100000_a_declaration_has_a_short_name.sql +256 -0
- package/dist/assets/migrations/20261118144012_a_quote_carries_its_answer.sql +397 -0
- package/dist/assets/migrations/20261118170000_schema_version_0_26_0.sql +49 -0
- package/dist/assets/modules/budgets/supabase/migrations/20261109153212_budgets_policies_ask_once.sql +93 -0
- package/dist/assets/modules/consolidation/supabase/migrations/20261109154107_consolidation_policies_ask_once.sql +93 -0
- package/dist/assets/modules/contracts/supabase/migrations/20261109154842_contracts_policies_ask_once.sql +93 -0
- package/dist/assets/modules/corporate-tax/supabase/migrations/20261109160318_tax_policies_ask_once.sql +93 -0
- package/dist/assets/modules/crm/supabase/migrations/20261109120000_crm_reads_hold_at_volume.sql +95 -0
- package/dist/assets/modules/crm/supabase/migrations/20261115090000_crm_contact_vcard.sql +159 -0
- package/dist/assets/modules/crm/supabase/migrations/20261118145000_crm_deal_documents.sql +569 -0
- package/dist/assets/modules/einvoicing/supabase/migrations/20261109155021_einvoicing_policies_ask_once.sql +93 -0
- package/dist/assets/modules/fixed-assets/supabase/migrations/20261109155512_assets_policies_ask_once.sql +93 -0
- package/dist/assets/seed/00_currencies.sql +5 -0
- package/dist/assets/seed/00_territories.sql +57 -0
- package/dist/assets/seed/10_pack_be.sql +1 -1
- package/dist/assets/seed/11_pack_fr.sql +1 -1
- package/dist/assets/seed/13_pack_ee.sql +9 -4
- package/dist/assets/seed/15_pack_us.sql +1 -1
- package/dist/assets/seed/220_pack_ad.sql +666 -0
- package/dist/assets/seed/224_pack_pw.sql +642 -0
- package/dist/assets/seed/226_pack_sb.sql +614 -0
- package/dist/assets/seed/43_pack_pt.sql +14 -5
- package/dist/assets/seed/500_pack_nr.sql +468 -0
- package/dist/assets/seed/61_pack_se.sql +9 -5
- package/dist/assets/seed/64_pack_pl.sql +5 -2
- package/dist/assets/seed/65_pack_dk.sql +7 -4
- package/dist/assets/seed/67_pack_fi.sql +7 -2
- package/dist/assets/seed/68_pack_cz.sql +8 -2
- package/dist/assets/seed/70_pack_hu.sql +11 -7
- package/dist/assets/seed/72_pack_ro.sql +11 -7
- package/dist/cli.d.ts +1 -1
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +44 -4
- package/dist/cli.js.map +1 -1
- package/dist/commands/bank.d.ts +16 -0
- package/dist/commands/bank.d.ts.map +1 -0
- package/dist/commands/bank.js +82 -0
- package/dist/commands/bank.js.map +1 -0
- package/dist/commands/close.d.ts +33 -0
- package/dist/commands/close.d.ts.map +1 -0
- package/dist/commands/close.js +213 -0
- package/dist/commands/close.js.map +1 -0
- package/dist/commands/crm.d.ts +5 -4
- package/dist/commands/crm.d.ts.map +1 -1
- package/dist/commands/crm.js +70 -5
- package/dist/commands/crm.js.map +1 -1
- package/dist/commands/document.d.ts +2 -1
- package/dist/commands/document.d.ts.map +1 -1
- package/dist/commands/document.js +58 -4
- package/dist/commands/document.js.map +1 -1
- package/dist/commands/import.d.ts +3 -0
- package/dist/commands/import.d.ts.map +1 -1
- package/dist/commands/import.js +81 -6
- package/dist/commands/import.js.map +1 -1
- package/dist/company-choices.d.ts +2 -1
- package/dist/company-choices.d.ts.map +1 -1
- package/dist/company-choices.js +2 -1
- package/dist/company-choices.js.map +1 -1
- package/dist/inventory.d.ts.map +1 -1
- package/dist/inventory.js +8 -0
- package/dist/inventory.js.map +1 -1
- package/dist/output.d.ts.map +1 -1
- package/dist/output.js +5 -2
- package/dist/output.js.map +1 -1
- package/dist/pack/certification.js +1 -1
- package/dist/pack/certification.js.map +1 -1
- package/dist/pack/compile.js +10 -2
- package/dist/pack/compile.js.map +1 -1
- package/dist/pack/describe.d.ts +3 -1
- package/dist/pack/describe.d.ts.map +1 -1
- package/dist/pack/describe.js +3 -2
- package/dist/pack/describe.js.map +1 -1
- package/dist/pack/read.d.ts +7 -0
- package/dist/pack/read.d.ts.map +1 -1
- package/dist/pack/read.js +1 -0
- package/dist/pack/read.js.map +1 -1
- package/package.json +2 -2
- package/schema/output.1.json +105 -0
|
@@ -0,0 +1,397 @@
|
|
|
1
|
+
-- Ekwo OS — a quote carries its answer, as dated facts.
|
|
2
|
+
--
|
|
3
|
+
-- A `sale_quote` and a `purchase_order` are documents that are never booked:
|
|
4
|
+
-- `post_document()` refuses them (`document_not_accountable`). What happens to
|
|
5
|
+
-- one is that it is sent, and that the other party accepts it or declines
|
|
6
|
+
-- it. `sent_at` already said the first; nothing said the second, so a company
|
|
7
|
+
-- that issues quotes could not tell an accepted one from a draft.
|
|
8
|
+
--
|
|
9
|
+
-- **Facts, not a status.** Three columns, each a fact somebody records:
|
|
10
|
+
-- `accepted_at` and `declined_at`, when the other party answered, and
|
|
11
|
+
-- `accepted_by`, who accepted, in the words of whoever recorded it. Never both
|
|
12
|
+
-- dates at once, and only on the two kinds of document an answer is given
|
|
13
|
+
-- to — checks of the table, so no path writes anything else. `due_date` stays
|
|
14
|
+
-- what it is on a quote: the day it is valid until. Nothing is written to the
|
|
15
|
+
-- ledger.
|
|
16
|
+
--
|
|
17
|
+
-- **The status is read, never stored.** `quote_status(document)` derives it
|
|
18
|
+
-- from the facts, and every reader asks it — `quote_statuses()` for a list of
|
|
19
|
+
-- documents, the `crm` module for the documents of a deal, PostgREST as a
|
|
20
|
+
-- computed field: `draft`, then `sent` once `sent_at` is set, `accepted` or
|
|
21
|
+
-- `declined` once answered; `expired` when it is neither accepted nor declined
|
|
22
|
+
-- and its `due_date` has passed; `cancelled` stays the document's state. Null
|
|
23
|
+
-- for a document no answer is given to. One function, so no screen works the
|
|
24
|
+
-- rule out again.
|
|
25
|
+
--
|
|
26
|
+
-- **One function per fact.** `accept_document()`, `decline_document()` and
|
|
27
|
+
-- `undo_document_answer()` write them, check what can be refused by name, and
|
|
28
|
+
-- record the act on the audit trail. They are `security definer`, as
|
|
29
|
+
-- `approve_document()` is, because the trail is written by the schema and by
|
|
30
|
+
-- nobody else; they ask what the policy of `documents` would — `documents.read`
|
|
31
|
+
-- to see the document at all, `documents.write` to change it, the right that
|
|
32
|
+
-- writes a draft, which a quote always is.
|
|
33
|
+
--
|
|
34
|
+
-- **An agent answers in the mode its company chose.** The rule that holds an
|
|
35
|
+
-- agent's posting (decision 0089) holds its answer too, on the same trigger:
|
|
36
|
+
-- `documents_posting_mode` now also fires when `accepted_at` or `declined_at`
|
|
37
|
+
-- moves. In autopilot the agent records the answer; in review it needs the
|
|
38
|
+
-- approval of a person of the document as it stands (`approve_document()`),
|
|
39
|
+
-- `approval_required` otherwise; in a `review_only` company a session in
|
|
40
|
+
-- autopilot is refused, `autopilot_forbidden`. Every answer by an agent writes
|
|
41
|
+
-- `document_answered_by_agent` on the trail, as a posting writes
|
|
42
|
+
-- `document_posted_by_agent`. One rule, one trigger, never a second path.
|
|
43
|
+
--
|
|
44
|
+
-- **The socle knows no module.** What an answer means for a deal is the
|
|
45
|
+
-- `crm` module's business, in its own schema; this file says nothing of it.
|
|
46
|
+
|
|
47
|
+
-- ---------------------------------------------------------------------------
|
|
48
|
+
-- 1. The facts
|
|
49
|
+
-- ---------------------------------------------------------------------------
|
|
50
|
+
|
|
51
|
+
alter table ekwo.documents
|
|
52
|
+
add column if not exists accepted_at timestamptz,
|
|
53
|
+
add column if not exists accepted_by text,
|
|
54
|
+
add column if not exists declined_at timestamptz;
|
|
55
|
+
|
|
56
|
+
alter table ekwo.documents
|
|
57
|
+
drop constraint if exists documents_answered_once;
|
|
58
|
+
alter table ekwo.documents
|
|
59
|
+
add constraint documents_answered_once
|
|
60
|
+
check (accepted_at is null or declined_at is null);
|
|
61
|
+
|
|
62
|
+
alter table ekwo.documents
|
|
63
|
+
drop constraint if exists documents_answer_of_an_offer;
|
|
64
|
+
alter table ekwo.documents
|
|
65
|
+
add constraint documents_answer_of_an_offer
|
|
66
|
+
check ((accepted_at is null and declined_at is null and accepted_by is null)
|
|
67
|
+
or doc_type in ('sale_quote', 'purchase_order'));
|
|
68
|
+
|
|
69
|
+
alter table ekwo.documents
|
|
70
|
+
drop constraint if exists documents_accepted_by_on_acceptance;
|
|
71
|
+
alter table ekwo.documents
|
|
72
|
+
add constraint documents_accepted_by_on_acceptance
|
|
73
|
+
check (accepted_by is null or (accepted_at is not null and char_length(btrim(accepted_by)) between 1 and 200));
|
|
74
|
+
|
|
75
|
+
comment on column ekwo.documents.accepted_at is
|
|
76
|
+
'When the other party accepted this quote or order. Written by accept_document(), cleared by undo_document_answer(); never set together with declined_at, and only on a sale_quote or a purchase_order. Nothing is booked.';
|
|
77
|
+
comment on column ekwo.documents.accepted_by is
|
|
78
|
+
'Who accepted it, in the words of whoever recorded it: a name, a role, a reference to a signed copy. Free text, set only with accepted_at.';
|
|
79
|
+
comment on column ekwo.documents.declined_at is
|
|
80
|
+
'When the other party declined this quote or order. Written by decline_document(), cleared by undo_document_answer(); never set together with accepted_at, and only on a sale_quote or a purchase_order.';
|
|
81
|
+
|
|
82
|
+
-- ---------------------------------------------------------------------------
|
|
83
|
+
-- 2. The status, derived
|
|
84
|
+
-- ---------------------------------------------------------------------------
|
|
85
|
+
|
|
86
|
+
create or replace function ekwo.quote_status(p_document ekwo.documents)
|
|
87
|
+
returns text
|
|
88
|
+
language sql
|
|
89
|
+
stable
|
|
90
|
+
as $$
|
|
91
|
+
select case
|
|
92
|
+
when p_document.doc_type not in ('sale_quote', 'purchase_order') then null
|
|
93
|
+
when p_document.state = 'cancelled' then 'cancelled'
|
|
94
|
+
when p_document.accepted_at is not null then 'accepted'
|
|
95
|
+
when p_document.declined_at is not null then 'declined'
|
|
96
|
+
when p_document.due_date < current_date then 'expired'
|
|
97
|
+
when p_document.sent_at is not null then 'sent'
|
|
98
|
+
else 'draft'
|
|
99
|
+
end;
|
|
100
|
+
$$;
|
|
101
|
+
|
|
102
|
+
comment on function ekwo.quote_status(ekwo.documents) is
|
|
103
|
+
'The status of a quote or an order, derived from its facts and stored nowhere: cancelled when the document is; accepted (accepted_at) or declined (declined_at) once the other party answered; expired when it is neither and its due_date, the day it is valid until, has passed; sent once sent_at is set; draft otherwise. Null for a document no answer is given to. The one source every reader asks — quote_statuses(), the crm module, PostgREST as the computed field quote_status.';
|
|
104
|
+
|
|
105
|
+
create or replace function ekwo.quote_statuses(p_document_ids uuid[])
|
|
106
|
+
returns table (document_id uuid, quote_status text)
|
|
107
|
+
language sql
|
|
108
|
+
stable
|
|
109
|
+
security invoker
|
|
110
|
+
set search_path = ekwo, pg_temp
|
|
111
|
+
as $$
|
|
112
|
+
select d.id, ekwo.quote_status(d)
|
|
113
|
+
from ekwo.documents d
|
|
114
|
+
where d.id = any (p_document_ids)
|
|
115
|
+
and d.doc_type in ('sale_quote', 'purchase_order');
|
|
116
|
+
$$;
|
|
117
|
+
|
|
118
|
+
comment on function ekwo.quote_statuses(uuid[]) is
|
|
119
|
+
'quote_status() of each quote and order among the documents named, one row each; a document of another kind, or one the caller cannot read, is absent. What a client reads beside a list of documents, so the status is worked out once, here. Security invoker.';
|
|
120
|
+
|
|
121
|
+
-- ---------------------------------------------------------------------------
|
|
122
|
+
-- 3. The three facts, one function each
|
|
123
|
+
-- ---------------------------------------------------------------------------
|
|
124
|
+
|
|
125
|
+
-- The document an answer is recorded on, or the refusal by name.
|
|
126
|
+
create or replace function ekwo.document_to_answer(p_document_id uuid)
|
|
127
|
+
returns ekwo.documents
|
|
128
|
+
language plpgsql
|
|
129
|
+
stable
|
|
130
|
+
security invoker
|
|
131
|
+
set search_path = ekwo, pg_temp
|
|
132
|
+
as $$
|
|
133
|
+
declare
|
|
134
|
+
v_doc ekwo.documents%rowtype;
|
|
135
|
+
begin
|
|
136
|
+
select * into v_doc from ekwo.documents d where d.id = p_document_id;
|
|
137
|
+
if v_doc.id is null
|
|
138
|
+
or not (is_installer() or has_capability(v_doc.company_id, 'documents.read')) then
|
|
139
|
+
raise exception 'unknown_document: no document % in the companies you can see', p_document_id
|
|
140
|
+
using errcode = 'P0002';
|
|
141
|
+
end if;
|
|
142
|
+
if not is_installer() and not has_capability(v_doc.company_id, 'documents.write') then
|
|
143
|
+
raise exception 'not_allowed: recording the answer to a document needs documents.write'
|
|
144
|
+
using errcode = '42501';
|
|
145
|
+
end if;
|
|
146
|
+
if v_doc.doc_type not in ('sale_quote', 'purchase_order') then
|
|
147
|
+
raise exception 'document_not_answerable: a % is not accepted or declined; an answer is recorded on a sale_quote or a purchase_order', v_doc.doc_type
|
|
148
|
+
using errcode = '22023';
|
|
149
|
+
end if;
|
|
150
|
+
if v_doc.state = 'cancelled' then
|
|
151
|
+
raise exception 'document_cancelled: % % is cancelled, and a cancelled document is answered no more', v_doc.doc_type, coalesce(v_doc.number, v_doc.id::text)
|
|
152
|
+
using errcode = '55006';
|
|
153
|
+
end if;
|
|
154
|
+
return v_doc;
|
|
155
|
+
end;
|
|
156
|
+
$$;
|
|
157
|
+
|
|
158
|
+
comment on function ekwo.document_to_answer(uuid) is
|
|
159
|
+
'The document an answer is recorded on, or the refusal by name: unknown_document (absent, or outside the companies where the caller holds documents.read), not_allowed (documents.write), document_not_answerable (neither a sale_quote nor a purchase_order), document_cancelled. The first lines of accept_document(), decline_document() and undo_document_answer().';
|
|
160
|
+
|
|
161
|
+
create or replace function ekwo.accept_document(
|
|
162
|
+
p_document_id uuid,
|
|
163
|
+
p_accepted_by text default null,
|
|
164
|
+
p_at timestamptz default null
|
|
165
|
+
)
|
|
166
|
+
returns ekwo.documents
|
|
167
|
+
language plpgsql
|
|
168
|
+
security definer
|
|
169
|
+
set search_path = ekwo, pg_temp
|
|
170
|
+
as $$
|
|
171
|
+
declare
|
|
172
|
+
v_doc ekwo.documents%rowtype := ekwo.document_to_answer(p_document_id);
|
|
173
|
+
v_new ekwo.documents%rowtype;
|
|
174
|
+
begin
|
|
175
|
+
if v_doc.declined_at is not null then
|
|
176
|
+
raise exception 'document_already_answered: % % was declined on %. Undo that answer first (undo_document_answer(), ekwo doc answer undo).',
|
|
177
|
+
v_doc.doc_type, coalesce(v_doc.number, v_doc.id::text), v_doc.declined_at::date
|
|
178
|
+
using errcode = '55006';
|
|
179
|
+
end if;
|
|
180
|
+
if v_doc.accepted_at is not null then
|
|
181
|
+
-- Recorded already: the same call twice records it once.
|
|
182
|
+
return v_doc;
|
|
183
|
+
end if;
|
|
184
|
+
if p_at is not null and p_at > now() then
|
|
185
|
+
raise exception 'answer_in_the_future: an answer is recorded once it is given, and % has not come yet', p_at
|
|
186
|
+
using errcode = '22023';
|
|
187
|
+
end if;
|
|
188
|
+
|
|
189
|
+
update ekwo.documents d
|
|
190
|
+
set accepted_at = coalesce(p_at, now()),
|
|
191
|
+
accepted_by = nullif(btrim(p_accepted_by), '')
|
|
192
|
+
where d.id = v_doc.id
|
|
193
|
+
returning * into v_new;
|
|
194
|
+
|
|
195
|
+
perform audit_record(
|
|
196
|
+
v_new.company_id, 'documents', v_new.id, coalesce(v_new.number, v_new.id::text),
|
|
197
|
+
'update', 'document_accepted', null,
|
|
198
|
+
jsonb_strip_nulls(jsonb_build_object('accepted_at', v_new.accepted_at, 'accepted_by', v_new.accepted_by)));
|
|
199
|
+
return v_new;
|
|
200
|
+
end;
|
|
201
|
+
$$;
|
|
202
|
+
|
|
203
|
+
comment on function ekwo.accept_document(uuid, text, timestamptz) is
|
|
204
|
+
'Records that the other party accepted a sale_quote or a purchase_order: accepted_at (p_at, now when not given, never in the future) and accepted_by (free text). The same call on an accepted document returns it as it is; a declined one is refused, document_already_answered — undo_document_answer() first. Writes document_accepted on the audit trail; books nothing. Needs documents.write; an agent records it in the mode of its company, as it posts (documents_posting_mode).';
|
|
205
|
+
|
|
206
|
+
create or replace function ekwo.decline_document(
|
|
207
|
+
p_document_id uuid,
|
|
208
|
+
p_at timestamptz default null
|
|
209
|
+
)
|
|
210
|
+
returns ekwo.documents
|
|
211
|
+
language plpgsql
|
|
212
|
+
security definer
|
|
213
|
+
set search_path = ekwo, pg_temp
|
|
214
|
+
as $$
|
|
215
|
+
declare
|
|
216
|
+
v_doc ekwo.documents%rowtype := ekwo.document_to_answer(p_document_id);
|
|
217
|
+
v_new ekwo.documents%rowtype;
|
|
218
|
+
begin
|
|
219
|
+
if v_doc.accepted_at is not null then
|
|
220
|
+
raise exception 'document_already_answered: % % was accepted on %. Undo that answer first (undo_document_answer(), ekwo doc answer undo).',
|
|
221
|
+
v_doc.doc_type, coalesce(v_doc.number, v_doc.id::text), v_doc.accepted_at::date
|
|
222
|
+
using errcode = '55006';
|
|
223
|
+
end if;
|
|
224
|
+
if v_doc.declined_at is not null then
|
|
225
|
+
return v_doc;
|
|
226
|
+
end if;
|
|
227
|
+
if p_at is not null and p_at > now() then
|
|
228
|
+
raise exception 'answer_in_the_future: an answer is recorded once it is given, and % has not come yet', p_at
|
|
229
|
+
using errcode = '22023';
|
|
230
|
+
end if;
|
|
231
|
+
|
|
232
|
+
update ekwo.documents d
|
|
233
|
+
set declined_at = coalesce(p_at, now())
|
|
234
|
+
where d.id = v_doc.id
|
|
235
|
+
returning * into v_new;
|
|
236
|
+
|
|
237
|
+
perform audit_record(
|
|
238
|
+
v_new.company_id, 'documents', v_new.id, coalesce(v_new.number, v_new.id::text),
|
|
239
|
+
'update', 'document_declined', null,
|
|
240
|
+
jsonb_build_object('declined_at', v_new.declined_at));
|
|
241
|
+
return v_new;
|
|
242
|
+
end;
|
|
243
|
+
$$;
|
|
244
|
+
|
|
245
|
+
comment on function ekwo.decline_document(uuid, timestamptz) is
|
|
246
|
+
'Records that the other party declined a sale_quote or a purchase_order: declined_at (p_at, now when not given, never in the future). The same call on a declined document returns it as it is; an accepted one is refused, document_already_answered — undo_document_answer() first. Writes document_declined on the audit trail; books nothing. Needs documents.write; an agent records it in the mode of its company, as it posts (documents_posting_mode).';
|
|
247
|
+
|
|
248
|
+
create or replace function ekwo.undo_document_answer(p_document_id uuid)
|
|
249
|
+
returns ekwo.documents
|
|
250
|
+
language plpgsql
|
|
251
|
+
security definer
|
|
252
|
+
set search_path = ekwo, pg_temp
|
|
253
|
+
as $$
|
|
254
|
+
declare
|
|
255
|
+
v_doc ekwo.documents%rowtype := ekwo.document_to_answer(p_document_id);
|
|
256
|
+
v_new ekwo.documents%rowtype;
|
|
257
|
+
begin
|
|
258
|
+
if v_doc.accepted_at is null and v_doc.declined_at is null then
|
|
259
|
+
raise exception 'document_not_answered: % % carries no answer to undo', v_doc.doc_type, coalesce(v_doc.number, v_doc.id::text)
|
|
260
|
+
using errcode = '55006';
|
|
261
|
+
end if;
|
|
262
|
+
|
|
263
|
+
update ekwo.documents d
|
|
264
|
+
set accepted_at = null,
|
|
265
|
+
accepted_by = null,
|
|
266
|
+
declined_at = null
|
|
267
|
+
where d.id = v_doc.id
|
|
268
|
+
returning * into v_new;
|
|
269
|
+
|
|
270
|
+
perform audit_record(
|
|
271
|
+
v_new.company_id, 'documents', v_new.id, coalesce(v_new.number, v_new.id::text),
|
|
272
|
+
'update', 'document_answer_undone',
|
|
273
|
+
jsonb_strip_nulls(jsonb_build_object('accepted_at', v_doc.accepted_at, 'accepted_by', v_doc.accepted_by,
|
|
274
|
+
'declined_at', v_doc.declined_at)),
|
|
275
|
+
null);
|
|
276
|
+
return v_new;
|
|
277
|
+
end;
|
|
278
|
+
$$;
|
|
279
|
+
|
|
280
|
+
comment on function ekwo.undo_document_answer(uuid) is
|
|
281
|
+
'Takes back the answer recorded on a sale_quote or a purchase_order — accepted_at, accepted_by, declined_at back to null — and writes document_answer_undone on the audit trail with what it was. Refuses a document with no answer, document_not_answered. Needs documents.write; an agent undoes it in the mode of its company, as it posts (documents_posting_mode).';
|
|
282
|
+
|
|
283
|
+
-- ---------------------------------------------------------------------------
|
|
284
|
+
-- 4. An agent answers in the mode its company chose: the posting's rule
|
|
285
|
+
--
|
|
286
|
+
-- `20261023174709` as it was, holding one more act. The trigger fires on the
|
|
287
|
+
-- posting of a draft, as before, and now on a change of accepted_at or
|
|
288
|
+
-- declined_at; the mode, the approval of the document as it stands and the
|
|
289
|
+
-- record on the trail are the same, and only the words say which act it is.
|
|
290
|
+
-- ---------------------------------------------------------------------------
|
|
291
|
+
|
|
292
|
+
create or replace function ekwo.documents_posting_mode()
|
|
293
|
+
returns trigger
|
|
294
|
+
language plpgsql
|
|
295
|
+
security definer
|
|
296
|
+
set search_path = ekwo, pg_temp
|
|
297
|
+
as $$
|
|
298
|
+
declare
|
|
299
|
+
v_posting boolean := new.state = 'posted' and old.state = 'draft';
|
|
300
|
+
v_mode text;
|
|
301
|
+
v_version text;
|
|
302
|
+
v_approval ekwo.audit_log%rowtype;
|
|
303
|
+
begin
|
|
304
|
+
if not agent_call() or coalesce(current_setting('ekwo.rehearsal', true), '') = 'on' then
|
|
305
|
+
return new;
|
|
306
|
+
end if;
|
|
307
|
+
|
|
308
|
+
v_mode := agent_posting_mode(new.company_id);
|
|
309
|
+
|
|
310
|
+
if v_mode = 'autopilot_forbidden' then
|
|
311
|
+
raise exception 'autopilot_forbidden: this company''s agent_posting is review_only, so no session and no key posts in autopilot. Start the session in review (without --autopilot, or with the mark X-Ekwo-Agent: review), or use a key issued in review, and a person approves each draft (approve_document(), ekwo doc approve).'
|
|
312
|
+
using errcode = '42501';
|
|
313
|
+
end if;
|
|
314
|
+
|
|
315
|
+
if v_mode is distinct from 'autopilot' then
|
|
316
|
+
-- A trigger before the update reads the row as it still stands, so this
|
|
317
|
+
-- is the version of the draft that is being posted or answered.
|
|
318
|
+
v_version := document_version(old.id);
|
|
319
|
+
|
|
320
|
+
select a.* into v_approval
|
|
321
|
+
from ekwo.audit_log a
|
|
322
|
+
where a.table_name = 'documents'
|
|
323
|
+
and a.record_id = new.id
|
|
324
|
+
and a.action = 'document_approved'
|
|
325
|
+
and a.api_key_id is null
|
|
326
|
+
and a.new_values ->> 'version' = v_version
|
|
327
|
+
and a.id > coalesce((select max(b.id) from ekwo.audit_log b
|
|
328
|
+
where b.table_name = 'documents'
|
|
329
|
+
and b.record_id = new.id
|
|
330
|
+
and b.action = 'document_draft'), 0)
|
|
331
|
+
order by a.id desc
|
|
332
|
+
limit 1;
|
|
333
|
+
|
|
334
|
+
if v_approval.id is null then
|
|
335
|
+
if v_posting then
|
|
336
|
+
raise exception 'approval_required: % % has not been approved by a person as it stands, and agents post in review here. A person who may post it approves it (approve_document(), ekwo doc approve), or posts it; a draft changed after its approval is approved again.',
|
|
337
|
+
new.doc_type, coalesce(new.number, new.id::text)
|
|
338
|
+
using errcode = '42501';
|
|
339
|
+
end if;
|
|
340
|
+
raise exception 'approval_required: % % has not been approved by a person as it stands, and agents record an answer in review here. A person who may post it approves it (approve_document(), ekwo doc approve), or records the answer; a document changed after its approval is approved again.',
|
|
341
|
+
new.doc_type, coalesce(new.number, new.id::text)
|
|
342
|
+
using errcode = '42501';
|
|
343
|
+
end if;
|
|
344
|
+
end if;
|
|
345
|
+
|
|
346
|
+
perform audit_record(
|
|
347
|
+
new.company_id, 'documents', new.id, coalesce(new.number, new.id::text),
|
|
348
|
+
'update', case when v_posting then 'document_posted_by_agent' else 'document_answered_by_agent' end, null,
|
|
349
|
+
jsonb_strip_nulls(jsonb_build_object(
|
|
350
|
+
'posting_mode', v_mode,
|
|
351
|
+
'agent_posting', (select c.agent_posting from ekwo.companies c where c.id = new.company_id),
|
|
352
|
+
'session', agent_mark('agent_session'),
|
|
353
|
+
'version', v_version,
|
|
354
|
+
'approved_by', v_approval.actor_id,
|
|
355
|
+
'approved_at', v_approval.occurred_at,
|
|
356
|
+
'accepted_at', case when v_posting then null else new.accepted_at end,
|
|
357
|
+
'declined_at', case when v_posting then null else new.declined_at end)));
|
|
358
|
+
return new;
|
|
359
|
+
end;
|
|
360
|
+
$$;
|
|
361
|
+
|
|
362
|
+
comment on function ekwo.documents_posting_mode() is
|
|
363
|
+
'Holds an agent''s posting of a document, and its answer to a quote or an order (accepted_at, declined_at), to agent_posting_mode(): autopilot_forbidden when the key or the session is in autopilot in a company that is review_only; in review, approval_required unless a person approved the document as it stood (approve_document()) since it last went back to draft. Every posting by an agent writes document_posted_by_agent on the audit trail, every answer document_answered_by_agent: the mode, the company''s setting, the session, and in review the approval; the key is the row''s api_key_id. A rehearsal is not held to it.';
|
|
364
|
+
|
|
365
|
+
revoke execute on function ekwo.documents_posting_mode() from public, anon, authenticated, service_role;
|
|
366
|
+
|
|
367
|
+
drop trigger if exists documents_posting_mode on ekwo.documents;
|
|
368
|
+
create trigger documents_posting_mode
|
|
369
|
+
before update of state, accepted_at, declined_at on ekwo.documents
|
|
370
|
+
for each row when ((new.state = 'posted' and old.state = 'draft')
|
|
371
|
+
or new.accepted_at is distinct from old.accepted_at
|
|
372
|
+
or new.declined_at is distinct from old.declined_at)
|
|
373
|
+
execute function ekwo.documents_posting_mode();
|
|
374
|
+
|
|
375
|
+
-- ---------------------------------------------------------------------------
|
|
376
|
+
-- 5. Privileges, by name
|
|
377
|
+
-- ---------------------------------------------------------------------------
|
|
378
|
+
|
|
379
|
+
revoke execute on function ekwo.quote_status(ekwo.documents) from public, anon;
|
|
380
|
+
revoke execute on function ekwo.quote_statuses(uuid[]) from public, anon;
|
|
381
|
+
revoke execute on function ekwo.document_to_answer(uuid) from public, anon;
|
|
382
|
+
revoke execute on function ekwo.accept_document(uuid, text, timestamptz) from public, anon;
|
|
383
|
+
revoke execute on function ekwo.decline_document(uuid, timestamptz) from public, anon;
|
|
384
|
+
revoke execute on function ekwo.undo_document_answer(uuid) from public, anon;
|
|
385
|
+
|
|
386
|
+
grant execute on function ekwo.quote_status(ekwo.documents) to authenticated, service_role;
|
|
387
|
+
grant execute on function ekwo.quote_statuses(uuid[]) to authenticated, service_role;
|
|
388
|
+
grant execute on function ekwo.document_to_answer(uuid) to authenticated, service_role;
|
|
389
|
+
grant execute on function ekwo.accept_document(uuid, text, timestamptz) to authenticated, service_role;
|
|
390
|
+
grant execute on function ekwo.decline_document(uuid, timestamptz) to authenticated, service_role;
|
|
391
|
+
grant execute on function ekwo.undo_document_answer(uuid) to authenticated, service_role;
|
|
392
|
+
|
|
393
|
+
-- The revoke every file of this directory ends with, and the Data API told
|
|
394
|
+
-- the functions are there.
|
|
395
|
+
revoke execute on all functions in schema ekwo from public;
|
|
396
|
+
|
|
397
|
+
notify pgrst, 'reload schema';
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
-- Ekwo OS — the schema this release defines is 0.26.0.
|
|
2
|
+
--
|
|
3
|
+
-- `ekwo_schema_version()` is the one place the number lives, and a migration
|
|
4
|
+
-- is the only thing that moves it. A database then answers for the files it
|
|
5
|
+
-- has actually run rather than for what somebody believed it had run: the
|
|
6
|
+
-- column default and `init_instance()` both call this function, so a fresh
|
|
7
|
+
-- install records the new number without a second edit, and `ekwo migrate`
|
|
8
|
+
-- reads it back onto `instance.schema_version` for a database that was
|
|
9
|
+
-- installed at an older one.
|
|
10
|
+
--
|
|
11
|
+
-- Between 0.25.0 and here, all additive; those the packages read or call are
|
|
12
|
+
-- features of `SCHEMA_FEATURES` since this version:
|
|
13
|
+
--
|
|
14
|
+
-- - `20261107100000_a_control_account_reads_contact_by_contact`:
|
|
15
|
+
-- `subledger()` and the contacts of `general_ledger()` (`subledger`,
|
|
16
|
+
-- `ledger_by_contact`);
|
|
17
|
+
-- - `20261108120000_an_electronic_invoice_arrives_by_the_same_door`: an XML
|
|
18
|
+
-- page through `receive_purchase_document()`;
|
|
19
|
+
-- - `20261108152128_a_bank_account_keeps_the_profile_of_its_csv_statements`:
|
|
20
|
+
-- `bank_accounts.statement_profile` (`statement_profile`);
|
|
21
|
+
-- - `20261110124117_the_socle_policies_ask_once`: fifteen policies of the
|
|
22
|
+
-- socle asked once per statement, and `companies_of_caller()`;
|
|
23
|
+
-- - `20261115070000_a_financial_year_opens_on_any_month`: the twelve months
|
|
24
|
+
-- of `fiscal_year_default`;
|
|
25
|
+
-- - `20261115080000_the_portfolio_reads_what_is_overdue`:
|
|
26
|
+
-- `portfolio_overview()` (`portfolio_overview`);
|
|
27
|
+
-- - `20261115100000_a_declaration_has_a_short_name`:
|
|
28
|
+
-- `tax_report_templates.short_name` (`filing_short_name`);
|
|
29
|
+
-- - `20261118144012_a_quote_carries_its_answer`, decision 0098: the answer
|
|
30
|
+
-- of a quote and `quote_status()` (`quote_answer`).
|
|
31
|
+
--
|
|
32
|
+
-- The floor of `ekwo-os`, `@ekwo-ai/core` and `@ekwo-ai/mcp` stays at 0.19.0:
|
|
33
|
+
-- below this version a read leaves out what it adds.
|
|
34
|
+
|
|
35
|
+
create or replace function ekwo_schema_version()
|
|
36
|
+
returns text
|
|
37
|
+
language sql
|
|
38
|
+
immutable
|
|
39
|
+
as $$
|
|
40
|
+
select '0.26.0'::text;
|
|
41
|
+
$$;
|
|
42
|
+
|
|
43
|
+
comment on function ekwo_schema_version() is
|
|
44
|
+
'Schema version of the installed release. Bumped by a migration, never by hand.';
|
|
45
|
+
|
|
46
|
+
-- `create or replace` keeps the privileges the function already had, and the
|
|
47
|
+
-- revoke is what every file of this directory ends with.
|
|
48
|
+
revoke execute on all functions in schema ekwo from public;
|
|
49
|
+
grant execute on function ekwo_schema_version() to authenticated, service_role;
|
package/dist/assets/modules/budgets/supabase/migrations/20261109153212_budgets_policies_ask_once.sql
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
-- Ekwo — the `budgets` module: its policies ask once per statement, not once
|
|
2
|
+
-- per row.
|
|
3
|
+
--
|
|
4
|
+
-- Every policy of the module that guards a table of a company read
|
|
5
|
+
--
|
|
6
|
+
-- using (public.module_enabled(company_id, 'budgets')
|
|
7
|
+
-- and public.has_capability(company_id, 'budgets.…'))
|
|
8
|
+
--
|
|
9
|
+
-- With a column as its argument each function runs for every row a scan
|
|
10
|
+
-- visits, and neither is inlined: `has_capability()` is security definer, and
|
|
11
|
+
-- `module_enabled()` carries a search_path since the socle moved to its own
|
|
12
|
+
-- schema. On a company with many rows here, nearly all the time of a read is
|
|
13
|
+
-- the policy. `scripts/inventory-policies.mjs` found it on both of its tables, `budgets` and `lines`.
|
|
14
|
+
--
|
|
15
|
+
-- The `crm` module measured it and changed its own policies in
|
|
16
|
+
-- `20261109120000_crm_reads_hold_at_volume.sql`; the socle did the same for
|
|
17
|
+
-- its tables in `20260918141627_a_policy_asks_once.sql`. This file is that
|
|
18
|
+
-- change, made here: the policy compares `company_id` with the companies
|
|
19
|
+
-- where the answer is yes, worked out in a sub-select that depends on no
|
|
20
|
+
-- column — an InitPlan, run once per statement:
|
|
21
|
+
--
|
|
22
|
+
-- company_id = any ((select array(
|
|
23
|
+
-- select x from unnest(public.companies_with_capability('budgets.read')) x
|
|
24
|
+
-- where public.module_enabled(x, 'budgets')))::uuid[])
|
|
25
|
+
--
|
|
26
|
+
-- **The same question, asked the other way round.** `companies_with_capability()`
|
|
27
|
+
-- lists the companies where `has_capability()` answers yes — a test of the
|
|
28
|
+
-- socle compares the two over every preset and a machine key — and
|
|
29
|
+
-- `module_enabled()` is then asked about each of those, by name, so the
|
|
30
|
+
-- policy still routes through it as `tests/modules.test.ts` requires. A row
|
|
31
|
+
-- passes exactly when it passed before. A condition a policy adds to the two
|
|
32
|
+
-- (`and computation_id is null`) is carried over as Postgres wrote it.
|
|
33
|
+
-- `with check` is left as it was: it is evaluated once per row written, which
|
|
34
|
+
-- is what a check is for.
|
|
35
|
+
--
|
|
36
|
+
-- **Read from the catalogue.** The policies are found by their shape in
|
|
37
|
+
-- `pg_policies` rather than listed, so the capability each one names is
|
|
38
|
+
-- carried over and cannot be mistyped here. Run twice, it changes nothing the
|
|
39
|
+
-- second time. It refuses to finish if it found neither the old shape nor the
|
|
40
|
+
-- new one — the policies changed and this file has to be read again — or if
|
|
41
|
+
-- a policy of the schema still asks either function about `company_id` row by
|
|
42
|
+
-- row.
|
|
43
|
+
--
|
|
44
|
+
-- **Older files, older socle.** Names of the socle are written `public.`, as
|
|
45
|
+
-- the module's first migrations wrote them; the installer reads the file for
|
|
46
|
+
-- the socle's schema of the day, `ekwo.` since 20261028040000.
|
|
47
|
+
|
|
48
|
+
do $$
|
|
49
|
+
declare
|
|
50
|
+
v_schema constant text := 'budgets';
|
|
51
|
+
v_module constant text := 'budgets';
|
|
52
|
+
v_shape constant text :=
|
|
53
|
+
'^\((?:[a-z_]+\.)?module_enabled\(company_id, ''' || v_module || '''::text\) AND '
|
|
54
|
+
'(?:[a-z_]+\.)?has_capability\(company_id, ''([a-z_.]+)''::text\)( AND .+)?\)$';
|
|
55
|
+
r record;
|
|
56
|
+
v_changed integer := 0;
|
|
57
|
+
v_left integer;
|
|
58
|
+
begin
|
|
59
|
+
for r in
|
|
60
|
+
select p.tablename, p.policyname, m[1] as capability, coalesce(m[2], '') as rest
|
|
61
|
+
from pg_policies p
|
|
62
|
+
cross join lateral regexp_match(p.qual, v_shape) as m
|
|
63
|
+
where p.schemaname = v_schema
|
|
64
|
+
and m is not null
|
|
65
|
+
order by p.tablename, p.policyname
|
|
66
|
+
loop
|
|
67
|
+
execute format(
|
|
68
|
+
'alter policy %I on %I.%I using (company_id = any ((select array('
|
|
69
|
+
'select x from unnest(public.companies_with_capability(%L)) as x where public.module_enabled(x, %L)'
|
|
70
|
+
'))::uuid[])%s)',
|
|
71
|
+
r.policyname, v_schema, r.tablename, r.capability, v_module, r.rest);
|
|
72
|
+
v_changed := v_changed + 1;
|
|
73
|
+
end loop;
|
|
74
|
+
|
|
75
|
+
-- Run a second time, it finds the policies already rewritten and changes
|
|
76
|
+
-- nothing; finding neither shape means the policies changed and this file
|
|
77
|
+
-- has to be read again.
|
|
78
|
+
if v_changed = 0 and not exists (
|
|
79
|
+
select 1 from pg_policies p
|
|
80
|
+
where p.schemaname = v_schema and p.qual like '%companies_with_capability(%'
|
|
81
|
+
) then
|
|
82
|
+
raise exception 'no policy of % has the shape module_enabled(company_id, …) and has_capability(company_id, …), and none was rewritten: the policies changed shape and this migration has to be read again', v_schema;
|
|
83
|
+
end if;
|
|
84
|
+
|
|
85
|
+
select count(*) into v_left
|
|
86
|
+
from pg_policies p
|
|
87
|
+
where p.schemaname = v_schema
|
|
88
|
+
and p.qual ~ '(module_enabled|has_capability)\(company_id,';
|
|
89
|
+
if v_left > 0 then
|
|
90
|
+
raise exception '% policies of % still ask module_enabled() or has_capability() once per row', v_left, v_schema;
|
|
91
|
+
end if;
|
|
92
|
+
end;
|
|
93
|
+
$$;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
-- Ekwo — the `consolidation` module: its policies ask once per statement, not once
|
|
2
|
+
-- per row.
|
|
3
|
+
--
|
|
4
|
+
-- Every policy of the module that guards a table of a company read
|
|
5
|
+
--
|
|
6
|
+
-- using (public.module_enabled(company_id, 'consolidation')
|
|
7
|
+
-- and public.has_capability(company_id, 'consolidation.…'))
|
|
8
|
+
--
|
|
9
|
+
-- With a column as its argument each function runs for every row a scan
|
|
10
|
+
-- visits, and neither is inlined: `has_capability()` is security definer, and
|
|
11
|
+
-- `module_enabled()` carries a search_path since the socle moved to its own
|
|
12
|
+
-- schema. On a company with many rows here, nearly all the time of a read is
|
|
13
|
+
-- the policy. `scripts/inventory-policies.mjs` found it on its three tables, `groups`, `members` and `rates`.
|
|
14
|
+
--
|
|
15
|
+
-- The `crm` module measured it and changed its own policies in
|
|
16
|
+
-- `20261109120000_crm_reads_hold_at_volume.sql`; the socle did the same for
|
|
17
|
+
-- its tables in `20260918141627_a_policy_asks_once.sql`. This file is that
|
|
18
|
+
-- change, made here: the policy compares `company_id` with the companies
|
|
19
|
+
-- where the answer is yes, worked out in a sub-select that depends on no
|
|
20
|
+
-- column — an InitPlan, run once per statement:
|
|
21
|
+
--
|
|
22
|
+
-- company_id = any ((select array(
|
|
23
|
+
-- select x from unnest(public.companies_with_capability('consolidation.read')) x
|
|
24
|
+
-- where public.module_enabled(x, 'consolidation')))::uuid[])
|
|
25
|
+
--
|
|
26
|
+
-- **The same question, asked the other way round.** `companies_with_capability()`
|
|
27
|
+
-- lists the companies where `has_capability()` answers yes — a test of the
|
|
28
|
+
-- socle compares the two over every preset and a machine key — and
|
|
29
|
+
-- `module_enabled()` is then asked about each of those, by name, so the
|
|
30
|
+
-- policy still routes through it as `tests/modules.test.ts` requires. A row
|
|
31
|
+
-- passes exactly when it passed before. A condition a policy adds to the two
|
|
32
|
+
-- (`and computation_id is null`) is carried over as Postgres wrote it.
|
|
33
|
+
-- `with check` is left as it was: it is evaluated once per row written, which
|
|
34
|
+
-- is what a check is for.
|
|
35
|
+
--
|
|
36
|
+
-- **Read from the catalogue.** The policies are found by their shape in
|
|
37
|
+
-- `pg_policies` rather than listed, so the capability each one names is
|
|
38
|
+
-- carried over and cannot be mistyped here. Run twice, it changes nothing the
|
|
39
|
+
-- second time. It refuses to finish if it found neither the old shape nor the
|
|
40
|
+
-- new one — the policies changed and this file has to be read again — or if
|
|
41
|
+
-- a policy of the schema still asks either function about `company_id` row by
|
|
42
|
+
-- row.
|
|
43
|
+
--
|
|
44
|
+
-- **Older files, older socle.** Names of the socle are written `public.`, as
|
|
45
|
+
-- the module's first migrations wrote them; the installer reads the file for
|
|
46
|
+
-- the socle's schema of the day, `ekwo.` since 20261028040000.
|
|
47
|
+
|
|
48
|
+
do $$
|
|
49
|
+
declare
|
|
50
|
+
v_schema constant text := 'consolidation';
|
|
51
|
+
v_module constant text := 'consolidation';
|
|
52
|
+
v_shape constant text :=
|
|
53
|
+
'^\((?:[a-z_]+\.)?module_enabled\(company_id, ''' || v_module || '''::text\) AND '
|
|
54
|
+
'(?:[a-z_]+\.)?has_capability\(company_id, ''([a-z_.]+)''::text\)( AND .+)?\)$';
|
|
55
|
+
r record;
|
|
56
|
+
v_changed integer := 0;
|
|
57
|
+
v_left integer;
|
|
58
|
+
begin
|
|
59
|
+
for r in
|
|
60
|
+
select p.tablename, p.policyname, m[1] as capability, coalesce(m[2], '') as rest
|
|
61
|
+
from pg_policies p
|
|
62
|
+
cross join lateral regexp_match(p.qual, v_shape) as m
|
|
63
|
+
where p.schemaname = v_schema
|
|
64
|
+
and m is not null
|
|
65
|
+
order by p.tablename, p.policyname
|
|
66
|
+
loop
|
|
67
|
+
execute format(
|
|
68
|
+
'alter policy %I on %I.%I using (company_id = any ((select array('
|
|
69
|
+
'select x from unnest(public.companies_with_capability(%L)) as x where public.module_enabled(x, %L)'
|
|
70
|
+
'))::uuid[])%s)',
|
|
71
|
+
r.policyname, v_schema, r.tablename, r.capability, v_module, r.rest);
|
|
72
|
+
v_changed := v_changed + 1;
|
|
73
|
+
end loop;
|
|
74
|
+
|
|
75
|
+
-- Run a second time, it finds the policies already rewritten and changes
|
|
76
|
+
-- nothing; finding neither shape means the policies changed and this file
|
|
77
|
+
-- has to be read again.
|
|
78
|
+
if v_changed = 0 and not exists (
|
|
79
|
+
select 1 from pg_policies p
|
|
80
|
+
where p.schemaname = v_schema and p.qual like '%companies_with_capability(%'
|
|
81
|
+
) then
|
|
82
|
+
raise exception 'no policy of % has the shape module_enabled(company_id, …) and has_capability(company_id, …), and none was rewritten: the policies changed shape and this migration has to be read again', v_schema;
|
|
83
|
+
end if;
|
|
84
|
+
|
|
85
|
+
select count(*) into v_left
|
|
86
|
+
from pg_policies p
|
|
87
|
+
where p.schemaname = v_schema
|
|
88
|
+
and p.qual ~ '(module_enabled|has_capability)\(company_id,';
|
|
89
|
+
if v_left > 0 then
|
|
90
|
+
raise exception '% policies of % still ask module_enabled() or has_capability() once per row', v_left, v_schema;
|
|
91
|
+
end if;
|
|
92
|
+
end;
|
|
93
|
+
$$;
|