@company-six/qbosync 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +178 -2
- package/dist/client.d.ts +163 -0
- package/dist/client.js +273 -0
- package/dist/connectRequest.d.ts +126 -0
- package/dist/connectRequest.js +188 -0
- package/dist/connection.d.ts +108 -0
- package/dist/connection.js +191 -0
- package/dist/contract/answers.d.ts +103 -0
- package/dist/contract/answers.js +47 -0
- package/dist/contract/common.d.ts +107 -0
- package/dist/contract/common.js +86 -0
- package/dist/contract/creditMemo.d.ts +77 -0
- package/dist/contract/creditMemo.js +65 -0
- package/dist/contract/index.d.ts +145 -0
- package/dist/contract/index.js +39 -0
- package/dist/contract/invoice.d.ts +88 -0
- package/dist/contract/invoice.js +74 -0
- package/dist/contract/protocol.d.ts +28 -0
- package/dist/contract/protocol.js +26 -0
- package/dist/contract/statusReport.d.ts +20 -0
- package/dist/contract/statusReport.js +17 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +4 -0
- package/dist/statusReports.d.ts +82 -0
- package/dist/statusReports.js +129 -0
- package/dist/supabase.d.ts +38 -0
- package/dist/supabase.js +67 -0
- package/dist/testing.d.ts +38 -0
- package/dist/testing.js +31 -0
- package/package.json +42 -4
- package/sql/001_connection.sql +368 -0
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What QBO Sync answers an app. qbosync's own routes build these same types.
|
|
3
|
+
*/
|
|
4
|
+
export type ReceiveResultCode = "accepted" | "unchanged" | "stale" | "version_conflict" | "refused" | "ignored" | "error";
|
|
5
|
+
/** One document's result, in the order sent. */
|
|
6
|
+
export interface ReceiveResult {
|
|
7
|
+
index: number;
|
|
8
|
+
result: ReceiveResultCode;
|
|
9
|
+
type?: string;
|
|
10
|
+
source_ref?: string;
|
|
11
|
+
version?: number;
|
|
12
|
+
reasons?: string[];
|
|
13
|
+
changed_since_posted?: boolean;
|
|
14
|
+
}
|
|
15
|
+
/** What an app should do about one document's answer. */
|
|
16
|
+
export type SendOutcome =
|
|
17
|
+
/** QBO Sync holds it. Never send it again. */
|
|
18
|
+
{
|
|
19
|
+
kind: "sent";
|
|
20
|
+
unchanged: boolean;
|
|
21
|
+
changedSincePosted: boolean;
|
|
22
|
+
}
|
|
23
|
+
/** Sending it again changes nothing: a person has to look at it. */
|
|
24
|
+
| {
|
|
25
|
+
kind: "parked";
|
|
26
|
+
status: "refused" | "conflict";
|
|
27
|
+
error: string;
|
|
28
|
+
}
|
|
29
|
+
/** Not the document's fault: try again later. */
|
|
30
|
+
| {
|
|
31
|
+
kind: "failure";
|
|
32
|
+
error: string;
|
|
33
|
+
};
|
|
34
|
+
/** One entry of an answer, as read: a newer QBO Sync may answer something this version does not know. */
|
|
35
|
+
export interface SendAnswer {
|
|
36
|
+
result?: unknown;
|
|
37
|
+
reasons?: unknown;
|
|
38
|
+
changed_since_posted?: unknown;
|
|
39
|
+
}
|
|
40
|
+
/** Turns one answer into what the app should do about the document. */
|
|
41
|
+
export declare function outcomeOf(answer: SendAnswer | null | undefined): SendOutcome;
|
|
42
|
+
export interface PostedLine {
|
|
43
|
+
/** An invoice line's own ref. */
|
|
44
|
+
line_ref?: string;
|
|
45
|
+
/** A credit memo line: the invoice line it gives back, when it names one. */
|
|
46
|
+
applies_to_line_ref?: string;
|
|
47
|
+
quantity: number;
|
|
48
|
+
amount_cents: number;
|
|
49
|
+
}
|
|
50
|
+
/** What was posted, in the app's own terms. A credit memo has no discount. */
|
|
51
|
+
export interface PostedAmounts {
|
|
52
|
+
total_cents: number;
|
|
53
|
+
shipping_cents: number;
|
|
54
|
+
tax_cents: number;
|
|
55
|
+
discount_cents?: number;
|
|
56
|
+
lines: PostedLine[];
|
|
57
|
+
}
|
|
58
|
+
export interface PostedCheck {
|
|
59
|
+
result: "matches" | "differs" | "missing" | "not_checked";
|
|
60
|
+
/** When the nightly check last read it from QuickBooks; null when not checked. */
|
|
61
|
+
checked_at: string | null;
|
|
62
|
+
/** What differs, as sentences; empty unless `differs` or `missing`. */
|
|
63
|
+
differences: string[];
|
|
64
|
+
}
|
|
65
|
+
export interface UnknownEntry {
|
|
66
|
+
source_ref: string;
|
|
67
|
+
sync_status: "unknown";
|
|
68
|
+
}
|
|
69
|
+
export interface KnownEntry {
|
|
70
|
+
source_ref: string;
|
|
71
|
+
document_type: "invoice" | "credit_memo";
|
|
72
|
+
company_key: string | null;
|
|
73
|
+
company: string;
|
|
74
|
+
latest_version: number | null;
|
|
75
|
+
posted_version: number | null;
|
|
76
|
+
sync_status: string;
|
|
77
|
+
qbo_number: string | null;
|
|
78
|
+
synced_at: string | null;
|
|
79
|
+
error: string | null;
|
|
80
|
+
posted: PostedAmounts | null;
|
|
81
|
+
check: PostedCheck | null;
|
|
82
|
+
}
|
|
83
|
+
/** QBO Sync's answer about one document; `sync_status: "unknown"` means it never received it. */
|
|
84
|
+
export type ReportEntry = UnknownEntry | KnownEntry;
|
|
85
|
+
export type AppQuickBooksStatus = "connected" | "needs_reauth" | "not_connected";
|
|
86
|
+
export interface AppCompanyStatus {
|
|
87
|
+
/** The name this app uses for the company: its documents' `company_key`. */
|
|
88
|
+
company_key: string;
|
|
89
|
+
/** The company's name in QBO Sync. */
|
|
90
|
+
name: string;
|
|
91
|
+
connected: boolean;
|
|
92
|
+
/** The first time someone came in through this app for the company; null while not connected. */
|
|
93
|
+
connected_at: string | null;
|
|
94
|
+
quickbooks: AppQuickBooksStatus;
|
|
95
|
+
/** Whether the company's documents post to its QuickBooks (its sync switch). */
|
|
96
|
+
syncing: boolean;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Why QBO Sync ended a connection, as a sentence for the people who read it. QBO Sync's own answer
|
|
100
|
+
* (`invalid_grant: Invalid grant: refresh token is invalid`) is kept in `ended_reason` and logged, where
|
|
101
|
+
* it helps whoever investigates; pages and errors a person may see use this instead. Safe in a browser.
|
|
102
|
+
*/
|
|
103
|
+
export declare function endedReasonInWords(reason: string | null | undefined): string;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What QBO Sync answers an app. qbosync's own routes build these same types.
|
|
3
|
+
*/
|
|
4
|
+
function reasonsOf(answer) {
|
|
5
|
+
if (!Array.isArray(answer.reasons))
|
|
6
|
+
return "";
|
|
7
|
+
return answer.reasons.filter((x) => typeof x === "string" && x.trim() !== "").join("; ");
|
|
8
|
+
}
|
|
9
|
+
/** Turns one answer into what the app should do about the document. */
|
|
10
|
+
export function outcomeOf(answer) {
|
|
11
|
+
const result = typeof answer?.result === "string" ? answer.result : "";
|
|
12
|
+
const reasons = answer ? reasonsOf(answer) : "";
|
|
13
|
+
switch (result) {
|
|
14
|
+
case "accepted":
|
|
15
|
+
return { kind: "sent", unchanged: false, changedSincePosted: answer?.changed_since_posted === true };
|
|
16
|
+
case "unchanged":
|
|
17
|
+
return { kind: "sent", unchanged: true, changedSincePosted: false };
|
|
18
|
+
case "refused":
|
|
19
|
+
return { kind: "parked", status: "refused", error: reasons || "QBO Sync refused the document (no reason given)" };
|
|
20
|
+
case "stale":
|
|
21
|
+
return { kind: "parked", status: "conflict", error: `stale: ${reasons || "QBO Sync already holds a higher version"}` };
|
|
22
|
+
case "version_conflict":
|
|
23
|
+
return { kind: "parked", status: "conflict", error: `version_conflict: ${reasons || "QBO Sync holds this version with different content"}` };
|
|
24
|
+
case "ignored":
|
|
25
|
+
return { kind: "failure", error: `ignored: ${reasons || "QBO Sync did not take the document"}` };
|
|
26
|
+
case "error":
|
|
27
|
+
return { kind: "failure", error: `QBO Sync could not store it: ${reasons || "no reason given"}` };
|
|
28
|
+
default:
|
|
29
|
+
return { kind: "failure", error: `QBO Sync answered ${JSON.stringify(answer?.result ?? null)}, which this version of the SDK does not know` };
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Why QBO Sync ended a connection, as a sentence for the people who read it. QBO Sync's own answer
|
|
34
|
+
* (`invalid_grant: Invalid grant: refresh token is invalid`) is kept in `ended_reason` and logged, where
|
|
35
|
+
* it helps whoever investigates; pages and errors a person may see use this instead. Safe in a browser.
|
|
36
|
+
*/
|
|
37
|
+
export function endedReasonInWords(reason) {
|
|
38
|
+
const code = (reason ?? "").trim().split(":")[0].trim();
|
|
39
|
+
switch (code) {
|
|
40
|
+
case "invalid_grant":
|
|
41
|
+
return "QBO Sync no longer accepts this connection: QBO Sync staff revoked it, or it went unused for 90 days.";
|
|
42
|
+
case "invalid_client":
|
|
43
|
+
return "QBO Sync no longer recognises this app: its registration was removed or replaced in QBO Sync.";
|
|
44
|
+
default:
|
|
45
|
+
return "QBO Sync ended this connection.";
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
/**
|
|
3
|
+
* The pieces every v1 document shares. These schemas ARE the contract: QBO Sync checks
|
|
4
|
+
* every document it receives with this same code.
|
|
5
|
+
*
|
|
6
|
+
* Only rules a document can be checked against ON ITS OWN live here. The ones that need
|
|
7
|
+
* QBO Sync's records (a number already used within the company, an unknown invoice, a
|
|
8
|
+
* company the app is not linked to) are checked when the document arrives.
|
|
9
|
+
*
|
|
10
|
+
* Objects are STRICT: a key the contract does not define is refused, not dropped. A
|
|
11
|
+
* misspelt optional field (`accrued_fee` for `accrued_fees`) would otherwise vanish
|
|
12
|
+
* silently and post a document without it.
|
|
13
|
+
*/
|
|
14
|
+
/** Money: whole cents, never negative. "All money is whole cents." */
|
|
15
|
+
export declare const cents: z.ZodInt;
|
|
16
|
+
/** Money the contract says is positive: more than zero. */
|
|
17
|
+
export declare const positiveCents: z.ZodInt;
|
|
18
|
+
/** Money that may be either sign: a payout member (payments +, refunds −). */
|
|
19
|
+
export declare const signedCents: z.ZodInt;
|
|
20
|
+
/** A required piece of text: not empty once surrounding spaces are removed. */
|
|
21
|
+
export declare const text: z.ZodString;
|
|
22
|
+
/**
|
|
23
|
+
* A calendar date, YYYY-MM-DD, that exists (no 2026-02-30). One check does both: a string
|
|
24
|
+
* survives the round trip only if it IS the YYYY-MM-DD form of a real day. (A separate
|
|
25
|
+
* format regex was removed: mutation testing showed it could never be the deciding check.)
|
|
26
|
+
*/
|
|
27
|
+
export declare const isoDate: z.ZodString;
|
|
28
|
+
/** An ISO 4217 currency code, upper case: "USD". */
|
|
29
|
+
export declare const currency: z.ZodString;
|
|
30
|
+
/**
|
|
31
|
+
* `order_ref`: "<system>:<id>", e.g. "shopify:5551234567". Two apps' order ids can
|
|
32
|
+
* collide; the system prefix is what keeps them apart, so it is required.
|
|
33
|
+
*/
|
|
34
|
+
export declare const orderRef: z.ZodString;
|
|
35
|
+
/** A document's number as QuickBooks holds it: at most 21 characters (QuickBooks' DocNumber limit). */
|
|
36
|
+
export declare const docNumber: z.ZodPipe<z.ZodString, z.ZodString>;
|
|
37
|
+
/** Written into the memo in this order. */
|
|
38
|
+
export declare const references: z.ZodArray<z.ZodObject<{
|
|
39
|
+
label: z.ZodString;
|
|
40
|
+
value: z.ZodString;
|
|
41
|
+
}, z.core.$strict>>;
|
|
42
|
+
export declare const address: z.ZodObject<{
|
|
43
|
+
line1: z.ZodOptional<z.ZodString>;
|
|
44
|
+
line2: z.ZodOptional<z.ZodString>;
|
|
45
|
+
city: z.ZodOptional<z.ZodString>;
|
|
46
|
+
region: z.ZodOptional<z.ZodString>;
|
|
47
|
+
postal_code: z.ZodOptional<z.ZodString>;
|
|
48
|
+
country: z.ZodOptional<z.ZodString>;
|
|
49
|
+
}, z.core.$strict>;
|
|
50
|
+
/** One QuickBooks customer per `ref` per company. */
|
|
51
|
+
export declare const customer: z.ZodObject<{
|
|
52
|
+
ref: z.ZodString;
|
|
53
|
+
name: z.ZodString;
|
|
54
|
+
company: z.ZodOptional<z.ZodString>;
|
|
55
|
+
email: z.ZodOptional<z.ZodString>;
|
|
56
|
+
bill_to: z.ZodOptional<z.ZodObject<{
|
|
57
|
+
line1: z.ZodOptional<z.ZodString>;
|
|
58
|
+
line2: z.ZodOptional<z.ZodString>;
|
|
59
|
+
city: z.ZodOptional<z.ZodString>;
|
|
60
|
+
region: z.ZodOptional<z.ZodString>;
|
|
61
|
+
postal_code: z.ZodOptional<z.ZodString>;
|
|
62
|
+
country: z.ZodOptional<z.ZodString>;
|
|
63
|
+
}, z.core.$strict>>;
|
|
64
|
+
ship_to: z.ZodOptional<z.ZodObject<{
|
|
65
|
+
line1: z.ZodOptional<z.ZodString>;
|
|
66
|
+
line2: z.ZodOptional<z.ZodString>;
|
|
67
|
+
city: z.ZodOptional<z.ZodString>;
|
|
68
|
+
region: z.ZodOptional<z.ZodString>;
|
|
69
|
+
postal_code: z.ZodOptional<z.ZodString>;
|
|
70
|
+
country: z.ZodOptional<z.ZodString>;
|
|
71
|
+
}, z.core.$strict>>;
|
|
72
|
+
}, z.core.$strict>;
|
|
73
|
+
/** A fee, defined by the app: "stripe_fee", "marketplace_commission"… */
|
|
74
|
+
export declare const fee: z.ZodObject<{
|
|
75
|
+
code: z.ZodString;
|
|
76
|
+
amount_cents: z.ZodInt;
|
|
77
|
+
description: z.ZodOptional<z.ZodString>;
|
|
78
|
+
}, z.core.$strict>;
|
|
79
|
+
/** A fee given back (refunds): positive. */
|
|
80
|
+
export declare const returnedFee: z.ZodObject<{
|
|
81
|
+
code: z.ZodString;
|
|
82
|
+
description: z.ZodOptional<z.ZodString>;
|
|
83
|
+
amount_cents: z.ZodInt;
|
|
84
|
+
}, z.core.$strict>;
|
|
85
|
+
/** Every document's envelope. `version` is the app's own counter, never QBO Sync's revision. */
|
|
86
|
+
export declare const envelope: {
|
|
87
|
+
schema_version: z.ZodLiteral<"1">;
|
|
88
|
+
source_ref: z.ZodString;
|
|
89
|
+
company_key: z.ZodString;
|
|
90
|
+
version: z.ZodInt;
|
|
91
|
+
status: z.ZodEnum<{
|
|
92
|
+
active: "active";
|
|
93
|
+
voided: "voided";
|
|
94
|
+
}>;
|
|
95
|
+
};
|
|
96
|
+
/**
|
|
97
|
+
* A line's `amount_cents` must be exactly `quantity × unit_price_cents`. Compared with a
|
|
98
|
+
* tolerance only for floating point (a quantity of 0.1 × 30 cents is 3.0000000000000004);
|
|
99
|
+
* a product that is not whole cents can never match an integer amount, so it is refused.
|
|
100
|
+
*/
|
|
101
|
+
export declare function lineAmountMatches(line: {
|
|
102
|
+
quantity: number;
|
|
103
|
+
unit_price_cents: number;
|
|
104
|
+
amount_cents: number;
|
|
105
|
+
}): boolean;
|
|
106
|
+
/** The sum of some cents, as an integer. */
|
|
107
|
+
export declare const sum: (values: number[]) => number;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
/**
|
|
3
|
+
* The pieces every v1 document shares. These schemas ARE the contract: QBO Sync checks
|
|
4
|
+
* every document it receives with this same code.
|
|
5
|
+
*
|
|
6
|
+
* Only rules a document can be checked against ON ITS OWN live here. The ones that need
|
|
7
|
+
* QBO Sync's records (a number already used within the company, an unknown invoice, a
|
|
8
|
+
* company the app is not linked to) are checked when the document arrives.
|
|
9
|
+
*
|
|
10
|
+
* Objects are STRICT: a key the contract does not define is refused, not dropped. A
|
|
11
|
+
* misspelt optional field (`accrued_fee` for `accrued_fees`) would otherwise vanish
|
|
12
|
+
* silently and post a document without it.
|
|
13
|
+
*/
|
|
14
|
+
/** Money: whole cents, never negative. "All money is whole cents." */
|
|
15
|
+
export const cents = z.int("must be whole cents").min(0, "must not be negative");
|
|
16
|
+
/** Money the contract says is positive: more than zero. */
|
|
17
|
+
export const positiveCents = z.int("must be whole cents").positive("must be positive");
|
|
18
|
+
/** Money that may be either sign: a payout member (payments +, refunds −). */
|
|
19
|
+
export const signedCents = z.int("must be whole cents");
|
|
20
|
+
/** A required piece of text: not empty once surrounding spaces are removed. */
|
|
21
|
+
export const text = z.string().refine((s) => s.trim().length > 0, "must not be empty");
|
|
22
|
+
/**
|
|
23
|
+
* A calendar date, YYYY-MM-DD, that exists (no 2026-02-30). One check does both: a string
|
|
24
|
+
* survives the round trip only if it IS the YYYY-MM-DD form of a real day. (A separate
|
|
25
|
+
* format regex was removed: mutation testing showed it could never be the deciding check.)
|
|
26
|
+
*/
|
|
27
|
+
export const isoDate = z.string().refine((s) => {
|
|
28
|
+
const d = new Date(`${s}T00:00:00Z`);
|
|
29
|
+
return !Number.isNaN(d.getTime()) && d.toISOString().slice(0, 10) === s;
|
|
30
|
+
}, "must be a date as YYYY-MM-DD");
|
|
31
|
+
/** An ISO 4217 currency code, upper case: "USD". */
|
|
32
|
+
export const currency = z.string().regex(/^[A-Z]{3}$/, "must be a three-letter currency code such as USD");
|
|
33
|
+
/**
|
|
34
|
+
* `order_ref`: "<system>:<id>", e.g. "shopify:5551234567". Two apps' order ids can
|
|
35
|
+
* collide; the system prefix is what keeps them apart, so it is required.
|
|
36
|
+
*/
|
|
37
|
+
export const orderRef = z
|
|
38
|
+
.string()
|
|
39
|
+
.regex(/^[a-z][a-z0-9_-]*:\S+$/, 'must be "<system>:<id>", e.g. "shopify:5551234567"');
|
|
40
|
+
/** A document's number as QuickBooks holds it: at most 21 characters (QuickBooks' DocNumber limit). */
|
|
41
|
+
export const docNumber = text.pipe(z.string().max(21, "must be at most 21 characters"));
|
|
42
|
+
/** Written into the memo in this order. */
|
|
43
|
+
export const references = z.array(z.strictObject({ label: text, value: text }));
|
|
44
|
+
export const address = z.strictObject({
|
|
45
|
+
line1: z.string().optional(),
|
|
46
|
+
line2: z.string().optional(),
|
|
47
|
+
city: z.string().optional(),
|
|
48
|
+
region: z.string().optional(),
|
|
49
|
+
postal_code: z.string().optional(),
|
|
50
|
+
country: z.string().optional(),
|
|
51
|
+
});
|
|
52
|
+
/** One QuickBooks customer per `ref` per company. */
|
|
53
|
+
export const customer = z.strictObject({
|
|
54
|
+
ref: text,
|
|
55
|
+
name: text,
|
|
56
|
+
company: z.string().optional(),
|
|
57
|
+
email: z.string().optional(),
|
|
58
|
+
bill_to: address.optional(),
|
|
59
|
+
ship_to: address.optional(),
|
|
60
|
+
});
|
|
61
|
+
/** A fee, defined by the app: "stripe_fee", "marketplace_commission"… */
|
|
62
|
+
export const fee = z.strictObject({
|
|
63
|
+
code: text,
|
|
64
|
+
amount_cents: cents,
|
|
65
|
+
description: z.string().optional(),
|
|
66
|
+
});
|
|
67
|
+
/** A fee given back (refunds): positive. */
|
|
68
|
+
export const returnedFee = fee.extend({ amount_cents: positiveCents });
|
|
69
|
+
/** Every document's envelope. `version` is the app's own counter, never QBO Sync's revision. */
|
|
70
|
+
export const envelope = {
|
|
71
|
+
schema_version: z.literal("1", 'must be "1"'),
|
|
72
|
+
source_ref: text,
|
|
73
|
+
company_key: text,
|
|
74
|
+
version: z.int("must be a whole number").min(1, "must be 1 or more"),
|
|
75
|
+
status: z.enum(["active", "voided"], 'must be "active" or "voided"'),
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* A line's `amount_cents` must be exactly `quantity × unit_price_cents`. Compared with a
|
|
79
|
+
* tolerance only for floating point (a quantity of 0.1 × 30 cents is 3.0000000000000004);
|
|
80
|
+
* a product that is not whole cents can never match an integer amount, so it is refused.
|
|
81
|
+
*/
|
|
82
|
+
export function lineAmountMatches(line) {
|
|
83
|
+
return Math.abs(line.quantity * line.unit_price_cents - line.amount_cents) < 1e-6;
|
|
84
|
+
}
|
|
85
|
+
/** The sum of some cents, as an integer. */
|
|
86
|
+
export const sum = (values) => values.reduce((a, b) => a + b, 0);
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
/** A credit memo line: reverses an invoice line (`applies_to_line_ref`), or is a money-only credit. */
|
|
3
|
+
export declare const creditMemoLine: z.ZodObject<{
|
|
4
|
+
applies_to_line_ref: z.ZodOptional<z.ZodString>;
|
|
5
|
+
item_code: z.ZodOptional<z.ZodString>;
|
|
6
|
+
description: z.ZodString;
|
|
7
|
+
quantity: z.ZodNumber;
|
|
8
|
+
unit_price_cents: z.ZodInt;
|
|
9
|
+
amount_cents: z.ZodInt;
|
|
10
|
+
}, z.core.$strict>;
|
|
11
|
+
/**
|
|
12
|
+
* Reduces what the customer owes on one invoice; moves no cash. Posted as a CreditMemo.
|
|
13
|
+
*
|
|
14
|
+
* `lines` may be empty when the credit gives back only shipping or tax: those are fields here,
|
|
15
|
+
* not lines, and posting turns them into lines on the invoice's shipping and tax items.
|
|
16
|
+
*
|
|
17
|
+
* Refused on its own: no lines and no shipping or tax to give back; a line whose amount is not
|
|
18
|
+
* quantity × unit price; a total that is not lines + shipping + tax; a line reversing an invoice
|
|
19
|
+
* line without its `item_code`.
|
|
20
|
+
* Whether the invoice and the line exist is checked when the document arrives.
|
|
21
|
+
*/
|
|
22
|
+
export declare const creditMemoSchema: z.ZodObject<{
|
|
23
|
+
type: z.ZodLiteral<"credit_memo">;
|
|
24
|
+
number: z.ZodPipe<z.ZodString, z.ZodString>;
|
|
25
|
+
issue_date: z.ZodString;
|
|
26
|
+
currency: z.ZodString;
|
|
27
|
+
applies_to_invoice_ref: z.ZodString;
|
|
28
|
+
order_ref: z.ZodOptional<z.ZodString>;
|
|
29
|
+
references: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
30
|
+
label: z.ZodString;
|
|
31
|
+
value: z.ZodString;
|
|
32
|
+
}, z.core.$strict>>>;
|
|
33
|
+
class_key: z.ZodOptional<z.ZodString>;
|
|
34
|
+
customer: z.ZodObject<{
|
|
35
|
+
ref: z.ZodString;
|
|
36
|
+
name: z.ZodString;
|
|
37
|
+
company: z.ZodOptional<z.ZodString>;
|
|
38
|
+
email: z.ZodOptional<z.ZodString>;
|
|
39
|
+
bill_to: z.ZodOptional<z.ZodObject<{
|
|
40
|
+
line1: z.ZodOptional<z.ZodString>;
|
|
41
|
+
line2: z.ZodOptional<z.ZodString>;
|
|
42
|
+
city: z.ZodOptional<z.ZodString>;
|
|
43
|
+
region: z.ZodOptional<z.ZodString>;
|
|
44
|
+
postal_code: z.ZodOptional<z.ZodString>;
|
|
45
|
+
country: z.ZodOptional<z.ZodString>;
|
|
46
|
+
}, z.core.$strict>>;
|
|
47
|
+
ship_to: z.ZodOptional<z.ZodObject<{
|
|
48
|
+
line1: z.ZodOptional<z.ZodString>;
|
|
49
|
+
line2: z.ZodOptional<z.ZodString>;
|
|
50
|
+
city: z.ZodOptional<z.ZodString>;
|
|
51
|
+
region: z.ZodOptional<z.ZodString>;
|
|
52
|
+
postal_code: z.ZodOptional<z.ZodString>;
|
|
53
|
+
country: z.ZodOptional<z.ZodString>;
|
|
54
|
+
}, z.core.$strict>>;
|
|
55
|
+
}, z.core.$strict>;
|
|
56
|
+
lines: z.ZodArray<z.ZodObject<{
|
|
57
|
+
applies_to_line_ref: z.ZodOptional<z.ZodString>;
|
|
58
|
+
item_code: z.ZodOptional<z.ZodString>;
|
|
59
|
+
description: z.ZodString;
|
|
60
|
+
quantity: z.ZodNumber;
|
|
61
|
+
unit_price_cents: z.ZodInt;
|
|
62
|
+
amount_cents: z.ZodInt;
|
|
63
|
+
}, z.core.$strict>>;
|
|
64
|
+
shipping_cents: z.ZodInt;
|
|
65
|
+
tax_cents: z.ZodInt;
|
|
66
|
+
total_cents: z.ZodInt;
|
|
67
|
+
schema_version: z.ZodLiteral<"1">;
|
|
68
|
+
source_ref: z.ZodString;
|
|
69
|
+
company_key: z.ZodString;
|
|
70
|
+
version: z.ZodInt;
|
|
71
|
+
status: z.ZodEnum<{
|
|
72
|
+
active: "active";
|
|
73
|
+
voided: "voided";
|
|
74
|
+
}>;
|
|
75
|
+
}, z.core.$strict>;
|
|
76
|
+
export type CreditMemoDTO = z.infer<typeof creditMemoSchema>;
|
|
77
|
+
export type CreditMemoLine = z.infer<typeof creditMemoLine>;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { cents, currency, customer, docNumber, envelope, isoDate, lineAmountMatches, orderRef, positiveCents, references, sum, text, } from "./common.js";
|
|
3
|
+
/** A credit memo line: reverses an invoice line (`applies_to_line_ref`), or is a money-only credit. */
|
|
4
|
+
export const creditMemoLine = z.strictObject({
|
|
5
|
+
applies_to_line_ref: z.string().optional(),
|
|
6
|
+
item_code: z.string().optional(),
|
|
7
|
+
description: text,
|
|
8
|
+
quantity: z.number().positive("must be more than zero"),
|
|
9
|
+
unit_price_cents: cents,
|
|
10
|
+
amount_cents: cents,
|
|
11
|
+
});
|
|
12
|
+
/**
|
|
13
|
+
* Reduces what the customer owes on one invoice; moves no cash. Posted as a CreditMemo.
|
|
14
|
+
*
|
|
15
|
+
* `lines` may be empty when the credit gives back only shipping or tax: those are fields here,
|
|
16
|
+
* not lines, and posting turns them into lines on the invoice's shipping and tax items.
|
|
17
|
+
*
|
|
18
|
+
* Refused on its own: no lines and no shipping or tax to give back; a line whose amount is not
|
|
19
|
+
* quantity × unit price; a total that is not lines + shipping + tax; a line reversing an invoice
|
|
20
|
+
* line without its `item_code`.
|
|
21
|
+
* Whether the invoice and the line exist is checked when the document arrives.
|
|
22
|
+
*/
|
|
23
|
+
export const creditMemoSchema = z
|
|
24
|
+
.strictObject({
|
|
25
|
+
...envelope,
|
|
26
|
+
type: z.literal("credit_memo"),
|
|
27
|
+
number: docNumber,
|
|
28
|
+
issue_date: isoDate,
|
|
29
|
+
currency,
|
|
30
|
+
applies_to_invoice_ref: text,
|
|
31
|
+
order_ref: orderRef.optional(),
|
|
32
|
+
references: references.optional(),
|
|
33
|
+
class_key: z.string().optional(),
|
|
34
|
+
customer,
|
|
35
|
+
lines: z.array(creditMemoLine),
|
|
36
|
+
shipping_cents: cents,
|
|
37
|
+
tax_cents: cents,
|
|
38
|
+
// Positive: the document type supplies the sign.
|
|
39
|
+
total_cents: positiveCents,
|
|
40
|
+
})
|
|
41
|
+
.superRefine((doc, ctx) => {
|
|
42
|
+
if (doc.lines.length === 0 && !(doc.shipping_cents > 0) && !(doc.tax_cents > 0)) {
|
|
43
|
+
ctx.addIssue({ code: "custom", path: ["lines"], message: "a credit memo needs at least one line, or shipping or tax to give back" });
|
|
44
|
+
}
|
|
45
|
+
doc.lines.forEach((line, i) => {
|
|
46
|
+
if (!lineAmountMatches(line)) {
|
|
47
|
+
ctx.addIssue({
|
|
48
|
+
code: "custom",
|
|
49
|
+
path: ["lines", i, "amount_cents"],
|
|
50
|
+
message: `must equal quantity × unit_price_cents (${line.quantity} × ${line.unit_price_cents})`,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
if (line.applies_to_line_ref?.trim() && !line.item_code?.trim()) {
|
|
54
|
+
ctx.addIssue({
|
|
55
|
+
code: "custom",
|
|
56
|
+
path: ["lines", i, "item_code"],
|
|
57
|
+
message: "is required when applies_to_line_ref is set",
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
const expected = sum(doc.lines.map((l) => l.amount_cents)) + doc.shipping_cents + doc.tax_cents;
|
|
62
|
+
if (doc.total_cents !== expected) {
|
|
63
|
+
ctx.addIssue({ code: "custom", path: ["total_cents"], message: `must equal lines + shipping + tax (${expected})` });
|
|
64
|
+
}
|
|
65
|
+
});
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { type CreditMemoDTO } from "./creditMemo.ts";
|
|
3
|
+
import { type InvoiceDTO } from "./invoice.ts";
|
|
4
|
+
/**
|
|
5
|
+
* What passes between an app and QBO Sync: the documents an app sends, the status reports it gets
|
|
6
|
+
* back, and what each answer means. No I/O and no Node-only code, so it is safe to import anywhere.
|
|
7
|
+
* QBO Sync checks every document with this same code.
|
|
8
|
+
*/
|
|
9
|
+
export * from "./common.ts";
|
|
10
|
+
export * from "./invoice.ts";
|
|
11
|
+
export * from "./creditMemo.ts";
|
|
12
|
+
export * from "./statusReport.ts";
|
|
13
|
+
export * from "./protocol.ts";
|
|
14
|
+
export * from "./answers.ts";
|
|
15
|
+
/** An invoice or a credit memo: what an app may send. */
|
|
16
|
+
export declare const appDocumentSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
17
|
+
type: z.ZodLiteral<"invoice">;
|
|
18
|
+
number: z.ZodPipe<z.ZodString, z.ZodString>;
|
|
19
|
+
issue_date: z.ZodString;
|
|
20
|
+
due_date: z.ZodOptional<z.ZodString>;
|
|
21
|
+
terms_code: z.ZodOptional<z.ZodString>;
|
|
22
|
+
currency: z.ZodString;
|
|
23
|
+
order_ref: z.ZodOptional<z.ZodString>;
|
|
24
|
+
references: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
25
|
+
label: z.ZodString;
|
|
26
|
+
value: z.ZodString;
|
|
27
|
+
}, z.core.$strict>>>;
|
|
28
|
+
class_key: z.ZodOptional<z.ZodString>;
|
|
29
|
+
customer: z.ZodObject<{
|
|
30
|
+
ref: z.ZodString;
|
|
31
|
+
name: z.ZodString;
|
|
32
|
+
company: z.ZodOptional<z.ZodString>;
|
|
33
|
+
email: z.ZodOptional<z.ZodString>;
|
|
34
|
+
bill_to: z.ZodOptional<z.ZodObject<{
|
|
35
|
+
line1: z.ZodOptional<z.ZodString>;
|
|
36
|
+
line2: z.ZodOptional<z.ZodString>;
|
|
37
|
+
city: z.ZodOptional<z.ZodString>;
|
|
38
|
+
region: z.ZodOptional<z.ZodString>;
|
|
39
|
+
postal_code: z.ZodOptional<z.ZodString>;
|
|
40
|
+
country: z.ZodOptional<z.ZodString>;
|
|
41
|
+
}, z.core.$strict>>;
|
|
42
|
+
ship_to: z.ZodOptional<z.ZodObject<{
|
|
43
|
+
line1: z.ZodOptional<z.ZodString>;
|
|
44
|
+
line2: z.ZodOptional<z.ZodString>;
|
|
45
|
+
city: z.ZodOptional<z.ZodString>;
|
|
46
|
+
region: z.ZodOptional<z.ZodString>;
|
|
47
|
+
postal_code: z.ZodOptional<z.ZodString>;
|
|
48
|
+
country: z.ZodOptional<z.ZodString>;
|
|
49
|
+
}, z.core.$strict>>;
|
|
50
|
+
}, z.core.$strict>;
|
|
51
|
+
lines: z.ZodArray<z.ZodObject<{
|
|
52
|
+
line_ref: z.ZodString;
|
|
53
|
+
item_code: z.ZodOptional<z.ZodString>;
|
|
54
|
+
description: z.ZodString;
|
|
55
|
+
quantity: z.ZodNumber;
|
|
56
|
+
unit_price_cents: z.ZodInt;
|
|
57
|
+
amount_cents: z.ZodInt;
|
|
58
|
+
}, z.core.$strict>>;
|
|
59
|
+
shipping_cents: z.ZodInt;
|
|
60
|
+
tax_cents: z.ZodInt;
|
|
61
|
+
discount_cents: z.ZodInt;
|
|
62
|
+
total_cents: z.ZodInt;
|
|
63
|
+
accrued_fees: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
64
|
+
code: z.ZodString;
|
|
65
|
+
amount_cents: z.ZodInt;
|
|
66
|
+
description: z.ZodOptional<z.ZodString>;
|
|
67
|
+
}, z.core.$strict>>>;
|
|
68
|
+
schema_version: z.ZodLiteral<"1">;
|
|
69
|
+
source_ref: z.ZodString;
|
|
70
|
+
company_key: z.ZodString;
|
|
71
|
+
version: z.ZodInt;
|
|
72
|
+
status: z.ZodEnum<{
|
|
73
|
+
active: "active";
|
|
74
|
+
voided: "voided";
|
|
75
|
+
}>;
|
|
76
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
77
|
+
type: z.ZodLiteral<"credit_memo">;
|
|
78
|
+
number: z.ZodPipe<z.ZodString, z.ZodString>;
|
|
79
|
+
issue_date: z.ZodString;
|
|
80
|
+
currency: z.ZodString;
|
|
81
|
+
applies_to_invoice_ref: z.ZodString;
|
|
82
|
+
order_ref: z.ZodOptional<z.ZodString>;
|
|
83
|
+
references: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
84
|
+
label: z.ZodString;
|
|
85
|
+
value: z.ZodString;
|
|
86
|
+
}, z.core.$strict>>>;
|
|
87
|
+
class_key: z.ZodOptional<z.ZodString>;
|
|
88
|
+
customer: z.ZodObject<{
|
|
89
|
+
ref: z.ZodString;
|
|
90
|
+
name: z.ZodString;
|
|
91
|
+
company: z.ZodOptional<z.ZodString>;
|
|
92
|
+
email: z.ZodOptional<z.ZodString>;
|
|
93
|
+
bill_to: z.ZodOptional<z.ZodObject<{
|
|
94
|
+
line1: z.ZodOptional<z.ZodString>;
|
|
95
|
+
line2: z.ZodOptional<z.ZodString>;
|
|
96
|
+
city: z.ZodOptional<z.ZodString>;
|
|
97
|
+
region: z.ZodOptional<z.ZodString>;
|
|
98
|
+
postal_code: z.ZodOptional<z.ZodString>;
|
|
99
|
+
country: z.ZodOptional<z.ZodString>;
|
|
100
|
+
}, z.core.$strict>>;
|
|
101
|
+
ship_to: z.ZodOptional<z.ZodObject<{
|
|
102
|
+
line1: z.ZodOptional<z.ZodString>;
|
|
103
|
+
line2: z.ZodOptional<z.ZodString>;
|
|
104
|
+
city: z.ZodOptional<z.ZodString>;
|
|
105
|
+
region: z.ZodOptional<z.ZodString>;
|
|
106
|
+
postal_code: z.ZodOptional<z.ZodString>;
|
|
107
|
+
country: z.ZodOptional<z.ZodString>;
|
|
108
|
+
}, z.core.$strict>>;
|
|
109
|
+
}, z.core.$strict>;
|
|
110
|
+
lines: z.ZodArray<z.ZodObject<{
|
|
111
|
+
applies_to_line_ref: z.ZodOptional<z.ZodString>;
|
|
112
|
+
item_code: z.ZodOptional<z.ZodString>;
|
|
113
|
+
description: z.ZodString;
|
|
114
|
+
quantity: z.ZodNumber;
|
|
115
|
+
unit_price_cents: z.ZodInt;
|
|
116
|
+
amount_cents: z.ZodInt;
|
|
117
|
+
}, z.core.$strict>>;
|
|
118
|
+
shipping_cents: z.ZodInt;
|
|
119
|
+
tax_cents: z.ZodInt;
|
|
120
|
+
total_cents: z.ZodInt;
|
|
121
|
+
schema_version: z.ZodLiteral<"1">;
|
|
122
|
+
source_ref: z.ZodString;
|
|
123
|
+
company_key: z.ZodString;
|
|
124
|
+
version: z.ZodInt;
|
|
125
|
+
status: z.ZodEnum<{
|
|
126
|
+
active: "active";
|
|
127
|
+
voided: "voided";
|
|
128
|
+
}>;
|
|
129
|
+
}, z.core.$strict>], "type">;
|
|
130
|
+
export type AppDocument = InvoiceDTO | CreditMemoDTO;
|
|
131
|
+
export type CheckedDocument = {
|
|
132
|
+
ok: true;
|
|
133
|
+
document: AppDocument;
|
|
134
|
+
} | {
|
|
135
|
+
ok: false;
|
|
136
|
+
reasons: string[];
|
|
137
|
+
};
|
|
138
|
+
/** One problem with a document as a readable line naming the field: `lines.0.amount_cents: must equal …`. */
|
|
139
|
+
export declare function formatIssue(issue: z.core.$ZodIssue): string;
|
|
140
|
+
/**
|
|
141
|
+
* Checks a document the way QBO Sync will, before it is sent. `reasons` are the same lines QBO Sync
|
|
142
|
+
* would answer. Rules that need QBO Sync's own records (a number already used, an unknown invoice)
|
|
143
|
+
* are checked when it arrives, so passing here is not a promise it will be accepted.
|
|
144
|
+
*/
|
|
145
|
+
export declare function checkDocument(input: unknown): CheckedDocument;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { creditMemoSchema } from "./creditMemo.js";
|
|
3
|
+
import { invoiceSchema } from "./invoice.js";
|
|
4
|
+
import { APP_DOCUMENT_TYPES } from "./protocol.js";
|
|
5
|
+
/**
|
|
6
|
+
* What passes between an app and QBO Sync: the documents an app sends, the status reports it gets
|
|
7
|
+
* back, and what each answer means. No I/O and no Node-only code, so it is safe to import anywhere.
|
|
8
|
+
* QBO Sync checks every document with this same code.
|
|
9
|
+
*/
|
|
10
|
+
export * from "./common.js";
|
|
11
|
+
export * from "./invoice.js";
|
|
12
|
+
export * from "./creditMemo.js";
|
|
13
|
+
export * from "./statusReport.js";
|
|
14
|
+
export * from "./protocol.js";
|
|
15
|
+
export * from "./answers.js";
|
|
16
|
+
/** An invoice or a credit memo: what an app may send. */
|
|
17
|
+
export const appDocumentSchema = z.discriminatedUnion("type", [invoiceSchema, creditMemoSchema], {
|
|
18
|
+
error: `type must be one of ${APP_DOCUMENT_TYPES.join(", ")}`,
|
|
19
|
+
});
|
|
20
|
+
/** One problem with a document as a readable line naming the field: `lines.0.amount_cents: must equal …`. */
|
|
21
|
+
export function formatIssue(issue) {
|
|
22
|
+
const where = issue.path.map(String).join(".");
|
|
23
|
+
// zod's own wording ("Unrecognized key: ...") is written for developers; these are read in a panel too.
|
|
24
|
+
const message = issue.code === "unrecognized_keys"
|
|
25
|
+
? `unknown field${issue.keys.length > 1 ? "s" : ""} ${issue.keys.map((k) => `"${k}"`).join(", ")}`
|
|
26
|
+
: issue.message;
|
|
27
|
+
return where ? `${where}: ${message}` : message;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Checks a document the way QBO Sync will, before it is sent. `reasons` are the same lines QBO Sync
|
|
31
|
+
* would answer. Rules that need QBO Sync's own records (a number already used, an unknown invoice)
|
|
32
|
+
* are checked when it arrives, so passing here is not a promise it will be accepted.
|
|
33
|
+
*/
|
|
34
|
+
export function checkDocument(input) {
|
|
35
|
+
const parsed = appDocumentSchema.safeParse(input);
|
|
36
|
+
if (parsed.success)
|
|
37
|
+
return { ok: true, document: parsed.data };
|
|
38
|
+
return { ok: false, reasons: parsed.error.issues.map(formatIssue) };
|
|
39
|
+
}
|