@baseworks/invoice 0.1.1 → 1.0.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 +58 -14
- package/dist/{chunk-7P7ARN3X.js → chunk-ITMZVGLQ.js} +22 -22
- package/dist/cli.js +38 -38
- package/dist/client.d.ts +99 -23
- package/dist/client.js +81 -23
- package/dist/events.d.ts +72 -66
- package/dist/events.js +1 -1
- package/dist/graphql.d.ts +22 -0
- package/dist/graphql.js +17 -2
- package/dist/mcp.d.ts +13 -0
- package/dist/mcp.js +131 -0
- package/dist/projections.js +20 -20
- package/package.json +14 -3
- package/src/__tests__/cli.test.ts +25 -25
- package/src/__tests__/client.test.ts +108 -0
- package/src/__tests__/dts-exports.test.ts +75 -0
- package/src/__tests__/mcp.test.ts +150 -0
- package/src/cli.ts +49 -49
- package/src/client.ts +44 -25
- package/src/compat.ts +61 -0
- package/src/events.ts +28 -22
- package/src/graphql.ts +17 -2
- package/src/mcp.ts +179 -0
- package/src/projections.ts +19 -19
- package/src/types.ts +94 -20
package/README.md
CHANGED
|
@@ -10,35 +10,38 @@ pnpm add @baseworks/invoice
|
|
|
10
10
|
|
|
11
11
|
## Client
|
|
12
12
|
|
|
13
|
-
Invoices are **org-scoped** (`X-Org-Id` +
|
|
13
|
+
Invoices are **org-scoped** (`X-Org-Id` + token). **Money is integer
|
|
14
14
|
cents** throughout. Status machine: `draft → finalized → paid | void`. A finalized
|
|
15
15
|
invoice is immutable (only payments / void follow).
|
|
16
16
|
|
|
17
|
+
Every field is **snake_case** (since 0.3.0, as the service speaks): options `base_url`, `org_id`; inputs and answers
|
|
18
|
+
`customer_id`, `unit_price_cents`, `total_cents`, `short_id`, …
|
|
19
|
+
|
|
17
20
|
```ts
|
|
18
21
|
import { createInvoiceClient } from '@baseworks/invoice'
|
|
19
22
|
|
|
20
23
|
const invoices = createInvoiceClient({
|
|
21
|
-
|
|
22
|
-
token:
|
|
23
|
-
|
|
24
|
+
base_url: process.env.INVOICE_SERVICE_URL!,
|
|
25
|
+
token: realmToken, // a service forwards its request's `c.get('realm_token')`, never the raw Authorization header
|
|
26
|
+
org_id: activeOrgId,
|
|
24
27
|
})
|
|
25
28
|
|
|
26
|
-
// 1. draft —
|
|
29
|
+
// 1. draft — customer_id is a customers-service ref (validated + name snapshotted)
|
|
27
30
|
const draft = await invoices.create({
|
|
28
|
-
|
|
31
|
+
customer_id: 'acct-42',
|
|
29
32
|
currency: 'USD',
|
|
30
33
|
lines: [
|
|
31
|
-
{ description: 'Consulting', quantity: 10,
|
|
32
|
-
{ description: 'Setup', quantity: 1,
|
|
34
|
+
{ description: 'Consulting', quantity: 10, unit_price_cents: 15000 }, // $150.00 × 10
|
|
35
|
+
{ description: 'Setup', quantity: 1, unit_price_cents: 50000 },
|
|
33
36
|
],
|
|
34
37
|
})
|
|
35
38
|
|
|
36
39
|
// 2. finalize — assigns a per-org number, freezes lines + totals
|
|
37
|
-
const final = await invoices.finalize(draft.id, {
|
|
40
|
+
const final = await invoices.finalize(draft.id, { due_at: Date.parse('2026-08-01') })
|
|
38
41
|
|
|
39
42
|
// 3. record payments — balance can't go below zero (overpayment is rejected)
|
|
40
|
-
await invoices.recordPayment(final.id, {
|
|
41
|
-
await invoices.recordPayment(final.id, {
|
|
43
|
+
await invoices.recordPayment(final.id, { amount_cents: 100000, method: 'bank', reference: 'TXN-1' })
|
|
44
|
+
await invoices.recordPayment(final.id, { amount_cents: 100000 }) // → status 'paid'
|
|
42
45
|
|
|
43
46
|
// 4. the ledger — one row per money movement
|
|
44
47
|
await invoices.transactions() // all org transactions
|
|
@@ -49,8 +52,29 @@ await invoices.get(final.id)
|
|
|
49
52
|
await invoices.void(final.id, 'issued in error')
|
|
50
53
|
```
|
|
51
54
|
|
|
52
|
-
Errors throw `InvoiceError` (`.status`, `.code`): `
|
|
53
|
-
invoice), `409 overpayment` / `409 already_paid`, `404 customer_not_found`.
|
|
55
|
+
Errors throw `InvoiceError` (`.status`, `.code`): `403 forbidden` (with the missing permission, e.g. `invoice.write`),
|
|
56
|
+
`409 not_draft` (edit a finalized invoice), `409 overpayment` / `409 already_paid`, `404 customer_not_found`.
|
|
57
|
+
|
|
58
|
+
### Deprecated names
|
|
59
|
+
|
|
60
|
+
Until 0.3.0 the client and the service spoke camelCase. A caller that has not moved (billing-service, stripe-service, apps)
|
|
61
|
+
still builds and runs: **every old name is kept as a deprecated alias** and goes when the last caller has moved.
|
|
62
|
+
|
|
63
|
+
| | Old | Now |
|
|
64
|
+
|---|---|---|
|
|
65
|
+
| options | `baseUrl`, `orgId` | `base_url`, `org_id` |
|
|
66
|
+
| invoice | `shortId`, `customerId`, `customerName`, `subtotalCents`, `totalCents`, `amountPaidCents`, `balanceCents`, `issuedAt`, `dueAt`, `createdAt`, `updatedAt` | `short_id`, `customer_id`, `customer_name`, `subtotal_cents`, `total_cents`, `amount_paid_cents`, `balance_cents`, `issued_at`, `due_at`, `created_at`, `updated_at` |
|
|
67
|
+
| line | `unitPriceCents`, `amountCents`, `productId` | `unit_price_cents`, `amount_cents`, `product_id` |
|
|
68
|
+
| transaction | `invoiceId`, `invoiceNumber`, `customerId`, `customerName`, `amountCents`, `createdAt` | `invoice_id`, `invoice_number`, `customer_id`, `customer_name`, `amount_cents`, `created_at` |
|
|
69
|
+
| inputs | `customerId`, `unitPriceCents`, `productId`, `issuedAt`, `dueAt`, `amountCents` | `customer_id`, `unit_price_cents`, `product_id`, `issued_at`, `due_at`, `amount_cents` |
|
|
70
|
+
|
|
71
|
+
On **answers** the old names are readable properties (`invoice.totalCents`) but not data: they are non-enumerable getters of
|
|
72
|
+
the snake_case field, so `JSON.stringify`, spreading and `toEqual` see the snake_case object only. On **inputs** the old names
|
|
73
|
+
are renamed to snake_case before they are sent, and the new name wins when both are given. The types carry the aliases
|
|
74
|
+
marked `@deprecated`. The method names (`recordPayment`, `invoiceTransactions`, …) are unchanged. The GraphQL module's schema is
|
|
75
|
+
camelCase, as that contract's own, and resolves from the snake_case data.
|
|
76
|
+
|
|
77
|
+
The service needs **invoice-service 0.3.0**: an older service answers camelCase and this client no longer reads it.
|
|
54
78
|
|
|
55
79
|
## CLI plugin
|
|
56
80
|
|
|
@@ -58,12 +82,31 @@ invoice), `409 overpayment` / `409 already_paid`, `404 customer_not_found`.
|
|
|
58
82
|
import { buildInvoiceCommand } from '@baseworks/invoice/cli'
|
|
59
83
|
|
|
60
84
|
program.addCommand(buildInvoiceCommand({ http, cliName: 'dtab' }))
|
|
85
|
+
// the CLI reads and sends the service's snake_case, so it needs invoice-service 0.3.0
|
|
61
86
|
// dtab invoices | inv list (--status) · create · get · update · finalize · pay · void
|
|
62
87
|
// dtab invoices create --customer <ref> --currency USD --line "Consulting|10|150.00"
|
|
63
88
|
// dtab invoices pay <ref> --amount 150.00 --method bank
|
|
64
89
|
// dtab invoices payments [<ref>] (the transaction ledger)
|
|
65
90
|
```
|
|
66
91
|
|
|
92
|
+
## MCP tools
|
|
93
|
+
|
|
94
|
+
`@baseworks/invoice/mcp` is the tool set a model uses, for an MCP server built with `@baseworks/mcp` (`composeTools(invoiceTools())`;
|
|
95
|
+
`ctx.clients.invoice` is an `InvoiceClient` built from the realm token). `@baseworks/mcp` is a dependency, the MCP SDK an
|
|
96
|
+
optional peer.
|
|
97
|
+
|
|
98
|
+
| Tool | Access | Permission |
|
|
99
|
+
|---|---|---|
|
|
100
|
+
| `invoice_list`, `invoice_get`, `invoice_transactions` | read | `invoice.read` |
|
|
101
|
+
| `invoice_create`, `invoice_update` (drafts only) | write | `invoice.write` |
|
|
102
|
+
| `invoice_finalize` (irreversible) | write | `invoice.finalize` |
|
|
103
|
+
| `invoice_record_payment` | write | `invoice.record_payment` |
|
|
104
|
+
| `invoice_void` (irreversible) | write | `invoice.void` |
|
|
105
|
+
|
|
106
|
+
A tool is a thin wrapper over the same client, so it can never do what the API would refuse, and `permission` is the name
|
|
107
|
+
the service registered with org-service, used to hide a tool from somebody who may not use it. Money is integer cents; a
|
|
108
|
+
finalized invoice is never edited.
|
|
109
|
+
|
|
67
110
|
## Service integration
|
|
68
111
|
|
|
69
112
|
```ts
|
|
@@ -72,7 +115,8 @@ import { InvoiceEvents, TransactionEvents } from '@baseworks/invoice/events'
|
|
|
72
115
|
```
|
|
73
116
|
|
|
74
117
|
Projections `invoices` → `read_invoices`, `transactions` → `read_transactions`
|
|
75
|
-
(one row per payment). Tenant = org id.
|
|
118
|
+
(one row per payment). Tenant = org id. Event payloads are snake_case since 0.3.0; older camelCase events still fold
|
|
119
|
+
(foldbase reads a snake_case path from a camelCase payload).
|
|
76
120
|
|
|
77
121
|
## See also
|
|
78
122
|
|
|
@@ -6,43 +6,43 @@ var InvoiceEvents = defineAggregate("invoice", {
|
|
|
6
6
|
// Draft created (with its initial lines + computed totals). Numeric fields are
|
|
7
7
|
// initialised to 0 so their read-model columns are stable integers.
|
|
8
8
|
InvoiceDrafted: z.object({
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
customer_id: z.string().min(1),
|
|
10
|
+
customer_name: z.string(),
|
|
11
11
|
// snapshot at draft time; '' if unknown
|
|
12
12
|
currency: z.string().length(3),
|
|
13
13
|
lines: z.string(),
|
|
14
|
-
// JSON array of { description, quantity,
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
// JSON array of { description, quantity, unit_price_cents, amount_cents, product_id }
|
|
15
|
+
subtotal_cents: z.number().int(),
|
|
16
|
+
total_cents: z.number().int(),
|
|
17
|
+
short_id: z.string().min(1),
|
|
18
18
|
at: z.number().int()
|
|
19
19
|
}),
|
|
20
20
|
// Draft edited (customer / lines). Only valid while status = draft.
|
|
21
21
|
InvoiceUpdated: z.object({
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
customer_id: z.string().min(1),
|
|
23
|
+
customer_name: z.string(),
|
|
24
24
|
lines: z.string(),
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
subtotal_cents: z.number().int(),
|
|
26
|
+
total_cents: z.number().int(),
|
|
27
27
|
at: z.number().int()
|
|
28
28
|
}),
|
|
29
29
|
// Finalize: assign the per-org number, freeze lines + totals, set issue/due.
|
|
30
30
|
InvoiceFinalized: z.object({
|
|
31
31
|
number: z.number().int(),
|
|
32
32
|
lines: z.string(),
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
33
|
+
subtotal_cents: z.number().int(),
|
|
34
|
+
total_cents: z.number().int(),
|
|
35
|
+
issued_at: z.number().int(),
|
|
36
|
+
due_at: z.number().int(),
|
|
37
37
|
// 0 = no due date
|
|
38
38
|
at: z.number().int()
|
|
39
39
|
}),
|
|
40
|
-
// A payment applied to a finalized invoice. `
|
|
40
|
+
// A payment applied to a finalized invoice. `amount_paid_cents` is the new running
|
|
41
41
|
// total; `status` is the resulting status ('paid' when balance ≤ 0, else
|
|
42
42
|
// 'finalized') — both computed by the store.
|
|
43
43
|
PaymentRecorded: z.object({
|
|
44
|
-
|
|
45
|
-
|
|
44
|
+
amount_cents: z.number().int(),
|
|
45
|
+
amount_paid_cents: z.number().int(),
|
|
46
46
|
status: InvoiceStatus,
|
|
47
47
|
method: z.string(),
|
|
48
48
|
reference: z.string(),
|
|
@@ -56,13 +56,13 @@ var InvoiceEvents = defineAggregate("invoice", {
|
|
|
56
56
|
var TransactionKind = z.enum(["payment", "refund", "adjustment"]);
|
|
57
57
|
var TransactionEvents = defineAggregate("transaction", {
|
|
58
58
|
TransactionRecorded: z.object({
|
|
59
|
-
|
|
60
|
-
|
|
59
|
+
invoice_id: z.string().min(1),
|
|
60
|
+
invoice_number: z.number().int(),
|
|
61
61
|
// the invoice's number at the time
|
|
62
|
-
|
|
63
|
-
|
|
62
|
+
customer_id: z.string(),
|
|
63
|
+
customer_name: z.string(),
|
|
64
64
|
kind: TransactionKind,
|
|
65
|
-
|
|
65
|
+
amount_cents: z.number().int(),
|
|
66
66
|
currency: z.string().length(3),
|
|
67
67
|
method: z.string(),
|
|
68
68
|
reference: z.string(),
|
package/dist/cli.js
CHANGED
|
@@ -20,7 +20,7 @@ function parseLine(spec) {
|
|
|
20
20
|
const quantity = Number(parts[1]);
|
|
21
21
|
if (!description) fatal(`--line description is empty in '${spec}'`);
|
|
22
22
|
if (!Number.isFinite(quantity) || quantity <= 0) fatal(`--line qty must be > 0 in '${spec}'`);
|
|
23
|
-
return { description, quantity,
|
|
23
|
+
return { description, quantity, unit_price_cents: toCents(parts[2].trim(), "--line unitPrice") };
|
|
24
24
|
}
|
|
25
25
|
var NUMBER_COL = (v) => v ? `#${v}` : "\u2014";
|
|
26
26
|
function buildInvoiceCommand(deps) {
|
|
@@ -28,16 +28,16 @@ function buildInvoiceCommand(deps) {
|
|
|
28
28
|
const cli = deps.cliName ?? "cli";
|
|
29
29
|
const listInvoices = async (opts) => {
|
|
30
30
|
const q = opts.status ? `?status=${encodeURIComponent(opts.status)}` : "";
|
|
31
|
-
const res = await http.get(`/
|
|
31
|
+
const res = await http.get(`/invoices${q}`).catch(die);
|
|
32
32
|
printOutput(res.invoices, opts.output, (wide) => {
|
|
33
33
|
table(res.invoices, [
|
|
34
|
-
{ key: "
|
|
34
|
+
{ key: "short_id", label: "ID" },
|
|
35
35
|
{ key: "number", label: "NO", fmt: NUMBER_COL },
|
|
36
36
|
{ key: "status", label: "STATUS" },
|
|
37
|
-
{ key: "
|
|
38
|
-
{ key: "
|
|
39
|
-
{ key: "
|
|
40
|
-
{ key: "
|
|
37
|
+
{ key: "customer_name", label: "CUSTOMER", fmt: (v, r) => v ?? r.customer_id },
|
|
38
|
+
{ key: "total_cents", label: "TOTAL", fmt: (v, r) => fmtMoney(v, r.currency) },
|
|
39
|
+
{ key: "balance_cents", label: "BALANCE", fmt: (v, r) => fmtMoney(v, r.currency) },
|
|
40
|
+
{ key: "created_at", label: "CREATED", wide: true, fmt: (v) => fmtDate(v) },
|
|
41
41
|
{ key: "id", label: "FULL ID", wide: true }
|
|
42
42
|
], { wide, emptyHint: `No invoices yet. Run: ${cli} invoices create --customer <ref> --currency USD --line "<desc>|<qty>|<price>"` });
|
|
43
43
|
summary(`${res.invoices.length} invoice${res.invoices.length !== 1 ? "s" : ""}`);
|
|
@@ -57,28 +57,28 @@ function buildInvoiceCommand(deps) {
|
|
|
57
57
|
cmd.addCommand(
|
|
58
58
|
new Command("create").description("Create a draft invoice").requiredOption("--customer <ref>", "Customer id, short_id, or external_id").requiredOption("--currency <cur>", "ISO-4217 currency (e.g. USD)").requiredOption("--line <spec...>", 'Line "description|qty|unitPrice" (repeatable)').action(async (opts) => {
|
|
59
59
|
const lines = opts.line.map(parseLine);
|
|
60
|
-
const res = await http.post("/
|
|
61
|
-
|
|
60
|
+
const res = await http.post("/invoices", {
|
|
61
|
+
customer_id: opts.customer,
|
|
62
62
|
currency: opts.currency,
|
|
63
63
|
lines
|
|
64
64
|
}).catch(die);
|
|
65
|
-
success(`Draft invoice created for ${res.invoice.
|
|
66
|
-
kv([["id", res.invoice.
|
|
65
|
+
success(`Draft invoice created for ${res.invoice.customer_name ?? res.invoice.customer_id}`);
|
|
66
|
+
kv([["id", res.invoice.short_id], ["status", res.invoice.status], ["total", fmtMoney(res.invoice.total_cents, res.invoice.currency)]]);
|
|
67
67
|
})
|
|
68
68
|
);
|
|
69
69
|
cmd.addCommand(
|
|
70
70
|
new Command("get").argument("<ref>", "id or short_id").description("Show invoice details (lines, totals, balance)").option(...outputOption()).action(async (ref, opts) => {
|
|
71
|
-
const res = await http.get(`/
|
|
71
|
+
const res = await http.get(`/invoices/${ref}`).catch(die);
|
|
72
72
|
const inv = res.invoice;
|
|
73
73
|
const cur = String(inv["currency"]);
|
|
74
74
|
printOutput(inv, opts.output, () => {
|
|
75
75
|
kv([
|
|
76
|
-
["id", String(inv["
|
|
76
|
+
["id", String(inv["short_id"])],
|
|
77
77
|
["number", inv["number"] ? `#${inv["number"]}` : "\u2014 (draft)"],
|
|
78
78
|
["status", String(inv["status"])],
|
|
79
|
-
["customer", String(inv["
|
|
80
|
-
["issued", inv["
|
|
81
|
-
["due", inv["
|
|
79
|
+
["customer", String(inv["customer_name"] ?? inv["customer_id"])],
|
|
80
|
+
["issued", inv["issued_at"] ? fmtDate(inv["issued_at"]) : "\u2014"],
|
|
81
|
+
["due", inv["due_at"] ? fmtDate(inv["due_at"]) : "\u2014"]
|
|
82
82
|
]);
|
|
83
83
|
const lines = inv["lines"] ?? [];
|
|
84
84
|
if (lines.length) {
|
|
@@ -86,15 +86,15 @@ function buildInvoiceCommand(deps) {
|
|
|
86
86
|
table(lines, [
|
|
87
87
|
{ key: "description", label: "DESCRIPTION" },
|
|
88
88
|
{ key: "quantity", label: "QTY" },
|
|
89
|
-
{ key: "
|
|
90
|
-
{ key: "
|
|
89
|
+
{ key: "unit_price_cents", label: "UNIT", fmt: (v) => fmtMoney(v, cur) },
|
|
90
|
+
{ key: "amount_cents", label: "AMOUNT", fmt: (v) => fmtMoney(v, cur) }
|
|
91
91
|
]);
|
|
92
92
|
}
|
|
93
93
|
kv([
|
|
94
|
-
["subtotal", fmtMoney(inv["
|
|
95
|
-
["total", fmtMoney(inv["
|
|
96
|
-
["paid", fmtMoney(inv["
|
|
97
|
-
["balance", fmtMoney(inv["
|
|
94
|
+
["subtotal", fmtMoney(inv["subtotal_cents"], cur)],
|
|
95
|
+
["total", fmtMoney(inv["total_cents"], cur)],
|
|
96
|
+
["paid", fmtMoney(inv["amount_paid_cents"], cur)],
|
|
97
|
+
["balance", fmtMoney(inv["balance_cents"], cur)],
|
|
98
98
|
["full_id", String(inv["id"])]
|
|
99
99
|
]);
|
|
100
100
|
}, "id");
|
|
@@ -103,12 +103,12 @@ function buildInvoiceCommand(deps) {
|
|
|
103
103
|
cmd.addCommand(
|
|
104
104
|
new Command("update").argument("<ref>").description("Edit a DRAFT invoice (replaces lines / customer)").option("--customer <ref>", "New customer ref").option("--line <spec...>", 'Replace lines: "description|qty|unitPrice" (repeatable)').action(async (ref, opts) => {
|
|
105
105
|
const body = {};
|
|
106
|
-
if (opts.customer !== void 0) body["
|
|
106
|
+
if (opts.customer !== void 0) body["customer_id"] = opts.customer;
|
|
107
107
|
if (opts.line !== void 0) body["lines"] = opts.line.map(parseLine);
|
|
108
108
|
if (!Object.keys(body).length) fatal("Provide --customer and/or --line");
|
|
109
|
-
const res = await http.patch(`/
|
|
109
|
+
const res = await http.patch(`/invoices/${ref}`, body).catch(die);
|
|
110
110
|
success("Draft updated.");
|
|
111
|
-
kv([["id", res.invoice.
|
|
111
|
+
kv([["id", res.invoice.short_id], ["total", fmtMoney(res.invoice.total_cents, res.invoice.currency)]]);
|
|
112
112
|
})
|
|
113
113
|
);
|
|
114
114
|
cmd.addCommand(
|
|
@@ -117,41 +117,41 @@ function buildInvoiceCommand(deps) {
|
|
|
117
117
|
if (opts.due) {
|
|
118
118
|
const ms = Date.parse(opts.due);
|
|
119
119
|
if (Number.isNaN(ms)) fatal(`--due is not a valid date: '${opts.due}'`);
|
|
120
|
-
body["
|
|
120
|
+
body["due_at"] = ms;
|
|
121
121
|
}
|
|
122
|
-
const res = await http.post(`/
|
|
122
|
+
const res = await http.post(`/invoices/${ref}/finalize`, body).catch(die);
|
|
123
123
|
success(`Invoice finalized: #${res.invoice.number}`);
|
|
124
|
-
kv([["id", res.invoice.
|
|
124
|
+
kv([["id", res.invoice.short_id], ["number", `#${res.invoice.number}`], ["total", fmtMoney(res.invoice.total_cents, res.invoice.currency)], ["balance", fmtMoney(res.invoice.balance_cents, res.invoice.currency)]]);
|
|
125
125
|
})
|
|
126
126
|
);
|
|
127
127
|
cmd.addCommand(
|
|
128
128
|
new Command("pay").argument("<ref>").description("Record a payment against a finalized invoice").requiredOption("--amount <amount>", "Amount, e.g. 150.00").option("--method <method>", "e.g. bank, cash, card").option("--ref <reference>", "External payment reference").action(async (ref, opts) => {
|
|
129
|
-
const res = await http.post(`/
|
|
130
|
-
|
|
129
|
+
const res = await http.post(`/invoices/${ref}/payments`, {
|
|
130
|
+
amount_cents: toCents(opts.amount),
|
|
131
131
|
...opts.method ? { method: opts.method } : {},
|
|
132
132
|
...opts.ref ? { reference: opts.ref } : {}
|
|
133
133
|
}).catch(die);
|
|
134
134
|
success(`Payment recorded. Invoice is now ${res.invoice.status}.`);
|
|
135
|
-
kv([["id", res.invoice.
|
|
135
|
+
kv([["id", res.invoice.short_id], ["paid", fmtMoney(res.invoice.amount_paid_cents, res.invoice.currency)], ["balance", fmtMoney(res.invoice.balance_cents, res.invoice.currency)]]);
|
|
136
136
|
})
|
|
137
137
|
);
|
|
138
138
|
cmd.addCommand(
|
|
139
139
|
new Command("void").argument("<ref>").description("Void a finalized invoice").option("--reason <reason>", "Reason").action(async (ref, opts) => {
|
|
140
|
-
const res = await http.post(`/
|
|
141
|
-
success(`Invoice voided: ${res.invoice.
|
|
140
|
+
const res = await http.post(`/invoices/${ref}/void`, { reason: opts.reason ?? "" }).catch(die);
|
|
141
|
+
success(`Invoice voided: ${res.invoice.short_id}`);
|
|
142
142
|
})
|
|
143
143
|
);
|
|
144
144
|
cmd.addCommand(
|
|
145
145
|
new Command("payments").alias("transactions").argument("[ref]", "invoice id/short_id (omit for all org transactions)").description("List payments / transactions (the money ledger)").option(...outputOption()).action(async (ref, opts) => {
|
|
146
|
-
const path = ref ? `/
|
|
146
|
+
const path = ref ? `/invoices/${ref}/payments` : "/transactions";
|
|
147
147
|
const res = await http.get(path).catch(die);
|
|
148
148
|
printOutput(res.transactions, opts.output, (wide) => {
|
|
149
149
|
table(res.transactions, [
|
|
150
|
-
{ key: "
|
|
151
|
-
{ key: "
|
|
152
|
-
{ key: "
|
|
150
|
+
{ key: "created_at", label: "DATE", fmt: (v) => fmtDate(v) },
|
|
151
|
+
{ key: "invoice_number", label: "INVOICE", fmt: (v) => v ? `#${v}` : "\u2014" },
|
|
152
|
+
{ key: "customer_name", label: "CUSTOMER", fmt: (v, r) => v ?? r.customer_id },
|
|
153
153
|
{ key: "kind", label: "KIND" },
|
|
154
|
-
{ key: "
|
|
154
|
+
{ key: "amount_cents", label: "AMOUNT", fmt: (v, r) => fmtMoney(v, r.currency) },
|
|
155
155
|
{ key: "method", label: "METHOD", fmt: (v) => v ?? "" },
|
|
156
156
|
{ key: "reference", label: "REF", wide: true, fmt: (v) => v ?? "" },
|
|
157
157
|
{ key: "id", label: "TXN ID", wide: true }
|
package/dist/client.d.ts
CHANGED
|
@@ -19,63 +19,115 @@ interface InvoiceRow {
|
|
|
19
19
|
created_at: number;
|
|
20
20
|
updated_at: number;
|
|
21
21
|
}
|
|
22
|
-
interface
|
|
22
|
+
interface InvoiceLineDto {
|
|
23
23
|
description: string;
|
|
24
24
|
/** May be fractional (e.g. hours). */
|
|
25
25
|
quantity: number;
|
|
26
26
|
/** Unit price in integer cents. */
|
|
27
|
+
unit_price_cents: number;
|
|
28
|
+
/** quantity × unit_price_cents, rounded to cents. */
|
|
29
|
+
amount_cents: number;
|
|
30
|
+
/** Optional catalog product reference. */
|
|
31
|
+
product_id: string | null;
|
|
32
|
+
}
|
|
33
|
+
interface InvoiceLineDeprecated {
|
|
34
|
+
/** @deprecated Read `unit_price_cents`. */
|
|
27
35
|
unitPriceCents: number;
|
|
28
|
-
/**
|
|
36
|
+
/** @deprecated Read `amount_cents`. */
|
|
29
37
|
amountCents: number;
|
|
30
|
-
/**
|
|
38
|
+
/** @deprecated Read `product_id`. */
|
|
31
39
|
productId: string | null;
|
|
32
40
|
}
|
|
33
|
-
|
|
41
|
+
type InvoiceLine = InvoiceLineDto & InvoiceLineDeprecated;
|
|
42
|
+
interface InvoiceDto {
|
|
34
43
|
id: string;
|
|
35
|
-
|
|
44
|
+
short_id: string;
|
|
36
45
|
/** Per-org invoice number; `null` while draft. */
|
|
37
46
|
number: number | null;
|
|
38
47
|
status: InvoiceStatus;
|
|
48
|
+
customer_id: string;
|
|
49
|
+
customer_name: string | null;
|
|
50
|
+
currency: string;
|
|
51
|
+
lines: InvoiceLineDto[];
|
|
52
|
+
subtotal_cents: number;
|
|
53
|
+
total_cents: number;
|
|
54
|
+
amount_paid_cents: number;
|
|
55
|
+
/** total_cents − amount_paid_cents (0 for draft/void semantics aside). */
|
|
56
|
+
balance_cents: number;
|
|
57
|
+
issued_at: number | null;
|
|
58
|
+
due_at: number | null;
|
|
59
|
+
created_at: number;
|
|
60
|
+
updated_at: number;
|
|
61
|
+
}
|
|
62
|
+
interface InvoiceDeprecated {
|
|
63
|
+
/** @deprecated Read `short_id`. */
|
|
64
|
+
shortId: string;
|
|
65
|
+
/** @deprecated Read `customer_id`. */
|
|
39
66
|
customerId: string;
|
|
67
|
+
/** @deprecated Read `customer_name`. */
|
|
40
68
|
customerName: string | null;
|
|
41
|
-
|
|
42
|
-
lines: InvoiceLine[];
|
|
69
|
+
/** @deprecated Read `subtotal_cents`. */
|
|
43
70
|
subtotalCents: number;
|
|
71
|
+
/** @deprecated Read `total_cents`. */
|
|
44
72
|
totalCents: number;
|
|
73
|
+
/** @deprecated Read `amount_paid_cents`. */
|
|
45
74
|
amountPaidCents: number;
|
|
46
|
-
/**
|
|
75
|
+
/** @deprecated Read `balance_cents`. */
|
|
47
76
|
balanceCents: number;
|
|
77
|
+
/** @deprecated Read `issued_at`. */
|
|
48
78
|
issuedAt: number | null;
|
|
79
|
+
/** @deprecated Read `due_at`. */
|
|
49
80
|
dueAt: number | null;
|
|
81
|
+
/** @deprecated Read `created_at`. */
|
|
50
82
|
createdAt: number;
|
|
83
|
+
/** @deprecated Read `updated_at`. */
|
|
51
84
|
updatedAt: number;
|
|
52
85
|
}
|
|
53
|
-
|
|
86
|
+
type Invoice = Omit<InvoiceDto, 'lines'> & {
|
|
87
|
+
lines: InvoiceLine[];
|
|
88
|
+
} & InvoiceDeprecated;
|
|
89
|
+
/** A line as supplied by the caller (amount_cents is computed server-side). */
|
|
54
90
|
interface InvoiceLineInput {
|
|
55
91
|
description: string;
|
|
56
92
|
quantity: number;
|
|
57
|
-
|
|
93
|
+
/** Integer cents. */
|
|
94
|
+
unit_price_cents?: number;
|
|
95
|
+
product_id?: string;
|
|
96
|
+
/** @deprecated Send `unit_price_cents`. */
|
|
97
|
+
unitPriceCents?: number;
|
|
98
|
+
/** @deprecated Send `product_id`. */
|
|
58
99
|
productId?: string;
|
|
59
100
|
}
|
|
60
101
|
interface CreateInvoiceInput {
|
|
61
|
-
|
|
102
|
+
/** A customers-service ref: id, short id or external id. */
|
|
103
|
+
customer_id?: string;
|
|
62
104
|
currency: string;
|
|
63
105
|
lines: InvoiceLineInput[];
|
|
106
|
+
/** @deprecated Send `customer_id`. */
|
|
107
|
+
customerId?: string;
|
|
64
108
|
}
|
|
65
109
|
interface UpdateInvoiceInput {
|
|
66
|
-
|
|
110
|
+
customer_id?: string;
|
|
67
111
|
lines?: InvoiceLineInput[];
|
|
112
|
+
/** @deprecated Send `customer_id`. */
|
|
113
|
+
customerId?: string;
|
|
68
114
|
}
|
|
69
115
|
interface FinalizeInvoiceInput {
|
|
70
116
|
/** Epoch millis; defaults to now server-side. */
|
|
71
|
-
|
|
117
|
+
issued_at?: number;
|
|
72
118
|
/** Epoch millis; 0/omitted = no due date. */
|
|
119
|
+
due_at?: number;
|
|
120
|
+
/** @deprecated Send `issued_at`. */
|
|
121
|
+
issuedAt?: number;
|
|
122
|
+
/** @deprecated Send `due_at`. */
|
|
73
123
|
dueAt?: number;
|
|
74
124
|
}
|
|
75
125
|
interface RecordPaymentInput {
|
|
76
|
-
|
|
126
|
+
amount_cents?: number;
|
|
77
127
|
method?: string;
|
|
78
128
|
reference?: string;
|
|
129
|
+
/** @deprecated Send `amount_cents`. */
|
|
130
|
+
amountCents?: number;
|
|
79
131
|
}
|
|
80
132
|
interface TransactionRow {
|
|
81
133
|
id: string;
|
|
@@ -91,26 +143,45 @@ interface TransactionRow {
|
|
|
91
143
|
created_at: number;
|
|
92
144
|
updated_at: number;
|
|
93
145
|
}
|
|
94
|
-
interface
|
|
146
|
+
interface TransactionDto {
|
|
95
147
|
id: string;
|
|
148
|
+
invoice_id: string;
|
|
149
|
+
invoice_number: number;
|
|
150
|
+
customer_id: string;
|
|
151
|
+
customer_name: string | null;
|
|
152
|
+
kind: string;
|
|
153
|
+
amount_cents: number;
|
|
154
|
+
currency: string;
|
|
155
|
+
method: string | null;
|
|
156
|
+
reference: string | null;
|
|
157
|
+
created_at: number;
|
|
158
|
+
}
|
|
159
|
+
interface TransactionDeprecated {
|
|
160
|
+
/** @deprecated Read `invoice_id`. */
|
|
96
161
|
invoiceId: string;
|
|
162
|
+
/** @deprecated Read `invoice_number`. */
|
|
97
163
|
invoiceNumber: number;
|
|
164
|
+
/** @deprecated Read `customer_id`. */
|
|
98
165
|
customerId: string;
|
|
166
|
+
/** @deprecated Read `customer_name`. */
|
|
99
167
|
customerName: string | null;
|
|
100
|
-
|
|
168
|
+
/** @deprecated Read `amount_cents`. */
|
|
101
169
|
amountCents: number;
|
|
102
|
-
|
|
103
|
-
method: string | null;
|
|
104
|
-
reference: string | null;
|
|
170
|
+
/** @deprecated Read `created_at`. */
|
|
105
171
|
createdAt: number;
|
|
106
172
|
}
|
|
173
|
+
type Transaction = TransactionDto & TransactionDeprecated;
|
|
107
174
|
|
|
108
175
|
interface InvoiceClientOptions {
|
|
109
176
|
/** Base URL of invoice-service (resolved by the SDK, e.g. INVOICE_SERVICE_URL). */
|
|
110
|
-
|
|
111
|
-
/** Bearer token — the caller's
|
|
177
|
+
base_url?: string;
|
|
178
|
+
/** Bearer token — the caller's token (a service forwards its request's `realm_token`). */
|
|
112
179
|
token?: string;
|
|
113
180
|
/** Active org id — sent as X-Org-Id (invoices are org-scoped, tenant = org). */
|
|
181
|
+
org_id?: string;
|
|
182
|
+
/** @deprecated Read `base_url`. Accepted until the last caller has moved. */
|
|
183
|
+
baseUrl?: string;
|
|
184
|
+
/** @deprecated Read `org_id`. Accepted until the last caller has moved. */
|
|
114
185
|
orgId?: string;
|
|
115
186
|
/** Injectable fetch (tests / non-global environments). */
|
|
116
187
|
fetch?: typeof fetch;
|
|
@@ -134,9 +205,14 @@ interface HttpClient {
|
|
|
134
205
|
* service validates + owns the ledger math. Every call carries the org id
|
|
135
206
|
* (X-Org-Id) — invoices are per-org, and the identity JWT is org-less (ADR-009).
|
|
136
207
|
*/
|
|
208
|
+
/**
|
|
209
|
+
* Paths here are the service's own — `/invoices`, not `/v1/invoices`. A gateway
|
|
210
|
+
* publishes the service at `/v1/<name>` and strips that prefix (ADR-042 §4), so
|
|
211
|
+
* the client says what the service serves and `baseUrl` says where it is.
|
|
212
|
+
*/
|
|
137
213
|
declare class InvoiceClient {
|
|
138
214
|
private readonly opts;
|
|
139
|
-
private readonly
|
|
215
|
+
private readonly root;
|
|
140
216
|
private readonly fetchImpl;
|
|
141
217
|
constructor(opts: InvoiceClientOptions);
|
|
142
218
|
withOrg(orgId: string): InvoiceClient;
|
|
@@ -166,4 +242,4 @@ declare class InvoiceClient {
|
|
|
166
242
|
/** Factory used by the SDK capability registry. */
|
|
167
243
|
declare function createInvoiceClient(opts: InvoiceClientOptions): InvoiceClient;
|
|
168
244
|
|
|
169
|
-
export { type CreateInvoiceInput, type FinalizeInvoiceInput, type HttpClient, type Invoice, InvoiceClient, type InvoiceClientOptions, InvoiceError, type InvoiceLine, type InvoiceLineInput, type InvoiceRow, InvoiceStatus, type RecordPaymentInput, type Transaction, type TransactionRow, type UpdateInvoiceInput, createInvoiceClient };
|
|
245
|
+
export { type CreateInvoiceInput, type FinalizeInvoiceInput, type HttpClient, type Invoice, InvoiceClient, type InvoiceClientOptions, type InvoiceDeprecated, type InvoiceDto, InvoiceError, type InvoiceLine, type InvoiceLineDeprecated, type InvoiceLineDto, type InvoiceLineInput, type InvoiceRow, InvoiceStatus, type RecordPaymentInput, type Transaction, type TransactionDeprecated, type TransactionDto, type TransactionRow, type UpdateInvoiceInput, createInvoiceClient };
|