@company-six/qbosync 0.6.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 +334 -325
- package/package.json +44 -44
package/README.md
CHANGED
|
@@ -1,325 +1,334 @@
|
|
|
1
|
-
# @company-six/qbosync
|
|
2
|
-
|
|
3
|
-
Connect your app to QBO Sync, which posts your sales and costs to each company's QuickBooks. Your app sends invoices and credit memos, and bills and vendor credits if it records what the company owes its vendors; 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
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
```ts
|
|
94
|
-
const
|
|
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
|
-
|
|
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
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
- **
|
|
304
|
-
- **
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
1
|
+
# @company-six/qbosync
|
|
2
|
+
|
|
3
|
+
Connect your app to QBO Sync, which posts your sales and costs to each company's QuickBooks. Your app sends invoices and credit memos, and bills and vendor credits if it records what the company owes its vendors; 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`. (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.
|
|
99
|
+
|
|
100
|
+
## 5. Send documents
|
|
101
|
+
|
|
102
|
+
```ts
|
|
103
|
+
const sent = await qbosync.sendDocuments([invoice, creditMemo]);
|
|
104
|
+
|
|
105
|
+
for (const { source_ref, outcome } of sent.results) {
|
|
106
|
+
if (outcome.kind === "sent") markSent(source_ref); // QBO Sync holds it. Never send it again.
|
|
107
|
+
if (outcome.kind === "parked") needsAPerson(source_ref, outcome.error); // sending again changes nothing
|
|
108
|
+
if (outcome.kind === "failure") retryLater(source_ref); // not the document's fault
|
|
109
|
+
}
|
|
110
|
+
if (!sent.ok) console.error(sent.error); // a batch failed; what has no result here is safe to send again
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
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`.
|
|
114
|
+
|
|
115
|
+
**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`.
|
|
116
|
+
|
|
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.
|
|
118
|
+
|
|
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:
|
|
120
|
+
|
|
121
|
+
```ts
|
|
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
|
|
124
|
+
item_codes: [
|
|
125
|
+
{ code: "SALES", label: "Products, decoration and other charges" },
|
|
126
|
+
{ code: "SHIPPING", label: "Shipping and freight", description: "Every shipping line" },
|
|
127
|
+
],
|
|
128
|
+
cost_codes: [{ code: "freight", label: "Inbound freight" }],
|
|
129
|
+
});
|
|
130
|
+
if (!listed.ok) console.error(listed.error);
|
|
131
|
+
```
|
|
132
|
+
|
|
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`.
|
|
134
|
+
|
|
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.
|
|
136
|
+
|
|
137
|
+
### Bills and vendor credits (0.5.0)
|
|
138
|
+
|
|
139
|
+
A bill is what the company owes a vendor; a vendor credit is what a vendor gave back. QBO Sync posts them as QuickBooks Bills and Vendor Credits.
|
|
140
|
+
|
|
141
|
+
```ts
|
|
142
|
+
import type { BillDTO } from "@company-six/qbosync/contract";
|
|
143
|
+
|
|
144
|
+
const bill: BillDTO = {
|
|
145
|
+
schema_version: "1", source_ref: "po-2207-bill", company_key: "Acme", version: 1, status: "active",
|
|
146
|
+
type: "bill",
|
|
147
|
+
number: "BT-55120", // the vendor's bill number
|
|
148
|
+
issue_date: "2026-10-18",
|
|
149
|
+
terms_code: "net_30", // or due_date; QuickBooks works the date out from terms
|
|
150
|
+
currency: "USD",
|
|
151
|
+
vendor: { ref: "vendor-77", name: "Blank Tee Supply" }, // one QuickBooks vendor per ref
|
|
152
|
+
customer: { ref: "customer-3141", name: "Corner Market" }, // optional: whose sale this cost belongs to
|
|
153
|
+
lines: [
|
|
154
|
+
{ description: "Freight", amount_cents: 1500, cost_code: "freight" }, // an account line
|
|
155
|
+
{ description: "Black tee, M", amount_cents: 19800, item_code: "TEE-BLK-M", quantity: 48, unit_cost_cents: 412.5 }, // an item line
|
|
156
|
+
],
|
|
157
|
+
total_cents: 21300,
|
|
158
|
+
};
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
- A line is an **account line** (`cost_code`: your word for what was paid for) or an **item line** (`item_code`, `quantity`, `unit_cost_cents`: a product bought, by SKU). Each company maps your cost codes to its accounts once, in QBO Sync's Setup; a bill whose code is not mapped waits, and its status report says so. An item line posts on the product the company chose for its `item_code` in Setup → Products (the same choice your invoices use), else the QuickBooks product with that SKU; a bill never creates a product, so with neither it fails, or waits until the company chooses one if it has set your app to never create products.
|
|
162
|
+
- `customer` (on the bill, or on one line) puts the cost on that customer for job costing; it is not billed to them again.
|
|
163
|
+
- A vendor credit has the same fields, no terms, and may name its bill with `applies_to_bill_ref`. QuickBooks offers the credit when the company next pays that vendor.
|
|
164
|
+
- `number` is unique per vendor among your bills. When QuickBooks already holds a bill from that vendor with that number, the bill waits for a person in QBO Sync to link it or post it anyway.
|
|
165
|
+
- Like an invoice, a bill is kept as first received: correct it with a vendor credit.
|
|
166
|
+
|
|
167
|
+
## 6. Receive status reports
|
|
168
|
+
|
|
169
|
+
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:
|
|
170
|
+
|
|
171
|
+
```ts
|
|
172
|
+
import { SIGNATURE_HEADER, isNewerReport } from "@company-six/qbosync";
|
|
173
|
+
|
|
174
|
+
export async function POST(req: Request) {
|
|
175
|
+
const checked = await qbosync.checkStatusReports(await req.text(), req.headers.get(SIGNATURE_HEADER));
|
|
176
|
+
if (!checked.ok) return Response.json({ error: checked.error }, { status: checked.status });
|
|
177
|
+
|
|
178
|
+
// Every report in the order it came: one that reads, or why it does not.
|
|
179
|
+
const results = [];
|
|
180
|
+
for (const entry of checked.entries) {
|
|
181
|
+
if ("invalid" in entry) {
|
|
182
|
+
const { source_ref = "", document_type = "", reasons } = entry.invalid;
|
|
183
|
+
results.push({ source_ref, document_type, result: "error", reason: reasons.join("; ") });
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
const { report } = entry;
|
|
187
|
+
const id = { source_ref: report.source_ref, document_type: report.document_type };
|
|
188
|
+
const row = await findDocument(report.source_ref);
|
|
189
|
+
if (!row) results.push({ ...id, result: "unknown" });
|
|
190
|
+
else if (!isNewerReport(report, row.lastSequence)) results.push({ ...id, result: "stale" });
|
|
191
|
+
else {
|
|
192
|
+
await saveStatus(row, report);
|
|
193
|
+
results.push({ ...id, result: "applied" });
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return Response.json({ results });
|
|
197
|
+
}
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
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.)
|
|
201
|
+
|
|
202
|
+
### Testing your status report route
|
|
203
|
+
|
|
204
|
+
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:
|
|
205
|
+
|
|
206
|
+
```ts
|
|
207
|
+
import { SIGNATURE_HEADER } from "@company-six/qbosync";
|
|
208
|
+
import { createTestSigningKey, signStatusReports, testKeysFetch } from "@company-six/qbosync/testing";
|
|
209
|
+
|
|
210
|
+
const key = createTestSigningKey();
|
|
211
|
+
const client = new QbosyncClient({ baseUrl, appName: "my-app", ...stores, fetch: testKeysFetch(baseUrl, [key]) });
|
|
212
|
+
const { body, headers } = signStatusReports(key, { reports: [report] }); // a third argument makes an old signature
|
|
213
|
+
const checked = await client.checkStatusReports(body, headers[SIGNATURE_HEADER]);
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
Never use these outside tests: anyone can make such a key.
|
|
217
|
+
|
|
218
|
+
## 7. A daily report (optional)
|
|
219
|
+
|
|
220
|
+
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.
|
|
221
|
+
|
|
222
|
+
To ask about any number of your documents (in batches of 100, answered in the order asked):
|
|
223
|
+
|
|
224
|
+
```ts
|
|
225
|
+
const asked = await qbosync.documentReport(refs);
|
|
226
|
+
// each entry: its status, QuickBooks number, what was posted and the nightly check, or sync_status "unknown" if never received
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
The rest of this section builds the report around that call. Your app does all of it; only the tables come with the SDK.
|
|
230
|
+
|
|
231
|
+
### Add the report tables
|
|
232
|
+
|
|
233
|
+
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:
|
|
234
|
+
|
|
235
|
+
- **`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.
|
|
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.
|
|
237
|
+
- **`qbosync_report_start(day, time_zone)`**, **`qbosync_report_finish(run_id, counts)`** and **`qbosync_report_fail(run_id, error)`**.
|
|
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
|
+
|
|
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.
|
|
242
|
+
|
|
243
|
+
### Run it each morning
|
|
244
|
+
|
|
245
|
+
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.
|
|
246
|
+
|
|
247
|
+
```ts
|
|
248
|
+
import type { ReportEntry } from "@company-six/qbosync/contract";
|
|
249
|
+
|
|
250
|
+
const TIME_ZONE = "America/Chicago"; // your app's day
|
|
251
|
+
|
|
252
|
+
async function reportDay(day: string) { // "2026-11-08"
|
|
253
|
+
const { data: run, error } = await supabase.rpc("qbosync_report_start", { p_report_date: day, p_time_zone: TIME_ZONE });
|
|
254
|
+
if (error) throw new Error(error.message); // another run is running: stop, the next one makes the day up
|
|
255
|
+
|
|
256
|
+
try {
|
|
257
|
+
// From YOUR records: the documents you created in [window_start, window_end), each with the ref you sent it under.
|
|
258
|
+
const docs = await documentsCreated(run.window_start, run.window_end);
|
|
259
|
+
const asked = await qbosync.documentReport(docs.map((d) => d.source_ref));
|
|
260
|
+
if (!asked.ok) throw new Error(asked.error);
|
|
261
|
+
|
|
262
|
+
const rows = docs.map((doc, i) => ({
|
|
263
|
+
run_id: run.id,
|
|
264
|
+
document_type: doc.document_type, // "invoice" | "credit_memo" | "bill" | "vendor_credit"
|
|
265
|
+
source_ref: doc.source_ref,
|
|
266
|
+
company_key: doc.company_key,
|
|
267
|
+
document_id: doc.id,
|
|
268
|
+
label: doc.order_number,
|
|
269
|
+
...compare(doc, asked.documents[i]),
|
|
270
|
+
}));
|
|
271
|
+
if (rows.length) {
|
|
272
|
+
const inserted = await supabase.from("qbosync_report_rows").insert(rows);
|
|
273
|
+
if (inserted.error) throw new Error(inserted.error.message);
|
|
274
|
+
}
|
|
275
|
+
const finished = await supabase.rpc("qbosync_report_finish", { p_run_id: run.id, p_counts: countByResult(rows) });
|
|
276
|
+
if (finished.error) throw new Error(finished.error.message);
|
|
277
|
+
} catch (err) {
|
|
278
|
+
await supabase.rpc("qbosync_report_fail", { p_run_id: run.id, p_error: err instanceof Error ? err.message : String(err) });
|
|
279
|
+
throw err; // stop: the next morning makes this day up
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
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.
|
|
285
|
+
|
|
286
|
+
Insert in pages of a few hundred rows on a busy day.
|
|
287
|
+
|
|
288
|
+
### Compare
|
|
289
|
+
|
|
290
|
+
Give each document **one** result, checked in this order:
|
|
291
|
+
|
|
292
|
+
| Result | When |
|
|
293
|
+
|---|---|
|
|
294
|
+
| `not_received` | `sync_status` is `"unknown"`: QBO Sync never received it. Keep your own record of the send beside it (`send_status`, `error`). |
|
|
295
|
+
| `failed` | `sync_status` is `"failed"`. Keep QBO Sync's `error`. |
|
|
296
|
+
| `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. |
|
|
297
|
+
| `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. |
|
|
298
|
+
| `differs` | Something below does not match. Say what in `differences` (a JSON array, never empty, in whatever shape your page shows). |
|
|
299
|
+
| `matches` | Everything below matches. |
|
|
300
|
+
|
|
301
|
+
What to check for `differs`:
|
|
302
|
+
|
|
303
|
+
- **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.
|
|
304
|
+
- **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.
|
|
305
|
+
- **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.
|
|
306
|
+
|
|
307
|
+
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 }`.
|
|
308
|
+
|
|
309
|
+
### Show it
|
|
310
|
+
|
|
311
|
+
- **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.
|
|
312
|
+
- **One day**: its rows, by `company_key`.
|
|
313
|
+
- **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.
|
|
314
|
+
|
|
315
|
+
Keep the report read-only, and send people to QBO Sync to fix a document.
|
|
316
|
+
|
|
317
|
+
## When a call fails
|
|
318
|
+
|
|
319
|
+
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`:
|
|
320
|
+
|
|
321
|
+
| `reason` | Meaning | What to do |
|
|
322
|
+
|---|---|---|
|
|
323
|
+
| `not_connected` | Nobody has connected the app yet. | An admin presses Connect. |
|
|
324
|
+
| `ended` | QBO Sync ended the connection. | An admin presses Connect again. |
|
|
325
|
+
| `unavailable` | QBO Sync or your database could not answer. | Try again later. |
|
|
326
|
+
| `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. |
|
|
327
|
+
| `rejected` | QBO Sync refused the request. | Show `detail`, QBO Sync's own message, when there is one. |
|
|
328
|
+
| `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. |
|
|
329
|
+
|
|
330
|
+
`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.
|
|
331
|
+
|
|
332
|
+
## Versions
|
|
333
|
+
|
|
334
|
+
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/package.json
CHANGED
|
@@ -1,44 +1,44 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@company-six/qbosync",
|
|
3
|
-
"version": "0.6.
|
|
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
|
-
"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.6.1",
|
|
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
|
+
"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
|
+
}
|