@processfocus/plugin-xero 0.1.0-next.1
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/LICENSE.md +28 -0
- package/README.md +207 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/contact-resolution.d.ts +39 -0
- package/dist/lib/contact-resolution.js +64 -0
- package/dist/lib/contact-resolution.js.map +1 -0
- package/dist/lib/errors.d.ts +38 -0
- package/dist/lib/errors.js +87 -0
- package/dist/lib/errors.js.map +1 -0
- package/dist/lib/idempotency.d.ts +15 -0
- package/dist/lib/idempotency.js +21 -0
- package/dist/lib/idempotency.js.map +1 -0
- package/dist/lib/identities.d.ts +63 -0
- package/dist/lib/identities.js +48 -0
- package/dist/lib/identities.js.map +1 -0
- package/dist/lib/invoice-reconciliation.d.ts +34 -0
- package/dist/lib/invoice-reconciliation.js +72 -0
- package/dist/lib/invoice-reconciliation.js.map +1 -0
- package/dist/lib/money.d.ts +6 -0
- package/dist/lib/money.js +23 -0
- package/dist/lib/money.js.map +1 -0
- package/dist/lib/xero-draft-invoice.d.ts +53 -0
- package/dist/lib/xero-draft-invoice.js +70 -0
- package/dist/lib/xero-draft-invoice.js.map +1 -0
- package/dist/lib/xero-http.d.ts +12 -0
- package/dist/lib/xero-http.js +55 -0
- package/dist/lib/xero-http.js.map +1 -0
- package/dist/lib/xero-invoice-live.d.ts +57 -0
- package/dist/lib/xero-invoice-live.js +636 -0
- package/dist/lib/xero-invoice-live.js.map +1 -0
- package/dist/lib/xero-invoice-lookup.d.ts +26 -0
- package/dist/lib/xero-invoice-lookup.js +24 -0
- package/dist/lib/xero-invoice-lookup.js.map +1 -0
- package/dist/lib/xero-invoice-step.d.ts +23 -0
- package/dist/lib/xero-invoice-step.js +26 -0
- package/dist/lib/xero-invoice-step.js.map +1 -0
- package/dist/lib/xero-invoice-test.d.ts +159 -0
- package/dist/lib/xero-invoice-test.js +497 -0
- package/dist/lib/xero-invoice-test.js.map +1 -0
- package/dist/lib/xero-invoice.d.ts +12 -0
- package/dist/lib/xero-invoice.js +8 -0
- package/dist/lib/xero-invoice.js.map +1 -0
- package/dist/lib/xero-token.d.ts +22 -0
- package/dist/lib/xero-token.js +68 -0
- package/dist/lib/xero-token.js.map +1 -0
- package/dist/lib/xero-wire.d.ts +92 -0
- package/dist/lib/xero-wire.js +65 -0
- package/dist/lib/xero-wire.js.map +1 -0
- package/dist/runtime.d.ts +2 -0
- package/dist/runtime.js +3 -0
- package/dist/runtime.js.map +1 -0
- package/dist/test-support.d.ts +1 -0
- package/dist/test-support.js +2 -0
- package/dist/test-support.js.map +1 -0
- package/dist/testing.d.ts +2 -0
- package/dist/testing.js +3 -0
- package/dist/testing.js.map +1 -0
- package/package.json +54 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# O'Saasy License Agreement
|
|
2
|
+
|
|
3
|
+
Copyright © 2026, Xplain Technology Ltd.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
1. The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
2. No licensee or downstream recipient may use the Software (including
|
|
16
|
+
any modified or derivative versions) to directly compete with the
|
|
17
|
+
original Licensor by offering it to third parties as a hosted,
|
|
18
|
+
managed, or Software-as-a-Service (SaaS) product or cloud service
|
|
19
|
+
where the primary value of the service is the functionality of the
|
|
20
|
+
Software itself.
|
|
21
|
+
|
|
22
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
23
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
24
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
25
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
26
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
27
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
28
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
# @processfocus/plugin-xero
|
|
2
|
+
|
|
3
|
+
Xero Sales Invoice plugin for Process Focus business processes. Process
|
|
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.1"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Configuration
|
|
22
|
+
|
|
23
|
+
The live Layer authenticates with a single-organisation Xero Custom Connection.
|
|
24
|
+
Set these environment variables:
|
|
25
|
+
|
|
26
|
+
- `XERO_CLIENT_ID` - Custom Connection client id
|
|
27
|
+
- `XERO_CLIENT_SECRET` - Custom Connection client secret
|
|
28
|
+
- `XERO_SCOPES` - optional space-separated scopes. Defaults to
|
|
29
|
+
`accounting.invoices accounting.contacts`
|
|
30
|
+
|
|
31
|
+
For cloud-hosted deployments, use `pfcli` to set the secret:
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
bun cli/pfcli/src/main.ts config set XERO_CLIENT_ID="..." --project <project-id> --stage "<stage-name>"
|
|
35
|
+
bun cli/pfcli/src/main.ts config set XERO_CLIENT_SECRET="..." --project <project-id> --stage "<stage-name>" --secret
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Provide the live Layer from the organisation's custom job layer. Deployed
|
|
39
|
+
runtimes must require this configuration; they never fall back to the stateful
|
|
40
|
+
test adapter.
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
import { Layer } from "effect"
|
|
44
|
+
import {
|
|
45
|
+
XeroInvoiceConfigFromEnv,
|
|
46
|
+
XeroInvoiceLive,
|
|
47
|
+
} from "@processfocus/plugin-xero/runtime"
|
|
48
|
+
|
|
49
|
+
export const CustomJobLayer = Layer.provide(
|
|
50
|
+
XeroInvoiceLive,
|
|
51
|
+
XeroInvoiceConfigFromEnv,
|
|
52
|
+
)
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Usage
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
import { Effect } from "effect"
|
|
59
|
+
import {
|
|
60
|
+
decodeIssueAuthorisedSalesInvoiceInput,
|
|
61
|
+
XeroInvoiceStep,
|
|
62
|
+
} from "@processfocus/plugin-xero"
|
|
63
|
+
|
|
64
|
+
const createInvoice = new XeroInvoiceStep(flow, "Create Xero Invoice", {
|
|
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
|
+
}),
|
|
82
|
+
})
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
The step returns `{ invoiceId, invoiceNumber }` from Xero.
|
|
86
|
+
|
|
87
|
+
The invoice is one ACCREC AUTHORISED line with quantity one. Product SKU is
|
|
88
|
+
sent as ItemCode. Amounts are tax-inclusive. The Xero Item supplies account
|
|
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
|
+
```
|
|
166
|
+
|
|
167
|
+
Tests import `makeXeroInvoiceTestPlugin()` from
|
|
168
|
+
`@processfocus/plugin-xero/test-support`. It returns `{ layer, controller }`.
|
|
169
|
+
|
|
170
|
+
An opt-in Demo Company canary reuses those scenarios against the live
|
|
171
|
+
adapter. Default tests skip it unless `XERO_DEMO_CONFORMANCE=1`, so
|
|
172
|
+
routine CI never contacts Xero:
|
|
173
|
+
|
|
174
|
+
```bash
|
|
175
|
+
bun scripts/nx-quiet.ts run @processfocus/plugin-xero:conformance
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
The canary requires `XERO_CLIENT_ID` and `XERO_CLIENT_SECRET` for a
|
|
179
|
+
dedicated NZ Demo Company Custom Connection, uses unique Order Number
|
|
180
|
+
References, and compares observable live results with the stateful test
|
|
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.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { CalendarDate, CurrencyCode, CustomerDetails, CustomerName, EmailAddress, ExactMoney, InvoiceReference, IssueAuthorisedSalesInvoiceInput, IssuedSalesInvoice, ProductSku, ShippingAddress, XeroContactId, XeroInvoiceId, XeroInvoiceNumber, } from "./lib/identities.js";
|
|
2
|
+
export { DraftInvoiceResult, DraftSalesInvoice, XeroDraftInvoice, matchesInvoiceReference, } from "./lib/xero-draft-invoice.js";
|
|
3
|
+
export { XeroInvoice, XeroInvoiceError, decodeIssueAuthorisedSalesInvoiceInput, } from "./lib/xero-invoice.js";
|
|
4
|
+
export { InvoiceDirectory, XeroInvoiceLookup, } from "./lib/xero-invoice-lookup.js";
|
|
5
|
+
export { XeroInvoiceStep, type XeroInvoiceStepProps, xeroInvoiceOutput, } from "./lib/xero-invoice-step.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { CalendarDate, CurrencyCode, CustomerDetails, CustomerName, EmailAddress, ExactMoney, InvoiceReference, IssueAuthorisedSalesInvoiceInput, IssuedSalesInvoice, ProductSku, ShippingAddress, XeroContactId, XeroInvoiceId, XeroInvoiceNumber, } from "./lib/identities.js";
|
|
2
|
+
export { DraftInvoiceResult, DraftSalesInvoice, XeroDraftInvoice, matchesInvoiceReference, } from "./lib/xero-draft-invoice.js";
|
|
3
|
+
export { XeroInvoice, XeroInvoiceError, decodeIssueAuthorisedSalesInvoiceInput, } from "./lib/xero-invoice.js";
|
|
4
|
+
export { InvoiceDirectory, XeroInvoiceLookup, } from "./lib/xero-invoice-lookup.js";
|
|
5
|
+
export { XeroInvoiceStep, xeroInvoiceOutput, } from "./lib/xero-invoice-step.js";
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +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,UAAU,EACV,gBAAgB,EAChB,gCAAgC,EAChC,kBAAkB,EAClB,UAAU,EACV,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,gBAAgB,EAChB,iBAAiB,GAClB,MAAM,2BAA2B,CAAA;AAClC,OAAO,EACL,eAAe,EAEf,iBAAiB,GAClB,MAAM,yBAAyB,CAAA"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import type { XeroInvoiceError } from "./errors.js";
|
|
3
|
+
import { type CustomerName, type InvoiceReference, type IssueAuthorisedSalesInvoiceInput } from "./identities.js";
|
|
4
|
+
/**
|
|
5
|
+
* Xero contact identity after provider responses have been validated.
|
|
6
|
+
*/
|
|
7
|
+
export interface ResolvedXeroContact {
|
|
8
|
+
readonly contactId: string;
|
|
9
|
+
readonly name?: string;
|
|
10
|
+
readonly email?: string;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Contact persistence used by Sales Invoice contact resolution.
|
|
14
|
+
*
|
|
15
|
+
* One normalized email may correspond to multiple Xero contacts. Callers
|
|
16
|
+
* must not pick an arbitrary email match.
|
|
17
|
+
*/
|
|
18
|
+
export interface XeroContactOperations {
|
|
19
|
+
readonly findByName: (name: string) => Effect.Effect<readonly ResolvedXeroContact[], XeroInvoiceError>;
|
|
20
|
+
readonly findByEmail: (email: string) => Effect.Effect<readonly ResolvedXeroContact[], XeroInvoiceError>;
|
|
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>;
|
|
23
|
+
}
|
|
24
|
+
export declare const conflictContactName: ({ customerName, orderNumber, }: {
|
|
25
|
+
readonly customerName: CustomerName;
|
|
26
|
+
readonly orderNumber: InvoiceReference;
|
|
27
|
+
}) => string;
|
|
28
|
+
export declare const isContactNameConflict: (message: string) => boolean;
|
|
29
|
+
export declare const XERO_CONTACT_NAME_MUST_BE_UNIQUE = "The contact name must be unique.";
|
|
30
|
+
/**
|
|
31
|
+
* Resolve the Xero contact for a Sales Invoice.
|
|
32
|
+
*
|
|
33
|
+
* Looks up a prior suffixed contact first, then applies email matching:
|
|
34
|
+
* exactly one normalized email match is updated, while zero or multiple
|
|
35
|
+
* matches create a contact. A unique-name conflict first recovers a prior
|
|
36
|
+
* create under the original name with the same email, then retries with the
|
|
37
|
+
* Order Number suffix.
|
|
38
|
+
*/
|
|
39
|
+
export declare const resolveInvoiceContact: (contacts: XeroContactOperations, input: IssueAuthorisedSalesInvoiceInput, todoId: string) => Effect.Effect<string, XeroInvoiceError>;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { xeroIdempotencyKey } from "./idempotency.js";
|
|
3
|
+
import { isUniqueEmailMatch, normalizeEmail, } from "./identities.js";
|
|
4
|
+
export const conflictContactName = ({ customerName, orderNumber, }) => `${customerName} ${orderNumber}`;
|
|
5
|
+
export const isContactNameConflict = (message) => {
|
|
6
|
+
const lower = message.toLowerCase();
|
|
7
|
+
const mentionsName = lower.includes("contact name") || lower.includes("contactname");
|
|
8
|
+
const mentionsUniqueness = lower.includes("unique") || lower.includes("already");
|
|
9
|
+
return mentionsName && mentionsUniqueness;
|
|
10
|
+
};
|
|
11
|
+
export const XERO_CONTACT_NAME_MUST_BE_UNIQUE = "The contact name must be unique.";
|
|
12
|
+
const onlyItem = (items) => items.length === 1 ? items[0] : undefined;
|
|
13
|
+
const hasMatchingEmail = (contact, email) => contact.email !== undefined &&
|
|
14
|
+
normalizeEmail(contact.email) === normalizeEmail(email);
|
|
15
|
+
const recoverOriginalNameOrCreateSuffix = (contacts, input, todoId, conflictName) => Effect.gen(function* () {
|
|
16
|
+
const originalNamed = yield* contacts.findByName(input.customer.name);
|
|
17
|
+
const recoveredOriginal = onlyItem(originalNamed.filter((contact) => hasMatchingEmail(contact, input.customer.email)));
|
|
18
|
+
if (recoveredOriginal !== undefined) {
|
|
19
|
+
return yield* contacts.update(recoveredOriginal, input, xeroIdempotencyKey(todoId, "contact.update"));
|
|
20
|
+
}
|
|
21
|
+
return yield* contacts.create(input, conflictName, xeroIdempotencyKey(todoId, "contact.create.conflict"));
|
|
22
|
+
});
|
|
23
|
+
/**
|
|
24
|
+
* Resolve the Xero contact for a Sales Invoice.
|
|
25
|
+
*
|
|
26
|
+
* Looks up a prior suffixed contact first, then applies email matching:
|
|
27
|
+
* exactly one normalized email match is updated, while zero or multiple
|
|
28
|
+
* matches create a contact. A unique-name conflict first recovers a prior
|
|
29
|
+
* create under the original name with the same email, then retries with the
|
|
30
|
+
* Order Number suffix.
|
|
31
|
+
*/
|
|
32
|
+
export const resolveInvoiceContact = (contacts, input, todoId) => Effect.gen(function* () {
|
|
33
|
+
const conflictName = conflictContactName({
|
|
34
|
+
customerName: input.customer.name,
|
|
35
|
+
orderNumber: input.reference,
|
|
36
|
+
});
|
|
37
|
+
const named = yield* contacts.findByName(conflictName);
|
|
38
|
+
const priorSuffixed = onlyItem(named);
|
|
39
|
+
if (priorSuffixed !== undefined) {
|
|
40
|
+
return priorSuffixed.contactId;
|
|
41
|
+
}
|
|
42
|
+
const emailMatches = yield* contacts.findByEmail(input.customer.email);
|
|
43
|
+
const uniqueMatch = isUniqueEmailMatch(emailMatches.length)
|
|
44
|
+
? emailMatches[0]
|
|
45
|
+
: undefined;
|
|
46
|
+
if (uniqueMatch !== undefined) {
|
|
47
|
+
const updated = yield* contacts.update(uniqueMatch, input, xeroIdempotencyKey(todoId, "contact.update"));
|
|
48
|
+
return updated.contactId;
|
|
49
|
+
}
|
|
50
|
+
if (emailMatches.length > 1) {
|
|
51
|
+
yield* Effect.log("Creating another Xero contact because one normalized email matched multiple Xero contacts").pipe(Effect.annotateLogs({
|
|
52
|
+
reference: input.reference,
|
|
53
|
+
customerName: input.customer.name,
|
|
54
|
+
customerEmail: input.customer.email,
|
|
55
|
+
}));
|
|
56
|
+
}
|
|
57
|
+
const created = yield* contacts
|
|
58
|
+
.create(input, input.customer.name, xeroIdempotencyKey(todoId, "contact.create"))
|
|
59
|
+
.pipe(Effect.catchAll((error) => isContactNameConflict(error.message)
|
|
60
|
+
? recoverOriginalNameOrCreateSuffix(contacts, input, todoId, conflictName)
|
|
61
|
+
: Effect.fail(error)));
|
|
62
|
+
return created.contactId;
|
|
63
|
+
});
|
|
64
|
+
//# sourceMappingURL=contact-resolution.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"contact-resolution.js","sourceRoot":"","sources":["../../src/lib/contact-resolution.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAE/B,OAAO,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAA;AAClD,OAAO,EAIL,kBAAkB,EAClB,cAAc,GACf,MAAM,cAAc,CAAA;AAoCrB,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,EAClC,YAAY,EACZ,WAAW,GAIZ,EAAU,EAAE,CAAC,GAAG,YAAY,IAAI,WAAW,EAAE,CAAA;AAE9C,MAAM,CAAC,MAAM,qBAAqB,GAAG,CAAC,OAAe,EAAW,EAAE;IAChE,MAAM,KAAK,GAAG,OAAO,CAAC,WAAW,EAAE,CAAA;IACnC,MAAM,YAAY,GAChB,KAAK,CAAC,QAAQ,CAAC,cAAc,CAAC,IAAI,KAAK,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAA;IACjE,MAAM,kBAAkB,GACtB,KAAK,CAAC,QAAQ,CAAC,QAAQ,CAAC,IAAI,KAAK,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAA;IACvD,OAAO,YAAY,IAAI,kBAAkB,CAAA;AAC3C,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,gCAAgC,GAC3C,kCAAkC,CAAA;AAEpC,MAAM,QAAQ,GAAG,CAAI,KAAmB,EAAiB,EAAE,CACzD,KAAK,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,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gBAAgB,CAAC,CAC7C;SACA,IAAI,CACH,MAAM,CAAC,QAAQ,CAAC,CAAC,KAAK,EAAE,EAAE,CACxB,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"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
declare const XeroInvoiceError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => import("effect/Cause").YieldableError & {
|
|
2
|
+
readonly _tag: "XeroInvoiceError";
|
|
3
|
+
} & Readonly<A>;
|
|
4
|
+
/**
|
|
5
|
+
* Failure from the Xero invoice operation.
|
|
6
|
+
*
|
|
7
|
+
* `retryable: true` marks failures that may succeed unchanged after a later
|
|
8
|
+
* attempt: network, timeout, rate limit, and provider-server errors.
|
|
9
|
+
* `retryable: false` marks deterministic refusals and deployment or
|
|
10
|
+
* provider-contract faults that must fail immediately.
|
|
11
|
+
*/
|
|
12
|
+
export declare class XeroInvoiceError extends XeroInvoiceError_base<{
|
|
13
|
+
readonly message: string;
|
|
14
|
+
readonly retryable: boolean;
|
|
15
|
+
readonly code?: string;
|
|
16
|
+
readonly fields?: readonly string[];
|
|
17
|
+
readonly retryAfterSeconds?: number;
|
|
18
|
+
readonly cause?: unknown;
|
|
19
|
+
}> {
|
|
20
|
+
}
|
|
21
|
+
export declare const xeroValidationError: (message: string, options?: {
|
|
22
|
+
readonly code?: string;
|
|
23
|
+
readonly fields?: readonly string[];
|
|
24
|
+
}) => XeroInvoiceError;
|
|
25
|
+
export declare const xeroUnknownItemCodeError: (productSku: string) => XeroInvoiceError;
|
|
26
|
+
export declare const xeroTransportError: (message: string) => XeroInvoiceError;
|
|
27
|
+
export declare const xeroTimeoutError: (message: string) => XeroInvoiceError;
|
|
28
|
+
export declare const xeroRateLimitError: (retryAfterSeconds: number) => XeroInvoiceError;
|
|
29
|
+
export declare const xeroProviderServerError: (status: number) => XeroInvoiceError;
|
|
30
|
+
export declare const xeroAuthError: (message: string) => XeroInvoiceError;
|
|
31
|
+
export declare const xeroPermissionError: (message: string) => XeroInvoiceError;
|
|
32
|
+
export declare const xeroMalformedResponseError: (message: string) => XeroInvoiceError;
|
|
33
|
+
export declare const xeroDeploymentError: (message: string) => XeroInvoiceError;
|
|
34
|
+
export declare const xeroUnsupportedOperationError: (message: string) => XeroInvoiceError;
|
|
35
|
+
export declare const xeroIdempotencyConflictError: () => XeroInvoiceError;
|
|
36
|
+
export declare const xeroAmbiguousReferenceError: (reference: string) => XeroInvoiceError;
|
|
37
|
+
export declare const xeroInvoiceIntentConflictError: (reference: string) => XeroInvoiceError;
|
|
38
|
+
export {};
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { Data } from "effect";
|
|
2
|
+
/**
|
|
3
|
+
* Failure from the Xero invoice operation.
|
|
4
|
+
*
|
|
5
|
+
* `retryable: true` marks failures that may succeed unchanged after a later
|
|
6
|
+
* attempt: network, timeout, rate limit, and provider-server errors.
|
|
7
|
+
* `retryable: false` marks deterministic refusals and deployment or
|
|
8
|
+
* provider-contract faults that must fail immediately.
|
|
9
|
+
*/
|
|
10
|
+
export class XeroInvoiceError extends Data.TaggedError("XeroInvoiceError") {
|
|
11
|
+
}
|
|
12
|
+
export const xeroValidationError = (message, options) => new XeroInvoiceError({
|
|
13
|
+
message,
|
|
14
|
+
retryable: false,
|
|
15
|
+
code: options?.code ?? "validation",
|
|
16
|
+
...(options?.fields !== undefined && options.fields.length > 0
|
|
17
|
+
? { fields: options.fields }
|
|
18
|
+
: {}),
|
|
19
|
+
});
|
|
20
|
+
export const xeroUnknownItemCodeError = (productSku) => new XeroInvoiceError({
|
|
21
|
+
message: `Unknown Product SKU '${productSku}'`,
|
|
22
|
+
retryable: false,
|
|
23
|
+
code: "unknown_item_code",
|
|
24
|
+
fields: ["ItemCode"],
|
|
25
|
+
});
|
|
26
|
+
export const xeroTransportError = (message) => new XeroInvoiceError({
|
|
27
|
+
message,
|
|
28
|
+
retryable: true,
|
|
29
|
+
code: "transport",
|
|
30
|
+
});
|
|
31
|
+
export const xeroTimeoutError = (message) => new XeroInvoiceError({
|
|
32
|
+
message,
|
|
33
|
+
retryable: true,
|
|
34
|
+
code: "timeout",
|
|
35
|
+
});
|
|
36
|
+
export const xeroRateLimitError = (retryAfterSeconds) => new XeroInvoiceError({
|
|
37
|
+
message: `Xero rate limited; retry after ${String(retryAfterSeconds)} seconds`,
|
|
38
|
+
retryable: true,
|
|
39
|
+
code: "rate_limit",
|
|
40
|
+
retryAfterSeconds,
|
|
41
|
+
});
|
|
42
|
+
export const xeroProviderServerError = (status) => new XeroInvoiceError({
|
|
43
|
+
message: `Xero request failed (${String(status)})`,
|
|
44
|
+
retryable: true,
|
|
45
|
+
code: "provider_server",
|
|
46
|
+
});
|
|
47
|
+
export const xeroAuthError = (message) => new XeroInvoiceError({
|
|
48
|
+
message,
|
|
49
|
+
retryable: false,
|
|
50
|
+
code: "auth",
|
|
51
|
+
});
|
|
52
|
+
export const xeroPermissionError = (message) => new XeroInvoiceError({
|
|
53
|
+
message,
|
|
54
|
+
retryable: false,
|
|
55
|
+
code: "permission",
|
|
56
|
+
});
|
|
57
|
+
export const xeroMalformedResponseError = (message) => new XeroInvoiceError({
|
|
58
|
+
message,
|
|
59
|
+
retryable: false,
|
|
60
|
+
code: "malformed_response",
|
|
61
|
+
});
|
|
62
|
+
export const xeroDeploymentError = (message) => new XeroInvoiceError({
|
|
63
|
+
message,
|
|
64
|
+
retryable: false,
|
|
65
|
+
code: "deployment",
|
|
66
|
+
});
|
|
67
|
+
export const xeroUnsupportedOperationError = (message) => new XeroInvoiceError({
|
|
68
|
+
message,
|
|
69
|
+
retryable: false,
|
|
70
|
+
code: "unsupported_operation",
|
|
71
|
+
});
|
|
72
|
+
export const xeroIdempotencyConflictError = () => new XeroInvoiceError({
|
|
73
|
+
message: "Xero idempotency key was reused with a different method, target, or body",
|
|
74
|
+
retryable: false,
|
|
75
|
+
code: "idempotency_conflict",
|
|
76
|
+
});
|
|
77
|
+
export const xeroAmbiguousReferenceError = (reference) => new XeroInvoiceError({
|
|
78
|
+
message: `Multiple Xero invoices share Order Number Reference '${reference}'`,
|
|
79
|
+
retryable: false,
|
|
80
|
+
code: "ambiguous_reference",
|
|
81
|
+
});
|
|
82
|
+
export const xeroInvoiceIntentConflictError = (reference) => new XeroInvoiceError({
|
|
83
|
+
message: `Existing Xero invoice for Order Number Reference '${reference}' does not match this Sales Invoice intent`,
|
|
84
|
+
retryable: false,
|
|
85
|
+
code: "intent_conflict",
|
|
86
|
+
});
|
|
87
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +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,CAOvE;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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
+
/**
|
|
2
|
+
* Xero retains idempotency results for six minutes from the first call.
|
|
3
|
+
* After expiry, the same key is treated as a new write.
|
|
4
|
+
*/
|
|
5
|
+
export declare const XERO_IDEMPOTENCY_RETENTION_MS: number;
|
|
6
|
+
export type XeroIdempotencyOperation = "contact.create" | "contact.create.conflict" | "contact.update" | "invoice.create";
|
|
7
|
+
export declare const xeroIdempotencyKey: (todoId: string, operation: XeroIdempotencyOperation) => string;
|
|
8
|
+
/** Stable across executions as well as retries for one contact/reference intent. */
|
|
9
|
+
export declare const xeroDraftIdempotencyKey: (tenantId: string, contactId: string, reference: string) => string;
|
|
10
|
+
export declare const xeroIdempotencyFingerprint: ({ method, target, body, }: {
|
|
11
|
+
readonly method: string;
|
|
12
|
+
readonly target: string;
|
|
13
|
+
readonly body: unknown;
|
|
14
|
+
}) => string;
|
|
15
|
+
export declare const isXeroIdempotencyConflictMessage: (message: string) => boolean;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Xero retains idempotency results for six minutes from the first call.
|
|
3
|
+
* After expiry, the same key is treated as a new write.
|
|
4
|
+
*/
|
|
5
|
+
export const XERO_IDEMPOTENCY_RETENTION_MS = 6 * 60 * 1000;
|
|
6
|
+
export const xeroIdempotencyKey = (todoId, operation) => `${todoId}:${operation}`;
|
|
7
|
+
/** Stable across executions as well as retries for one contact/reference intent. */
|
|
8
|
+
export const xeroDraftIdempotencyKey = (tenantId, contactId, reference) => `pf-draft:${createHash("sha256")
|
|
9
|
+
.update(JSON.stringify([
|
|
10
|
+
tenantId.toLowerCase(),
|
|
11
|
+
contactId.toLowerCase(),
|
|
12
|
+
reference.trim().replace(/\s+/g, " ").toUpperCase(),
|
|
13
|
+
]))
|
|
14
|
+
.digest("hex")}`;
|
|
15
|
+
export const xeroIdempotencyFingerprint = ({ method, target, body, }) => `${method}:${target}:${JSON.stringify(body)}`;
|
|
16
|
+
export const isXeroIdempotencyConflictMessage = (message) => {
|
|
17
|
+
const lower = message.toLowerCase();
|
|
18
|
+
return (lower.includes("idempotency key") && lower.includes("different request"));
|
|
19
|
+
};
|
|
20
|
+
import { createHash } from "node:crypto";
|
|
21
|
+
//# sourceMappingURL=idempotency.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"idempotency.js","sourceRoot":"","sources":["../../src/lib/idempotency.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,CAAC,MAAM,6BAA6B,GAAG,CAAC,GAAG,EAAE,GAAG,IAAI,CAAA;AAQ1D,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAChC,MAAc,EACd,SAAmC,EAC3B,EAAE,CAAC,GAAG,MAAM,IAAI,SAAS,EAAE,CAAA;AAErC,oFAAoF;AACpF,MAAM,CAAC,MAAM,uBAAuB,GAAG,CACrC,QAAgB,EAChB,SAAiB,EACjB,SAAiB,EACT,EAAE,CACV,YAAY,UAAU,CAAC,QAAQ,CAAC;KAC7B,MAAM,CACL,IAAI,CAAC,SAAS,CAAC;IACb,QAAQ,CAAC,WAAW,EAAE;IACtB,SAAS,CAAC,WAAW,EAAE;IACvB,SAAS,CAAC,IAAI,EAAE,CAAC,OAAO,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,WAAW,EAAE;CACpD,CAAC,CACH;KACA,MAAM,CAAC,KAAK,CAAC,EAAE,CAAA;AAEpB,MAAM,CAAC,MAAM,0BAA0B,GAAG,CAAC,EACzC,MAAM,EACN,MAAM,EACN,IAAI,GAKL,EAAU,EAAE,CAAC,GAAG,MAAM,IAAI,MAAM,IAAI,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,CAAA;AAE3D,MAAM,CAAC,MAAM,gCAAgC,GAAG,CAAC,OAAe,EAAW,EAAE;IAC3E,MAAM,KAAK,GAAG,OAAO,CAAC,WAAW,EAAE,CAAA;IACnC,OAAO,CACL,KAAK,CAAC,QAAQ,CAAC,iBAAiB,CAAC,IAAI,KAAK,CAAC,QAAQ,CAAC,mBAAmB,CAAC,CACzE,CAAA;AACH,CAAC,CAAA;AAED,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
export declare const CustomerName: Schema.brand<typeof Schema.NonEmptyString, "CustomerName">;
|
|
3
|
+
export type CustomerName = typeof CustomerName.Type;
|
|
4
|
+
export declare const EmailAddress: Schema.brand<Schema.filter<typeof Schema.String>, "EmailAddress">;
|
|
5
|
+
export type EmailAddress = typeof EmailAddress.Type;
|
|
6
|
+
export declare const ProductSku: Schema.brand<typeof Schema.NonEmptyString, "ProductSku">;
|
|
7
|
+
export type ProductSku = typeof ProductSku.Type;
|
|
8
|
+
export declare const InvoiceReference: Schema.brand<typeof Schema.NonEmptyString, "InvoiceReference">;
|
|
9
|
+
export type InvoiceReference = typeof InvoiceReference.Type;
|
|
10
|
+
export declare const ExactMoney: Schema.brand<Schema.filter<Schema.filter<typeof Schema.String>>, "ExactMoney">;
|
|
11
|
+
export type ExactMoney = typeof ExactMoney.Type;
|
|
12
|
+
export declare const CurrencyCode: Schema.Literal<["NZD"]>;
|
|
13
|
+
export type CurrencyCode = typeof CurrencyCode.Type;
|
|
14
|
+
export declare const CalendarDate: Schema.brand<Schema.filter<typeof Schema.String>, "CalendarDate">;
|
|
15
|
+
export type CalendarDate = typeof CalendarDate.Type;
|
|
16
|
+
export declare const XeroContactId: Schema.brand<Schema.filter<typeof Schema.String>, "XeroContactId">;
|
|
17
|
+
export type XeroContactId = typeof XeroContactId.Type;
|
|
18
|
+
export declare const XeroInvoiceId: Schema.brand<Schema.filter<typeof Schema.String>, "XeroInvoiceId">;
|
|
19
|
+
export type XeroInvoiceId = typeof XeroInvoiceId.Type;
|
|
20
|
+
export declare const XeroInvoiceNumber: Schema.brand<typeof Schema.NonEmptyString, "XeroInvoiceNumber">;
|
|
21
|
+
export type XeroInvoiceNumber = typeof XeroInvoiceNumber.Type;
|
|
22
|
+
export declare const CustomerDetails: Schema.Struct<{
|
|
23
|
+
name: Schema.brand<typeof Schema.NonEmptyString, "CustomerName">;
|
|
24
|
+
email: Schema.brand<Schema.filter<typeof Schema.String>, "EmailAddress">;
|
|
25
|
+
}>;
|
|
26
|
+
export type CustomerDetails = typeof CustomerDetails.Type;
|
|
27
|
+
export declare const ShippingAddress: Schema.Struct<{
|
|
28
|
+
line1: typeof Schema.NonEmptyString;
|
|
29
|
+
line2: Schema.optional<typeof Schema.NonEmptyString>;
|
|
30
|
+
city: typeof Schema.NonEmptyString;
|
|
31
|
+
region: Schema.optional<typeof Schema.String>;
|
|
32
|
+
postalCode: Schema.optional<typeof Schema.String>;
|
|
33
|
+
country: Schema.optional<typeof Schema.String>;
|
|
34
|
+
}>;
|
|
35
|
+
export type ShippingAddress = typeof ShippingAddress.Type;
|
|
36
|
+
export declare const IssueAuthorisedSalesInvoiceInput: Schema.filter<Schema.Struct<{
|
|
37
|
+
customer: Schema.Struct<{
|
|
38
|
+
name: Schema.brand<typeof Schema.NonEmptyString, "CustomerName">;
|
|
39
|
+
email: Schema.brand<Schema.filter<typeof Schema.String>, "EmailAddress">;
|
|
40
|
+
}>;
|
|
41
|
+
shippingAddress: Schema.Struct<{
|
|
42
|
+
line1: typeof Schema.NonEmptyString;
|
|
43
|
+
line2: Schema.optional<typeof Schema.NonEmptyString>;
|
|
44
|
+
city: typeof Schema.NonEmptyString;
|
|
45
|
+
region: Schema.optional<typeof Schema.String>;
|
|
46
|
+
postalCode: Schema.optional<typeof Schema.String>;
|
|
47
|
+
country: Schema.optional<typeof Schema.String>;
|
|
48
|
+
}>;
|
|
49
|
+
productSku: Schema.brand<typeof Schema.NonEmptyString, "ProductSku">;
|
|
50
|
+
agreedPrice: Schema.brand<Schema.filter<Schema.filter<typeof Schema.String>>, "ExactMoney">;
|
|
51
|
+
currency: Schema.Literal<["NZD"]>;
|
|
52
|
+
reference: Schema.brand<typeof Schema.NonEmptyString, "InvoiceReference">;
|
|
53
|
+
date: Schema.brand<Schema.filter<typeof Schema.String>, "CalendarDate">;
|
|
54
|
+
dueDate: Schema.brand<Schema.filter<typeof Schema.String>, "CalendarDate">;
|
|
55
|
+
}>>;
|
|
56
|
+
export type IssueAuthorisedSalesInvoiceInput = typeof IssueAuthorisedSalesInvoiceInput.Type;
|
|
57
|
+
export declare const IssuedSalesInvoice: Schema.Struct<{
|
|
58
|
+
invoiceId: Schema.brand<Schema.filter<typeof Schema.String>, "XeroInvoiceId">;
|
|
59
|
+
invoiceNumber: Schema.brand<typeof Schema.NonEmptyString, "XeroInvoiceNumber">;
|
|
60
|
+
}>;
|
|
61
|
+
export type IssuedSalesInvoice = typeof IssuedSalesInvoice.Type;
|
|
62
|
+
export declare const normalizeEmail: (email: string) => string;
|
|
63
|
+
export declare const isUniqueEmailMatch: (matchCount: number) => boolean;
|