@company-six/qbosync 0.2.0 → 0.3.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 +275 -179
- package/dist/client.d.ts +5 -1
- package/dist/client.js +4 -1
- package/dist/connectRequest.d.ts +6 -0
- package/dist/connectRequest.js +1 -1
- package/package.json +44 -44
- package/sql/001_connection.sql +368 -368
- package/sql/002_daily_report.sql +260 -0
package/README.md
CHANGED
|
@@ -1,179 +1,275 @@
|
|
|
1
|
-
# @company-six/qbosync
|
|
2
|
-
|
|
3
|
-
Connect your app to QBO Sync, which posts your sales to each company's QuickBooks. Your app sends invoices and credit memos; QBO Sync checks them, posts them and tells your app what happened to each one.
|
|
4
|
-
|
|
5
|
-
Server-side only (Node 20 or later): it holds your app's tokens. TypeScript types are included (TypeScript 5 or later).
|
|
6
|
-
|
|
7
|
-
## Install
|
|
8
|
-
|
|
9
|
-
```bash
|
|
10
|
-
npm install @company-six/qbosync zod
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
Before you start, ask Company Six staff to set your app's **callback address**, the https address QBO Sync sends status reports to, and to turn on **Sign people in** if your app has a Connect button for its companies.
|
|
14
|
-
|
|
15
|
-
## 1. Add the two tables
|
|
16
|
-
|
|
17
|
-
Your app keeps its connection in its own database. Run [`sql/001_connection.sql`](sql/001_connection.sql) once (in the Supabase SQL editor, for instance). It adds two one-row tables that only your server can read, with the locks that keep two servers from spending the same renewal token.
|
|
18
|
-
|
|
19
|
-
Nothing else needs storing: status reports are checked with QBO Sync's public key, so your app keeps no signing secret.
|
|
20
|
-
|
|
21
|
-
**Coming from a hand-built client?** Compare your functions with `sql/001_connection.sql` before switching. Supabase finds a function by the names of its arguments, so one that takes an extra argument (an old `p_signing_secret`, say) or names one differently is not found, and every renewal save fails. A renewal that cannot be saved loses the new renewal token, and QBO Sync then ends the connection. Change the functions first, in a way the old code still works with (give the extra argument a default), switch, then drop what is left.
|
|
22
|
-
|
|
23
|
-
## 2. Create the client
|
|
24
|
-
|
|
25
|
-
On Supabase, with a **service-role** client:
|
|
26
|
-
|
|
27
|
-
```ts
|
|
28
|
-
import { QbosyncClient } from "@company-six/qbosync";
|
|
29
|
-
import { createSupabaseStores } from "@company-six/qbosync/supabase";
|
|
30
|
-
|
|
31
|
-
export const qbosync = new QbosyncClient({
|
|
32
|
-
baseUrl: process.env.QBOSYNC_URL!,
|
|
33
|
-
appName: "my-app",
|
|
34
|
-
...createSupabaseStores(supabase),
|
|
35
|
-
});
|
|
36
|
-
```
|
|
37
|
-
|
|
38
|
-
Make **one client per server** and reuse it, as above: it keeps QBO Sync's public keys once fetched, so a client made per request would fetch them again for every status report.
|
|
39
|
-
|
|
40
|
-
On another database, implement `ConnectionStore` and `ConnectRequestStore` (their comments say what each method must promise) and pass them as `connections` and `connectRequests`.
|
|
41
|
-
|
|
42
|
-
## 3. Connect
|
|
43
|
-
|
|
44
|
-
When an admin of your app presses Connect:
|
|
45
|
-
|
|
46
|
-
```ts
|
|
47
|
-
const opened = await qbosync.openConnectRequest({
|
|
48
|
-
redirectUri: "https://my-app.com/qbosync/callback",
|
|
49
|
-
startedBy: admin.email,
|
|
50
|
-
});
|
|
51
|
-
if (opened.ok) redirect(opened.verificationUri);
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
A QBO Sync administrator approves then, or later. Collect the connection from a job your app runs on a schedule, and from the page the admin comes back to. It is safe to call often: only one server collects.
|
|
55
|
-
|
|
56
|
-
```ts
|
|
57
|
-
const collected = await qbosync.collectConnectRequest();
|
|
58
|
-
// collected.state: "connected", "waiting", "denied", "expired", "failed", "none", "other" or "unavailable"
|
|
59
|
-
```
|
|
60
|
-
|
|
61
|
-
From then on the client renews its tokens by itself. If QBO Sync ends the connection, calls answer `reason: "ended"` and an admin presses Connect again.
|
|
62
|
-
|
|
63
|
-
For a settings page, or to know whether there is anything to send with, read the connection without calling QBO Sync:
|
|
64
|
-
|
|
65
|
-
```ts
|
|
66
|
-
const now = await qbosync.connectionState();
|
|
67
|
-
// now.state: "connected" (who, when), "ended" (when, and why in words), "not_connected" or "unavailable"
|
|
68
|
-
```
|
|
69
|
-
|
|
70
|
-
## 4. Connect a company
|
|
71
|
-
|
|
72
|
-
A company comes to exist in QBO Sync when your app asks for a sign-in link for it. This is what a company's own Connect button calls:
|
|
73
|
-
|
|
74
|
-
```ts
|
|
75
|
-
const link = await qbosync.createSignInLink({ email: user.email, company: "Acme", role: "company_user" });
|
|
76
|
-
if (link.ok) redirect(link.url); // works once, for five minutes
|
|
77
|
-
```
|
|
78
|
-
|
|
79
|
-
The link is always QBO Sync's own sign-in page on the address the client calls; any other answer is refused as `unexpected_answer`. If QBO Sync refuses (the app may not sign people in yet, say), `link.detail` is its message, written for the person.
|
|
80
|
-
|
|
81
|
-
`company` is the name your documents carry as `company_key`. Ask which of your companies have connected with `qbosync.listCompanies()`, and send a company's documents only once it has: QBO Sync refuses the rest.
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
```ts
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
if (
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
```
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
```
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
```
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
1
|
+
# @company-six/qbosync
|
|
2
|
+
|
|
3
|
+
Connect your app to QBO Sync, which posts your sales to each company's QuickBooks. Your app sends invoices and credit memos; QBO Sync checks them, posts them and tells your app what happened to each one.
|
|
4
|
+
|
|
5
|
+
Server-side only (Node 20 or later): it holds your app's tokens. TypeScript types are included (TypeScript 5 or later).
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @company-six/qbosync zod
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Before you start, ask Company Six staff to set your app's **callback address**, the https address QBO Sync sends status reports to, and to turn on **Sign people in** if your app has a Connect button for its companies.
|
|
14
|
+
|
|
15
|
+
## 1. Add the two tables
|
|
16
|
+
|
|
17
|
+
Your app keeps its connection in its own database. Run [`sql/001_connection.sql`](sql/001_connection.sql) once (in the Supabase SQL editor, for instance). It adds two one-row tables that only your server can read, with the locks that keep two servers from spending the same renewal token.
|
|
18
|
+
|
|
19
|
+
Nothing else needs storing: status reports are checked with QBO Sync's public key, so your app keeps no signing secret.
|
|
20
|
+
|
|
21
|
+
**Coming from a hand-built client?** Compare your functions with `sql/001_connection.sql` before switching. Supabase finds a function by the names of its arguments, so one that takes an extra argument (an old `p_signing_secret`, say) or names one differently is not found, and every renewal save fails. A renewal that cannot be saved loses the new renewal token, and QBO Sync then ends the connection. Change the functions first, in a way the old code still works with (give the extra argument a default), switch, then drop what is left.
|
|
22
|
+
|
|
23
|
+
## 2. Create the client
|
|
24
|
+
|
|
25
|
+
On Supabase, with a **service-role** client:
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import { QbosyncClient } from "@company-six/qbosync";
|
|
29
|
+
import { createSupabaseStores } from "@company-six/qbosync/supabase";
|
|
30
|
+
|
|
31
|
+
export const qbosync = new QbosyncClient({
|
|
32
|
+
baseUrl: process.env.QBOSYNC_URL!,
|
|
33
|
+
appName: "my-app",
|
|
34
|
+
...createSupabaseStores(supabase),
|
|
35
|
+
});
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Make **one client per server** and reuse it, as above: it keeps QBO Sync's public keys once fetched, so a client made per request would fetch them again for every status report.
|
|
39
|
+
|
|
40
|
+
On another database, implement `ConnectionStore` and `ConnectRequestStore` (their comments say what each method must promise) and pass them as `connections` and `connectRequests`.
|
|
41
|
+
|
|
42
|
+
## 3. Connect
|
|
43
|
+
|
|
44
|
+
When an admin of your app presses Connect:
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
const opened = await qbosync.openConnectRequest({
|
|
48
|
+
redirectUri: "https://my-app.com/qbosync/callback",
|
|
49
|
+
startedBy: admin.email,
|
|
50
|
+
});
|
|
51
|
+
if (opened.ok) redirect(opened.verificationUri);
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
A QBO Sync administrator approves then, or later. Collect the connection from a job your app runs on a schedule, and from the page the admin comes back to. It is safe to call often: only one server collects.
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
const collected = await qbosync.collectConnectRequest();
|
|
58
|
+
// collected.state: "connected", "waiting", "denied", "expired", "failed", "none", "other" or "unavailable"
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
From then on the client renews its tokens by itself. If QBO Sync ends the connection, calls answer `reason: "ended"` and an admin presses Connect again.
|
|
62
|
+
|
|
63
|
+
For a settings page, or to know whether there is anything to send with, read the connection without calling QBO Sync:
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
const now = await qbosync.connectionState();
|
|
67
|
+
// now.state: "connected" (who, when), "ended" (when, and why in words), "not_connected" or "unavailable"
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## 4. Connect a company
|
|
71
|
+
|
|
72
|
+
A company comes to exist in QBO Sync when your app asks for a sign-in link for it. This is what a company's own Connect button calls:
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
const link = await qbosync.createSignInLink({ email: user.email, company: "Acme", role: "company_user" });
|
|
76
|
+
if (link.ok) redirect(link.url); // works once, for five minutes
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
The link is always QBO Sync's own sign-in page on the address the client calls; any other answer is refused as `unexpected_answer`. If QBO Sync refuses (the app may not sign people in yet, say), `link.detail` is its message, written for the person.
|
|
80
|
+
|
|
81
|
+
`company` is the name your documents carry as `company_key`. Ask which of your companies have connected with `qbosync.listCompanies()`, and send a company's documents only once it has: QBO Sync refuses the rest.
|
|
82
|
+
|
|
83
|
+
**Does your app sell for itself?** Then it is a company too. Say so on its first Connect:
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
const opened = await qbosync.openConnectRequest({ redirectUri, startedBy: admin.email, ownCompany: true });
|
|
87
|
+
```
|
|
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`: you need no sign-in links and no Connect button for that company. (Staff can also set this up before your first Connect, by adding your app as one that sends for *only itself*.) 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
|
+
|
|
91
|
+
## 5. Send documents
|
|
92
|
+
|
|
93
|
+
```ts
|
|
94
|
+
const sent = await qbosync.sendDocuments([invoice, creditMemo]);
|
|
95
|
+
|
|
96
|
+
for (const { source_ref, outcome } of sent.results) {
|
|
97
|
+
if (outcome.kind === "sent") markSent(source_ref); // QBO Sync holds it. Never send it again.
|
|
98
|
+
if (outcome.kind === "parked") needsAPerson(source_ref, outcome.error); // sending again changes nothing
|
|
99
|
+
if (outcome.kind === "failure") retryLater(source_ref); // not the document's fault
|
|
100
|
+
}
|
|
101
|
+
if (!sent.ok) console.error(sent.error); // a batch failed; what has no result here is safe to send again
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
Each document is checked against the contract first; one that fails is not sent and does not stop the others. You can run the same check yourself, in the browser too, with `checkDocument` from `@company-six/qbosync/contract`.
|
|
105
|
+
|
|
106
|
+
**A document is sent once.** QBO Sync keeps it exactly as first received, so `version` is 1 and a later version is refused. To correct an invoice, send a credit memo, or have a person roll it back in QBO Sync. Sending again after a failure is always safe: QBO Sync answers `unchanged`.
|
|
107
|
+
|
|
108
|
+
Apps send invoices and credit memos only. 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.
|
|
109
|
+
|
|
110
|
+
## 6. Receive status reports
|
|
111
|
+
|
|
112
|
+
QBO Sync posts what happened to your documents to your callback address + `STATUS_REPORT_PATH` (`/api/qbosync/status`). Pass the body **exactly as it arrived**, before any parsing:
|
|
113
|
+
|
|
114
|
+
```ts
|
|
115
|
+
import { SIGNATURE_HEADER, isNewerReport } from "@company-six/qbosync";
|
|
116
|
+
|
|
117
|
+
export async function POST(req: Request) {
|
|
118
|
+
const checked = await qbosync.checkStatusReports(await req.text(), req.headers.get(SIGNATURE_HEADER));
|
|
119
|
+
if (!checked.ok) return Response.json({ error: checked.error }, { status: checked.status });
|
|
120
|
+
|
|
121
|
+
// Every report in the order it came: one that reads, or why it does not.
|
|
122
|
+
const results = [];
|
|
123
|
+
for (const entry of checked.entries) {
|
|
124
|
+
if ("invalid" in entry) {
|
|
125
|
+
const { source_ref = "", document_type = "", reasons } = entry.invalid;
|
|
126
|
+
results.push({ source_ref, document_type, result: "error", reason: reasons.join("; ") });
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
const { report } = entry;
|
|
130
|
+
const id = { source_ref: report.source_ref, document_type: report.document_type };
|
|
131
|
+
const row = await findDocument(report.source_ref);
|
|
132
|
+
if (!row) results.push({ ...id, result: "unknown" });
|
|
133
|
+
else if (!isNewerReport(report, row.lastSequence)) results.push({ ...id, result: "stale" });
|
|
134
|
+
else {
|
|
135
|
+
await saveStatus(row, report);
|
|
136
|
+
results.push({ ...id, result: "applied" });
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return Response.json({ results });
|
|
140
|
+
}
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
Answer `error` for a report you could not apply, and for every invalid one: QBO Sync sends those again later. Keep the sequence you applied for each document: a delayed report must not hide a newer one. (`checked.reports` and `checked.invalid` hold the same reports split in two, for an app that does not answer in order.)
|
|
144
|
+
|
|
145
|
+
### Testing your status report route
|
|
146
|
+
|
|
147
|
+
Only QBO Sync can sign a real report, so tests make their own key. `@company-six/qbosync/testing` signs a call the way QBO Sync does and answers the public half where the client looks for it:
|
|
148
|
+
|
|
149
|
+
```ts
|
|
150
|
+
import { SIGNATURE_HEADER } from "@company-six/qbosync";
|
|
151
|
+
import { createTestSigningKey, signStatusReports, testKeysFetch } from "@company-six/qbosync/testing";
|
|
152
|
+
|
|
153
|
+
const key = createTestSigningKey();
|
|
154
|
+
const client = new QbosyncClient({ baseUrl, appName: "my-app", ...stores, fetch: testKeysFetch(baseUrl, [key]) });
|
|
155
|
+
const { body, headers } = signStatusReports(key, { reports: [report] }); // a third argument makes an old signature
|
|
156
|
+
const checked = await client.checkStatusReports(body, headers[SIGNATURE_HEADER]);
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
Never use these outside tests: anyone can make such a key.
|
|
160
|
+
|
|
161
|
+
## 7. A daily report (optional)
|
|
162
|
+
|
|
163
|
+
Each morning, set the invoices and credit memos your app created the day before beside what QBO Sync did with them: posted with the same amounts, posted but different, waiting, failed, or never received. QBO Sync answers for each document; your app picks the documents, compares, keeps the report and shows it. Fixing anything (a resync, a rollback) is done in QBO Sync.
|
|
164
|
+
|
|
165
|
+
To ask about any number of your documents (in batches of 100, answered in the order asked):
|
|
166
|
+
|
|
167
|
+
```ts
|
|
168
|
+
const asked = await qbosync.documentReport(refs);
|
|
169
|
+
// each entry: its status, QuickBooks number, what was posted and the nightly check, or sync_status "unknown" if never received
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
The rest of this section builds the report around that call. Your app does all of it; only the tables come with the SDK.
|
|
173
|
+
|
|
174
|
+
### Add the report tables
|
|
175
|
+
|
|
176
|
+
Run [`sql/002_daily_report.sql`](sql/002_daily_report.sql) once, as you ran the first file (it is in `node_modules/@company-six/qbosync/sql/`). It adds:
|
|
177
|
+
|
|
178
|
+
- **`qbosync_report_runs`**: one row per run. It holds the day, its window in UTC, whether it is running, done or failed, and a finished day's totals.
|
|
179
|
+
- **`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.
|
|
180
|
+
- **`qbosync_report_start(day, time_zone)`**, **`qbosync_report_finish(run_id, counts)`** and **`qbosync_report_fail(run_id, error)`**.
|
|
181
|
+
|
|
182
|
+
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.
|
|
183
|
+
|
|
184
|
+
### Run it each morning
|
|
185
|
+
|
|
186
|
+
Run a scheduled job after QBO Sync's nightly QuickBooks check, which starts at 08:00 UTC (11:00 UTC leaves room). Report **yesterday in your own time zone**. Then make up any earlier day that has no finished report, counting from the first day that has one, oldest first and a few days at most per run. That way a morning that failed fills itself in.
|
|
187
|
+
|
|
188
|
+
```ts
|
|
189
|
+
import type { ReportEntry } from "@company-six/qbosync/contract";
|
|
190
|
+
|
|
191
|
+
const TIME_ZONE = "America/Chicago"; // your app's day
|
|
192
|
+
|
|
193
|
+
async function reportDay(day: string) { // "2026-11-08"
|
|
194
|
+
const { data: run, error } = await supabase.rpc("qbosync_report_start", { p_report_date: day, p_time_zone: TIME_ZONE });
|
|
195
|
+
if (error) throw new Error(error.message); // another run is running: stop, the next one makes the day up
|
|
196
|
+
|
|
197
|
+
try {
|
|
198
|
+
// From YOUR records: the documents you created in [window_start, window_end), each with the ref you sent it under.
|
|
199
|
+
const docs = await documentsCreated(run.window_start, run.window_end);
|
|
200
|
+
const asked = await qbosync.documentReport(docs.map((d) => d.source_ref));
|
|
201
|
+
if (!asked.ok) throw new Error(asked.error);
|
|
202
|
+
|
|
203
|
+
const rows = docs.map((doc, i) => ({
|
|
204
|
+
run_id: run.id,
|
|
205
|
+
document_type: doc.document_type, // "invoice" | "credit_memo"
|
|
206
|
+
source_ref: doc.source_ref,
|
|
207
|
+
company_key: doc.company_key,
|
|
208
|
+
document_id: doc.id,
|
|
209
|
+
label: doc.order_number,
|
|
210
|
+
...compare(doc, asked.documents[i]),
|
|
211
|
+
}));
|
|
212
|
+
if (rows.length) {
|
|
213
|
+
const inserted = await supabase.from("qbosync_report_rows").insert(rows);
|
|
214
|
+
if (inserted.error) throw new Error(inserted.error.message);
|
|
215
|
+
}
|
|
216
|
+
const finished = await supabase.rpc("qbosync_report_finish", { p_run_id: run.id, p_counts: countByResult(rows) });
|
|
217
|
+
if (finished.error) throw new Error(finished.error.message);
|
|
218
|
+
} catch (err) {
|
|
219
|
+
await supabase.rpc("qbosync_report_fail", { p_run_id: run.id, p_error: err instanceof Error ? err.message : String(err) });
|
|
220
|
+
throw err; // stop: the next morning makes this day up
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
Pick the documents **by when your app created them**, not by their issue date or by when QBO Sync received them. QBO Sync may receive a document hours later (a retry, a company with syncing off), and only your app knows when it made it. Leave out what your app never meant to send, such as a company that has not connected. Otherwise every one of its documents reads as never received.
|
|
226
|
+
|
|
227
|
+
Insert in pages of a few hundred rows on a busy day.
|
|
228
|
+
|
|
229
|
+
### Compare
|
|
230
|
+
|
|
231
|
+
Give each document **one** result, checked in this order:
|
|
232
|
+
|
|
233
|
+
| Result | When |
|
|
234
|
+
|---|---|
|
|
235
|
+
| `not_received` | `sync_status` is `"unknown"`: QBO Sync never received it. Keep your own record of the send beside it (`send_status`, `error`). |
|
|
236
|
+
| `failed` | `sync_status` is `"failed"`. Keep QBO Sync's `error`. |
|
|
237
|
+
| `not_posted` | `posted` is null, or `sync_status` is `"deleted"` or `"excluded"`. It is waiting, syncing is off, or it was excluded or rolled back. |
|
|
238
|
+
| `changed_since_posted` | `posted_version` is not `latest_version`, or your document changed after QBO Sync accepted it. A newer version has not been posted. |
|
|
239
|
+
| `differs` | Something below does not match. Say what in `differences` (a JSON array, never empty, in whatever shape your page shows). |
|
|
240
|
+
| `matches` | Everything below matches. |
|
|
241
|
+
|
|
242
|
+
What to check for `differs`:
|
|
243
|
+
|
|
244
|
+
- **Amounts**: your totals against `posted`. `total_cents`, `shipping_cents`, `tax_cents` and, on an invoice, `discount_cents`. Then each line by its `line_ref` (on a credit memo, `applies_to_line_ref`): its `quantity` and `amount_cents`, and any line only one side has. `posted` is in your own terms, in cents, and a credit memo's amounts are positive.
|
|
245
|
+
- **Voided**: a document you voided should read `sync_status: "voided"`, and the other way round. When both are voided, there are no amounts to compare.
|
|
246
|
+
- **QuickBooks**: `check.result` is QBO Sync's nightly read-back. `differs` means someone changed the document in QuickBooks; `check.differences` says how, in sentences you can show. `missing` means it was deleted there. Either one makes the result `differs`. `not_checked` (the check has not reached it yet) does not keep a document from matching.
|
|
247
|
+
|
|
248
|
+
Copy `qbo_number`, `sync_status`, `synced_at` and `check.result` (as `qbo_check`) into every row QBO Sync knows, so the row stands on its own later. For totals, count rows by result and add `documents`: `{ "documents": 12, "matches": 11, "failed": 1 }`.
|
|
249
|
+
|
|
250
|
+
### Show it
|
|
251
|
+
|
|
252
|
+
- **The list of days**: one line per `report_date`, newest first. Show the day's `done` run with its `counts`. A day with no finished run shows its latest failed run with its `error`, so a gap is visible until a later run makes it up.
|
|
253
|
+
- **One day**: its rows, by `company_key`.
|
|
254
|
+
- **Who sees what**: the tables are for your server only. Show each person only the rows of the companies they belong to, through your own route.
|
|
255
|
+
|
|
256
|
+
Keep the report read-only, and send people to QBO Sync to fix a document.
|
|
257
|
+
|
|
258
|
+
## When a call fails
|
|
259
|
+
|
|
260
|
+
Nothing here throws for something QBO Sync or the network did. Every call answers `{ ok: true, ... }` or a failure with an `error` you can show a person, and a `reason`:
|
|
261
|
+
|
|
262
|
+
| `reason` | Meaning | What to do |
|
|
263
|
+
|---|---|---|
|
|
264
|
+
| `not_connected` | Nobody has connected the app yet. | An admin presses Connect. |
|
|
265
|
+
| `ended` | QBO Sync ended the connection. | An admin presses Connect again. |
|
|
266
|
+
| `unavailable` | QBO Sync or your database could not answer. | Try again later. |
|
|
267
|
+
| `rate_limited` | Over your limit. `retryAfterSeconds` says how long to wait; a wait of five seconds or less is handled for you. | Try again after that. |
|
|
268
|
+
| `rejected` | QBO Sync refused the request. | Show `detail`, QBO Sync's own message, when there is one. |
|
|
269
|
+
| `unexpected_answer` | QBO Sync answered, but with something this version cannot use (a link to another address, a list that does not line up). Nothing was applied. | Try again later; if it keeps happening, update the package. |
|
|
270
|
+
|
|
271
|
+
`status` is set whenever QBO Sync answered. With `ended`, `error` says why in words and `endedReason` keeps QBO Sync's own reason for your logs; `endedReasonInWords(reason)` (also in `@company-six/qbosync/contract`, safe in a browser) says a stored reason the same way.
|
|
272
|
+
|
|
273
|
+
## Versions
|
|
274
|
+
|
|
275
|
+
Version 1 of the protocol only grows: new fields are optional, and none is removed or renamed. This package follows semver: a patch fixes something, a minor adds something, and a major may break your code.
|
package/dist/client.d.ts
CHANGED
|
@@ -98,10 +98,14 @@ export declare class QbosyncClient {
|
|
|
98
98
|
private readonly log;
|
|
99
99
|
private readonly keys;
|
|
100
100
|
constructor(config: QbosyncClientConfig);
|
|
101
|
-
/**
|
|
101
|
+
/**
|
|
102
|
+
* An admin pressed Connect: opens a connect request. Send their browser to `verificationUri`. `ownCompany`: the app sells
|
|
103
|
+
* for itself, so on its first Connect QBO Sync offers to make it a company too (0.3.0).
|
|
104
|
+
*/
|
|
102
105
|
openConnectRequest(input: {
|
|
103
106
|
redirectUri: string;
|
|
104
107
|
startedBy: string;
|
|
108
|
+
ownCompany?: boolean;
|
|
105
109
|
}): Promise<OpenResult>;
|
|
106
110
|
/**
|
|
107
111
|
* Collects the connection once a QBO Sync administrator has decided. Safe to call often (on a
|
package/dist/client.js
CHANGED
|
@@ -44,7 +44,10 @@ export class QbosyncClient {
|
|
|
44
44
|
this.keys = new SigningKeys(this.baseUrl, this.fetchImpl);
|
|
45
45
|
}
|
|
46
46
|
// ---- connecting
|
|
47
|
-
/**
|
|
47
|
+
/**
|
|
48
|
+
* An admin pressed Connect: opens a connect request. Send their browser to `verificationUri`. `ownCompany`: the app sells
|
|
49
|
+
* for itself, so on its first Connect QBO Sync offers to make it a company too (0.3.0).
|
|
50
|
+
*/
|
|
48
51
|
async openConnectRequest(input) {
|
|
49
52
|
const { baseUrl, config, fetchImpl, log } = this;
|
|
50
53
|
let clientId = null;
|
package/dist/connectRequest.d.ts
CHANGED
|
@@ -85,6 +85,12 @@ export declare function openConnectRequest(input: {
|
|
|
85
85
|
redirectUri: string;
|
|
86
86
|
/** Who pressed Connect, for the record. */
|
|
87
87
|
startedBy: string;
|
|
88
|
+
/**
|
|
89
|
+
* The app sells for itself, so it is a company too (0.3.0). On a first Connect, QBO Sync's approval page then offers to
|
|
90
|
+
* create that company, under the app's name and connected; the approving admin can untick it. Ignored once the app is
|
|
91
|
+
* registered. It is not a lasting mode: the app can still sign other companies in later.
|
|
92
|
+
*/
|
|
93
|
+
ownCompany?: boolean;
|
|
88
94
|
store: ConnectRequestStore;
|
|
89
95
|
fetchImpl?: typeof fetch;
|
|
90
96
|
log?: Log;
|
package/dist/connectRequest.js
CHANGED
|
@@ -78,7 +78,7 @@ export async function openConnectRequest(input) {
|
|
|
78
78
|
redirect_uri: input.redirectUri,
|
|
79
79
|
code_challenge: challenge,
|
|
80
80
|
code_challenge_method: "S256",
|
|
81
|
-
...(input.clientId ? { client_id: input.clientId } : { client_name: input.appName }),
|
|
81
|
+
...(input.clientId ? { client_id: input.clientId } : { client_name: input.appName, ...(input.ownCompany ? { own_company: "true" } : {}) }),
|
|
82
82
|
};
|
|
83
83
|
let status;
|
|
84
84
|
let body;
|
package/package.json
CHANGED
|
@@ -1,44 +1,44 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@company-six/qbosync",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Connect an app to QBO Sync: send invoices and credit memos to QuickBooks, and hear back what happened to them.",
|
|
5
|
-
"license": "UNLICENSED",
|
|
6
|
-
"type": "module",
|
|
7
|
-
"sideEffects": false,
|
|
8
|
-
"engines": {
|
|
9
|
-
"node": ">=20"
|
|
10
|
-
},
|
|
11
|
-
"exports": {
|
|
12
|
-
".": {
|
|
13
|
-
"types": "./dist/index.d.ts",
|
|
14
|
-
"default": "./dist/index.js"
|
|
15
|
-
},
|
|
16
|
-
"./contract": {
|
|
17
|
-
"types": "./dist/contract/index.d.ts",
|
|
18
|
-
"default": "./dist/contract/index.js"
|
|
19
|
-
},
|
|
20
|
-
"./supabase": {
|
|
21
|
-
"types": "./dist/supabase.d.ts",
|
|
22
|
-
"default": "./dist/supabase.js"
|
|
23
|
-
},
|
|
24
|
-
"./testing": {
|
|
25
|
-
"types": "./dist/testing.d.ts",
|
|
26
|
-
"default": "./dist/testing.js"
|
|
27
|
-
}
|
|
28
|
-
},
|
|
29
|
-
"files": [
|
|
30
|
-
"dist",
|
|
31
|
-
"sql/*.sql",
|
|
32
|
-
"README.md"
|
|
33
|
-
],
|
|
34
|
-
"scripts": {
|
|
35
|
-
"build": "tsc -p tsconfig.json",
|
|
36
|
-
"prepublishOnly": "npm run build"
|
|
37
|
-
},
|
|
38
|
-
"peerDependencies": {
|
|
39
|
-
"zod": "^4.1.13"
|
|
40
|
-
},
|
|
41
|
-
"publishConfig": {
|
|
42
|
-
"access": "public"
|
|
43
|
-
}
|
|
44
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@company-six/qbosync",
|
|
3
|
+
"version": "0.3.1",
|
|
4
|
+
"description": "Connect an app to QBO Sync: send invoices and credit memos to QuickBooks, and hear back what happened to them.",
|
|
5
|
+
"license": "UNLICENSED",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"engines": {
|
|
9
|
+
"node": ">=20"
|
|
10
|
+
},
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"default": "./dist/index.js"
|
|
15
|
+
},
|
|
16
|
+
"./contract": {
|
|
17
|
+
"types": "./dist/contract/index.d.ts",
|
|
18
|
+
"default": "./dist/contract/index.js"
|
|
19
|
+
},
|
|
20
|
+
"./supabase": {
|
|
21
|
+
"types": "./dist/supabase.d.ts",
|
|
22
|
+
"default": "./dist/supabase.js"
|
|
23
|
+
},
|
|
24
|
+
"./testing": {
|
|
25
|
+
"types": "./dist/testing.d.ts",
|
|
26
|
+
"default": "./dist/testing.js"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"dist",
|
|
31
|
+
"sql/*.sql",
|
|
32
|
+
"README.md"
|
|
33
|
+
],
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "tsc -p tsconfig.json",
|
|
36
|
+
"prepublishOnly": "npm run build"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"zod": "^4.1.13"
|
|
40
|
+
},
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"access": "public"
|
|
43
|
+
}
|
|
44
|
+
}
|