@baseworks/invoice 0.1.1 → 1.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/src/types.ts CHANGED
@@ -1,5 +1,5 @@
1
- import type { InvoiceStatus } from './events.js'
2
- export type { InvoiceStatus } from './events.js'
1
+ import type { FiscalStatus, InvoiceSource, InvoiceStatus } from './events.js'
2
+ export type { FiscalStatus, InvoiceSource, InvoiceStatus } from './events.js'
3
3
 
4
4
  // ── read-model row (snake_case — foldbase read_invoices table) ────────────────
5
5
 
@@ -19,73 +19,241 @@ export interface InvoiceRow {
19
19
  due_at: number // 0 = none
20
20
  created_at: number
21
21
  updated_at: number
22
+ // 1.1.0: NULL on rows written before it (read as `local`, no refs, `{}`, no tax, no fiscal state).
23
+ source: string | null
24
+ mirror_key: string | null
25
+ external_refs: string | null // JSON array
26
+ metadata: string | null // JSON object
27
+ tax_cents: number | null
28
+ paid_at: number | null
29
+ voided_at: number | null
30
+ provider_updated_at: number | null
31
+ fiscal_status: string | null
32
+ fiscal_system: string | null
33
+ fiscal_external_id: string | null
34
+ fiscal_message: string | null
35
+ fiscal_updated_at: number | null
22
36
  }
23
37
 
24
- // ── REST DTOs (camelCase) ─────────────────────────────────────────────────────
38
+ // ── REST DTOs (snake_case, as every service in the realm speaks: ADR-0003) ───
39
+ //
40
+ // Until 0.3.0 the wire was camelCase. The service now answers snake_case only;
41
+ // this client puts the old camelCase names back on what it returns, as
42
+ // non-enumerable properties (readable, but `JSON.stringify` and
43
+ // `toEqual` do not see them), so a caller that has not moved still builds and
44
+ // runs. Every such name is marked @deprecated and listed in the README.
25
45
 
