@processfocus/plugin-xero 0.1.0-next.2 → 0.1.0-next.4
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 +49 -185
- package/dist/index.d.ts +5 -1
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/dist/lib/contact-resolution.d.ts +2 -6
- package/dist/lib/contact-resolution.js +17 -5
- package/dist/lib/contact-resolution.js.map +1 -1
- package/dist/lib/errors.d.ts +4 -0
- package/dist/lib/errors.js.map +1 -1
- package/dist/lib/identities.d.ts +63 -39
- package/dist/lib/identities.js +36 -13
- package/dist/lib/identities.js.map +1 -1
- package/dist/lib/invoice-reconciliation.d.ts +2 -4
- package/dist/lib/invoice-reconciliation.js +23 -13
- package/dist/lib/invoice-reconciliation.js.map +1 -1
- package/dist/lib/money.d.ts +4 -4
- package/dist/lib/money.js +1 -9
- package/dist/lib/money.js.map +1 -1
- package/dist/lib/xero-configuration.d.ts +24 -0
- package/dist/lib/xero-configuration.js +15 -0
- package/dist/lib/xero-configuration.js.map +1 -0
- package/dist/lib/xero-draft-invoice.d.ts +39 -39
- package/dist/lib/xero-draft-invoice.js +25 -22
- package/dist/lib/xero-draft-invoice.js.map +1 -1
- package/dist/lib/xero-http.d.ts +1 -1
- package/dist/lib/xero-http.js.map +1 -1
- package/dist/lib/xero-invoice-cancellation-step.d.ts +16 -0
- package/dist/lib/xero-invoice-cancellation-step.js +17 -0
- package/dist/lib/xero-invoice-cancellation-step.js.map +1 -0
- package/dist/lib/xero-invoice-cancellation.d.ts +19 -0
- package/dist/lib/xero-invoice-cancellation.js +15 -0
- package/dist/lib/xero-invoice-cancellation.js.map +1 -0
- package/dist/lib/xero-invoice-live.d.ts +11 -7
- package/dist/lib/xero-invoice-live.js +468 -210
- package/dist/lib/xero-invoice-live.js.map +1 -1
- package/dist/lib/xero-invoice-lookup.d.ts +13 -13
- package/dist/lib/xero-invoice-lookup.js +1 -1
- package/dist/lib/xero-invoice-lookup.js.map +1 -1
- package/dist/lib/xero-invoice-preparation.d.ts +52 -0
- package/dist/lib/xero-invoice-preparation.js +47 -0
- package/dist/lib/xero-invoice-preparation.js.map +1 -0
- package/dist/lib/xero-invoice-step.d.ts +10 -3
- package/dist/lib/xero-invoice-step.js +4 -1
- package/dist/lib/xero-invoice-step.js.map +1 -1
- package/dist/lib/xero-invoice-test.d.ts +11 -5
- package/dist/lib/xero-invoice-test.js +109 -56
- package/dist/lib/xero-invoice-test.js.map +1 -1
- package/dist/lib/xero-invoice.d.ts +1 -1
- package/dist/lib/xero-invoice.js +2 -2
- package/dist/lib/xero-invoice.js.map +1 -1
- package/dist/lib/xero-repeated-draft-invoice.d.ts +43 -0
- package/dist/lib/xero-repeated-draft-invoice.js +32 -0
- package/dist/lib/xero-repeated-draft-invoice.js.map +1 -0
- package/dist/lib/xero-token.js +2 -2
- package/dist/lib/xero-token.js.map +1 -1
- package/dist/lib/xero-wire.d.ts +96 -62
- package/dist/lib/xero-wire.js +22 -1
- package/dist/lib/xero-wire.js.map +1 -1
- package/dist/runtime.d.ts +2 -1
- package/dist/runtime.js +2 -1
- package/dist/runtime.js.map +1 -1
- package/package.json +3 -4
package/README.md
CHANGED
|
@@ -1,207 +1,71 @@
|
|
|
1
1
|
# @processfocus/plugin-xero
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
authors receive one `XeroInvoiceStep` that issues a single authorised
|
|
5
|
-
accounts-receivable invoice. Custom Connection authentication, REST transport,
|
|
6
|
-
Xero wire schemas, catalog defaults, and response decoding stay behind that
|
|
7
|
-
interface.
|
|
8
|
-
|
|
9
|
-
## Installation
|
|
10
|
-
|
|
11
|
-
This package is part of the workspace. Add it to your process dependencies:
|
|
12
|
-
|
|
13
|
-
```json
|
|
14
|
-
{
|
|
15
|
-
"dependencies": {
|
|
16
|
-
"@processfocus/plugin-xero": "0.1.0-next.2"
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
```
|
|
3
|
+
Connects Process Focus processes to Xero sales invoicing.
|
|
20
4
|
|
|
21
|
-
##
|
|
5
|
+
## Setup
|
|
22
6
|
|
|
23
|
-
|
|
24
|
-
Set these environment variables:
|
|
7
|
+
Use a single-organisation Xero Custom Connection:
|
|
25
8
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
9
|
+
| Setting | Purpose |
|
|
10
|
+
| --- | --- |
|
|
11
|
+
| `XERO_CLIENT_ID` | Custom Connection client ID |
|
|
12
|
+
| `XERO_CLIENT_SECRET` | Secret client credential |
|
|
13
|
+
| `XERO_SCOPES` | Defaults to `accounting.invoices accounting.contacts` |
|
|
30
14
|
|
|
31
|
-
|
|
15
|
+
Provide the live services from `@processfocus/plugin-xero/runtime` through the
|
|
16
|
+
organisation's custom job layer, with `XeroInvoiceConfigFromEnv` and an HTTP
|
|
17
|
+
client. Include `XeroConfiguration` in the organisation's configuration contract.
|
|
18
|
+
Use `makeXeroConfiguration` when binding credentials to different setting names.
|
|
32
19
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
bun cli/pfcli/src/main.ts config set XERO_CLIENT_SECRET="..." --project <project-id> --stage "<stage-name>" --secret
|
|
36
|
-
```
|
|
20
|
+
Store hosted secrets with `pfcli config set --secret` and redeploy after changes.
|
|
21
|
+
Xero Items must have the sales account and tax settings needed by your invoices.
|
|
37
22
|
|
|
38
|
-
|
|
39
|
-
runtimes must require this configuration; they never fall back to the stateful
|
|
40
|
-
test adapter.
|
|
23
|
+
## Choose an operation
|
|
41
24
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
25
|
+
| API | Use |
|
|
26
|
+
| --- | --- |
|
|
27
|
+
| `XeroInvoiceStep` | Issue an authorised sales invoice from a process |
|
|
28
|
+
| `XeroInvoiceCancellationStep` | Void an invoice using its stored Xero identity |
|
|
29
|
+
| `XeroInvoiceLookup` | Read contacts and invoice references for planning |
|
|
30
|
+
| `XeroInvoicePreparation` | Read contacts and current sales-item details |
|
|
31
|
+
| `XeroDraftInvoice.createIfAbsent` | Create a draft only when its contact/reference is absent |
|
|
32
|
+
| `XeroRepeatedDraftInvoice.create` | Intentionally allow another draft with the same reference |
|
|
33
|
+
|
|
34
|
+
Read-only preparation does not issue an invoice. Item preparation also requires
|
|
35
|
+
the `accounting.settings` scope. Draft creation does not authorise or email invoices.
|
|
36
|
+
|
|
37
|
+
## Process authoring
|
|
54
38
|
|
|
55
|
-
|
|
39
|
+
Import the step and input decoder from the package root:
|
|
56
40
|
|
|
57
|
-
```
|
|
58
|
-
import { Effect } from "effect"
|
|
41
|
+
```ts
|
|
59
42
|
import {
|
|
60
|
-
decodeIssueAuthorisedSalesInvoiceInput,
|
|
61
43
|
XeroInvoiceStep,
|
|
44
|
+
decodeIssueAuthorisedSalesInvoiceInput,
|
|
62
45
|
} from "@processfocus/plugin-xero"
|
|
63
46
|
|
|
64
|
-
const createInvoice = new XeroInvoiceStep(flow, "Create
|
|
65
|
-
input: (state) =>
|
|
66
|
-
decodeIssueAuthorisedSalesInvoiceInput({
|
|
67
|
-
customer: {
|
|
68
|
-
name: state.customerName,
|
|
69
|
-
email: state.customerEmail,
|
|
70
|
-
},
|
|
71
|
-
shippingAddress: {
|
|
72
|
-
line1: state.addressLine1,
|
|
73
|
-
city: state.city,
|
|
74
|
-
},
|
|
75
|
-
productSku: state.productSku,
|
|
76
|
-
agreedPrice: state.agreedPrice,
|
|
77
|
-
currency: "NZD",
|
|
78
|
-
reference: state.orderNumber,
|
|
79
|
-
date: state.orderDate,
|
|
80
|
-
dueDate: state.orderDate,
|
|
81
|
-
}),
|
|
47
|
+
const createInvoice = new XeroInvoiceStep(flow, "Create invoice", {
|
|
48
|
+
input: (state) => decodeIssueAuthorisedSalesInvoiceInput(state.invoice),
|
|
82
49
|
})
|
|
83
50
|
```
|
|
84
51
|
|
|
85
|
-
The
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
and tax defaults, so a zero-tax Item and a GST Item keep the same Agreed Price
|
|
90
|
-
as the invoice total.
|
|
91
|
-
|
|
92
|
-
Contact matching uses trimmed, lowercase email comparison. Exactly one match
|
|
93
|
-
is reused and updated with the current customer name, email, and physical
|
|
94
|
-
STREET address from the Shipping Address. Postal and billing addresses are
|
|
95
|
-
left unchanged. Zero matches or multiple matches create a new contact. One
|
|
96
|
-
normalized email can correspond to multiple Xero contacts; the plugin never
|
|
97
|
-
picks an arbitrary match. If Xero rejects the contact name as not unique, the
|
|
98
|
-
plugin retries with the customer name plus the Order Number. A later attempt
|
|
99
|
-
looks up that deterministic name first so the created contact can be
|
|
100
|
-
reconciled.
|
|
101
|
-
|
|
102
|
-
Contact and invoice writes send distinct `Idempotency-Key` headers derived
|
|
103
|
-
from the system-step Todo identity. Identical retries inside Xero's six-minute
|
|
104
|
-
window replay the original result. After that window expires, contact creation
|
|
105
|
-
is recovered from the existing provider contact, and invoice creation queries
|
|
106
|
-
the exact Order Number Reference before another create. One materially
|
|
107
|
-
compatible invoice is treated as success. Multiple invoices with that
|
|
108
|
-
Reference, or one whose contact, Product SKU, amount, currency, dates, type,
|
|
109
|
-
or status conflict, fail without changing an authorised invoice.
|
|
110
|
-
|
|
111
|
-
## Draft-Only Creation
|
|
112
|
-
|
|
113
|
-
`XeroDraftInvoice.createIfAbsent(DraftSalesInvoice)` creates a multi-line ACCREC
|
|
114
|
-
**DRAFT** for an already resolved contact. Provide `XeroDraftInvoiceLive` from
|
|
115
|
-
`/runtime` with `XeroInvoiceConfig`. The schema requires internally consistent
|
|
116
|
-
integer-cent inclusive amounts and rejects any status other than DRAFT.
|
|
117
|
-
|
|
118
|
-
Every attempt rereads all invoices for that contact. A matching reference, either
|
|
119
|
-
bare or followed by a whitespace-separated suffix, is skipped regardless of
|
|
120
|
-
status or amount. Failed, malformed or incomplete lookups never imply absence.
|
|
121
|
-
The POST omits InvoiceID and InvoiceNumber, leaves contacts untouched, and never
|
|
122
|
-
authorises or emails the invoice. The response must match the requested contact,
|
|
123
|
-
reference, dates, signed lines, totals and GST before returning `created-draft`.
|
|
124
|
-
Before a new write, the adapter rereads the contact and requires its primary
|
|
125
|
-
email plus `IncludeInEmails` contact-person addresses to exactly match
|
|
126
|
-
`expectedRecipientEmails`. Legacy saved previews without that intent can still
|
|
127
|
-
be decoded, but a new write is refused until they are prepared again.
|
|
128
|
-
|
|
129
|
-
The stable idempotency key is derived from tenant, contact and normalized
|
|
130
|
-
reference, so competing executions use the same provider key. An optional
|
|
131
|
-
`duplicateReference` supplies the stable scope for both the lookup and key when
|
|
132
|
-
`reference` includes a display suffix (for example `T4 2026 Lexine Y10` with
|
|
133
|
-
scope `T4 2026`). It must match the display reference using the same
|
|
134
|
-
whitespace-separated suffix rule; it is not sent to Xero. After Xero's
|
|
135
|
-
six-minute key retention expires, the fresh lookup still prevents re-creation.
|
|
136
|
-
The adapter serializes its own lookup/write pairs, paces accounting requests and
|
|
137
|
-
retains the shared Retry-After/error handling. Manual external writers cannot be
|
|
138
|
-
locked by this API; the lookup is performed immediately before each write.
|
|
139
|
-
|
|
140
|
-
## Testing
|
|
141
|
-
|
|
142
|
-
### Read-Only Invoice Planning
|
|
143
|
-
|
|
144
|
-
Processes that need to plan invoices without writing can use `XeroInvoiceLookup`
|
|
145
|
-
from the main entry point, backed by `XeroInvoiceLookupLive` from `/runtime` and
|
|
146
|
-
the same `XeroInvoiceConfigFromEnv` configuration. `readDirectory(reference, previousReference?)`
|
|
147
|
-
returns all contacts (including archived contacts, normalized main/contact-person
|
|
148
|
-
emails) and ACCREC invoice identities whose references contain either supplied
|
|
149
|
-
reference. `invoiceRecipientEmails` contains only enabled invoice recipients,
|
|
150
|
-
separately from all emails usable for identity matching.
|
|
151
|
-
It performs paginated reads only; it never creates or updates a contact or invoice.
|
|
152
|
-
The caller owns exact reference/contact matching and the decision to skip.
|
|
153
|
-
Pagination rejects repeated identities, partial responses and malformed required
|
|
154
|
-
fields, and fails if the 1,000-page bound is reached. Transport, authentication,
|
|
155
|
-
token caching and rate-limit handling are shared with the existing live adapter.
|
|
156
|
-
|
|
157
|
-
This interface does not change the order-invoice step's write or reconciliation
|
|
158
|
-
semantics. In particular, a read-only preview is not issuance and does not provide
|
|
159
|
-
concurrent-write idempotency.
|
|
160
|
-
|
|
161
|
-
### Test Adapters
|
|
162
|
-
|
|
163
|
-
```bash
|
|
164
|
-
bun scripts/nx-quiet.ts run @processfocus/plugin-xero:test
|
|
165
|
-
```
|
|
52
|
+
The authorised invoice input describes the customer, shipping address, item
|
|
53
|
+
lines, dates and reference. Prices are tax-inclusive NZD amounts. Use a stable,
|
|
54
|
+
unique business reference so retries can find an invoice already issued.
|
|
55
|
+
Keep the returned invoice identity for later cancellation or reconciliation.
|
|
166
56
|
|
|
167
|
-
|
|
168
|
-
|
|
57
|
+
Cancellation voids the existing invoice; it does not refund payments or issue a
|
|
58
|
+
replacement. Xero determines whether the invoice can be voided.
|
|
169
59
|
|
|
170
|
-
|
|
171
|
-
adapter. Default tests skip it unless `XERO_DEMO_CONFORMANCE=1`, so
|
|
172
|
-
routine CI never contacts Xero:
|
|
60
|
+
## Recovery and development
|
|
173
61
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
62
|
+
After an uncertain write, check Xero before starting another operation.
|
|
63
|
+
Repeated-reference draft creation deliberately permits duplicates across
|
|
64
|
+
invocations.
|
|
65
|
+
|
|
66
|
+
For a rejected invoice after correcting item settings, follow the
|
|
67
|
+
[recovery policy](docs/cached-validation-retries.md#recovery-policy).
|
|
177
68
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
adapter. See `docs/demo-company-conformance.md` for pre-provisioned
|
|
182
|
-
`PF-ZERO` and `PF-GST15` Items, reset/reconnection, and the pinned
|
|
183
|
-
Accounting OpenAPI revision. The canary does not email invoices and must
|
|
184
|
-
not be pointed at a production organisation.
|
|
185
|
-
|
|
186
|
-
The controller seeds contacts, catalog Items, invoices, and rate budgets, and
|
|
187
|
-
inspects created resources without network access. Seeding may create
|
|
188
|
-
duplicate normalized emails, duplicate names, physical STREET addresses, and
|
|
189
|
-
billing/postal addresses. It also models Xero idempotency retention and
|
|
190
|
-
expiry, changed-body key conflicts, timeout-after-commit, seeded References,
|
|
191
|
-
deterministic time, and scripted transport, timeout, validation, auth,
|
|
192
|
-
permission, rate-limit, malformed-response, and provider-server failures.
|
|
193
|
-
Test support is not re-exported from the package root or runtime entry point.
|
|
194
|
-
|
|
195
|
-
Network failures, timeouts, HTTP 429, and Xero 500/503 responses are
|
|
196
|
-
retryable. Rate-limit responses wait for Retry-After before the retryable
|
|
197
|
-
failure is returned. Validation, unknown ItemCode, ambiguous Reference,
|
|
198
|
-
conflicting intent, and unsupported operations fail immediately without
|
|
199
|
-
retry. Missing or malformed Custom Connection configuration, malformed
|
|
200
|
-
success payloads, and persistent 401/403 failures are deployment or
|
|
201
|
-
provider-contract faults and also fail immediately.
|
|
202
|
-
|
|
203
|
-
The live adapter caches Custom Connection access tokens until shortly before
|
|
204
|
-
expiry and coalesces concurrent refreshes. Logs may include Order Number,
|
|
205
|
-
Xero resource IDs, customer name, and customer email. Logs and errors omit
|
|
206
|
-
Shipping Address, raw request and response bodies, access tokens, client
|
|
207
|
-
secrets, and Authorization headers.
|
|
69
|
+
`@processfocus/plugin-xero/test-support` supplies a stateful adapter for local
|
|
70
|
+
development. [Live conformance checks](docs/demo-company-conformance.md) are
|
|
71
|
+
available separately.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
|
-
export { CalendarDate, CurrencyCode, CustomerDetails, CustomerName, EmailAddress,
|
|
1
|
+
export { CalendarDate, CurrencyCode, CustomerDetails, CustomerName, EmailAddress, InvoiceReference, IssueAuthorisedSalesInvoiceInput, IssuedSalesInvoice, IssuedSalesInvoiceLine, ProductSku, SalesInvoiceLine, ShippingAddress, XeroContactId, XeroInvoiceId, XeroInvoiceNumber, } from "./lib/identities.js";
|
|
2
2
|
export { DraftInvoiceResult, DraftSalesInvoice, XeroDraftInvoice, matchesInvoiceReference, } from "./lib/xero-draft-invoice.js";
|
|
3
3
|
export { XeroInvoice, XeroInvoiceError, decodeIssueAuthorisedSalesInvoiceInput, } from "./lib/xero-invoice.js";
|
|
4
|
+
export { CancelSalesInvoiceInput, CancelledSalesInvoice, XeroInvoiceCancellation, decodeCancelSalesInvoiceInput, } from "./lib/xero-invoice-cancellation.js";
|
|
5
|
+
export { XeroInvoiceCancellationStep, type XeroInvoiceCancellationStepProps, } from "./lib/xero-invoice-cancellation-step.js";
|
|
4
6
|
export { InvoiceDirectory, XeroInvoiceLookup, } from "./lib/xero-invoice-lookup.js";
|
|
7
|
+
export { SalesInvoiceProposal, SalesItemResult, XeroInvoicePreparation, } from "./lib/xero-invoice-preparation.js";
|
|
5
8
|
export { XeroInvoiceStep, type XeroInvoiceStepProps, xeroInvoiceOutput, } from "./lib/xero-invoice-step.js";
|
|
9
|
+
export { CreatedRepeatedDraft, RepeatedDraftSalesInvoice, XeroRepeatedDraftInvoice, } from "./lib/xero-repeated-draft-invoice.js";
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
export { CalendarDate, CurrencyCode, CustomerDetails, CustomerName, EmailAddress,
|
|
1
|
+
export { CalendarDate, CurrencyCode, CustomerDetails, CustomerName, EmailAddress, InvoiceReference, IssueAuthorisedSalesInvoiceInput, IssuedSalesInvoice, IssuedSalesInvoiceLine, ProductSku, SalesInvoiceLine, ShippingAddress, XeroContactId, XeroInvoiceId, XeroInvoiceNumber, } from "./lib/identities.js";
|
|
2
2
|
export { DraftInvoiceResult, DraftSalesInvoice, XeroDraftInvoice, matchesInvoiceReference, } from "./lib/xero-draft-invoice.js";
|
|
3
3
|
export { XeroInvoice, XeroInvoiceError, decodeIssueAuthorisedSalesInvoiceInput, } from "./lib/xero-invoice.js";
|
|
4
|
+
export { CancelSalesInvoiceInput, CancelledSalesInvoice, XeroInvoiceCancellation, decodeCancelSalesInvoiceInput, } from "./lib/xero-invoice-cancellation.js";
|
|
5
|
+
export { XeroInvoiceCancellationStep, } from "./lib/xero-invoice-cancellation-step.js";
|
|
4
6
|
export { InvoiceDirectory, XeroInvoiceLookup, } from "./lib/xero-invoice-lookup.js";
|
|
7
|
+
export { SalesInvoiceProposal, SalesItemResult, XeroInvoicePreparation, } from "./lib/xero-invoice-preparation.js";
|
|
5
8
|
export { XeroInvoiceStep, xeroInvoiceOutput, } from "./lib/xero-invoice-step.js";
|
|
9
|
+
export { CreatedRepeatedDraft, RepeatedDraftSalesInvoice, XeroRepeatedDraftInvoice, } from "./lib/xero-repeated-draft-invoice.js";
|
|
6
10
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,YAAY,EACZ,YAAY,EACZ,eAAe,EACf,YAAY,EACZ,YAAY,EACZ,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,YAAY,EACZ,YAAY,EACZ,eAAe,EACf,YAAY,EACZ,YAAY,EACZ,gBAAgB,EAChB,gCAAgC,EAChC,kBAAkB,EAClB,sBAAsB,EACtB,UAAU,EACV,gBAAgB,EAChB,eAAe,EACf,aAAa,EACb,aAAa,EACb,iBAAiB,GAClB,MAAM,kBAAkB,CAAA;AACzB,OAAO,EACL,kBAAkB,EAClB,iBAAiB,EACjB,gBAAgB,EAChB,uBAAuB,GACxB,MAAM,0BAA0B,CAAA;AACjC,OAAO,EACL,WAAW,EACX,gBAAgB,EAChB,sCAAsC,GACvC,MAAM,oBAAoB,CAAA;AAC3B,OAAO,EACL,uBAAuB,EACvB,qBAAqB,EACrB,uBAAuB,EACvB,6BAA6B,GAC9B,MAAM,iCAAiC,CAAA;AACxC,OAAO,EACL,2BAA2B,GAE5B,MAAM,sCAAsC,CAAA;AAC7C,OAAO,EACL,gBAAgB,EAChB,iBAAiB,GAClB,MAAM,2BAA2B,CAAA;AAClC,OAAO,EACL,oBAAoB,EACpB,eAAe,EACf,sBAAsB,GACvB,MAAM,gCAAgC,CAAA;AACvC,OAAO,EACL,eAAe,EAEf,iBAAiB,GAClB,MAAM,yBAAyB,CAAA;AAChC,OAAO,EACL,oBAAoB,EACpB,yBAAyB,EACzB,wBAAwB,GACzB,MAAM,mCAAmC,CAAA"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
2
|
import type { XeroInvoiceError } from "./errors.js";
|
|
3
|
-
import { type
|
|
3
|
+
import { type IssueAuthorisedSalesInvoiceInput } from "./identities.js";
|
|
4
4
|
/**
|
|
5
5
|
* Xero contact identity after provider responses have been validated.
|
|
6
6
|
*/
|
|
@@ -19,12 +19,8 @@ export interface XeroContactOperations {
|
|
|
19
19
|
readonly findByName: (name: string) => Effect.Effect<readonly ResolvedXeroContact[], XeroInvoiceError>;
|
|
20
20
|
readonly findByEmail: (email: string) => Effect.Effect<readonly ResolvedXeroContact[], XeroInvoiceError>;
|
|
21
21
|
readonly create: (input: IssueAuthorisedSalesInvoiceInput, name: string, idempotencyKey: string) => Effect.Effect<ResolvedXeroContact, XeroInvoiceError>;
|
|
22
|
-
readonly update: (contact: ResolvedXeroContact, input: IssueAuthorisedSalesInvoiceInput, idempotencyKey: string) => Effect.Effect<ResolvedXeroContact, XeroInvoiceError>;
|
|
22
|
+
readonly update: (contact: ResolvedXeroContact, input: IssueAuthorisedSalesInvoiceInput, idempotencyKey: string, name: string) => Effect.Effect<ResolvedXeroContact, XeroInvoiceError>;
|
|
23
23
|
}
|
|
24
|
-
export declare const conflictContactName: ({ customerName, orderNumber, }: {
|
|
25
|
-
readonly customerName: CustomerName;
|
|
26
|
-
readonly orderNumber: InvoiceReference;
|
|
27
|
-
}) => string;
|
|
28
24
|
export declare const isContactNameConflict: (message: string) => boolean;
|
|
29
25
|
export declare const XERO_CONTACT_NAME_MUST_BE_UNIQUE = "The contact name must be unique.";
|
|
30
26
|
/**
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
2
|
import { xeroIdempotencyKey } from "./idempotency.js";
|
|
3
3
|
import { isUniqueEmailMatch, normalizeEmail, } from "./identities.js";
|
|
4
|
-
|
|
4
|
+
const conflictContactName = ({ customerName, orderNumber, }) => `${customerName} ${orderNumber}`;
|
|
5
5
|
export const isContactNameConflict = (message) => {
|
|
6
6
|
const lower = message.toLowerCase();
|
|
7
7
|
const mentionsName = lower.includes("contact name") || lower.includes("contactname");
|
|
@@ -16,7 +16,7 @@ const recoverOriginalNameOrCreateSuffix = (contacts, input, todoId, conflictName
|
|
|
16
16
|
const originalNamed = yield* contacts.findByName(input.customer.name);
|
|
17
17
|
const recoveredOriginal = onlyItem(originalNamed.filter((contact) => hasMatchingEmail(contact, input.customer.email)));
|
|
18
18
|
if (recoveredOriginal !== undefined) {
|
|
19
|
-
return yield* contacts.update(recoveredOriginal, input, xeroIdempotencyKey(todoId, "contact.update"));
|
|
19
|
+
return yield* contacts.update(recoveredOriginal, input, xeroIdempotencyKey(todoId, "contact.update"), input.customer.name);
|
|
20
20
|
}
|
|
21
21
|
return yield* contacts.create(input, conflictName, xeroIdempotencyKey(todoId, "contact.create.conflict"));
|
|
22
22
|
});
|
|
@@ -37,14 +37,26 @@ export const resolveInvoiceContact = (contacts, input, todoId) => Effect.gen(fun
|
|
|
37
37
|
const named = yield* contacts.findByName(conflictName);
|
|
38
38
|
const priorSuffixed = onlyItem(named);
|
|
39
39
|
if (priorSuffixed !== undefined) {
|
|
40
|
-
return priorSuffixed.contactId;
|
|
40
|
+
return (yield* contacts.update(priorSuffixed, input, xeroIdempotencyKey(todoId, "contact.update"), conflictName)).contactId;
|
|
41
|
+
}
|
|
42
|
+
if (input.legacyReference && input.legacyReference !== input.reference) {
|
|
43
|
+
const legacyNamed = yield* contacts.findByName(conflictContactName({
|
|
44
|
+
customerName: input.customer.name,
|
|
45
|
+
orderNumber: input.legacyReference,
|
|
46
|
+
}));
|
|
47
|
+
const legacySuffixed = onlyItem(legacyNamed);
|
|
48
|
+
if (legacySuffixed !== undefined)
|
|
49
|
+
return (yield* contacts.update(legacySuffixed, input, xeroIdempotencyKey(todoId, "contact.update"), conflictContactName({
|
|
50
|
+
customerName: input.customer.name,
|
|
51
|
+
orderNumber: input.legacyReference,
|
|
52
|
+
}))).contactId;
|
|
41
53
|
}
|
|
42
54
|
const emailMatches = yield* contacts.findByEmail(input.customer.email);
|
|
43
55
|
const uniqueMatch = isUniqueEmailMatch(emailMatches.length)
|
|
44
56
|
? emailMatches[0]
|
|
45
57
|
: undefined;
|
|
46
58
|
if (uniqueMatch !== undefined) {
|
|
47
|
-
const updated = yield* contacts.update(uniqueMatch, input, xeroIdempotencyKey(todoId, "contact.update"));
|
|
59
|
+
const updated = yield* contacts.update(uniqueMatch, input, xeroIdempotencyKey(todoId, "contact.update"), input.customer.name);
|
|
48
60
|
return updated.contactId;
|
|
49
61
|
}
|
|
50
62
|
if (emailMatches.length > 1) {
|
|
@@ -56,7 +68,7 @@ export const resolveInvoiceContact = (contacts, input, todoId) => Effect.gen(fun
|
|
|
56
68
|
}
|
|
57
69
|
const created = yield* contacts
|
|
58
70
|
.create(input, input.customer.name, xeroIdempotencyKey(todoId, "contact.create"))
|
|
59
|
-
.pipe(Effect.
|
|
71
|
+
.pipe(Effect.catch((error) => isContactNameConflict(error.message)
|
|
60
72
|
? recoverOriginalNameOrCreateSuffix(contacts, input, todoId, conflictName)
|
|
61
73
|
: Effect.fail(error)));
|
|
62
74
|
return created.contactId;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"contact-resolution.js","sourceRoot":"","sources":["../../src/lib/contact-resolution.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ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MAAM,OAAO,GAAG,KAAK,CAAC,CAAC,QAAQ,CAAC,MAAM,CACpC,WAAW,EACX,KAAK,EACL,kBAAkB,CAAC,MAAM,EAAE,gBAAgB,CAAC,EAC5C,KAAK,CAAC,QAAQ,CAAC,IAAI,CACpB,CAAA;QACD,OAAO,OAAO,CAAC,SAAS,CAAA;IAC1B,CAAC;IAED,IAAI,YAAY,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC5B,KAAK,CAAC,CAAC,MAAM,CAAC,GAAG,CACf,2FAA2F,CAC5F,CAAC,IAAI,CACJ,MAAM,CAAC,YAAY,CAAC;YAClB,SAAS,EAAE,KAAK,CAAC,SAAS;YAC1B,YAAY,EAAE,KAAK,CAAC,QAAQ,CAAC,IAAI;YACjC,aAAa,EAAE,KAAK,CAAC,QAAQ,CAAC,KAAK;SACpC,CAAC,CACH,CAAA;IACH,CAAC;IAED,MAAM,OAAO,GAAG,KAAK,CAAC,CAAC,QAAQ;SAC5B,MAAM,CACL,KAAK,EACL,KAAK,CAAC,QAAQ,CAAC,IAAI,EACnB,kBAAkB,CAAC,MAAM,EAAE,gBAAgB,CAAC,CAC7C;SACA,IAAI,CACH,MAAM,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE,CACrB,qBAAqB,CAAC,KAAK,CAAC,OAAO,CAAC;QAClC,CAAC,CAAC,iCAAiC,CAC/B,QAAQ,EACR,KAAK,EACL,MAAM,EACN,YAAY,CACb;QACH,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CACvB,CACF,CAAA;IACH,OAAO,OAAO,CAAC,SAAS,CAAA;AAC1B,CAAC,CAAC,CAAA"}
|
package/dist/lib/errors.d.ts
CHANGED
|
@@ -16,6 +16,10 @@ export declare class XeroInvoiceError extends XeroInvoiceError_base<{
|
|
|
16
16
|
readonly fields?: readonly string[];
|
|
17
17
|
readonly retryAfterSeconds?: number;
|
|
18
18
|
readonly cause?: unknown;
|
|
19
|
+
/** The invoice POST was explicitly rejected; a later manual retry may use a new key. */
|
|
20
|
+
readonly externalWriteRejected?: boolean;
|
|
21
|
+
/** Repeated-draft failures before POST are known not to have created an invoice. */
|
|
22
|
+
readonly draftCreationOutcome?: "not-created" | "unconfirmed";
|
|
19
23
|
}> {
|
|
20
24
|
}
|
|
21
25
|
export declare const xeroValidationError: (message: string, options?: {
|
package/dist/lib/errors.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/lib/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAA;AAE7B;;;;;;;GAOG;AACH,MAAM,OAAO,gBAAiB,SAAQ,IAAI,CAAC,WAAW,CAAC,kBAAkB,
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/lib/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAA;AAE7B;;;;;;;GAOG;AACH,MAAM,OAAO,gBAAiB,SAAQ,IAAI,CAAC,WAAW,CAAC,kBAAkB,CAWvE;CAAG;AAEL,MAAM,CAAC,MAAM,mBAAmB,GAAG,CACjC,OAAe,EACf,OAGC,EACiB,EAAE,CACpB,IAAI,gBAAgB,CAAC;IACnB,OAAO;IACP,SAAS,EAAE,KAAK;IAChB,IAAI,EAAE,OAAO,EAAE,IAAI,IAAI,YAAY;IACnC,GAAG,CAAC,OAAO,EAAE,MAAM,KAAK,SAAS,IAAI,OAAO,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC;QAC5D,CAAC,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,EAAE;QAC5B,CAAC,CAAC,EAAE,CAAC;CACR,CAAC,CAAA;AAEJ,MAAM,CAAC,MAAM,wBAAwB,GAAG,CACtC,UAAkB,EACA,EAAE,CACpB,IAAI,gBAAgB,CAAC;IACnB,OAAO,EAAE,wBAAwB,UAAU,GAAG;IAC9C,SAAS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M,0BAA0B,GAAG,CAAC,OAAe,EAAoB,EAAE,CAC9E,IAAI,gBAAgB,CAAC;IACnB,OAAO;IACP,SAAS,EAAE,KAAK;IAChB,IAAI,EAAE,oBAAoB;CAC3B,CAAC,CAAA;AAEJ,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,OAAe,EAAoB,EAAE,CACvE,IAAI,gBAAgB,CAAC;IACnB,OAAO;IACP,SAAS,EAAE,KAAK;IAChB,IAAI,EAAE,YAAY;CACnB,CAAC,CAAA;AAEJ,MAAM,CAAC,MAAM,6BAA6B,GAAG,CAC3C,OAAe,EACG,EAAE,CACpB,IAAI,gBAAgB,CAAC;IACnB,OAAO;IACP,SAAS,EAAE,KAAK;IAChB,IAAI,EAAE,uBAAuB;CAC9B,CAAC,CAAA;AAEJ,MAAM,CAAC,MAAM,4BAA4B,GAAG,GAAqB,EAAE,CACjE,IAAI,gBAAgB,CAAC;IACnB,OAAO,EACL,0EAA0E;IAC5E,SAAS,EAAE,KAAK;IAChB,IAAI,EAAE,sBAAsB;CAC7B,CAAC,CAAA;AAEJ,MAAM,CAAC,MAAM,2BAA2B,GAAG,CACzC,SAAiB,EACC,EAAE,CACpB,IAAI,gBAAgB,CAAC;IACnB,OAAO,EAAE,wDAAwD,SAAS,GAAG;IAC7E,SAAS,EAAE,KAAK;IAChB,IAAI,EAAE,qBAAqB;CAC5B,CAAC,CAAA;AAEJ,MAAM,CAAC,MAAM,8BAA8B,GAAG,CAC5C,SAAiB,EACC,EAAE,CACpB,IAAI,gBAAgB,CAAC;IACnB,OAAO,EAAE,qDAAqD,SAAS,4CAA4C;IACnH,SAAS,EAAE,KAAK;IAChB,IAAI,EAAE,iBAAiB;CACxB,CAAC,CAAA"}
|
package/dist/lib/identities.d.ts
CHANGED
|
@@ -1,62 +1,86 @@
|
|
|
1
1
|
import { Schema } from "effect";
|
|
2
|
-
export declare const CustomerName: Schema.brand<
|
|
2
|
+
export declare const CustomerName: Schema.brand<Schema.NonEmptyString, "CustomerName">;
|
|
3
3
|
export type CustomerName = typeof CustomerName.Type;
|
|
4
|
-
export declare const EmailAddress: Schema.brand<Schema.
|
|
4
|
+
export declare const EmailAddress: Schema.brand<Schema.String, "EmailAddress">;
|
|
5
5
|
export type EmailAddress = typeof EmailAddress.Type;
|
|
6
|
-
export declare const ProductSku: Schema.brand<
|
|
6
|
+
export declare const ProductSku: Schema.brand<Schema.NonEmptyString, "ProductSku">;
|
|
7
7
|
export type ProductSku = typeof ProductSku.Type;
|
|
8
|
-
export declare const InvoiceReference: Schema.brand<
|
|
8
|
+
export declare const InvoiceReference: Schema.brand<Schema.NonEmptyString, "InvoiceReference">;
|
|
9
9
|
export type InvoiceReference = typeof InvoiceReference.Type;
|
|
10
|
-
export declare const
|
|
11
|
-
export type ExactMoney = typeof ExactMoney.Type;
|
|
12
|
-
export declare const CurrencyCode: Schema.Literal<["NZD"]>;
|
|
10
|
+
export declare const CurrencyCode: Schema.Literal<"NZD">;
|
|
13
11
|
export type CurrencyCode = typeof CurrencyCode.Type;
|
|
14
|
-
export declare const CalendarDate: Schema.brand<Schema.
|
|
12
|
+
export declare const CalendarDate: Schema.brand<Schema.String, "CalendarDate">;
|
|
15
13
|
export type CalendarDate = typeof CalendarDate.Type;
|
|
16
|
-
export declare const XeroContactId: Schema.brand<Schema.
|
|
14
|
+
export declare const XeroContactId: Schema.brand<Schema.String, "XeroContactId">;
|
|
17
15
|
export type XeroContactId = typeof XeroContactId.Type;
|
|
18
|
-
export declare const XeroInvoiceId: Schema.brand<Schema.
|
|
16
|
+
export declare const XeroInvoiceId: Schema.brand<Schema.String, "XeroInvoiceId">;
|
|
19
17
|
export type XeroInvoiceId = typeof XeroInvoiceId.Type;
|
|
20
|
-
export declare const XeroInvoiceNumber: Schema.brand<
|
|
18
|
+
export declare const XeroInvoiceNumber: Schema.brand<Schema.NonEmptyString, "XeroInvoiceNumber">;
|
|
21
19
|
export type XeroInvoiceNumber = typeof XeroInvoiceNumber.Type;
|
|
22
20
|
export declare const CustomerDetails: Schema.Struct<{
|
|
23
|
-
name: Schema.brand<
|
|
24
|
-
email: Schema.brand<Schema.
|
|
21
|
+
readonly name: Schema.brand<Schema.NonEmptyString, "CustomerName">;
|
|
22
|
+
readonly email: Schema.brand<Schema.String, "EmailAddress">;
|
|
25
23
|
}>;
|
|
26
24
|
export type CustomerDetails = typeof CustomerDetails.Type;
|
|
27
25
|
export declare const ShippingAddress: Schema.Struct<{
|
|
28
|
-
line1:
|
|
29
|
-
line2: Schema.optional<
|
|
30
|
-
city:
|
|
31
|
-
region: Schema.optional<
|
|
32
|
-
postalCode: Schema.optional<
|
|
33
|
-
country: Schema.optional<
|
|
26
|
+
readonly line1: Schema.NonEmptyString;
|
|
27
|
+
readonly line2: Schema.optional<Schema.NonEmptyString>;
|
|
28
|
+
readonly city: Schema.NonEmptyString;
|
|
29
|
+
readonly region: Schema.optional<Schema.String>;
|
|
30
|
+
readonly postalCode: Schema.optional<Schema.String>;
|
|
31
|
+
readonly country: Schema.optional<Schema.String>;
|
|
34
32
|
}>;
|
|
35
33
|
export type ShippingAddress = typeof ShippingAddress.Type;
|
|
36
|
-
export declare const
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
34
|
+
export declare const SalesInvoiceLine: Schema.Struct<{
|
|
35
|
+
readonly productSku: Schema.brand<Schema.NonEmptyString, "ProductSku">;
|
|
36
|
+
readonly quantity: Schema.Int;
|
|
37
|
+
readonly unitAmount: Schema.String;
|
|
38
|
+
}>;
|
|
39
|
+
export type SalesInvoiceLine = typeof SalesInvoiceLine.Type;
|
|
40
|
+
export declare const IssueAuthorisedSalesInvoiceInput: Schema.Struct<{
|
|
41
|
+
readonly customer: Schema.Struct<{
|
|
42
|
+
readonly name: Schema.brand<Schema.NonEmptyString, "CustomerName">;
|
|
43
|
+
readonly email: Schema.brand<Schema.String, "EmailAddress">;
|
|
40
44
|
}>;
|
|
41
|
-
shippingAddress: Schema.Struct<{
|
|
42
|
-
line1:
|
|
43
|
-
line2: Schema.optional<
|
|
44
|
-
city:
|
|
45
|
-
region: Schema.optional<
|
|
46
|
-
postalCode: Schema.optional<
|
|
47
|
-
country: Schema.optional<
|
|
45
|
+
readonly shippingAddress: Schema.Struct<{
|
|
46
|
+
readonly line1: Schema.NonEmptyString;
|
|
47
|
+
readonly line2: Schema.optional<Schema.NonEmptyString>;
|
|
48
|
+
readonly city: Schema.NonEmptyString;
|
|
49
|
+
readonly region: Schema.optional<Schema.String>;
|
|
50
|
+
readonly postalCode: Schema.optional<Schema.String>;
|
|
51
|
+
readonly country: Schema.optional<Schema.String>;
|
|
48
52
|
}>;
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
53
|
+
readonly currency: Schema.Literal<"NZD">;
|
|
54
|
+
readonly reference: Schema.brand<Schema.NonEmptyString, "InvoiceReference">;
|
|
55
|
+
readonly legacyReference: Schema.optional<Schema.brand<Schema.NonEmptyString, "InvoiceReference">>;
|
|
56
|
+
readonly recoveryOnly: Schema.optional<Schema.Literal<true>>;
|
|
57
|
+
readonly internalNote: Schema.optional<Schema.NonEmptyString>;
|
|
58
|
+
readonly date: Schema.brand<Schema.String, "CalendarDate">;
|
|
59
|
+
readonly dueDate: Schema.brand<Schema.String, "CalendarDate">;
|
|
60
|
+
readonly lines: Schema.NonEmptyArray<Schema.Struct<{
|
|
61
|
+
readonly productSku: Schema.brand<Schema.NonEmptyString, "ProductSku">;
|
|
62
|
+
readonly quantity: Schema.Int;
|
|
63
|
+
readonly unitAmount: Schema.String;
|
|
64
|
+
}>>;
|
|
65
|
+
}>;
|
|
56
66
|
export type IssueAuthorisedSalesInvoiceInput = typeof IssueAuthorisedSalesInvoiceInput.Type;
|
|
67
|
+
export declare const IssuedSalesInvoiceLine: Schema.Struct<{
|
|
68
|
+
readonly itemCode: Schema.String;
|
|
69
|
+
readonly quantity: Schema.Int;
|
|
70
|
+
readonly unitAmount: Schema.String;
|
|
71
|
+
readonly description: Schema.String;
|
|
72
|
+
}>;
|
|
73
|
+
export type IssuedSalesInvoiceLine = typeof IssuedSalesInvoiceLine.Type;
|
|
57
74
|
export declare const IssuedSalesInvoice: Schema.Struct<{
|
|
58
|
-
invoiceId: Schema.brand<Schema.
|
|
59
|
-
invoiceNumber: Schema.brand<
|
|
75
|
+
readonly invoiceId: Schema.brand<Schema.String, "XeroInvoiceId">;
|
|
76
|
+
readonly invoiceNumber: Schema.brand<Schema.NonEmptyString, "XeroInvoiceNumber">;
|
|
77
|
+
readonly productDescription: Schema.String;
|
|
78
|
+
readonly lines: Schema.$Array<Schema.Struct<{
|
|
79
|
+
readonly itemCode: Schema.String;
|
|
80
|
+
readonly quantity: Schema.Int;
|
|
81
|
+
readonly unitAmount: Schema.String;
|
|
82
|
+
readonly description: Schema.String;
|
|
83
|
+
}>>;
|
|
60
84
|
}>;
|
|
61
85
|
export type IssuedSalesInvoice = typeof IssuedSalesInvoice.Type;
|
|
62
86
|
export declare const normalizeEmail: (email: string) => string;
|
package/dist/lib/identities.js
CHANGED
|
@@ -1,19 +1,17 @@
|
|
|
1
1
|
import { Schema } from "effect";
|
|
2
|
+
import { invoiceLinesTotalCents } from "./money.js";
|
|
2
3
|
const GUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
3
4
|
const EMAIL_PATTERN = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
4
5
|
const EXACT_MONEY_PATTERN = /^(?:0|[1-9]\d*)\.\d{2}$/;
|
|
5
6
|
const CALENDAR_DATE_PATTERN = /^\d{4}-\d{2}-\d{2}$/;
|
|
6
7
|
export const CustomerName = Schema.NonEmptyString.pipe(Schema.brand("CustomerName"));
|
|
7
|
-
export const EmailAddress = Schema.String.pipe(Schema.
|
|
8
|
+
export const EmailAddress = Schema.String.pipe(Schema.check(Schema.isPattern(EMAIL_PATTERN)), Schema.brand("EmailAddress"));
|
|
8
9
|
export const ProductSku = Schema.NonEmptyString.pipe(Schema.brand("ProductSku"));
|
|
9
10
|
export const InvoiceReference = Schema.NonEmptyString.pipe(Schema.brand("InvoiceReference"));
|
|
10
|
-
export const ExactMoney = Schema.String.pipe(Schema.pattern(EXACT_MONEY_PATTERN), Schema.filter((value) => value !== "0.00", {
|
|
11
|
-
message: () => "Agreed Price must be greater than 0.00",
|
|
12
|
-
}), Schema.brand("ExactMoney"));
|
|
13
11
|
export const CurrencyCode = Schema.Literal("NZD");
|
|
14
|
-
export const CalendarDate = Schema.String.pipe(Schema.
|
|
15
|
-
export const XeroContactId = Schema.String.pipe(Schema.
|
|
16
|
-
export const XeroInvoiceId = Schema.String.pipe(Schema.
|
|
12
|
+
export const CalendarDate = Schema.String.pipe(Schema.check(Schema.isPattern(CALENDAR_DATE_PATTERN)), Schema.brand("CalendarDate"));
|
|
13
|
+
export const XeroContactId = Schema.String.pipe(Schema.check(Schema.isPattern(GUID_PATTERN)), Schema.brand("XeroContactId"));
|
|
14
|
+
export const XeroInvoiceId = Schema.String.pipe(Schema.check(Schema.isPattern(GUID_PATTERN)), Schema.brand("XeroInvoiceId"));
|
|
17
15
|
export const XeroInvoiceNumber = Schema.NonEmptyString.pipe(Schema.brand("XeroInvoiceNumber"));
|
|
18
16
|
export const CustomerDetails = Schema.Struct({
|
|
19
17
|
name: CustomerName,
|
|
@@ -27,21 +25,46 @@ export const ShippingAddress = Schema.Struct({
|
|
|
27
25
|
postalCode: Schema.optional(Schema.String),
|
|
28
26
|
country: Schema.optional(Schema.String),
|
|
29
27
|
});
|
|
30
|
-
|
|
28
|
+
const SalesInvoiceFields = {
|
|
31
29
|
customer: CustomerDetails,
|
|
32
30
|
shippingAddress: ShippingAddress,
|
|
33
|
-
productSku: ProductSku,
|
|
34
|
-
agreedPrice: ExactMoney,
|
|
35
31
|
currency: CurrencyCode,
|
|
36
32
|
reference: InvoiceReference,
|
|
33
|
+
// Lookup-only alias for invoices issued before a reference migration.
|
|
34
|
+
legacyReference: Schema.optional(InvoiceReference),
|
|
35
|
+
// Recover old executions whose customer-facing reference is unavailable.
|
|
36
|
+
recoveryOnly: Schema.optional(Schema.Literal(true)),
|
|
37
|
+
// Written only to Xero History & Notes, never to the invoice payload.
|
|
38
|
+
internalNote: Schema.optional(Schema.NonEmptyString.pipe(Schema.check(Schema.isMaxLength(2500)))),
|
|
37
39
|
date: CalendarDate,
|
|
38
40
|
dueDate: CalendarDate,
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
|
|
41
|
+
};
|
|
42
|
+
const UnitAmount = Schema.String.pipe(Schema.check(Schema.isPattern(EXACT_MONEY_PATTERN)), Schema.check(Schema.makeFilter((amount) => BigInt(amount.replace(".", "")) <= BigInt(Number.MAX_SAFE_INTEGER), { message: "Unit amount must be safe integer cents" })));
|
|
43
|
+
const Quantity = Schema.Int.pipe(Schema.check(Schema.isGreaterThan(0)));
|
|
44
|
+
export const SalesInvoiceLine = Schema.Struct({
|
|
45
|
+
productSku: ProductSku,
|
|
46
|
+
quantity: Quantity,
|
|
47
|
+
unitAmount: UnitAmount,
|
|
48
|
+
});
|
|
49
|
+
export const IssueAuthorisedSalesInvoiceInput = Schema.Struct({
|
|
50
|
+
...SalesInvoiceFields,
|
|
51
|
+
lines: Schema.NonEmptyArray(SalesInvoiceLine),
|
|
52
|
+
}).pipe(Schema.check(Schema.makeFilter((input) => invoiceLinesTotalCents(input.lines) <= BigInt(Number.MAX_SAFE_INTEGER), { message: "Invoice total must be safe integer cents" })), Schema.check(Schema.makeFilter((input) => input.date === input.dueDate, {
|
|
53
|
+
message: "DueDate must equal Date so the Sales Invoice is immediately due",
|
|
54
|
+
})));
|
|
55
|
+
export const IssuedSalesInvoiceLine = Schema.Struct({
|
|
56
|
+
itemCode: Schema.String,
|
|
57
|
+
quantity: Quantity,
|
|
58
|
+
unitAmount: UnitAmount,
|
|
59
|
+
description: Schema.String,
|
|
60
|
+
});
|
|
42
61
|
export const IssuedSalesInvoice = Schema.Struct({
|
|
43
62
|
invoiceId: XeroInvoiceId,
|
|
44
63
|
invoiceNumber: XeroInvoiceNumber,
|
|
64
|
+
// Description availability must not discard an already issued invoice.
|
|
65
|
+
// Processes that require it for customer instructions validate before sending.
|
|
66
|
+
productDescription: Schema.String,
|
|
67
|
+
lines: Schema.Array(IssuedSalesInvoiceLine),
|
|
45
68
|
});
|
|
46
69
|
export const normalizeEmail = (email) => email.trim().toLowerCase();
|
|
47
70
|
export const isUniqueEmailMatch = (matchCount) => matchCount === 1;
|