@company-six/qbosync 0.5.0 → 0.6.1
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 +15 -3
- package/dist/client.d.ts +7 -5
- package/dist/client.js +9 -6
- package/dist/contract/codes.d.ts +10 -2
- package/dist/contract/codes.js +10 -1
- package/package.json +1 -1
- package/sql/002_daily_report.sql +260 -260
- package/sql/003_daily_report_bills.sql +25 -0
package/README.md
CHANGED
|
@@ -86,7 +86,16 @@ The link is always QBO Sync's own sign-in page on the address the client calls;
|
|
|
86
86
|
const opened = await qbosync.openConnectRequest({ redirectUri, startedBy: admin.email, ownCompany: true });
|
|
87
87
|
```
|
|
88
88
|
|
|
89
|
-
The approval page offers to create a company under your app's name, already connected (`listCompanies()` says so), and the approving admin keeps it ticked. Send your own documents with that name as `company_key
|
|
89
|
+
The approval page offers to create a company under your app's name, already connected (`listCompanies()` says so), and the approving admin keeps it ticked. Send your own documents with that name as `company_key`. (Staff can also set this up before your first Connect, by adding your app as one that sends for *only itself*.)
|
|
90
|
+
|
|
91
|
+
Your people reach that company in QBO Sync the same way any company's people do: a button in your app that asks for a sign-in link naming your own company. The approval also offers "Let your app sign its people in", ticked for an app that is also a company, so the first person can set the company up (connect QuickBooks, turn syncing on):
|
|
92
|
+
|
|
93
|
+
```ts
|
|
94
|
+
const link = await qbosync.createSignInLink({ email: user.email, company: "My App", role: "app_admin" });
|
|
95
|
+
if (link.ok) redirect(link.url);
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
The first time, the link makes that person a QBO Sync user who sees only your company, and lands them on its Setup. Give the button only to the people who should see your company's transactions in QBO Sync. It is not a lasting mode: if your app later sells for other companies too, add a Connect button for them as above; nothing about your own company changes.
|
|
90
99
|
|
|
91
100
|
## 5. Send documents
|
|
92
101
|
|
|
@@ -107,10 +116,11 @@ Each document is checked against the contract first; one that fails is not sent
|
|
|
107
116
|
|
|
108
117
|
**Item codes and products.** QuickBooks puts every invoice and credit memo line on a product, and QBO Sync picks it by the line's `item_code`: the product the company chose for your code in QBO Sync's Setup → Products, else the QuickBooks product with that SKU, else (by default) a new product named after the line. Send real SKUs if you have a catalogue, or a few fixed category codes (`SALES`, `SHIPPING`) if you don't, the same code for the same thing every time. A company can set your app to never create products; then a line whose code has no product waits in QBO Sync until the company chooses one, and its status report says so. Nothing changes in what you send.
|
|
109
118
|
|
|
110
|
-
**Listing your codes (0.5.0).** If you send a few fixed codes, list them
|
|
119
|
+
**Listing your codes (0.5.0; per company since 0.6.0).** If you send a few fixed codes, list them for each company, each with a label, so that company sees them by name in Setup → Products (item codes) and Setup → Cost codes (bill cost codes) before your first document arrives:
|
|
111
120
|
|
|
112
121
|
```ts
|
|
113
122
|
const listed = await qbosync.declareCodes({
|
|
123
|
+
company_key: "Acme", // as your documents name the company; may be left out if you send for one company only
|
|
114
124
|
item_codes: [
|
|
115
125
|
{ code: "SALES", label: "Products, decoration and other charges" },
|
|
116
126
|
{ code: "SHIPPING", label: "Shipping and freight", description: "Every shipping line" },
|
|
@@ -120,7 +130,7 @@ const listed = await qbosync.declareCodes({
|
|
|
120
130
|
if (!listed.ok) console.error(listed.error);
|
|
121
131
|
```
|
|
122
132
|
|
|
123
|
-
Each list you send replaces
|
|
133
|
+
A list is that company's alone: no other company sees it, even one your app also sends for. Each list you send replaces that company's whole list of that kind (a code left out is removed); a kind you leave out is kept. It is checked against the contract before anything is sent, changes nothing about posting, and is safe to call again with the same lists, for example each time your app starts. `declaredCodes(companyKey)` reads back what QBO Sync holds for a company. An app with real catalogue SKUs lists nothing. **Upgrading from 0.5.x:** lists sent before 0.6.0 were kept per app; an app with one company keeps its list, one with several must list again with `company_key`.
|
|
124
134
|
|
|
125
135
|
Apps send invoices, credit memos, bills and vendor credits. Payments, refunds and payouts reach QBO Sync through its own connectors, matched to your invoices by `order_ref`, so give every invoice the `order_ref` of its order. Paying a bill is a money movement too: apps never send one.
|
|
126
136
|
|
|
@@ -226,6 +236,8 @@ Run [`sql/002_daily_report.sql`](sql/002_daily_report.sql) once, as you ran the
|
|
|
226
236
|
- **`qbosync_report_rows`**: one row per document per run. It holds what was true that morning, so a past report shows what it showed then.
|
|
227
237
|
- **`qbosync_report_start(day, time_zone)`**, **`qbosync_report_finish(run_id, counts)`** and **`qbosync_report_fail(run_id, error)`**.
|
|
228
238
|
|
|
239
|
+
The rows take all four document types: `invoice`, `credit_memo`, `bill` and `vendor_credit`. **Ran `002` before 0.5.1?** Its check took only invoices and credit memos, so a morning with a bill failed whole. Run [`sql/003_daily_report_bills.sql`](sql/003_daily_report_bills.sql) once to widen it; it is safe to run on newer tables too.
|
|
240
|
+
|
|
229
241
|
The database keeps the rules for you. Only one run runs at a time, and a day has one finished report: finishing a rerun replaces the earlier report. A failed run keeps no rows and leaves the earlier report in place. A run left running for 30 minutes (its server died) is marked failed by the next start.
|
|
230
242
|
|
|
231
243
|
### Run it each morning
|
package/dist/client.d.ts
CHANGED
|
@@ -138,9 +138,11 @@ export declare class QbosyncClient {
|
|
|
138
138
|
documents: ReportEntry[];
|
|
139
139
|
} | Failure>;
|
|
140
140
|
/**
|
|
141
|
-
* Lists the codes the app uses, each with a label, so
|
|
142
|
-
* document carries them: item codes under Setup -> Products, cost codes under Setup -> Cost
|
|
143
|
-
*
|
|
141
|
+
* Lists the codes the app uses for one company, each with a label, so that company sees them in QBO Sync's
|
|
142
|
+
* Setup before any document carries them: item codes under Setup -> Products, cost codes under Setup -> Cost
|
|
143
|
+
* codes. `company_key` names the company as the app's documents do; an app with a single company may leave it
|
|
144
|
+
* out, one with several must send it (0.6.0), and no company sees what was listed for another. Each kind sent is
|
|
145
|
+
* that company's whole list of that kind (a code left out is removed); a kind not sent is left as it is.
|
|
144
146
|
* Changes nothing about posting. Checked here first: a list that fails is not sent (`rejected`, `status` absent).
|
|
145
147
|
* Safe to call again with the same list, for example each time the app starts.
|
|
146
148
|
*/
|
|
@@ -148,8 +150,8 @@ export declare class QbosyncClient {
|
|
|
148
150
|
ok: true;
|
|
149
151
|
codes: DeclaredCodes;
|
|
150
152
|
} | Failure>;
|
|
151
|
-
/** The codes QBO Sync holds for the app, as last listed with `declareCodes`. */
|
|
152
|
-
declaredCodes(): Promise<{
|
|
153
|
+
/** The codes QBO Sync holds for the app and one company, as last listed with `declareCodes`. */
|
|
154
|
+
declaredCodes(companyKey?: string): Promise<{
|
|
153
155
|
ok: true;
|
|
154
156
|
codes: DeclaredCodes;
|
|
155
157
|
} | Failure>;
|
package/dist/client.js
CHANGED
|
@@ -221,9 +221,11 @@ export class QbosyncClient {
|
|
|
221
221
|
}
|
|
222
222
|
// ---- codes
|
|
223
223
|
/**
|
|
224
|
-
* Lists the codes the app uses, each with a label, so
|
|
225
|
-
* document carries them: item codes under Setup -> Products, cost codes under Setup -> Cost
|
|
226
|
-
*
|
|
224
|
+
* Lists the codes the app uses for one company, each with a label, so that company sees them in QBO Sync's
|
|
225
|
+
* Setup before any document carries them: item codes under Setup -> Products, cost codes under Setup -> Cost
|
|
226
|
+
* codes. `company_key` names the company as the app's documents do; an app with a single company may leave it
|
|
227
|
+
* out, one with several must send it (0.6.0), and no company sees what was listed for another. Each kind sent is
|
|
228
|
+
* that company's whole list of that kind (a code left out is removed); a kind not sent is left as it is.
|
|
227
229
|
* Changes nothing about posting. Checked here first: a list that fails is not sent (`rejected`, `status` absent).
|
|
228
230
|
* Safe to call again with the same list, for example each time the app starts.
|
|
229
231
|
*/
|
|
@@ -240,9 +242,10 @@ export class QbosyncClient {
|
|
|
240
242
|
});
|
|
241
243
|
return this.codesAnswer(called);
|
|
242
244
|
}
|
|
243
|
-
/** The codes QBO Sync holds for the app, as last listed with `declareCodes`. */
|
|
244
|
-
async declaredCodes() {
|
|
245
|
-
|
|
245
|
+
/** The codes QBO Sync holds for the app and one company, as last listed with `declareCodes`. */
|
|
246
|
+
async declaredCodes(companyKey) {
|
|
247
|
+
const query = companyKey ? `?company_key=${encodeURIComponent(companyKey)}` : "";
|
|
248
|
+
return this.codesAnswer(await this.call(`/api/v1/codes${query}`));
|
|
246
249
|
}
|
|
247
250
|
async codesAnswer(called) {
|
|
248
251
|
if (!called.ok)
|
package/dist/contract/codes.d.ts
CHANGED
|
@@ -8,6 +8,9 @@ import { z } from "zod";
|
|
|
8
8
|
* Listing changes nothing about posting, and is optional: an app with real catalogue SKUs lists nothing, and an app
|
|
9
9
|
* may still send a code it never listed. Each kind sent is the app's whole list of that kind: a code left out is
|
|
10
10
|
* removed, a kind not sent is left as it is.
|
|
11
|
+
*
|
|
12
|
+
* A list is for ONE company (0.6.0): the `company_key` its documents carry. A company sees only the codes listed for
|
|
13
|
+
* it, never those an app listed for its other companies. An app with a single company may leave `company_key` out.
|
|
11
14
|
*/
|
|
12
15
|
/** Longest item code QBO Sync keeps (Setup -> Products). */
|
|
13
16
|
export declare const MAX_ITEM_CODE_LENGTH = 200;
|
|
@@ -31,8 +34,13 @@ export declare const declaredCostCodeSchema: z.ZodObject<{
|
|
|
31
34
|
}, z.core.$strict>;
|
|
32
35
|
export type DeclaredItemCode = z.infer<typeof declaredItemCodeSchema>;
|
|
33
36
|
export type DeclaredCostCode = z.infer<typeof declaredCostCodeSchema>;
|
|
34
|
-
/** What `PUT /api/v1/codes` takes. At least one kind; each kind sent replaces
|
|
37
|
+
/** What `PUT /api/v1/codes` takes. At least one kind; each kind sent replaces that company's list of that kind. */
|
|
35
38
|
export declare const codesDeclarationSchema: z.ZodObject<{
|
|
39
|
+
/**
|
|
40
|
+
* The company the lists are for, as the app's documents name it. Needed when the app sends for several
|
|
41
|
+
* companies; with one, it may be left out (0.6.0).
|
|
42
|
+
*/
|
|
43
|
+
company_key: z.ZodOptional<z.ZodString>;
|
|
36
44
|
item_codes: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
37
45
|
/** Exactly as the app puts it on a line's `item_code`. */
|
|
38
46
|
code: z.ZodString;
|
|
@@ -49,7 +57,7 @@ export declare const codesDeclarationSchema: z.ZodObject<{
|
|
|
49
57
|
}, z.core.$strict>>>;
|
|
50
58
|
}, z.core.$strict>;
|
|
51
59
|
export type CodesDeclaration = z.infer<typeof codesDeclarationSchema>;
|
|
52
|
-
/** What QBO Sync holds for the app: `GET /api/v1/codes`, and the answer to a `PUT`. */
|
|
60
|
+
/** What QBO Sync holds for the app and one company: `GET /api/v1/codes`, and the answer to a `PUT`. */
|
|
53
61
|
export interface DeclaredCodes {
|
|
54
62
|
item_codes: DeclaredItemCode[];
|
|
55
63
|
cost_codes: DeclaredCostCode[];
|
package/dist/contract/codes.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import { costCode } from "./bill.js";
|
|
3
|
+
import { text } from "./common.js";
|
|
3
4
|
/**
|
|
4
5
|
* The codes an app uses, listed ahead of any document (0.5.0). Each company maps an app's codes in QBO Sync's
|
|
5
6
|
* Setup: item codes to QuickBooks products (Setup -> Products), cost codes to QuickBooks accounts (Setup -> Cost
|
|
@@ -9,6 +10,9 @@ import { costCode } from "./bill.js";
|
|
|
9
10
|
* Listing changes nothing about posting, and is optional: an app with real catalogue SKUs lists nothing, and an app
|
|
10
11
|
* may still send a code it never listed. Each kind sent is the app's whole list of that kind: a code left out is
|
|
11
12
|
* removed, a kind not sent is left as it is.
|
|
13
|
+
*
|
|
14
|
+
* A list is for ONE company (0.6.0): the `company_key` its documents carry. A company sees only the codes listed for
|
|
15
|
+
* it, never those an app listed for its other companies. An app with a single company may leave `company_key` out.
|
|
12
16
|
*/
|
|
13
17
|
/** Longest item code QBO Sync keeps (Setup -> Products). */
|
|
14
18
|
export const MAX_ITEM_CODE_LENGTH = 200;
|
|
@@ -48,9 +52,14 @@ function noRepeats(list, ctx, field) {
|
|
|
48
52
|
seen.add(entry.code);
|
|
49
53
|
});
|
|
50
54
|
}
|
|
51
|
-
/** What `PUT /api/v1/codes` takes. At least one kind; each kind sent replaces
|
|
55
|
+
/** What `PUT /api/v1/codes` takes. At least one kind; each kind sent replaces that company's list of that kind. */
|
|
52
56
|
export const codesDeclarationSchema = z
|
|
53
57
|
.strictObject({
|
|
58
|
+
/**
|
|
59
|
+
* The company the lists are for, as the app's documents name it. Needed when the app sends for several
|
|
60
|
+
* companies; with one, it may be left out (0.6.0).
|
|
61
|
+
*/
|
|
62
|
+
company_key: text.optional(),
|
|
54
63
|
item_codes: z.array(declaredItemCodeSchema).max(MAX_DECLARED_CODES, `at most ${MAX_DECLARED_CODES} codes`).optional(),
|
|
55
64
|
cost_codes: z.array(declaredCostCodeSchema).max(MAX_DECLARED_CODES, `at most ${MAX_DECLARED_CODES} codes`).optional(),
|
|
56
65
|
})
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@company-six/qbosync",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.1",
|
|
4
4
|
"description": "Connect an app to QBO Sync: send invoices, credit memos, bills and vendor credits to QuickBooks, and hear back what happened to them.",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"type": "module",
|
package/sql/002_daily_report.sql
CHANGED
|
@@ -1,260 +1,260 @@
|
|
|
1
|
-
-- An app's daily report: each morning, the invoices
|
|
2
|
-
-- QBO Sync did with them. Optional; run this once in the app's own Postgres database (the Supabase
|
|
3
|
-
-- instance) if the app keeps one. One transaction; re-running it is a no-op. It does not need 001_connection.sql.
|
|
4
|
-
--
|
|
5
|
-
-- qbosync_report_runs one row per run of the report: the day it covers, whether it is running, finished or failed,
|
|
6
|
-
-- and a finished day's totals by result, for the page that lists the days.
|
|
7
|
-
-- qbosync_report_rows one row per document per run, holding what was true THAT morning, so a past report can be
|
|
8
|
-
-- opened again and still shows what it showed then.
|
|
9
|
-
--
|
|
10
|
-
-- The SDK's guide ("A daily report") says how a run fills them. In short:
|
|
11
|
-
--
|
|
12
|
-
-- select * from qbosync_report_start('2026-11-08', 'America/Chicago'); -- the run, with that day's window in UTC
|
|
13
|
-
-- -- list the documents your app created in [window_start, window_end), ask QBO Sync, compare, then
|
|
14
|
-
-- insert into qbosync_report_rows (run_id, ...) values (...); -- one per document
|
|
15
|
-
-- select qbosync_report_finish(run_id, '{"documents": 12, "matches": 11, "failed": 1}');
|
|
16
|
-
-- -- or, when the day cannot be built:
|
|
17
|
-
-- select qbosync_report_fail(run_id, 'QBO Sync could not be reached');
|
|
18
|
-
--
|
|
19
|
-
-- Rules the database keeps:
|
|
20
|
-
-- - ONE run is running at a time: two jobs, or a manual run during the scheduled one, never overlap;
|
|
21
|
-
-- - ONE finished report per day: finishing a second report for a day replaces the first in the same transaction,
|
|
22
|
-
-- so the day always has one, never none;
|
|
23
|
-
-- - a run still running after 30 minutes (its server died) is marked failed by the next start;
|
|
24
|
-
-- - rows go only into a running run, and a failed run keeps none.
|
|
25
|
-
--
|
|
26
|
-
-- The tables have row level security on and no policy, and every function is granted to service_role only: your server
|
|
27
|
-
-- reads and writes them, and shows each person only their own companies' rows. Outside Supabase, grant the tables and
|
|
28
|
-
-- functions to the role your server connects with.
|
|
29
|
-
--
|
|
30
|
-
-- Undo (nothing else depends on these):
|
|
31
|
-
-- drop function public.qbosync_report_start(date, text);
|
|
32
|
-
-- drop function public.qbosync_report_finish(uuid, jsonb);
|
|
33
|
-
-- drop function public.qbosync_report_fail(uuid, text);
|
|
34
|
-
-- drop table public.qbosync_report_rows;
|
|
35
|
-
-- drop table public.qbosync_report_runs;
|
|
36
|
-
-- drop function public.qbosync_report_rows_only_running();
|
|
37
|
-
|
|
38
|
-
begin;
|
|
39
|
-
|
|
40
|
-
-- ── 1. Runs ─────────────────────────────────────────────────────────────────────────
|
|
41
|
-
|
|
42
|
-
create table if not exists public.qbosync_report_runs (
|
|
43
|
-
id uuid primary key default gen_random_uuid(),
|
|
44
|
-
-- The day reported, in the app's own time zone.
|
|
45
|
-
report_date date not null,
|
|
46
|
-
-- That zone (an IANA name, such as America/Chicago) and the day's midnight to midnight in it, in UTC. Worked out by
|
|
47
|
-
-- qbosync_report_start, so daylight saving is the database's job, not the app's.
|
|
48
|
-
time_zone text not null,
|
|
49
|
-
window_start timestamptz not null,
|
|
50
|
-
window_end timestamptz not null,
|
|
51
|
-
status text not null default 'running',
|
|
52
|
-
started_at timestamptz not null default now(),
|
|
53
|
-
finished_at timestamptz,
|
|
54
|
-
-- Why a failed run failed, for the page: a day that failed stays listed with this until a later run makes it up.
|
|
55
|
-
error text,
|
|
56
|
-
-- A finished run's totals: {"documents": 12, "matches": 11, "failed": 1, ...}.
|
|
57
|
-
counts jsonb,
|
|
58
|
-
constraint qbosync_report_runs_status_check check (status in ('running', 'done', 'failed')),
|
|
59
|
-
constraint qbosync_report_runs_window_check check (window_start < window_end),
|
|
60
|
-
constraint qbosync_report_runs_time_zone_check check (length(btrim(time_zone)) > 0),
|
|
61
|
-
constraint qbosync_report_runs_shape_check check (
|
|
62
|
-
(status = 'running' and finished_at is null and error is null and counts is null)
|
|
63
|
-
or (status = 'done' and finished_at is not null and error is null
|
|
64
|
-
and counts is not null and jsonb_typeof(counts) = 'object')
|
|
65
|
-
or (status = 'failed' and finished_at is not null and counts is null
|
|
66
|
-
and error is not null and length(btrim(error)) > 0))
|
|
67
|
-
);
|
|
68
|
-
|
|
69
|
-
-- One run at a time: every running run has the same key.
|
|
70
|
-
create unique index if not exists qbosync_report_runs_one_running
|
|
71
|
-
on public.qbosync_report_runs ((true)) where status = 'running';
|
|
72
|
-
-- One finished report per day.
|
|
73
|
-
create unique index if not exists qbosync_report_runs_one_done_per_day
|
|
74
|
-
on public.qbosync_report_runs (report_date) where status = 'done';
|
|
75
|
-
create index if not exists qbosync_report_runs_day_idx
|
|
76
|
-
on public.qbosync_report_runs (report_date desc, started_at desc);
|
|
77
|
-
|
|
78
|
-
-- ── 2. Rows ─────────────────────────────────────────────────────────────────────────
|
|
79
|
-
|
|
80
|
-
create table if not exists public.qbosync_report_rows (
|
|
81
|
-
id uuid primary key default gen_random_uuid(),
|
|
82
|
-
run_id uuid not null references public.qbosync_report_runs (id) on delete cascade,
|
|
83
|
-
document_type text not null,
|
|
84
|
-
-- The ref your app sent the document under: what QBO Sync was asked about.
|
|
85
|
-
source_ref text not null,
|
|
86
|
-
-- The company, as your app names it (the document's company_key): for showing each company only its own rows.
|
|
87
|
-
company_key text not null,
|
|
88
|
-
-- Optional: your own id for the document (any id, as text), and what people know it by (an order number, say).
|
|
89
|
-
document_id text,
|
|
90
|
-
label text,
|
|
91
|
-
-- The one result the document got. See the guide for when each applies:
|
|
92
|
-
-- matches posted, and every amount is what your app holds
|
|
93
|
-
-- differs posted, but an amount differs, or QuickBooks no longer holds what was posted
|
|
94
|
-
-- changed_since_posted your document changed after the version QBO Sync posted
|
|
95
|
-
-- not_posted QBO Sync has it and nothing is posted (waiting, syncing off, excluded, rolled back)
|
|
96
|
-
-- failed QBO Sync tried and failed; its error is kept
|
|
97
|
-
-- not_received QBO Sync never received it
|
|
98
|
-
result text not null,
|
|
99
|
-
-- As QBO Sync answered that morning.
|
|
100
|
-
qbo_number text,
|
|
101
|
-
sync_status text,
|
|
102
|
-
synced_at timestamptz,
|
|
103
|
-
-- The nightly QuickBooks check's result for what was posted; null while nothing is posted.
|
|
104
|
-
qbo_check text,
|
|
105
|
-
-- For "differs" only: each thing that did not match, as your app chooses to describe it (a JSON array, never empty).
|
|
106
|
-
differences jsonb,
|
|
107
|
-
-- QBO Sync's error for "failed"; for "not_received", your own record of why the send did not get through, if any.
|
|
108
|
-
error text,
|
|
109
|
-
-- Optional: your own record of sending it (shown beside "not_received").
|
|
110
|
-
send_status text,
|
|
111
|
-
created_at timestamptz not null default now(),
|
|
112
|
-
constraint qbosync_report_rows_type_check check (document_type in ('invoice', 'credit_memo')),
|
|
113
|
-
constraint qbosync_report_rows_ref_check check (length(btrim(source_ref)) > 0),
|
|
114
|
-
constraint qbosync_report_rows_company_check check (length(btrim(company_key)) > 0),
|
|
115
|
-
constraint qbosync_report_rows_result_check check (result in
|
|
116
|
-
('matches', 'differs', 'changed_since_posted', 'not_posted', 'failed', 'not_received')),
|
|
117
|
-
constraint qbosync_report_rows_qbo_check_check check (
|
|
118
|
-
qbo_check is null or qbo_check in ('matches', 'differs', 'missing', 'not_checked')),
|
|
119
|
-
constraint qbosync_report_rows_differences_check check (
|
|
120
|
-
(result = 'differs') = (differences is not null and jsonb_typeof(differences) = 'array' and jsonb_array_length(differences) > 0))
|
|
121
|
-
);
|
|
122
|
-
|
|
123
|
-
-- A document once per run.
|
|
124
|
-
create unique index if not exists qbosync_report_rows_one_per_document
|
|
125
|
-
on public.qbosync_report_rows (run_id, document_type, source_ref);
|
|
126
|
-
create index if not exists qbosync_report_rows_company_idx
|
|
127
|
-
on public.qbosync_report_rows (run_id, company_key);
|
|
128
|
-
|
|
129
|
-
-- Rows go only into a running run: a finished report never changes, and a failed one stays empty.
|
|
130
|
-
create or replace function public.qbosync_report_rows_only_running()
|
|
131
|
-
returns trigger
|
|
132
|
-
language plpgsql
|
|
133
|
-
set search_path = public
|
|
134
|
-
as $$
|
|
135
|
-
begin
|
|
136
|
-
if not exists (select 1 from public.qbosync_report_runs where id = new.run_id and status = 'running') then
|
|
137
|
-
raise exception 'qbosync_report_rows: run % is not running', new.run_id;
|
|
138
|
-
end if;
|
|
139
|
-
return new;
|
|
140
|
-
end;
|
|
141
|
-
$$;
|
|
142
|
-
|
|
143
|
-
drop trigger if exists qbosync_report_rows_only_running on public.qbosync_report_rows;
|
|
144
|
-
create trigger qbosync_report_rows_only_running
|
|
145
|
-
before insert or update on public.qbosync_report_rows
|
|
146
|
-
for each row execute function public.qbosync_report_rows_only_running();
|
|
147
|
-
|
|
148
|
-
alter table public.qbosync_report_runs enable row level security;
|
|
149
|
-
alter table public.qbosync_report_rows enable row level security;
|
|
150
|
-
|
|
151
|
-
-- ── 3. Starting, finishing, failing ─────────────────────────────────────────────────
|
|
152
|
-
|
|
153
|
-
-- Starts a run for one day in the given time zone and answers it, with the day's window in UTC. A run still running after
|
|
154
|
-
-- 30 minutes is marked failed first (its server died); one running for less makes this raise, so two runs never overlap.
|
|
155
|
-
create or replace function public.qbosync_report_start(p_report_date date, p_time_zone text)
|
|
156
|
-
returns public.qbosync_report_runs
|
|
157
|
-
language plpgsql
|
|
158
|
-
set search_path = public
|
|
159
|
-
as $$
|
|
160
|
-
declare
|
|
161
|
-
run public.qbosync_report_runs;
|
|
162
|
-
begin
|
|
163
|
-
if p_report_date is null then
|
|
164
|
-
raise exception 'qbosync_report_start: a day is required';
|
|
165
|
-
end if;
|
|
166
|
-
if p_time_zone is null or not exists (select 1 from pg_timezone_names where name = p_time_zone) then
|
|
167
|
-
raise exception 'qbosync_report_start: % is not a time zone name (such as America/Chicago)', coalesce(p_time_zone, 'null');
|
|
168
|
-
end if;
|
|
169
|
-
update public.qbosync_report_runs
|
|
170
|
-
set status = 'failed', finished_at = now(), error = 'abandoned: still running after 30 minutes'
|
|
171
|
-
where status = 'running' and started_at < now() - interval '30 minutes';
|
|
172
|
-
-- An abandoned run keeps no rows, like any failed run.
|
|
173
|
-
delete from public.qbosync_report_rows r
|
|
174
|
-
using public.qbosync_report_runs u
|
|
175
|
-
where r.run_id = u.id and u.status = 'failed';
|
|
176
|
-
|
|
177
|
-
insert into public.qbosync_report_runs (report_date, time_zone, window_start, window_end)
|
|
178
|
-
values (p_report_date,
|
|
179
|
-
p_time_zone,
|
|
180
|
-
(p_report_date::timestamp) at time zone p_time_zone,
|
|
181
|
-
((p_report_date + 1)::timestamp) at time zone p_time_zone)
|
|
182
|
-
returning * into run;
|
|
183
|
-
return run;
|
|
184
|
-
exception when unique_violation then
|
|
185
|
-
raise exception 'qbosync_report_start: another report is running';
|
|
186
|
-
end;
|
|
187
|
-
$$;
|
|
188
|
-
|
|
189
|
-
-- Finishes a running run with its totals. An earlier finished report for the same day is deleted in the same transaction
|
|
190
|
-
-- (its rows with it), so the day always has exactly one, never none while a rerun is being finished.
|
|
191
|
-
create or replace function public.qbosync_report_finish(p_run_id uuid, p_counts jsonb)
|
|
192
|
-
returns void
|
|
193
|
-
language plpgsql
|
|
194
|
-
set search_path = public
|
|
195
|
-
as $$
|
|
196
|
-
declare
|
|
197
|
-
day date;
|
|
198
|
-
begin
|
|
199
|
-
if p_counts is null or jsonb_typeof(p_counts) <> 'object' then
|
|
200
|
-
raise exception 'qbosync_report_finish: counts must be a JSON object';
|
|
201
|
-
end if;
|
|
202
|
-
select report_date into day from public.qbosync_report_runs where id = p_run_id and status = 'running' for update;
|
|
203
|
-
if not found then
|
|
204
|
-
raise exception 'qbosync_report_finish: run % is not running', p_run_id;
|
|
205
|
-
end if;
|
|
206
|
-
delete from public.qbosync_report_runs where report_date = day and status = 'done';
|
|
207
|
-
update public.qbosync_report_runs
|
|
208
|
-
set status = 'done', finished_at = now(), counts = p_counts
|
|
209
|
-
where id = p_run_id;
|
|
210
|
-
end;
|
|
211
|
-
$$;
|
|
212
|
-
|
|
213
|
-
-- Marks a running run failed, keeping why, and drops the rows it had written. An earlier finished report for the day stays.
|
|
214
|
-
create or replace function public.qbosync_report_fail(p_run_id uuid, p_error text)
|
|
215
|
-
returns void
|
|
216
|
-
language plpgsql
|
|
217
|
-
set search_path = public
|
|
218
|
-
as $$
|
|
219
|
-
begin
|
|
220
|
-
delete from public.qbosync_report_rows where run_id = p_run_id;
|
|
221
|
-
update public.qbosync_report_runs
|
|
222
|
-
set status = 'failed', finished_at = now(),
|
|
223
|
-
error = coalesce(nullif(btrim(p_error), ''), 'the report could not be built')
|
|
224
|
-
where id = p_run_id and status = 'running';
|
|
225
|
-
end;
|
|
226
|
-
$$;
|
|
227
|
-
|
|
228
|
-
do $$
|
|
229
|
-
declare
|
|
230
|
-
fn text;
|
|
231
|
-
tbl text;
|
|
232
|
-
r text;
|
|
233
|
-
begin
|
|
234
|
-
foreach tbl in array array['public.qbosync_report_runs', 'public.qbosync_report_rows'] loop
|
|
235
|
-
execute format('revoke all on table %s from public', tbl);
|
|
236
|
-
foreach r in array array['anon', 'authenticated'] loop
|
|
237
|
-
if exists (select 1 from pg_roles where rolname = r) then
|
|
238
|
-
execute format('revoke all on table %s from %I', tbl, r);
|
|
239
|
-
end if;
|
|
240
|
-
end loop;
|
|
241
|
-
end loop;
|
|
242
|
-
foreach fn in array array[
|
|
243
|
-
'public.qbosync_report_start(date, text)',
|
|
244
|
-
'public.qbosync_report_finish(uuid, jsonb)',
|
|
245
|
-
'public.qbosync_report_fail(uuid, text)',
|
|
246
|
-
'public.qbosync_report_rows_only_running()'
|
|
247
|
-
] loop
|
|
248
|
-
execute format('revoke all on function %s from public', fn);
|
|
249
|
-
foreach r in array array['anon', 'authenticated'] loop
|
|
250
|
-
if exists (select 1 from pg_roles where rolname = r) then
|
|
251
|
-
execute format('revoke all on function %s from %I', fn, r);
|
|
252
|
-
end if;
|
|
253
|
-
end loop;
|
|
254
|
-
if exists (select 1 from pg_roles where rolname = 'service_role') then
|
|
255
|
-
execute format('grant execute on function %s to service_role', fn);
|
|
256
|
-
end if;
|
|
257
|
-
end loop;
|
|
258
|
-
end $$;
|
|
259
|
-
|
|
260
|
-
commit;
|
|
1
|
+
-- An app's daily report: each morning, the invoices, credit memos, bills and vendor credits the app created the day
|
|
2
|
+
-- before, set beside what QBO Sync did with them. Optional; run this once in the app's own Postgres database (the Supabase
|
|
3
|
+
-- SQL editor, for instance) if the app keeps one. One transaction; re-running it is a no-op. It does not need 001_connection.sql.
|
|
4
|
+
--
|
|
5
|
+
-- qbosync_report_runs one row per run of the report: the day it covers, whether it is running, finished or failed,
|
|
6
|
+
-- and a finished day's totals by result, for the page that lists the days.
|
|
7
|
+
-- qbosync_report_rows one row per document per run, holding what was true THAT morning, so a past report can be
|
|
8
|
+
-- opened again and still shows what it showed then.
|
|
9
|
+
--
|
|
10
|
+
-- The SDK's guide ("A daily report") says how a run fills them. In short:
|
|
11
|
+
--
|
|
12
|
+
-- select * from qbosync_report_start('2026-11-08', 'America/Chicago'); -- the run, with that day's window in UTC
|
|
13
|
+
-- -- list the documents your app created in [window_start, window_end), ask QBO Sync, compare, then
|
|
14
|
+
-- insert into qbosync_report_rows (run_id, ...) values (...); -- one per document
|
|
15
|
+
-- select qbosync_report_finish(run_id, '{"documents": 12, "matches": 11, "failed": 1}');
|
|
16
|
+
-- -- or, when the day cannot be built:
|
|
17
|
+
-- select qbosync_report_fail(run_id, 'QBO Sync could not be reached');
|
|
18
|
+
--
|
|
19
|
+
-- Rules the database keeps:
|
|
20
|
+
-- - ONE run is running at a time: two jobs, or a manual run during the scheduled one, never overlap;
|
|
21
|
+
-- - ONE finished report per day: finishing a second report for a day replaces the first in the same transaction,
|
|
22
|
+
-- so the day always has one, never none;
|
|
23
|
+
-- - a run still running after 30 minutes (its server died) is marked failed by the next start;
|
|
24
|
+
-- - rows go only into a running run, and a failed run keeps none.
|
|
25
|
+
--
|
|
26
|
+
-- The tables have row level security on and no policy, and every function is granted to service_role only: your server
|
|
27
|
+
-- reads and writes them, and shows each person only their own companies' rows. Outside Supabase, grant the tables and
|
|
28
|
+
-- functions to the role your server connects with.
|
|
29
|
+
--
|
|
30
|
+
-- Undo (nothing else depends on these):
|
|
31
|
+
-- drop function public.qbosync_report_start(date, text);
|
|
32
|
+
-- drop function public.qbosync_report_finish(uuid, jsonb);
|
|
33
|
+
-- drop function public.qbosync_report_fail(uuid, text);
|
|
34
|
+
-- drop table public.qbosync_report_rows;
|
|
35
|
+
-- drop table public.qbosync_report_runs;
|
|
36
|
+
-- drop function public.qbosync_report_rows_only_running();
|
|
37
|
+
|
|
38
|
+
begin;
|
|
39
|
+
|
|
40
|
+
-- ── 1. Runs ─────────────────────────────────────────────────────────────────────────
|
|
41
|
+
|
|
42
|
+
create table if not exists public.qbosync_report_runs (
|
|
43
|
+
id uuid primary key default gen_random_uuid(),
|
|
44
|
+
-- The day reported, in the app's own time zone.
|
|
45
|
+
report_date date not null,
|
|
46
|
+
-- That zone (an IANA name, such as America/Chicago) and the day's midnight to midnight in it, in UTC. Worked out by
|
|
47
|
+
-- qbosync_report_start, so daylight saving is the database's job, not the app's.
|
|
48
|
+
time_zone text not null,
|
|
49
|
+
window_start timestamptz not null,
|
|
50
|
+
window_end timestamptz not null,
|
|
51
|
+
status text not null default 'running',
|
|
52
|
+
started_at timestamptz not null default now(),
|
|
53
|
+
finished_at timestamptz,
|
|
54
|
+
-- Why a failed run failed, for the page: a day that failed stays listed with this until a later run makes it up.
|
|
55
|
+
error text,
|
|
56
|
+
-- A finished run's totals: {"documents": 12, "matches": 11, "failed": 1, ...}.
|
|
57
|
+
counts jsonb,
|
|
58
|
+
constraint qbosync_report_runs_status_check check (status in ('running', 'done', 'failed')),
|
|
59
|
+
constraint qbosync_report_runs_window_check check (window_start < window_end),
|
|
60
|
+
constraint qbosync_report_runs_time_zone_check check (length(btrim(time_zone)) > 0),
|
|
61
|
+
constraint qbosync_report_runs_shape_check check (
|
|
62
|
+
(status = 'running' and finished_at is null and error is null and counts is null)
|
|
63
|
+
or (status = 'done' and finished_at is not null and error is null
|
|
64
|
+
and counts is not null and jsonb_typeof(counts) = 'object')
|
|
65
|
+
or (status = 'failed' and finished_at is not null and counts is null
|
|
66
|
+
and error is not null and length(btrim(error)) > 0))
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
-- One run at a time: every running run has the same key.
|
|
70
|
+
create unique index if not exists qbosync_report_runs_one_running
|
|
71
|
+
on public.qbosync_report_runs ((true)) where status = 'running';
|
|
72
|
+
-- One finished report per day.
|
|
73
|
+
create unique index if not exists qbosync_report_runs_one_done_per_day
|
|
74
|
+
on public.qbosync_report_runs (report_date) where status = 'done';
|
|
75
|
+
create index if not exists qbosync_report_runs_day_idx
|
|
76
|
+
on public.qbosync_report_runs (report_date desc, started_at desc);
|
|
77
|
+
|
|
78
|
+
-- ── 2. Rows ─────────────────────────────────────────────────────────────────────────
|
|
79
|
+
|
|
80
|
+
create table if not exists public.qbosync_report_rows (
|
|
81
|
+
id uuid primary key default gen_random_uuid(),
|
|
82
|
+
run_id uuid not null references public.qbosync_report_runs (id) on delete cascade,
|
|
83
|
+
document_type text not null,
|
|
84
|
+
-- The ref your app sent the document under: what QBO Sync was asked about.
|
|
85
|
+
source_ref text not null,
|
|
86
|
+
-- The company, as your app names it (the document's company_key): for showing each company only its own rows.
|
|
87
|
+
company_key text not null,
|
|
88
|
+
-- Optional: your own id for the document (any id, as text), and what people know it by (an order number, say).
|
|
89
|
+
document_id text,
|
|
90
|
+
label text,
|
|
91
|
+
-- The one result the document got. See the guide for when each applies:
|
|
92
|
+
-- matches posted, and every amount is what your app holds
|
|
93
|
+
-- differs posted, but an amount differs, or QuickBooks no longer holds what was posted
|
|
94
|
+
-- changed_since_posted your document changed after the version QBO Sync posted
|
|
95
|
+
-- not_posted QBO Sync has it and nothing is posted (waiting, syncing off, excluded, rolled back)
|
|
96
|
+
-- failed QBO Sync tried and failed; its error is kept
|
|
97
|
+
-- not_received QBO Sync never received it
|
|
98
|
+
result text not null,
|
|
99
|
+
-- As QBO Sync answered that morning.
|
|
100
|
+
qbo_number text,
|
|
101
|
+
sync_status text,
|
|
102
|
+
synced_at timestamptz,
|
|
103
|
+
-- The nightly QuickBooks check's result for what was posted; null while nothing is posted.
|
|
104
|
+
qbo_check text,
|
|
105
|
+
-- For "differs" only: each thing that did not match, as your app chooses to describe it (a JSON array, never empty).
|
|
106
|
+
differences jsonb,
|
|
107
|
+
-- QBO Sync's error for "failed"; for "not_received", your own record of why the send did not get through, if any.
|
|
108
|
+
error text,
|
|
109
|
+
-- Optional: your own record of sending it (shown beside "not_received").
|
|
110
|
+
send_status text,
|
|
111
|
+
created_at timestamptz not null default now(),
|
|
112
|
+
constraint qbosync_report_rows_type_check check (document_type in ('invoice', 'credit_memo', 'bill', 'vendor_credit')),
|
|
113
|
+
constraint qbosync_report_rows_ref_check check (length(btrim(source_ref)) > 0),
|
|
114
|
+
constraint qbosync_report_rows_company_check check (length(btrim(company_key)) > 0),
|
|
115
|
+
constraint qbosync_report_rows_result_check check (result in
|
|
116
|
+
('matches', 'differs', 'changed_since_posted', 'not_posted', 'failed', 'not_received')),
|
|
117
|
+
constraint qbosync_report_rows_qbo_check_check check (
|
|
118
|
+
qbo_check is null or qbo_check in ('matches', 'differs', 'missing', 'not_checked')),
|
|
119
|
+
constraint qbosync_report_rows_differences_check check (
|
|
120
|
+
(result = 'differs') = (differences is not null and jsonb_typeof(differences) = 'array' and jsonb_array_length(differences) > 0))
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
-- A document once per run.
|
|
124
|
+
create unique index if not exists qbosync_report_rows_one_per_document
|
|
125
|
+
on public.qbosync_report_rows (run_id, document_type, source_ref);
|
|
126
|
+
create index if not exists qbosync_report_rows_company_idx
|
|
127
|
+
on public.qbosync_report_rows (run_id, company_key);
|
|
128
|
+
|
|
129
|
+
-- Rows go only into a running run: a finished report never changes, and a failed one stays empty.
|
|
130
|
+
create or replace function public.qbosync_report_rows_only_running()
|
|
131
|
+
returns trigger
|
|
132
|
+
language plpgsql
|
|
133
|
+
set search_path = public
|
|
134
|
+
as $$
|
|
135
|
+
begin
|
|
136
|
+
if not exists (select 1 from public.qbosync_report_runs where id = new.run_id and status = 'running') then
|
|
137
|
+
raise exception 'qbosync_report_rows: run % is not running', new.run_id;
|
|
138
|
+
end if;
|
|
139
|
+
return new;
|
|
140
|
+
end;
|
|
141
|
+
$$;
|
|
142
|
+
|
|
143
|
+
drop trigger if exists qbosync_report_rows_only_running on public.qbosync_report_rows;
|
|
144
|
+
create trigger qbosync_report_rows_only_running
|
|
145
|
+
before insert or update on public.qbosync_report_rows
|
|
146
|
+
for each row execute function public.qbosync_report_rows_only_running();
|
|
147
|
+
|
|
148
|
+
alter table public.qbosync_report_runs enable row level security;
|
|
149
|
+
alter table public.qbosync_report_rows enable row level security;
|
|
150
|
+
|
|
151
|
+
-- ── 3. Starting, finishing, failing ─────────────────────────────────────────────────
|
|
152
|
+
|
|
153
|
+
-- Starts a run for one day in the given time zone and answers it, with the day's window in UTC. A run still running after
|
|
154
|
+
-- 30 minutes is marked failed first (its server died); one running for less makes this raise, so two runs never overlap.
|
|
155
|
+
create or replace function public.qbosync_report_start(p_report_date date, p_time_zone text)
|
|
156
|
+
returns public.qbosync_report_runs
|
|
157
|
+
language plpgsql
|
|
158
|
+
set search_path = public
|
|
159
|
+
as $$
|
|
160
|
+
declare
|
|
161
|
+
run public.qbosync_report_runs;
|
|
162
|
+
begin
|
|
163
|
+
if p_report_date is null then
|
|
164
|
+
raise exception 'qbosync_report_start: a day is required';
|
|
165
|
+
end if;
|
|
166
|
+
if p_time_zone is null or not exists (select 1 from pg_timezone_names where name = p_time_zone) then
|
|
167
|
+
raise exception 'qbosync_report_start: % is not a time zone name (such as America/Chicago)', coalesce(p_time_zone, 'null');
|
|
168
|
+
end if;
|
|
169
|
+
update public.qbosync_report_runs
|
|
170
|
+
set status = 'failed', finished_at = now(), error = 'abandoned: still running after 30 minutes'
|
|
171
|
+
where status = 'running' and started_at < now() - interval '30 minutes';
|
|
172
|
+
-- An abandoned run keeps no rows, like any failed run.
|
|
173
|
+
delete from public.qbosync_report_rows r
|
|
174
|
+
using public.qbosync_report_runs u
|
|
175
|
+
where r.run_id = u.id and u.status = 'failed';
|
|
176
|
+
|
|
177
|
+
insert into public.qbosync_report_runs (report_date, time_zone, window_start, window_end)
|
|
178
|
+
values (p_report_date,
|
|
179
|
+
p_time_zone,
|
|
180
|
+
(p_report_date::timestamp) at time zone p_time_zone,
|
|
181
|
+
((p_report_date + 1)::timestamp) at time zone p_time_zone)
|
|
182
|
+
returning * into run;
|
|
183
|
+
return run;
|
|
184
|
+
exception when unique_violation then
|
|
185
|
+
raise exception 'qbosync_report_start: another report is running';
|
|
186
|
+
end;
|
|
187
|
+
$$;
|
|
188
|
+
|
|
189
|
+
-- Finishes a running run with its totals. An earlier finished report for the same day is deleted in the same transaction
|
|
190
|
+
-- (its rows with it), so the day always has exactly one, never none while a rerun is being finished.
|
|
191
|
+
create or replace function public.qbosync_report_finish(p_run_id uuid, p_counts jsonb)
|
|
192
|
+
returns void
|
|
193
|
+
language plpgsql
|
|
194
|
+
set search_path = public
|
|
195
|
+
as $$
|
|
196
|
+
declare
|
|
197
|
+
day date;
|
|
198
|
+
begin
|
|
199
|
+
if p_counts is null or jsonb_typeof(p_counts) <> 'object' then
|
|
200
|
+
raise exception 'qbosync_report_finish: counts must be a JSON object';
|
|
201
|
+
end if;
|
|
202
|
+
select report_date into day from public.qbosync_report_runs where id = p_run_id and status = 'running' for update;
|
|
203
|
+
if not found then
|
|
204
|
+
raise exception 'qbosync_report_finish: run % is not running', p_run_id;
|
|
205
|
+
end if;
|
|
206
|
+
delete from public.qbosync_report_runs where report_date = day and status = 'done';
|
|
207
|
+
update public.qbosync_report_runs
|
|
208
|
+
set status = 'done', finished_at = now(), counts = p_counts
|
|
209
|
+
where id = p_run_id;
|
|
210
|
+
end;
|
|
211
|
+
$$;
|
|
212
|
+
|
|
213
|
+
-- Marks a running run failed, keeping why, and drops the rows it had written. An earlier finished report for the day stays.
|
|
214
|
+
create or replace function public.qbosync_report_fail(p_run_id uuid, p_error text)
|
|
215
|
+
returns void
|
|
216
|
+
language plpgsql
|
|
217
|
+
set search_path = public
|
|
218
|
+
as $$
|
|
219
|
+
begin
|
|
220
|
+
delete from public.qbosync_report_rows where run_id = p_run_id;
|
|
221
|
+
update public.qbosync_report_runs
|
|
222
|
+
set status = 'failed', finished_at = now(),
|
|
223
|
+
error = coalesce(nullif(btrim(p_error), ''), 'the report could not be built')
|
|
224
|
+
where id = p_run_id and status = 'running';
|
|
225
|
+
end;
|
|
226
|
+
$$;
|
|
227
|
+
|
|
228
|
+
do $$
|
|
229
|
+
declare
|
|
230
|
+
fn text;
|
|
231
|
+
tbl text;
|
|
232
|
+
r text;
|
|
233
|
+
begin
|
|
234
|
+
foreach tbl in array array['public.qbosync_report_runs', 'public.qbosync_report_rows'] loop
|
|
235
|
+
execute format('revoke all on table %s from public', tbl);
|
|
236
|
+
foreach r in array array['anon', 'authenticated'] loop
|
|
237
|
+
if exists (select 1 from pg_roles where rolname = r) then
|
|
238
|
+
execute format('revoke all on table %s from %I', tbl, r);
|
|
239
|
+
end if;
|
|
240
|
+
end loop;
|
|
241
|
+
end loop;
|
|
242
|
+
foreach fn in array array[
|
|
243
|
+
'public.qbosync_report_start(date, text)',
|
|
244
|
+
'public.qbosync_report_finish(uuid, jsonb)',
|
|
245
|
+
'public.qbosync_report_fail(uuid, text)',
|
|
246
|
+
'public.qbosync_report_rows_only_running()'
|
|
247
|
+
] loop
|
|
248
|
+
execute format('revoke all on function %s from public', fn);
|
|
249
|
+
foreach r in array array['anon', 'authenticated'] loop
|
|
250
|
+
if exists (select 1 from pg_roles where rolname = r) then
|
|
251
|
+
execute format('revoke all on function %s from %I', fn, r);
|
|
252
|
+
end if;
|
|
253
|
+
end loop;
|
|
254
|
+
if exists (select 1 from pg_roles where rolname = 'service_role') then
|
|
255
|
+
execute format('grant execute on function %s to service_role', fn);
|
|
256
|
+
end if;
|
|
257
|
+
end loop;
|
|
258
|
+
end $$;
|
|
259
|
+
|
|
260
|
+
commit;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
-- The daily report keeps rows for bills and vendor credits too (SDK 0.5.1).
|
|
2
|
+
--
|
|
3
|
+
-- Run this once if your app ran sql/002_daily_report.sql before 0.5.1 and sends bills or vendor credits. Until then,
|
|
4
|
+
-- 002's check allowed only 'invoice' and 'credit_memo', so the first morning with a bill failed whole ("new row for
|
|
5
|
+
-- relation qbosync_report_rows violates check constraint qbosync_report_rows_type_check") and every later morning with
|
|
6
|
+
-- one did too. 002 itself now allows all four types, so an app that runs 002 from 0.5.1 on does not need this file;
|
|
7
|
+
-- running it anyway changes nothing. One transaction; re-running it is a no-op.
|
|
8
|
+
--
|
|
9
|
+
-- Found by Aztec's end-to-end run on 2026-11-16, the first app to send bills.
|
|
10
|
+
--
|
|
11
|
+
-- Undo (only while no bill or vendor credit row exists):
|
|
12
|
+
-- alter table public.qbosync_report_rows drop constraint qbosync_report_rows_type_check;
|
|
13
|
+
-- alter table public.qbosync_report_rows add constraint qbosync_report_rows_type_check
|
|
14
|
+
-- check (document_type in ('invoice', 'credit_memo'));
|
|
15
|
+
|
|
16
|
+
begin;
|
|
17
|
+
|
|
18
|
+
alter table public.qbosync_report_rows
|
|
19
|
+
drop constraint if exists qbosync_report_rows_type_check;
|
|
20
|
+
|
|
21
|
+
alter table public.qbosync_report_rows
|
|
22
|
+
add constraint qbosync_report_rows_type_check
|
|
23
|
+
check (document_type in ('invoice', 'credit_memo', 'bill', 'vendor_credit'));
|
|
24
|
+
|
|
25
|
+
commit;
|