26
- export interface InvoiceLine {
46
+ export interface InvoiceLineDto {
27
47
  description: string
28
48
  /** May be fractional (e.g. hours). */
29
49
  quantity: number
30
50
  /** Unit price in integer cents. */
51
+ unit_price_cents: number
52
+ /** quantity × unit_price_cents, rounded to cents. */
53
+ amount_cents: number
54
+ /** Optional catalog product reference. */
55
+ product_id: string | null
56
+ }
57
+
58
+ export interface InvoiceLineDeprecated {
59
+ /** @deprecated Read `unit_price_cents`. */
31
60
  unitPriceCents: number
32
- /** quantity × unitPriceCents, rounded to cents. */
61
+ /** @deprecated Read `amount_cents`. */
33
62
  amountCents: number
34
- /** Optional catalog product reference. */
63
+ /** @deprecated Read `product_id`. */
35
64
  productId: string | null
36
65
  }
66
+ export type InvoiceLine = InvoiceLineDto & InvoiceLineDeprecated
37
67
 
38
- export interface Invoice {
68
+ /** A reference to the same document in another system. At most 10 per invoice. */
69
+ export interface ExternalRef {
70
+ system: string
39
71
  id: string
40
- shortId: string
72
+ number?: string
73
+ url?: string
74
+ pdf_url?: string
75
+ account_id?: string
76
+ livemode?: boolean
77
+ }
78
+
79
+ export type Metadata = Record<string, string | number | boolean | null>
80
+
81
+ /** `unpaid`, `partially_paid`, `paid`; `not_due` for a draft and a void invoice. Derived, never stored. */
82
+ export type PaymentStatus = 'unpaid' | 'partially_paid' | 'paid' | 'not_due'
83
+
84
+ export interface Fiscal {
85
+ status: FiscalStatus
86
+ system: string | null
87
+ external_id: string | null
88
+ message: string | null
89
+ updated_at: number
90
+ }
91
+
92
+ /** One payment on an invoice, as `GET /invoices/:ref` lists them. */
93
+ export interface InvoicePayment {
94
+ payment_id: string
95
+ amount_cents: number
96
+ currency: string
97
+ method: string | null
98
+ reference: string | null
99
+ paid_at: number
100
+ }
101
+
102
+ export interface InvoiceDto {
103
+ id: string
104
+ short_id: string
41
105
  /** Per-org invoice number; `null` while draft. */
42
106
  number: number | null
43
107
  status: InvoiceStatus
108
+ customer_id: string
109
+ customer_name: string | null
110
+ currency: string
111
+ lines: InvoiceLineDto[]
112
+ subtotal_cents: number
113
+ total_cents: number
114
+ amount_paid_cents: number
115
+ /** total_cents − amount_paid_cents (0 for draft/void semantics aside). */
116
+ balance_cents: number
117
+ issued_at: number | null
118
+ due_at: number | null
119
+ created_at: number
120
+ updated_at: number
121
+ /** `local` (made here), `stripe`, `manual` (entered from a document issued elsewhere). Old invoices are `local`. */
122
+ source: InvoiceSource
123
+ external_refs: ExternalRef[]
124
+ metadata: Metadata
125
+ tax_cents: number
126
+ payment_status: PaymentStatus
127
+ fiscal: Fiscal | null
128
+ /** Only `GET /invoices/:ref` includes the payments. */
129
+ payments?: InvoicePayment[]
130
+ }
131
+
132
+ export interface InvoiceDeprecated {
133
+ /** @deprecated Read `short_id`. */
134
+ shortId: string
135
+ /** @deprecated Read `customer_id`. */
44
136
  customerId: string
137
+ /** @deprecated Read `customer_name`. */
45
138
  customerName: string | null
46
- currency: string
47
- lines: InvoiceLine[]
139
+ /** @deprecated Read `subtotal_cents`. */
48
140
  subtotalCents: number
141
+ /** @deprecated Read `total_cents`. */
49
142
  totalCents: number
143
+ /** @deprecated Read `amount_paid_cents`. */
50
144
  amountPaidCents: number
51
- /** totalCents − amountPaidCents (0 for draft/void semantics aside). */
145
+ /** @deprecated Read `balance_cents`. */
52
146
  balanceCents: number
147
+ /** @deprecated Read `issued_at`. */
53
148
  issuedAt: number | null
149
+ /** @deprecated Read `due_at`. */
54
150
  dueAt: number | null
151
+ /** @deprecated Read `created_at`. */
55
152
  createdAt: number
153
+ /** @deprecated Read `updated_at`. */
56
154
  updatedAt: number
57
155
  }
156
+ export type Invoice = Omit<InvoiceDto, 'lines'> & { lines: InvoiceLine[] } & InvoiceDeprecated
58
157
 
59
- /** A line as supplied by the caller (amountCents is computed server-side). */
158
+ /** A line as supplied by the caller (amount_cents is computed server-side). */
60
159
  export interface InvoiceLineInput {
61
160
  description: string
62
161
  quantity: number
63
- unitPriceCents: number
162
+ /** Integer cents. */
163
+ unit_price_cents?: number
164
+ product_id?: string
165
+ /** @deprecated Send `unit_price_cents`. */
166
+ unitPriceCents?: number
167
+ /** @deprecated Send `product_id`. */
64
168
  productId?: string
65
169
  }
66
170
 
67
171
  export interface CreateInvoiceInput {
68
- customerId: string
172
+ /** A customers-service ref: id, short id or external id. */
173
+ customer_id?: string
69
174
  currency: string
70
175
  lines: InvoiceLineInput[]
176
+ metadata?: Metadata
177
+ /** @deprecated Send `customer_id`. */
178
+ customerId?: string
71
179
  }
72
180
 
73
181
  export interface UpdateInvoiceInput {
74
- customerId?: string
182
+ customer_id?: string
75
183
  lines?: InvoiceLineInput[]
184
+ /** Replaces the invoice's metadata. */
185
+ metadata?: Metadata
186
+ /** @deprecated Send `customer_id`. */
187
+ customerId?: string
76
188
  }
77
189
 
78
190
  export interface FinalizeInvoiceInput {
79
191
  /** Epoch millis; defaults to now server-side. */
80
- issuedAt?: number
192
+ issued_at?: number
81
193
  /** Epoch millis; 0/omitted = no due date. */
194
+ due_at?: number
195
+ /** @deprecated Send `issued_at`. */
196
+ issuedAt?: number
197
+ /** @deprecated Send `due_at`. */
82
198
  dueAt?: number
83
199
  }
84
200
 
85
201
  export interface RecordPaymentInput {
86
- amountCents: number
202
+ amount_cents?: number
87
203
  method?: string
88
204
  reference?: string
205
+ /** The pay record this settles. Idempotency key: the same id twice changes nothing. */
206
+ payment_id?: string
207
+ /** @deprecated Send `amount_cents`. */
208
+ amountCents?: number
209
+ }
210
+
211
+ export interface ListInvoicesOptions {
212
+ limit?: number
213
+ offset?: number
214
+ status?: string
215
+ /** One or several (an array is sent comma-separated). */
216
+ payment_status?: PaymentStatus | PaymentStatus[]
217
+ /** Unpaid or partially paid, with a due date in the past. */
218
+ overdue?: boolean
219
+ source?: InvoiceSource
220
+ fiscal_status?: FiscalStatus
221
+ customer_id?: string
222
+ }
223
+
224
+ /** The provider's whole document, for `PUT /invoices/mirror`. */
225
+ export interface MirrorInvoiceInput {
226
+ source: 'stripe' | 'manual'
227
+ external_ref: ExternalRef
228
+ customer_id: string
229
+ currency: string
230
+ lines: Array<{ description: string; quantity: number; unit_price_cents: number; amount_cents: number; product_id?: string }>
231
+ subtotal_cents: number
232
+ tax_cents: number
233
+ total_cents: number
234
+ status: 'finalized' | 'paid' | 'void'
235
+ issued_at: number
236
+ due_at?: number
237
+ paid_at?: number
238
+ voided_at?: number
239
+ /** The provider's own "updated" time: an upsert older than what is stored is ignored. */
240
+ provider_updated_at: number
241
+ metadata?: Metadata
242
+ }
243
+
244
+ export interface MirrorResult {
245
+ invoice: Invoice
246
+ /** True when this call created the invoice. */
247
+ created: boolean
248
+ /** True when the document was older than the one stored and was ignored. */
249
+ stale?: boolean
250
+ }
251
+
252
+ export interface FiscalInput {
253
+ status: FiscalStatus
254
+ system?: string
255
+ external_id?: string
256
+ message?: string
89
257
  }
90
258
 
91
259
  // ── transactions (the ledger) ─────────────────────────────────────────────────
@@ -101,20 +269,38 @@ export interface TransactionRow {
101
269
  currency: string
102
270
  method: string
103
271
  reference: string
272
+ payment_id: string | null
104
273
  created_at: number
105
274
  updated_at: number
106
275
  }
107
276
 
108
- export interface Transaction {
277
+ export interface TransactionDto {
109
278
  id: string
279
+ invoice_id: string
280
+ invoice_number: number
281
+ customer_id: string
282
+ customer_name: string | null
283
+ kind: string
284
+ amount_cents: number
285
+ currency: string
286
+ method: string | null
287
+ reference: string | null
288
+ payment_id: string | null
289
+ created_at: number
290
+ }
291
+
292
+ export interface TransactionDeprecated {
293
+ /** @deprecated Read `invoice_id`. */
110
294
  invoiceId: string
295
+ /** @deprecated Read `invoice_number`. */
111
296
  invoiceNumber: number
297
+ /** @deprecated Read `customer_id`. */
112
298
  customerId: string
299
+ /** @deprecated Read `customer_name`. */
113
300
  customerName: string | null
114
- kind: string
301
+ /** @deprecated Read `amount_cents`. */
115
302
  amountCents: number
116
- currency: string
117
- method: string | null
118
- reference: string | null
303
+ /** @deprecated Read `created_at`. */
119
304
  createdAt: number
120
305
  }
306
+ export type Transaction = TransactionDto & TransactionDeprecated
@@ -1,78 +0,0 @@
1
- // src/events.ts
2
- import { z } from "zod";
3
- import { defineAggregate } from "@baseworks/foldbase";
4
- var InvoiceStatus = z.enum(["draft", "finalized", "paid", "void"]);
5
- var InvoiceEvents = defineAggregate("invoice", {
6
- // Draft created (with its initial lines + computed totals). Numeric fields are
7
- // initialised to 0 so their read-model columns are stable integers.
8
- InvoiceDrafted: z.object({
9
- customerId: z.string().min(1),
10
- customerName: z.string(),
11
- // snapshot at draft time; '' if unknown
12
- currency: z.string().length(3),
13
- lines: z.string(),
14
- // JSON array of { description, quantity, unitPriceCents, amountCents, productId }
15
- subtotalCents: z.number().int(),
16
- totalCents: z.number().int(),
17
- shortId: z.string().min(1),
18
- at: z.number().int()
19
- }),
20
- // Draft edited (customer / lines). Only valid while status = draft.
21
- InvoiceUpdated: z.object({
22
- customerId: z.string().min(1),
23
- customerName: z.string(),
24
- lines: z.string(),
25
- subtotalCents: z.number().int(),
26
- totalCents: z.number().int(),
27
- at: z.number().int()
28
- }),
29
- // Finalize: assign the per-org number, freeze lines + totals, set issue/due.
30
- InvoiceFinalized: z.object({
31
- number: z.number().int(),
32
- lines: z.string(),
33
- subtotalCents: z.number().int(),
34
- totalCents: z.number().int(),
35
- issuedAt: z.number().int(),
36
- dueAt: z.number().int(),
37
- // 0 = no due date
38
- at: z.number().int()
39
- }),
40
- // A payment applied to a finalized invoice. `amountPaidCents` is the new running
41
- // total; `status` is the resulting status ('paid' when balance ≤ 0, else
42
- // 'finalized') — both computed by the store.
43
- PaymentRecorded: z.object({
44
- amountCents: z.number().int(),
45
- amountPaidCents: z.number().int(),
46
- status: InvoiceStatus,
47
- method: z.string(),
48
- reference: z.string(),
49
- at: z.number().int()
50
- }),
51
- InvoiceVoided: z.object({
52
- reason: z.string(),
53
- at: z.number().int()
54
- })
55
- });
56
- var TransactionKind = z.enum(["payment", "refund", "adjustment"]);
57
- var TransactionEvents = defineAggregate("transaction", {
58
- TransactionRecorded: z.object({
59
- invoiceId: z.string().min(1),
60
- invoiceNumber: z.number().int(),
61
- // the invoice's number at the time
62
- customerId: z.string(),
63
- customerName: z.string(),
64
- kind: TransactionKind,
65
- amountCents: z.number().int(),
66
- currency: z.string().length(3),
67
- method: z.string(),
68
- reference: z.string(),
69
- at: z.number().int()
70
- })
71
- });
72
-
73
- export {
74
- InvoiceStatus,
75
- InvoiceEvents,
76
- TransactionKind,
77
- TransactionEvents
78
- };