@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
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
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
|
+
const EMAIL_PATTERN = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
4
|
+
const EXACT_MONEY_PATTERN = /^(?:0|[1-9]\d*)\.\d{2}$/;
|
|
5
|
+
const CALENDAR_DATE_PATTERN = /^\d{4}-\d{2}-\d{2}$/;
|
|
6
|
+
export const CustomerName = Schema.NonEmptyString.pipe(Schema.brand("CustomerName"));
|
|
7
|
+
export const EmailAddress = Schema.String.pipe(Schema.pattern(EMAIL_PATTERN), Schema.brand("EmailAddress"));
|
|
8
|
+
export const ProductSku = Schema.NonEmptyString.pipe(Schema.brand("ProductSku"));
|
|
9
|
+
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
|
+
export const CurrencyCode = Schema.Literal("NZD");
|
|
14
|
+
export const CalendarDate = Schema.String.pipe(Schema.pattern(CALENDAR_DATE_PATTERN), Schema.brand("CalendarDate"));
|
|
15
|
+
export const XeroContactId = Schema.String.pipe(Schema.pattern(GUID_PATTERN), Schema.brand("XeroContactId"));
|
|
16
|
+
export const XeroInvoiceId = Schema.String.pipe(Schema.pattern(GUID_PATTERN), Schema.brand("XeroInvoiceId"));
|
|
17
|
+
export const XeroInvoiceNumber = Schema.NonEmptyString.pipe(Schema.brand("XeroInvoiceNumber"));
|
|
18
|
+
export const CustomerDetails = Schema.Struct({
|
|
19
|
+
name: CustomerName,
|
|
20
|
+
email: EmailAddress,
|
|
21
|
+
});
|
|
22
|
+
export const ShippingAddress = Schema.Struct({
|
|
23
|
+
line1: Schema.NonEmptyString,
|
|
24
|
+
line2: Schema.optional(Schema.NonEmptyString),
|
|
25
|
+
city: Schema.NonEmptyString,
|
|
26
|
+
region: Schema.optional(Schema.String),
|
|
27
|
+
postalCode: Schema.optional(Schema.String),
|
|
28
|
+
country: Schema.optional(Schema.String),
|
|
29
|
+
});
|
|
30
|
+
export const IssueAuthorisedSalesInvoiceInput = Schema.Struct({
|
|
31
|
+
customer: CustomerDetails,
|
|
32
|
+
shippingAddress: ShippingAddress,
|
|
33
|
+
productSku: ProductSku,
|
|
34
|
+
agreedPrice: ExactMoney,
|
|
35
|
+
currency: CurrencyCode,
|
|
36
|
+
reference: InvoiceReference,
|
|
37
|
+
date: CalendarDate,
|
|
38
|
+
dueDate: CalendarDate,
|
|
39
|
+
}).pipe(Schema.filter((input) => input.date === input.dueDate, {
|
|
40
|
+
message: () => "DueDate must equal Date so the Sales Invoice is immediately due",
|
|
41
|
+
}));
|
|
42
|
+
export const IssuedSalesInvoice = Schema.Struct({
|
|
43
|
+
invoiceId: XeroInvoiceId,
|
|
44
|
+
invoiceNumber: XeroInvoiceNumber,
|
|
45
|
+
});
|
|
46
|
+
export const normalizeEmail = (email) => email.trim().toLowerCase();
|
|
47
|
+
export const isUniqueEmailMatch = (matchCount) => matchCount === 1;
|
|
48
|
+
//# sourceMappingURL=identities.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"identities.js","sourceRoot":"","sources":["../../src/lib/identities.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAE/B,MAAM,YAAY,GAChB,iEAAiE,CAAA;AAEnE,MAAM,aAAa,GAAG,4BAA4B,CAAA;AAElD,MAAM,mBAAmB,GAAG,yBAAyB,CAAA;AAErD,MAAM,qBAAqB,GAAG,qBAAqB,CAAA;AAEnD,MAAM,CAAC,MAAM,YAAY,GAAG,MAAM,CAAC,cAAc,CAAC,IAAI,CACpD,MAAM,CAAC,KAAK,CAAC,cAAc,CAAC,CAC7B,CAAA;AAGD,MAAM,CAAC,MAAM,YAAY,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAC5C,MAAM,CAAC,OAAO,CAAC,aAAa,CAAC,EAC7B,MAAM,CAAC,KAAK,CAAC,cAAc,CAAC,CAC7B,CAAA;AAGD,MAAM,CAAC,MAAM,UAAU,GAAG,MAAM,CAAC,cAAc,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC,CAAA;AAGhF,MAAM,CAAC,MAAM,gBAAgB,GAAG,MAAM,CAAC,cAAc,CAAC,IAAI,CACxD,MAAM,CAAC,KAAK,CAAC,kBAAkB,CAAC,CACjC,CAAA;AAGD,MAAM,CAAC,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAC1C,MAAM,CAAC,OAAO,CAAC,mBAAmB,CAAC,EACnC,MAAM,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,KAAK,MAAM,EAAE;IACzC,OAAO,EAAE,GAAG,EAAE,CAAC,wCAAwC;CACxD,CAA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C,OAAO,EAAE,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,MAAM,CAAC;CACxC,CAAC,CAAA;AAGF,MAAM,CAAC,MAAM,gCAAgC,GAAG,MAAM,CAAC,MAAM,CAAC;IAC5D,QAAQ,EAAE,eAAe;IACzB,eAAe,EAAE,eAAe;IAChC,UAAU,EAAE,UAAU;IACtB,WAAW,EAAE,UAAU;IACvB,QAAQ,EAAE,YAAY;IACtB,SAAS,EAAE,gBAAgB;IAC3B,IAAI,EAAE,YAAY;IAClB,OAAO,EAAE,YAAY;CACtB,CAAC,CAAC,IAAI,CACL,MAAM,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,KAAK,KAAK,CAAC,OAAO,EAAE;IACrD,OAAO,EAAE,GAAG,EAAE,CACZ,iEAAiE;CACpE,CAAC,CACH,CAAA;AAID,MAAM,CAAC,MAAM,kBAAkB,GAAG,MAAM,CAAC,MAAM,CAAC;IAC9C,SAAS,EAAE,aAAa;IACxB,aAAa,EAAE,iBAAiB;CACjC,CAAC,CAAA;AAGF,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,KAAa,EAAU,EAAE,CACtD,KAAK,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,CAAA;AAE5B,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,UAAkB,EAAW,EAAE,CAChE,UAAU,KAAK,CAAC,CAAA"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { type XeroInvoiceError } from "./errors.js";
|
|
3
|
+
import { type IssueAuthorisedSalesInvoiceInput, type IssuedSalesInvoice } from "./identities.js";
|
|
4
|
+
export interface ReconcilableInvoice {
|
|
5
|
+
readonly invoiceId: string;
|
|
6
|
+
readonly invoiceNumber: string;
|
|
7
|
+
readonly type: string;
|
|
8
|
+
readonly status: string;
|
|
9
|
+
readonly currency: string;
|
|
10
|
+
readonly date: string;
|
|
11
|
+
readonly dueDate: string;
|
|
12
|
+
readonly contactId: string;
|
|
13
|
+
readonly itemCode: string;
|
|
14
|
+
readonly unitAmount: string;
|
|
15
|
+
readonly total: string;
|
|
16
|
+
readonly lineCount: number;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Xero date-only fields often serialise as a UTC instant of organisation-local
|
|
20
|
+
* midnight plus an offset such as +1300. The calendar date is the local day,
|
|
21
|
+
* not the UTC day of that instant.
|
|
22
|
+
*/
|
|
23
|
+
export declare const parseXeroCalendarDate: (value: string) => string | undefined;
|
|
24
|
+
export declare const wireAmountToMoney: (value: number | string | undefined) => string | undefined;
|
|
25
|
+
export declare const isMateriallyCompatibleInvoice: (invoice: ReconcilableInvoice, input: IssueAuthorisedSalesInvoiceInput, contactId: string) => boolean;
|
|
26
|
+
export declare const decodeIssuedSalesInvoice: (invoiceId: string, invoiceNumber: string) => Effect.Effect<IssuedSalesInvoice, XeroInvoiceError>;
|
|
27
|
+
/**
|
|
28
|
+
* Reconcile invoices that already share this Order Number Reference.
|
|
29
|
+
*
|
|
30
|
+
* Zero matches means create. Exactly one materially compatible invoice is
|
|
31
|
+
* success. Multiple invoices, or one with conflicting intent, fail without
|
|
32
|
+
* changing the existing authorised invoice.
|
|
33
|
+
*/
|
|
34
|
+
export declare const reconcileExistingInvoices: (invoices: readonly ReconcilableInvoice[], input: IssueAuthorisedSalesInvoiceInput, contactId: string) => Effect.Effect<IssuedSalesInvoice | undefined, XeroInvoiceError>;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
|
+
import { xeroAmbiguousReferenceError, xeroInvoiceIntentConflictError, xeroMalformedResponseError, } from "./errors.js";
|
|
3
|
+
import { IssuedSalesInvoice as IssuedSalesInvoiceSchema, } from "./identities.js";
|
|
4
|
+
import { centsToMoney } from "./money.js";
|
|
5
|
+
const MICROSOFT_JSON_DATE = /\/Date\((\d+)(?:([+-])(\d{2})(\d{2}))?\)\//;
|
|
6
|
+
/**
|
|
7
|
+
* Xero date-only fields often serialise as a UTC instant of organisation-local
|
|
8
|
+
* midnight plus an offset such as +1300. The calendar date is the local day,
|
|
9
|
+
* not the UTC day of that instant.
|
|
10
|
+
*/
|
|
11
|
+
export const parseXeroCalendarDate = (value) => {
|
|
12
|
+
const microsoft = MICROSOFT_JSON_DATE.exec(value);
|
|
13
|
+
if (microsoft?.[1] !== undefined) {
|
|
14
|
+
const ticks = Number(microsoft[1]);
|
|
15
|
+
const sign = microsoft[2] === "-" ? -1 : 1;
|
|
16
|
+
const offsetHours = Number(microsoft[3] ?? "0");
|
|
17
|
+
const offsetMinutes = Number(microsoft[4] ?? "0");
|
|
18
|
+
const offsetMs = sign * (offsetHours * 60 + offsetMinutes) * 60 * 1000;
|
|
19
|
+
return new Date(ticks + offsetMs).toISOString().slice(0, 10);
|
|
20
|
+
}
|
|
21
|
+
const isoDate = /^(\d{4}-\d{2}-\d{2})/.exec(value);
|
|
22
|
+
return isoDate?.[1];
|
|
23
|
+
};
|
|
24
|
+
export const wireAmountToMoney = (value) => {
|
|
25
|
+
if (typeof value === "number" && Number.isFinite(value)) {
|
|
26
|
+
return centsToMoney(Math.round(value * 100));
|
|
27
|
+
}
|
|
28
|
+
if (typeof value === "string" && value.trim() !== "") {
|
|
29
|
+
const parsed = Number(value);
|
|
30
|
+
if (!Number.isFinite(parsed)) {
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
return centsToMoney(Math.round(parsed * 100));
|
|
34
|
+
}
|
|
35
|
+
return undefined;
|
|
36
|
+
};
|
|
37
|
+
export const isMateriallyCompatibleInvoice = (invoice, input, contactId) => invoice.contactId === contactId &&
|
|
38
|
+
invoice.type === "ACCREC" &&
|
|
39
|
+
invoice.status === "AUTHORISED" &&
|
|
40
|
+
invoice.currency === input.currency &&
|
|
41
|
+
parseXeroCalendarDate(invoice.date) === input.date &&
|
|
42
|
+
parseXeroCalendarDate(invoice.dueDate) === input.dueDate &&
|
|
43
|
+
invoice.lineCount === 1 &&
|
|
44
|
+
invoice.itemCode === input.productSku &&
|
|
45
|
+
invoice.unitAmount === input.agreedPrice &&
|
|
46
|
+
invoice.total === input.agreedPrice;
|
|
47
|
+
export const decodeIssuedSalesInvoice = (invoiceId, invoiceNumber) => Schema.decodeUnknown(IssuedSalesInvoiceSchema)({
|
|
48
|
+
invoiceId,
|
|
49
|
+
invoiceNumber,
|
|
50
|
+
}).pipe(Effect.mapError(() => xeroMalformedResponseError("Malformed Xero invoice response")));
|
|
51
|
+
/**
|
|
52
|
+
* Reconcile invoices that already share this Order Number Reference.
|
|
53
|
+
*
|
|
54
|
+
* Zero matches means create. Exactly one materially compatible invoice is
|
|
55
|
+
* success. Multiple invoices, or one with conflicting intent, fail without
|
|
56
|
+
* changing the existing authorised invoice.
|
|
57
|
+
*/
|
|
58
|
+
export const reconcileExistingInvoices = (invoices, input, contactId) => {
|
|
59
|
+
if (invoices.length === 0) {
|
|
60
|
+
return Effect.succeed(undefined);
|
|
61
|
+
}
|
|
62
|
+
if (invoices.length > 1) {
|
|
63
|
+
return Effect.fail(xeroAmbiguousReferenceError(input.reference));
|
|
64
|
+
}
|
|
65
|
+
const invoice = invoices[0];
|
|
66
|
+
if (invoice === undefined ||
|
|
67
|
+
!isMateriallyCompatibleInvoice(invoice, input, contactId)) {
|
|
68
|
+
return Effect.fail(xeroInvoiceIntentConflictError(input.reference));
|
|
69
|
+
}
|
|
70
|
+
return decodeIssuedSalesInvoice(invoice.invoiceId, invoice.invoiceNumber);
|
|
71
|
+
};
|
|
72
|
+
//# sourceMappingURL=invoice-reconciliation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"invoice-reconciliation.js","sourceRoot":"","sources":["../../src/lib/invoice-reconciliation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AACvC,OAAO,EAEL,2BAA2B,EAC3B,8BAA8B,EAC9B,0BAA0B,GAC3B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AAC,CAAC;IAC7C,SAAS;IACT,aAAa;CACd,CAAC,CAAC,IAAI,CACL,MAAM,CAAC,QAAQ,CAAC,GAAG,EAAE,CACnB,0BAA0B,CAAC,iCAAiC,CAAC,CAC9D,CACF,CAAA;AAEH;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,yBAAyB,GAAG,CACvC,QAAwC,EACxC,KAAuC,EACvC,SAAiB,EACgD,EAAE;IACnE,IAAI,QAAQ,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC1B,OAAO,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,CAAA;IAClC,CAAC;IACD,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACxB,OAAO,MAAM,CAAC,IAAI,CAAC,2BAA2B,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,CAAA;IAClE,CAAC;IAED,MAAM,OAAO,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAA;IAC3B,IACE,OAAO,KAAK,SAAS;QACrB,CAAC,6BAA6B,CAAC,OAAO,EAAE,KAAK,EAAE,SAAS,CAAC,EACzD,CAAC;QACD,OAAO,MAAM,CAAC,IAAI,CAAC,8BAA8B,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,CAAA;IACrE,CAAC;IAED,OAAO,wBAAwB,CAAC,OAAO,CAAC,SAAS,EAAE,OAAO,CAAC,aAAa,CAAC,CAAA;AAC3E,CAAC,CAAA"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { type XeroInvoiceError } from "./errors.js";
|
|
3
|
+
import type { ExactMoney } from "./identities.js";
|
|
4
|
+
export declare const moneyToCents: (amount: ExactMoney) => Effect.Effect<number, XeroInvoiceError>;
|
|
5
|
+
export declare const centsToMoney: (cents: number) => string;
|
|
6
|
+
export declare const inclusiveTaxCents: (inclusiveCents: number, taxPercent: number) => number;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { xeroValidationError } from "./errors.js";
|
|
3
|
+
export const moneyToCents = (amount) => {
|
|
4
|
+
const [dollars, cents] = amount.split(".");
|
|
5
|
+
if (dollars === undefined || cents === undefined) {
|
|
6
|
+
return Effect.fail(xeroValidationError("Agreed Price is not exact money"));
|
|
7
|
+
}
|
|
8
|
+
return Effect.succeed(Number.parseInt(dollars, 10) * 100 + Number.parseInt(cents, 10));
|
|
9
|
+
};
|
|
10
|
+
export const centsToMoney = (cents) => {
|
|
11
|
+
const absolute = Math.abs(cents);
|
|
12
|
+
const dollars = Math.floor(absolute / 100);
|
|
13
|
+
const remainder = absolute % 100;
|
|
14
|
+
const sign = cents < 0 ? "-" : "";
|
|
15
|
+
return `${sign}${String(dollars)}.${String(remainder).padStart(2, "0")}`;
|
|
16
|
+
};
|
|
17
|
+
export const inclusiveTaxCents = (inclusiveCents, taxPercent) => {
|
|
18
|
+
if (taxPercent === 0) {
|
|
19
|
+
return 0;
|
|
20
|
+
}
|
|
21
|
+
return Math.round((inclusiveCents * taxPercent) / (100 + taxPercent));
|
|
22
|
+
};
|
|
23
|
+
//# sourceMappingURL=money.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"money.js","sourceRoot":"","sources":["../../src/lib/money.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAC/B,OAAO,EAAyB,mBAAmB,EAAE,MAAM,UAAU,CAAA;AAGrE,MAAM,CAAC,MAAM,YAAY,GAAG,CAC1B,MAAkB,EACuB,EAAE;IAC3C,MAAM,CAAC,OAAO,EAAE,KAAK,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IAC1C,IAAI,OAAO,KAAK,SAAS,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;QACjD,OAAO,MAAM,CAAC,IAAI,CAAC,mBAAmB,CAAC,iCAAiC,CAAC,CAAC,CAAA;IAC5E,CAAC;IAED,OAAO,MAAM,CAAC,OAAO,CACnB,MAAM,CAAC,QAAQ,CAAC,OAAO,EAAE,EAAE,CAAC,GAAG,GAAG,GAAG,MAAM,CAAC,QAAQ,CAAC,KAAK,EAAE,EAAE,CAAC,CAChE,CAAA;AACH,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,KAAa,EAAU,EAAE;IACpD,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,CAAA;IAChC,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,QAAQ,GAAG,GAAG,CAAC,CAAA;IAC1C,MAAM,SAAS,GAAG,QAAQ,GAAG,GAAG,CAAA;IAChC,MAAM,IAAI,GAAG,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAA;IACjC,OAAO,GAAG,IAAI,GAAG,MAAM,CAAC,OAAO,CAAC,IAAI,MAAM,CAAC,SAAS,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,CAAA;AAC1E,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAC/B,cAAsB,EACtB,UAAkB,EACV,EAAE;IACV,IAAI,UAAU,KAAK,CAAC,EAAE,CAAC;QACrB,OAAO,CAAC,CAAA;IACV,CAAC;IAED,OAAO,IAAI,CAAC,KAAK,CAAC,CAAC,cAAc,GAAG,UAAU,CAAC,GAAG,CAAC,GAAG,GAAG,UAAU,CAAC,CAAC,CAAA;AACvE,CAAC,CAAA"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { Context, type Effect, Schema } from "effect";
|
|
2
|
+
import type { XeroInvoiceError } from "./errors.js";
|
|
3
|
+
export declare const DraftSalesInvoice: Schema.filter<Schema.Struct<{
|
|
4
|
+
contactId: Schema.brand<Schema.filter<typeof Schema.String>, "XeroContactId">;
|
|
5
|
+
reference: Schema.filter<typeof Schema.Trim>;
|
|
6
|
+
duplicateReference: Schema.optional<Schema.filter<typeof Schema.Trim>>;
|
|
7
|
+
type: Schema.Literal<["ACCREC"]>;
|
|
8
|
+
status: Schema.Literal<["DRAFT"]>;
|
|
9
|
+
date: Schema.brand<Schema.filter<typeof Schema.String>, "CalendarDate">;
|
|
10
|
+
dueDate: Schema.brand<Schema.filter<typeof Schema.String>, "CalendarDate">;
|
|
11
|
+
currency: Schema.Literal<["NZD"]>;
|
|
12
|
+
lineAmountType: Schema.Literal<["Inclusive"]>;
|
|
13
|
+
lines: Schema.filter<Schema.Array$<Schema.Struct<{
|
|
14
|
+
itemCode: Schema.filter<typeof Schema.Trim>;
|
|
15
|
+
description: Schema.filter<typeof Schema.Trim>;
|
|
16
|
+
quantity: Schema.filter<typeof Schema.Int>;
|
|
17
|
+
unitAmountCents: Schema.filter<typeof Schema.Number>;
|
|
18
|
+
lineAmountCents: Schema.filter<typeof Schema.Number>;
|
|
19
|
+
taxCents: Schema.filter<typeof Schema.Number>;
|
|
20
|
+
taxType: Schema.filter<typeof Schema.Trim>;
|
|
21
|
+
tracking: Schema.optional<Schema.filter<Schema.filter<Schema.Array$<Schema.Struct<{
|
|
22
|
+
category: Schema.filter<typeof Schema.Trim>;
|
|
23
|
+
option: Schema.filter<typeof Schema.Trim>;
|
|
24
|
+
}>>>>>;
|
|
25
|
+
}>>>;
|
|
26
|
+
totalCents: Schema.filter<Schema.filter<typeof Schema.Number>>;
|
|
27
|
+
taxCents: Schema.filter<typeof Schema.Number>;
|
|
28
|
+
subtotalCents: Schema.filter<typeof Schema.Number>;
|
|
29
|
+
expectedRecipientEmails: Schema.optional<Schema.filter<Schema.Array$<Schema.refine<string & import("effect/Brand").Brand<"EmailAddress">, Schema.Schema<unknown, string, never>>>>>;
|
|
30
|
+
}>>;
|
|
31
|
+
export type DraftSalesInvoice = typeof DraftSalesInvoice.Type;
|
|
32
|
+
export declare const DraftInvoiceResult: Schema.Union<[Schema.Struct<{
|
|
33
|
+
kind: Schema.Literal<["created-draft"]>;
|
|
34
|
+
invoiceId: Schema.brand<Schema.filter<typeof Schema.String>, "XeroInvoiceId">;
|
|
35
|
+
invoiceNumber: Schema.brand<typeof Schema.NonEmptyString, "XeroInvoiceNumber">;
|
|
36
|
+
status: Schema.Literal<["DRAFT"]>;
|
|
37
|
+
}>, Schema.Struct<{
|
|
38
|
+
kind: Schema.Literal<["skipped-existing"]>;
|
|
39
|
+
existingInvoices: Schema.NonEmptyArray<Schema.Struct<{
|
|
40
|
+
invoiceId: Schema.brand<Schema.filter<typeof Schema.String>, "XeroInvoiceId">;
|
|
41
|
+
invoiceNumber: Schema.brand<typeof Schema.NonEmptyString, "XeroInvoiceNumber">;
|
|
42
|
+
status: typeof Schema.String;
|
|
43
|
+
}>>;
|
|
44
|
+
}>]>;
|
|
45
|
+
export type DraftInvoiceResult = typeof DraftInvoiceResult.Type;
|
|
46
|
+
export declare const matchesInvoiceReference: (actual: string, reference: string) => boolean;
|
|
47
|
+
declare const XeroDraftInvoice_base: Context.TagClass<XeroDraftInvoice, "@processfocus/plugin-xero/XeroDraftInvoice", {
|
|
48
|
+
readonly createIfAbsent: (invoice: DraftSalesInvoice) => Effect.Effect<DraftInvoiceResult, XeroInvoiceError>;
|
|
49
|
+
}>;
|
|
50
|
+
/** Creates drafts only, for an already resolved contact; never updates contacts. */
|
|
51
|
+
export declare class XeroDraftInvoice extends XeroDraftInvoice_base {
|
|
52
|
+
}
|
|
53
|
+
export {};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { Context, Schema } from "effect";
|
|
2
|
+
import { CalendarDate, EmailAddress, XeroContactId, XeroInvoiceId, XeroInvoiceNumber, } from "./identities.js";
|
|
3
|
+
const cents = Schema.Number.pipe(Schema.filter(Number.isSafeInteger));
|
|
4
|
+
const nonBlank = Schema.Trim.pipe(Schema.minLength(1));
|
|
5
|
+
const Tracking = Schema.Array(Schema.Struct({
|
|
6
|
+
category: nonBlank,
|
|
7
|
+
option: nonBlank,
|
|
8
|
+
})).pipe(Schema.minItems(1), Schema.filter((tracking) => new Set(tracking.map((entry) => entry.category.toLowerCase())).size ===
|
|
9
|
+
tracking.length));
|
|
10
|
+
export const DraftSalesInvoice = Schema.Struct({
|
|
11
|
+
contactId: XeroContactId,
|
|
12
|
+
reference: nonBlank,
|
|
13
|
+
// Stable duplicate-check/idempotency scope when the display adds a suffix.
|
|
14
|
+
duplicateReference: Schema.optional(nonBlank),
|
|
15
|
+
type: Schema.Literal("ACCREC"),
|
|
16
|
+
status: Schema.Literal("DRAFT"),
|
|
17
|
+
date: CalendarDate,
|
|
18
|
+
dueDate: CalendarDate,
|
|
19
|
+
currency: Schema.Literal("NZD"),
|
|
20
|
+
lineAmountType: Schema.Literal("Inclusive"),
|
|
21
|
+
lines: Schema.Array(Schema.Struct({
|
|
22
|
+
itemCode: nonBlank,
|
|
23
|
+
description: nonBlank,
|
|
24
|
+
quantity: Schema.Int.pipe(Schema.positive()),
|
|
25
|
+
unitAmountCents: cents,
|
|
26
|
+
lineAmountCents: cents,
|
|
27
|
+
taxCents: cents,
|
|
28
|
+
taxType: nonBlank,
|
|
29
|
+
// Xero resolves these category and option names to its tenant-owned IDs.
|
|
30
|
+
// Keeping names in the authoring contract avoids leaking a tenant's IDs.
|
|
31
|
+
tracking: Schema.optional(Tracking),
|
|
32
|
+
})).pipe(Schema.minItems(1)),
|
|
33
|
+
totalCents: cents.pipe(Schema.nonNegative()),
|
|
34
|
+
taxCents: cents,
|
|
35
|
+
subtotalCents: cents,
|
|
36
|
+
// Older saved previews did not capture recipient intent. They remain readable,
|
|
37
|
+
// but a new write requires this field and rechecks the live contact.
|
|
38
|
+
expectedRecipientEmails: Schema.optional(Schema.Array(Schema.Trim.pipe(Schema.filter(Schema.is(EmailAddress)))).pipe(Schema.minItems(1))),
|
|
39
|
+
}).pipe(Schema.filter((invoice) => invoice.dueDate >= invoice.date &&
|
|
40
|
+
(invoice.duplicateReference === undefined ||
|
|
41
|
+
matchesInvoiceReference(invoice.reference, invoice.duplicateReference)) &&
|
|
42
|
+
invoice.lines.every((line) => line.quantity * line.unitAmountCents === line.lineAmountCents) &&
|
|
43
|
+
invoice.lines.reduce((sum, line) => sum + line.lineAmountCents, 0) ===
|
|
44
|
+
invoice.totalCents &&
|
|
45
|
+
invoice.lines.reduce((sum, line) => sum + line.taxCents, 0) ===
|
|
46
|
+
invoice.taxCents &&
|
|
47
|
+
invoice.totalCents - invoice.taxCents === invoice.subtotalCents));
|
|
48
|
+
export const DraftInvoiceResult = Schema.Union(Schema.Struct({
|
|
49
|
+
kind: Schema.Literal("created-draft"),
|
|
50
|
+
invoiceId: XeroInvoiceId,
|
|
51
|
+
invoiceNumber: XeroInvoiceNumber,
|
|
52
|
+
status: Schema.Literal("DRAFT"),
|
|
53
|
+
}), Schema.Struct({
|
|
54
|
+
kind: Schema.Literal("skipped-existing"),
|
|
55
|
+
existingInvoices: Schema.NonEmptyArray(Schema.Struct({
|
|
56
|
+
invoiceId: XeroInvoiceId,
|
|
57
|
+
invoiceNumber: XeroInvoiceNumber,
|
|
58
|
+
status: Schema.String,
|
|
59
|
+
})),
|
|
60
|
+
}));
|
|
61
|
+
export const matchesInvoiceReference = (actual, reference) => {
|
|
62
|
+
const normalize = (value) => value.trim().replace(/\s+/g, " ").toUpperCase();
|
|
63
|
+
const candidate = normalize(actual);
|
|
64
|
+
const expected = normalize(reference);
|
|
65
|
+
return candidate === expected || candidate.startsWith(`${expected} `);
|
|
66
|
+
};
|
|
67
|
+
/** Creates drafts only, for an already resolved contact; never updates contacts. */
|
|
68
|
+
export class XeroDraftInvoice extends Context.Tag("@processfocus/plugin-xero/XeroDraftInvoice")() {
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=xero-draft-invoice.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"xero-draft-invoice.js","sourceRoot":"","sources":["../../src/lib/xero-draft-invoice.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAe,MAAM,EAAE,MAAM,QAA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,CAAA;IACnC,MAAM,QAAQ,GAAG,SAAS,CAAC,SAAS,CAAC,CAAA;IACrC,OAAO,SAAS,KAAK,QAAQ,IAAI,SAAS,CAAC,UAAU,CAAC,GAAG,QAAQ,GAAG,CAAC,CAAA;AACvE,CAAC,CAAA;AAED,oFAAoF;AACpF,MAAM,OAAO,gBAAiB,SAAQ,OAAO,CAAC,GAAG,CAC/C,4CAA4C,CAC7C,EAOE;CAAG"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { HttpClientError } from "@effect/platform/HttpClientError";
|
|
2
|
+
import { Effect } from "effect";
|
|
3
|
+
import { type XeroInvoiceError } from "./errors.js";
|
|
4
|
+
/**
|
|
5
|
+
* Parse a Xero Retry-After header as a bounded wait in seconds.
|
|
6
|
+
*
|
|
7
|
+
* Missing, zero, or unparseable values wait the minimum so retries cannot
|
|
8
|
+
* tight-loop against Xero. HTTP-date values are converted from `nowMs`.
|
|
9
|
+
*/
|
|
10
|
+
export declare const parseRetryAfterSeconds: (raw: string | undefined, nowMs: number) => number;
|
|
11
|
+
export declare const waitForRetryAfter: (seconds: number) => Effect.Effect<void>;
|
|
12
|
+
export declare const mapHttpClientError: (error: HttpClientError) => XeroInvoiceError;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { Duration, Effect } from "effect";
|
|
2
|
+
import { xeroTimeoutError, xeroTransportError, } from "./errors.js";
|
|
3
|
+
const XERO_MIN_RETRY_AFTER_SECONDS = 1;
|
|
4
|
+
const XERO_MAX_RETRY_AFTER_SECONDS = 60;
|
|
5
|
+
/**
|
|
6
|
+
* Parse a Xero Retry-After header as a bounded wait in seconds.
|
|
7
|
+
*
|
|
8
|
+
* Missing, zero, or unparseable values wait the minimum so retries cannot
|
|
9
|
+
* tight-loop against Xero. HTTP-date values are converted from `nowMs`.
|
|
10
|
+
*/
|
|
11
|
+
export const parseRetryAfterSeconds = (raw, nowMs) => {
|
|
12
|
+
if (raw === undefined || raw.trim() === "") {
|
|
13
|
+
return XERO_MIN_RETRY_AFTER_SECONDS;
|
|
14
|
+
}
|
|
15
|
+
const asNumber = Number(raw);
|
|
16
|
+
if (Number.isFinite(asNumber) && asNumber >= 0) {
|
|
17
|
+
return clampRetryAfterSeconds(Math.ceil(asNumber));
|
|
18
|
+
}
|
|
19
|
+
const dateMs = Date.parse(raw);
|
|
20
|
+
if (Number.isFinite(dateMs)) {
|
|
21
|
+
return clampRetryAfterSeconds(Math.ceil((dateMs - nowMs) / 1000));
|
|
22
|
+
}
|
|
23
|
+
return XERO_MIN_RETRY_AFTER_SECONDS;
|
|
24
|
+
};
|
|
25
|
+
export const waitForRetryAfter = (seconds) => Effect.sleep(Duration.seconds(clampRetryAfterSeconds(seconds)));
|
|
26
|
+
export const mapHttpClientError = (error) => {
|
|
27
|
+
if (isTimeoutHttpClientError(error)) {
|
|
28
|
+
return xeroTimeoutError("Xero request timed out");
|
|
29
|
+
}
|
|
30
|
+
return xeroTransportError("Xero request failed");
|
|
31
|
+
};
|
|
32
|
+
const clampRetryAfterSeconds = (seconds) => {
|
|
33
|
+
if (seconds < XERO_MIN_RETRY_AFTER_SECONDS) {
|
|
34
|
+
return XERO_MIN_RETRY_AFTER_SECONDS;
|
|
35
|
+
}
|
|
36
|
+
if (seconds > XERO_MAX_RETRY_AFTER_SECONDS) {
|
|
37
|
+
return XERO_MAX_RETRY_AFTER_SECONDS;
|
|
38
|
+
}
|
|
39
|
+
return seconds;
|
|
40
|
+
};
|
|
41
|
+
const isTimeoutHttpClientError = (error) => {
|
|
42
|
+
const haystack = `${error.message} ${error._tag} ${String(error.reason)} ${stringifyCause(error.cause)}`;
|
|
43
|
+
const lower = haystack.toLowerCase();
|
|
44
|
+
return (lower.includes("timeout") ||
|
|
45
|
+
lower.includes("timed out") ||
|
|
46
|
+
lower.includes("aborted") ||
|
|
47
|
+
lower.includes("abort"));
|
|
48
|
+
};
|
|
49
|
+
const stringifyCause = (cause) => {
|
|
50
|
+
if (cause instanceof Error) {
|
|
51
|
+
return `${cause.name} ${cause.message}`;
|
|
52
|
+
}
|
|
53
|
+
return "";
|
|
54
|
+
};
|
|
55
|
+
//# sourceMappingURL=xero-http.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"xero-http.js","sourceRoot":"","sources":["../../src/lib/xero-http.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AACzC,OAAO,EAEL,gBAAgB,EAChB,kBAAkB,GACnB,MAAM,UAAU,CAAA;AAEjB,MAAM,4BAA4B,GAAG,CAAC,CAAA;AACtC,MAAM,4BAA4B,GAAG,EAAE,CAAA;AAEvC;;;;;GAKG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAG,CACpC,GAAuB,EACvB,KAAa,EACL,EAAE;IACV,IAAI,GAAG,KAAK,SAAS,IAAI,GAAG,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC;QAC3C,OAAO,4BAA4B,CAAA;IACrC,CAAC;IAED,MAAM,QAAQ,GAAG,MAAM,CAAC,GAAG,CAAC,CAAA;IAC5B,IAAI,MAAM,CAAC,QAAQ,CAAC,QAAQ,CAAC,IAAI,QAAQ,IAAI,CAAC,EAAE,CAAC;QAC/C,OAAO,sBAAsB,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAA;IACpD,CAAC;IAED,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IAC9B,IAAI,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC;QAC5B,OAAO,sBAAsB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,MAAM,GAAG,KAAK,CAAC,GAAG,IAAI,CAAC,CAAC,CAAA;IACnE,CAAC;IAED,OAAO,4BAA4B,CAAA;AACrC,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,OAAe,EAAuB,EAAE,CACxE,MAAM,CA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;IACzC,CAAC;IACD,OAAO,EAAE,CAAA;AACX,CAAC,CAAA"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { HttpClient } from "@effect/platform";
|
|
2
|
+
import { Context, Layer, Redacted } from "effect";
|
|
3
|
+
import { XeroInvoiceError } from "./errors.js";
|
|
4
|
+
import { type IssueAuthorisedSalesInvoiceInput } from "./identities.js";
|
|
5
|
+
import { XeroDraftInvoice } from "./xero-draft-invoice.js";
|
|
6
|
+
import { XeroInvoice } from "./xero-invoice.js";
|
|
7
|
+
import { XeroInvoiceLookup } from "./xero-invoice-lookup.js";
|
|
8
|
+
export { XERO_TOKEN_REFRESH_BUFFER_MS } from "./xero-token.js";
|
|
9
|
+
export declare const XERO_DEFAULT_SCOPES = "accounting.invoices accounting.contacts";
|
|
10
|
+
declare const XeroInvoiceConfig_base: Context.TagClass<XeroInvoiceConfig, "@processfocus/plugin-xero/XeroInvoiceConfig", {
|
|
11
|
+
readonly clientId: string;
|
|
12
|
+
readonly clientSecret: Redacted.Redacted<string>;
|
|
13
|
+
readonly scopes: string;
|
|
14
|
+
}>;
|
|
15
|
+
export declare class XeroInvoiceConfig extends XeroInvoiceConfig_base {
|
|
16
|
+
}
|
|
17
|
+
export declare const XeroInvoiceConfigFromEnv: Layer.Layer<XeroInvoiceConfig, XeroInvoiceError, never>;
|
|
18
|
+
export declare const xeroContactWritePayload: (input: IssueAuthorisedSalesInvoiceInput, name: string, contactId: string | undefined) => {
|
|
19
|
+
Contacts: {
|
|
20
|
+
ContactID?: string;
|
|
21
|
+
Name: string;
|
|
22
|
+
EmailAddress: string & import("effect/Brand").Brand<"EmailAddress">;
|
|
23
|
+
Addresses: {
|
|
24
|
+
AddressType: string;
|
|
25
|
+
AddressLine1: string;
|
|
26
|
+
AddressLine2?: string;
|
|
27
|
+
City: string;
|
|
28
|
+
Region?: string;
|
|
29
|
+
PostalCode?: string;
|
|
30
|
+
Country?: string;
|
|
31
|
+
}[];
|
|
32
|
+
}[];
|
|
33
|
+
};
|
|
34
|
+
export declare const xeroInvoiceWritePayload: (contactId: string, input: IssueAuthorisedSalesInvoiceInput) => {
|
|
35
|
+
Invoices: {
|
|
36
|
+
Type: string;
|
|
37
|
+
Status: string;
|
|
38
|
+
Contact: {
|
|
39
|
+
ContactID: string;
|
|
40
|
+
};
|
|
41
|
+
Date: string & import("effect/Brand").Brand<"CalendarDate">;
|
|
42
|
+
DueDate: string & import("effect/Brand").Brand<"CalendarDate">;
|
|
43
|
+
LineAmountTypes: string;
|
|
44
|
+
CurrencyCode: "NZD";
|
|
45
|
+
Reference: string & import("effect/Brand").Brand<"InvoiceReference">;
|
|
46
|
+
LineItems: {
|
|
47
|
+
ItemCode: string & import("effect/Brand").Brand<"ProductSku">;
|
|
48
|
+
Description: string & import("effect/Brand").Brand<"ProductSku">;
|
|
49
|
+
Quantity: number;
|
|
50
|
+
UnitAmount: string & import("effect/Brand").Brand<"ExactMoney">;
|
|
51
|
+
}[];
|
|
52
|
+
}[];
|
|
53
|
+
};
|
|
54
|
+
export declare const XeroInvoiceLive: Layer.Layer<XeroInvoice, XeroInvoiceError, HttpClient.HttpClient | XeroInvoiceConfig>;
|
|
55
|
+
/** Read-only companion: shares authentication/error handling, never writes. */
|
|
56
|
+
export declare const XeroInvoiceLookupLive: Layer.Layer<XeroInvoiceLookup, XeroInvoiceError, HttpClient.HttpClient | XeroInvoiceConfig>;
|
|
57
|
+
export declare const XeroDraftInvoiceLive: Layer.Layer<XeroDraftInvoice, XeroInvoiceError, HttpClient.HttpClient | XeroInvoiceConfig>;
|