@baseworks/invoice 0.1.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 +79 -0
- package/dist/chunk-7P7ARN3X.js +78 -0
- package/dist/cli.d.ts +22 -0
- package/dist/cli.js +167 -0
- package/dist/client.d.ts +169 -0
- package/dist/client.js +115 -0
- package/dist/events.d.ts +180 -0
- package/dist/events.js +12 -0
- package/dist/projections.d.ts +22 -0
- package/dist/projections.js +72 -0
- package/package.json +39 -0
- package/src/__tests__/cli.test.ts +117 -0
- package/src/__tests__/helpers.ts +20 -0
- package/src/cli.ts +255 -0
- package/src/client.ts +168 -0
- package/src/events.ts +95 -0
- package/src/index.ts +1 -0
- package/src/projections.ts +78 -0
- package/src/types.ts +120 -0
package/dist/events.d.ts
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import * as _baseworks_foldbase from '@baseworks/foldbase';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Event catalog — the single source of truth for invoice-service payloads.
|
|
6
|
+
* One aggregate: `invoice`, the AR ledger entry. Status machine:
|
|
7
|
+
*
|
|
8
|
+
* draft ──finalize──▶ finalized ──recordPayment(balance≤0)──▶ paid
|
|
9
|
+
* │ │
|
|
10
|
+
* update void│──────────────────────────────────▶ void
|
|
11
|
+
*
|
|
12
|
+
* MONEY IS INTEGER CENTS everywhere (never a float). Derived state — line
|
|
13
|
+
* amounts, subtotal/total, the running amount-paid, the per-org invoice number,
|
|
14
|
+
* and the resulting status — is computed by the STORE before emit and carried as
|
|
15
|
+
* absolute values in the event, so the projection is a plain upsert (no
|
|
16
|
+
* accumulation). `lines` is a serialized JSON array. Timestamps are epoch millis.
|
|
17
|
+
* A FINALIZED invoice is immutable: only PaymentRecorded / InvoiceVoided follow.
|
|
18
|
+
*/
|
|
19
|
+
declare const InvoiceStatus: z.ZodEnum<["draft", "finalized", "paid", "void"]>;
|
|
20
|
+
type InvoiceStatus = z.infer<typeof InvoiceStatus>;
|
|
21
|
+
declare const InvoiceEvents: _baseworks_foldbase.EventCatalog<{
|
|
22
|
+
InvoiceDrafted: z.ZodObject<{
|
|
23
|
+
customerId: z.ZodString;
|
|
24
|
+
customerName: z.ZodString;
|
|
25
|
+
currency: z.ZodString;
|
|
26
|
+
lines: z.ZodString;
|
|
27
|
+
subtotalCents: z.ZodNumber;
|
|
28
|
+
totalCents: z.ZodNumber;
|
|
29
|
+
shortId: z.ZodString;
|
|
30
|
+
at: z.ZodNumber;
|
|
31
|
+
}, "strip", z.ZodTypeAny, {
|
|
32
|
+
at: number;
|
|
33
|
+
customerId: string;
|
|
34
|
+
customerName: string;
|
|
35
|
+
currency: string;
|
|
36
|
+
lines: string;
|
|
37
|
+
subtotalCents: number;
|
|
38
|
+
totalCents: number;
|
|
39
|
+
shortId: string;
|
|
40
|
+
}, {
|
|
41
|
+
at: number;
|
|
42
|
+
customerId: string;
|
|
43
|
+
customerName: string;
|
|
44
|
+
currency: string;
|
|
45
|
+
lines: string;
|
|
46
|
+
subtotalCents: number;
|
|
47
|
+
totalCents: number;
|
|
48
|
+
shortId: string;
|
|
49
|
+
}>;
|
|
50
|
+
InvoiceUpdated: z.ZodObject<{
|
|
51
|
+
customerId: z.ZodString;
|
|
52
|
+
customerName: z.ZodString;
|
|
53
|
+
lines: z.ZodString;
|
|
54
|
+
subtotalCents: z.ZodNumber;
|
|
55
|
+
totalCents: z.ZodNumber;
|
|
56
|
+
at: z.ZodNumber;
|
|
57
|
+
}, "strip", z.ZodTypeAny, {
|
|
58
|
+
at: number;
|
|
59
|
+
customerId: string;
|
|
60
|
+
customerName: string;
|
|
61
|
+
lines: string;
|
|
62
|
+
subtotalCents: number;
|
|
63
|
+
totalCents: number;
|
|
64
|
+
}, {
|
|
65
|
+
at: number;
|
|
66
|
+
customerId: string;
|
|
67
|
+
customerName: string;
|
|
68
|
+
lines: string;
|
|
69
|
+
subtotalCents: number;
|
|
70
|
+
totalCents: number;
|
|
71
|
+
}>;
|
|
72
|
+
InvoiceFinalized: z.ZodObject<{
|
|
73
|
+
number: z.ZodNumber;
|
|
74
|
+
lines: z.ZodString;
|
|
75
|
+
subtotalCents: z.ZodNumber;
|
|
76
|
+
totalCents: z.ZodNumber;
|
|
77
|
+
issuedAt: z.ZodNumber;
|
|
78
|
+
dueAt: z.ZodNumber;
|
|
79
|
+
at: z.ZodNumber;
|
|
80
|
+
}, "strip", z.ZodTypeAny, {
|
|
81
|
+
number: number;
|
|
82
|
+
at: number;
|
|
83
|
+
lines: string;
|
|
84
|
+
subtotalCents: number;
|
|
85
|
+
totalCents: number;
|
|
86
|
+
issuedAt: number;
|
|
87
|
+
dueAt: number;
|
|
88
|
+
}, {
|
|
89
|
+
number: number;
|
|
90
|
+
at: number;
|
|
91
|
+
lines: string;
|
|
92
|
+
subtotalCents: number;
|
|
93
|
+
totalCents: number;
|
|
94
|
+
issuedAt: number;
|
|
95
|
+
dueAt: number;
|
|
96
|
+
}>;
|
|
97
|
+
PaymentRecorded: z.ZodObject<{
|
|
98
|
+
amountCents: z.ZodNumber;
|
|
99
|
+
amountPaidCents: z.ZodNumber;
|
|
100
|
+
status: z.ZodEnum<["draft", "finalized", "paid", "void"]>;
|
|
101
|
+
method: z.ZodString;
|
|
102
|
+
reference: z.ZodString;
|
|
103
|
+
at: z.ZodNumber;
|
|
104
|
+
}, "strip", z.ZodTypeAny, {
|
|
105
|
+
method: string;
|
|
106
|
+
at: number;
|
|
107
|
+
status: "draft" | "finalized" | "paid" | "void";
|
|
108
|
+
amountCents: number;
|
|
109
|
+
amountPaidCents: number;
|
|
110
|
+
reference: string;
|
|
111
|
+
}, {
|
|
112
|
+
method: string;
|
|
113
|
+
at: number;
|
|
114
|
+
status: "draft" | "finalized" | "paid" | "void";
|
|
115
|
+
amountCents: number;
|
|
116
|
+
amountPaidCents: number;
|
|
117
|
+
reference: string;
|
|
118
|
+
}>;
|
|
119
|
+
InvoiceVoided: z.ZodObject<{
|
|
120
|
+
reason: z.ZodString;
|
|
121
|
+
at: z.ZodNumber;
|
|
122
|
+
}, "strip", z.ZodTypeAny, {
|
|
123
|
+
at: number;
|
|
124
|
+
reason: string;
|
|
125
|
+
}, {
|
|
126
|
+
at: number;
|
|
127
|
+
reason: string;
|
|
128
|
+
}>;
|
|
129
|
+
}> & {
|
|
130
|
+
newId(): string;
|
|
131
|
+
};
|
|
132
|
+
/**
|
|
133
|
+
* The transaction ledger — one stream (and one read-model row) PER money movement,
|
|
134
|
+
* unlike the invoice's cached `amount_paid`. A payment emits BOTH `PaymentRecorded`
|
|
135
|
+
* (on the invoice, to keep its balance/status fast) AND `TransactionRecorded` (here,
|
|
136
|
+
* a durable per-payment record). `kind` is 'payment' today; 'refund' / 'adjustment'
|
|
137
|
+
* extend it later without touching the invoice aggregate.
|
|
138
|
+
*/
|
|
139
|
+
declare const TransactionKind: z.ZodEnum<["payment", "refund", "adjustment"]>;
|
|
140
|
+
type TransactionKind = z.infer<typeof TransactionKind>;
|
|
141
|
+
declare const TransactionEvents: _baseworks_foldbase.EventCatalog<{
|
|
142
|
+
TransactionRecorded: z.ZodObject<{
|
|
143
|
+
invoiceId: z.ZodString;
|
|
144
|
+
invoiceNumber: z.ZodNumber;
|
|
145
|
+
customerId: z.ZodString;
|
|
146
|
+
customerName: z.ZodString;
|
|
147
|
+
kind: z.ZodEnum<["payment", "refund", "adjustment"]>;
|
|
148
|
+
amountCents: z.ZodNumber;
|
|
149
|
+
currency: z.ZodString;
|
|
150
|
+
method: z.ZodString;
|
|
151
|
+
reference: z.ZodString;
|
|
152
|
+
at: z.ZodNumber;
|
|
153
|
+
}, "strip", z.ZodTypeAny, {
|
|
154
|
+
method: string;
|
|
155
|
+
at: number;
|
|
156
|
+
customerId: string;
|
|
157
|
+
customerName: string;
|
|
158
|
+
currency: string;
|
|
159
|
+
amountCents: number;
|
|
160
|
+
reference: string;
|
|
161
|
+
invoiceId: string;
|
|
162
|
+
invoiceNumber: number;
|
|
163
|
+
kind: "payment" | "refund" | "adjustment";
|
|
164
|
+
}, {
|
|
165
|
+
method: string;
|
|
166
|
+
at: number;
|
|
167
|
+
customerId: string;
|
|
168
|
+
customerName: string;
|
|
169
|
+
currency: string;
|
|
170
|
+
amountCents: number;
|
|
171
|
+
reference: string;
|
|
172
|
+
invoiceId: string;
|
|
173
|
+
invoiceNumber: number;
|
|
174
|
+
kind: "payment" | "refund" | "adjustment";
|
|
175
|
+
}>;
|
|
176
|
+
}> & {
|
|
177
|
+
newId(): string;
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
export { InvoiceEvents, InvoiceStatus, TransactionEvents, TransactionKind };
|
package/dist/events.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import * as _baseworks_foldbase from '@baseworks/foldbase';
|
|
2
|
+
import { PolicyDef } from '@baseworks/foldbase';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Read-model definition registered on boot (idempotent). Each event upserts an
|
|
6
|
+
* absolute snapshot — the store computed the derived state — so no fold needs to
|
|
7
|
+
* read the current row. Numeric columns are seeded to 0 at draft so their types
|
|
8
|
+
* stay integer. Projection name has NO `read_` prefix (foldbase adds it → table
|
|
9
|
+
* `read_invoices`). Archive/void keeps the row (an invoice is a durable record);
|
|
10
|
+
* only status changes.
|
|
11
|
+
*/
|
|
12
|
+
declare const readInvoices: _baseworks_foldbase.ProjectionSpec;
|
|
13
|
+
/**
|
|
14
|
+
* The transaction ledger read model — one row per money movement (keyed by the
|
|
15
|
+
* transaction's own stream id). Immutable records: no update/delete events.
|
|
16
|
+
*/
|
|
17
|
+
declare const readTransactions: _baseworks_foldbase.ProjectionSpec;
|
|
18
|
+
declare const PROJECTIONS: readonly [_baseworks_foldbase.ProjectionSpec, _baseworks_foldbase.ProjectionSpec];
|
|
19
|
+
/** Permissive policies for the service's own `admin` identity (see ADR 0001). */
|
|
20
|
+
declare const POLICIES: PolicyDef[];
|
|
21
|
+
|
|
22
|
+
export { POLICIES, PROJECTIONS, readInvoices, readTransactions };
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import {
|
|
2
|
+
InvoiceEvents,
|
|
3
|
+
TransactionEvents
|
|
4
|
+
} from "./chunk-7P7ARN3X.js";
|
|
5
|
+
|
|
6
|
+
// src/projections.ts
|
|
7
|
+
import { defineProjection } from "@baseworks/foldbase";
|
|
8
|
+
var readInvoices = defineProjection("invoices", InvoiceEvents, (on) => ({
|
|
9
|
+
InvoiceDrafted: on.InvoiceDrafted.upsert((e) => ({
|
|
10
|
+
customer_id: e.customerId,
|
|
11
|
+
customer_name: e.customerName,
|
|
12
|
+
currency: e.currency,
|
|
13
|
+
lines: e.lines,
|
|
14
|
+
number: 0,
|
|
15
|
+
status: "draft",
|
|
16
|
+
subtotal_cents: e.subtotalCents,
|
|
17
|
+
total_cents: e.totalCents,
|
|
18
|
+
amount_paid_cents: 0,
|
|
19
|
+
issued_at: 0,
|
|
20
|
+
due_at: 0,
|
|
21
|
+
short_id: e.shortId,
|
|
22
|
+
created_at: e.at
|
|
23
|
+
})),
|
|
24
|
+
InvoiceUpdated: on.InvoiceUpdated.upsert((e) => ({
|
|
25
|
+
customer_id: e.customerId,
|
|
26
|
+
customer_name: e.customerName,
|
|
27
|
+
lines: e.lines,
|
|
28
|
+
subtotal_cents: e.subtotalCents,
|
|
29
|
+
total_cents: e.totalCents
|
|
30
|
+
})),
|
|
31
|
+
InvoiceFinalized: on.InvoiceFinalized.upsert((e) => ({
|
|
32
|
+
number: e.number,
|
|
33
|
+
status: "finalized",
|
|
34
|
+
lines: e.lines,
|
|
35
|
+
subtotal_cents: e.subtotalCents,
|
|
36
|
+
total_cents: e.totalCents,
|
|
37
|
+
issued_at: e.issuedAt,
|
|
38
|
+
due_at: e.dueAt
|
|
39
|
+
})),
|
|
40
|
+
PaymentRecorded: on.PaymentRecorded.upsert((e) => ({
|
|
41
|
+
amount_paid_cents: e.amountPaidCents,
|
|
42
|
+
status: e.status
|
|
43
|
+
})),
|
|
44
|
+
InvoiceVoided: on.InvoiceVoided.upsert(() => ({
|
|
45
|
+
status: "void"
|
|
46
|
+
}))
|
|
47
|
+
}));
|
|
48
|
+
var readTransactions = defineProjection("transactions", TransactionEvents, (on) => ({
|
|
49
|
+
TransactionRecorded: on.TransactionRecorded.upsert((e) => ({
|
|
50
|
+
invoice_id: e.invoiceId,
|
|
51
|
+
invoice_number: e.invoiceNumber,
|
|
52
|
+
customer_id: e.customerId,
|
|
53
|
+
customer_name: e.customerName,
|
|
54
|
+
kind: e.kind,
|
|
55
|
+
amount_cents: e.amountCents,
|
|
56
|
+
currency: e.currency,
|
|
57
|
+
method: e.method,
|
|
58
|
+
reference: e.reference,
|
|
59
|
+
created_at: e.at
|
|
60
|
+
}))
|
|
61
|
+
}));
|
|
62
|
+
var PROJECTIONS = [readInvoices, readTransactions];
|
|
63
|
+
var POLICIES = [
|
|
64
|
+
{ name: "invoices", role: "admin" },
|
|
65
|
+
{ name: "transactions", role: "admin" }
|
|
66
|
+
];
|
|
67
|
+
export {
|
|
68
|
+
POLICIES,
|
|
69
|
+
PROJECTIONS,
|
|
70
|
+
readInvoices,
|
|
71
|
+
readTransactions
|
|
72
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@baseworks/invoice",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Invoice capability: typed HTTP client, event catalog, projections, and CLI plugin for invoice-service (AR ledger). Contract-first, versioned with the service.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"publishConfig": {
|
|
7
|
+
"access": "public"
|
|
8
|
+
},
|
|
9
|
+
"exports": {
|
|
10
|
+
".": "./dist/client.js",
|
|
11
|
+
"./events": "./dist/events.js",
|
|
12
|
+
"./projections": "./dist/projections.js",
|
|
13
|
+
"./cli": "./dist/cli.js"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"src"
|
|
18
|
+
],
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@baseworks/cli": "^0.2.0",
|
|
21
|
+
"commander": "^15.0.0",
|
|
22
|
+
"zod": "^3.25.76",
|
|
23
|
+
"@baseworks/foldbase": "0.1.1"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@types/node": "^22.20.0",
|
|
27
|
+
"msw": "^2.14.6",
|
|
28
|
+
"tsup": "^8.5.1",
|
|
29
|
+
"typescript": "^5.9.3",
|
|
30
|
+
"vitest": "^4.1.9",
|
|
31
|
+
"@baseworks/tsconfig": "0.1.0"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "tsup",
|
|
35
|
+
"typecheck": "tsc --noEmit",
|
|
36
|
+
"test": "vitest run",
|
|
37
|
+
"test:watch": "vitest"
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/// <reference types="vitest/globals" />
|
|
2
|
+
import { beforeAll, afterEach, afterAll, describe, it, expect } from 'vitest'
|
|
3
|
+
import { http, HttpResponse } from 'msw'
|
|
4
|
+
import { run, mocks, server } from './helpers.js'
|
|
5
|
+
import { TEST_BASE } from './helpers.js'
|
|
6
|
+
|
|
7
|
+
beforeAll(() => server.listen({ onUnhandledRequest: 'warn' }))
|
|
8
|
+
afterEach(() => server.resetHandlers())
|
|
9
|
+
afterAll(() => server.close())
|
|
10
|
+
|
|
11
|
+
const DRAFT = {
|
|
12
|
+
id: '019f6a3e-1111-7c71-beff-496d1be3fd1f',
|
|
13
|
+
shortId: 'inv9x2',
|
|
14
|
+
number: null,
|
|
15
|
+
status: 'draft',
|
|
16
|
+
customerId: 'cst1',
|
|
17
|
+
customerName: 'Acme Co',
|
|
18
|
+
currency: 'USD',
|
|
19
|
+
lines: [{ description: 'Consulting', quantity: 10, unitPriceCents: 15000, amountCents: 150000, productId: null }],
|
|
20
|
+
subtotalCents: 150000,
|
|
21
|
+
totalCents: 150000,
|
|
22
|
+
amountPaidCents: 0,
|
|
23
|
+
balanceCents: 150000,
|
|
24
|
+
issuedAt: null,
|
|
25
|
+
dueAt: null,
|
|
26
|
+
createdAt: 1784200000000,
|
|
27
|
+
updatedAt: 1784200000000,
|
|
28
|
+
}
|
|
29
|
+
const FINAL = { ...DRAFT, number: 1, status: 'finalized', issuedAt: 1784200000000 }
|
|
30
|
+
const PAID = { ...FINAL, status: 'paid', amountPaidCents: 150000, balanceCents: 0 }
|
|
31
|
+
|
|
32
|
+
describe('dtab invoices (list)', () => {
|
|
33
|
+
it('columns incl. NO/STATUS/TOTAL/BALANCE, draft NO shows —', async () => {
|
|
34
|
+
mocks.mockGet('/v1/invoices', { invoices: [DRAFT, FINAL] })
|
|
35
|
+
const r = await run(['invoices'])
|
|
36
|
+
expect(r.exitCode).toBe(0)
|
|
37
|
+
expect(r.stdout).toContain('inv9x2')
|
|
38
|
+
expect(r.stdout).toContain('Acme Co')
|
|
39
|
+
expect(r.stdout).toContain('1500.00 USD') // 150000 cents
|
|
40
|
+
expect(r.stdout).toContain('#1') // finalized number
|
|
41
|
+
expect(r.stdout).toContain('—') // draft number
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
it('inv + invoice aliases list', async () => {
|
|
45
|
+
mocks.mockGet('/v1/invoices', { invoices: [DRAFT] })
|
|
46
|
+
expect((await run(['inv'])).stdout).toContain('inv9x2')
|
|
47
|
+
mocks.mockGet('/v1/invoices', { invoices: [DRAFT] })
|
|
48
|
+
expect((await run(['invoice'])).stdout).toContain('inv9x2')
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
it('--status filter passes through', async () => {
|
|
52
|
+
let seen = ''
|
|
53
|
+
server.use(http.get(`${TEST_BASE}/v1/invoices`, ({ request }) => {
|
|
54
|
+
seen = new URL(request.url).search
|
|
55
|
+
return HttpResponse.json({ invoices: [] })
|
|
56
|
+
}))
|
|
57
|
+
await run(['invoices', '--status', 'finalized'])
|
|
58
|
+
expect(seen).toContain('status=finalized')
|
|
59
|
+
})
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
describe('dtab invoices create', () => {
|
|
63
|
+
it('parses --line "desc|qty|price" → cents, sends customer + currency', async () => {
|
|
64
|
+
const cap = mocks.mockPostCapture('/v1/invoices', { invoice: DRAFT })
|
|
65
|
+
const r = await run(['invoices', 'create', '--customer', 'cst1', '--currency', 'USD', '--line', 'Consulting|10|150.00'])
|
|
66
|
+
expect(r.exitCode).toBe(0)
|
|
67
|
+
const body = cap.body as Record<string, unknown>
|
|
68
|
+
expect(body.customerId).toBe('cst1')
|
|
69
|
+
expect(body.currency).toBe('USD')
|
|
70
|
+
expect(body.lines).toEqual([{ description: 'Consulting', quantity: 10, unitPriceCents: 15000 }])
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
it('rejects malformed --line', async () => {
|
|
74
|
+
const r = await run(['invoices', 'create', '--customer', 'c', '--currency', 'USD', '--line', 'bad-line'])
|
|
75
|
+
expect(r.exitCode).not.toBe(0)
|
|
76
|
+
expect(r.stderr).toContain('description|qty|unitPrice')
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
it('rejects sub-cent unit price', async () => {
|
|
80
|
+
const r = await run(['invoices', 'create', '--customer', 'c', '--currency', 'USD', '--line', 'X|1|1.999'])
|
|
81
|
+
expect(r.exitCode).not.toBe(0)
|
|
82
|
+
expect(r.stderr).toContain('sub-cent')
|
|
83
|
+
})
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
describe('dtab invoices get / finalize / pay / void', () => {
|
|
87
|
+
it('get shows lines table + totals + balance', async () => {
|
|
88
|
+
mocks.mockGet('/v1/invoices/inv9x2', { invoice: FINAL })
|
|
89
|
+
const r = await run(['invoices', 'get', 'inv9x2'])
|
|
90
|
+
expect(r.stdout).toContain('Consulting')
|
|
91
|
+
expect(r.stdout).toContain('1500.00 USD')
|
|
92
|
+
expect(r.stdout).toContain('#1')
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
it('finalize posts to /finalize, shows number', async () => {
|
|
96
|
+
mocks.mockPost('/v1/invoices/inv9x2/finalize', { invoice: FINAL })
|
|
97
|
+
const r = await run(['invoices', 'finalize', 'inv9x2'])
|
|
98
|
+
expect(r.stdout).toContain('#1')
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
it('pay converts --amount to cents, posts payment', async () => {
|
|
102
|
+
const cap = mocks.mockPostCapture('/v1/invoices/inv9x2/payments', { invoice: PAID })
|
|
103
|
+
const r = await run(['invoices', 'pay', 'inv9x2', '--amount', '1500.00', '--method', 'bank'])
|
|
104
|
+
expect(r.exitCode).toBe(0)
|
|
105
|
+
const body = cap.body as Record<string, unknown>
|
|
106
|
+
expect(body.amountCents).toBe(150000)
|
|
107
|
+
expect(body.method).toBe('bank')
|
|
108
|
+
expect(r.stdout).toContain('paid')
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
it('void posts to /void', async () => {
|
|
112
|
+
mocks.mockPost('/v1/invoices/inv9x2/void', { invoice: { ...FINAL, status: 'void' } })
|
|
113
|
+
const r = await run(['invoices', 'void', 'inv9x2', '--reason', 'mistake'])
|
|
114
|
+
expect(r.exitCode).toBe(0)
|
|
115
|
+
expect(r.stdout).toContain('inv9x2')
|
|
116
|
+
})
|
|
117
|
+
})
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Command } from 'commander'
|
|
2
|
+
import { buildInvoiceCommand } from '../cli.js'
|
|
3
|
+
import { createApiClient } from '@baseworks/cli/client'
|
|
4
|
+
import { runCommand, createMocks, server } from '@baseworks/cli/testing'
|
|
5
|
+
|
|
6
|
+
export { runCommand, server }
|
|
7
|
+
|
|
8
|
+
export const TEST_BASE = 'http://invoice.test'
|
|
9
|
+
export const mocks = createMocks(TEST_BASE)
|
|
10
|
+
|
|
11
|
+
export function makeProgram(token = 'test-token') {
|
|
12
|
+
const http = createApiClient(() => token, () => TEST_BASE)
|
|
13
|
+
const program = new Command('dtab').exitOverride().enablePositionalOptions()
|
|
14
|
+
program.addCommand(buildInvoiceCommand({ http, cliName: 'dtab' }))
|
|
15
|
+
return program
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function run(args: string[]) {
|
|
19
|
+
return runCommand(makeProgram(), args)
|
|
20
|
+
}
|