@tallyui/connector-woocommerce 3.12.0 → 3.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +8 -2
- package/dist/index.js +72 -5
- package/package.json +3 -3
- package/src/customers.ts +61 -2
- package/src/reconcile/catalogue.ts +4 -3
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SyncContext, ServerCapabilities, StoreSettings, Customer, CustomerInput, TallyConnector, StoreOrder, OrderCreateEnvelope, CommandResult, ReconcileFeed, CatalogueReconcileAdapter, ProductTraits, CollectionSync, ReplicationAdapter, ConnectorUnauthorizedError } from '@tallyui/core';
|
|
1
|
+
import { SyncContext, ServerCapabilities, StoreSettings, Customer, CustomerInput, CustomerPatch, TallyConnector, StoreOrder, OrderCreateEnvelope, CommandResult, ReconcileFeed, CatalogueReconcileAdapter, ProductTraits, CollectionSync, ReplicationAdapter, ConnectorUnauthorizedError } from '@tallyui/core';
|
|
2
2
|
export { ConnectorUnauthorizedError } from '@tallyui/core';
|
|
3
3
|
import { RxJsonSchema } from 'rxdb';
|
|
4
4
|
|
|
@@ -19,6 +19,11 @@ type WooCustomerRow = {
|
|
|
19
19
|
phone?: string | null;
|
|
20
20
|
company?: string | null;
|
|
21
21
|
} | null;
|
|
22
|
+
meta_data?: {
|
|
23
|
+
key?: string;
|
|
24
|
+
value?: unknown;
|
|
25
|
+
}[];
|
|
26
|
+
_rxdb_revision?: unknown;
|
|
22
27
|
};
|
|
23
28
|
declare function toWooCustomer(raw: WooCustomerRow): Customer;
|
|
24
29
|
declare function createWooCustomers(options?: {
|
|
@@ -29,6 +34,7 @@ declare function createWooCustomers(options?: {
|
|
|
29
34
|
}): Promise<Customer[]>;
|
|
30
35
|
getCustomer(context: SyncContext, id: string): Promise<Customer | null>;
|
|
31
36
|
createCustomer(context: SyncContext, input: CustomerInput): Promise<Customer>;
|
|
37
|
+
updateCustomer(context: SyncContext, id: string, patch: CustomerPatch): Promise<Customer>;
|
|
32
38
|
emailReceipt(context: SyncContext, orderId: string, email: string, options?: {
|
|
33
39
|
saveToBilling?: boolean;
|
|
34
40
|
}): Promise<void>;
|
|
@@ -229,7 +235,7 @@ declare const wooReconcileFingerprint: (p: {
|
|
|
229
235
|
* fast-path request where the store offers it (#313), else page by page, with no date filter, and hands differences
|
|
230
236
|
* to `feed`, which reaches the collection only through the pull. `confirmGone` is the deletion proof: a by-id re-read.
|
|
231
237
|
*/
|
|
232
|
-
declare function wooCatalogueReconcile(feed: Pick<ReconcileFeed<any>, 'enqueue'>): CatalogueReconcileAdapter<any, number>;
|
|
238
|
+
declare function wooCatalogueReconcile(feed: Pick<ReconcileFeed<any>, 'enqueue'>): CatalogueReconcileAdapter<any, number | undefined>;
|
|
233
239
|
|
|
234
240
|
/** One part of X-WCPOS-Client as WCPOS keeps it: lowercase [a-z0-9._-], at most 32 characters. */
|
|
235
241
|
declare function wcposClientPart(part: string): string;
|
package/dist/index.js
CHANGED
|
@@ -676,7 +676,7 @@ function wooBulkListingUrl(baseUrl) {
|
|
|
676
676
|
var toEntries = (rows2) => rows2.filter((row) => Number.isInteger(row?.id)).map((row) => ({ key: String(row.id), fingerprint: wooReconcileFingerprint(row), remote: row.id }));
|
|
677
677
|
function wooCatalogueReconcile(feed) {
|
|
678
678
|
return {
|
|
679
|
-
async *fetchPages(context, from
|
|
679
|
+
async *fetchPages(context, from) {
|
|
680
680
|
const fast = await wooHasIdFastPath(context);
|
|
681
681
|
yield { entries: [], cursor: from };
|
|
682
682
|
if (fast) {
|
|
@@ -696,7 +696,7 @@ function wooCatalogueReconcile(feed) {
|
|
|
696
696
|
if (!failed) console.warn("WooCommerce catalogue reconcile: the fast path answered with something that is not a list; listing page by page.");
|
|
697
697
|
yield { entries: [], cursor: from };
|
|
698
698
|
}
|
|
699
|
-
for (let page = from; ; page++) {
|
|
699
|
+
for (let page = from ?? 1; ; page++) {
|
|
700
700
|
const params = new URLSearchParams({
|
|
701
701
|
per_page: String(PAGE_SIZE),
|
|
702
702
|
page: String(page),
|
|
@@ -826,7 +826,45 @@ function wooCouponReconcile(feed) {
|
|
|
826
826
|
}
|
|
827
827
|
|
|
828
828
|
// src/customers.ts
|
|
829
|
-
import {
|
|
829
|
+
import {
|
|
830
|
+
CustomerServiceError
|
|
831
|
+
} from "@tallyui/core";
|
|
832
|
+
var WOO_ADDRESS = [
|
|
833
|
+
["first_name", "firstName"],
|
|
834
|
+
["last_name", "lastName"],
|
|
835
|
+
["company", "company"],
|
|
836
|
+
["address_1", "address1"],
|
|
837
|
+
["address_2", "address2"],
|
|
838
|
+
["city", "city"],
|
|
839
|
+
["postcode", "postcode"],
|
|
840
|
+
["country", "country"],
|
|
841
|
+
["state", "state"],
|
|
842
|
+
["email", "email"],
|
|
843
|
+
["phone", "phone"]
|
|
844
|
+
];
|
|
845
|
+
function toWooAddress(address, kind) {
|
|
846
|
+
return Object.fromEntries(WOO_ADDRESS.filter(([key]) => kind === "billing" || key !== "email").map(([key, field]) => [key, address[field] ?? ""]));
|
|
847
|
+
}
|
|
848
|
+
function toWooPatch(patch) {
|
|
849
|
+
const contact = {
|
|
850
|
+
...patch.phone !== void 0 ? { phone: patch.phone } : {},
|
|
851
|
+
...patch.company !== void 0 ? { company: patch.company } : {}
|
|
852
|
+
};
|
|
853
|
+
const billing = patch.billing ? { ...toWooAddress(patch.billing, "billing"), ...contact } : contact;
|
|
854
|
+
return {
|
|
855
|
+
...patch.firstName !== void 0 ? { first_name: patch.firstName } : {},
|
|
856
|
+
...patch.lastName !== void 0 ? { last_name: patch.lastName } : {},
|
|
857
|
+
...patch.email !== void 0 ? { email: patch.email } : {},
|
|
858
|
+
...Object.keys(billing).length > 0 ? { billing } : {},
|
|
859
|
+
...patch.shipping ? { shipping: toWooAddress(patch.shipping, "shipping") } : {},
|
|
860
|
+
...patch.taxIds ? { tax_ids: patch.taxIds.map(({ type, value, country, label }) => ({
|
|
861
|
+
type,
|
|
862
|
+
value,
|
|
863
|
+
...country !== void 0 ? { country } : {},
|
|
864
|
+
...label !== void 0 ? { label } : {}
|
|
865
|
+
})) } : {}
|
|
866
|
+
};
|
|
867
|
+
}
|
|
830
868
|
function toWooCustomer(raw) {
|
|
831
869
|
const customer = { id: String(raw.id), name: String(raw.id) };
|
|
832
870
|
for (const [key, value] of [
|
|
@@ -864,7 +902,7 @@ async function customerRequest(context, route, init = {}) {
|
|
|
864
902
|
const body = await response.json().catch(() => void 0);
|
|
865
903
|
if (!response.ok) {
|
|
866
904
|
const invalid = [400, 404, 422].includes(response.status);
|
|
867
|
-
throw new CustomerServiceError(invalid ? "invalid" : "server", invalid ? body?.message ?? `HTTP ${response.status}` : `HTTP ${response.status}
|
|
905
|
+
throw new CustomerServiceError(invalid ? "invalid" : "server", invalid ? body?.message ?? `HTTP ${response.status}` : `HTTP ${response.status}`, { status: response.status });
|
|
868
906
|
}
|
|
869
907
|
return { status: response.status, body, headers: response.headers };
|
|
870
908
|
}
|
|
@@ -914,6 +952,35 @@ function createWooCustomers(options) {
|
|
|
914
952
|
if (status !== 200 && status !== 201 || !body?.document) throw new CustomerServiceError("server", "unexpected response");
|
|
915
953
|
return toWooCustomer(body.document);
|
|
916
954
|
},
|
|
955
|
+
// WCPOS's own write surface, as WCPOS v2's customer edit uses it: an `update` push of the changed Woo REST fields,
|
|
956
|
+
// keyed by the record's uuid and current revision, which only the customer read carries.
|
|
957
|
+
async updateCustomer(context, id, patch) {
|
|
958
|
+
if (!/^\d+$/.test(id)) throw new CustomerServiceError("invalid", "Unknown customer");
|
|
959
|
+
if (patch.shipping?.email) throw new CustomerServiceError("invalid", "WooCommerce shipping addresses have no email");
|
|
960
|
+
const params = new URLSearchParams({ include: id, per_page: "1", role: "all" });
|
|
961
|
+
const { body: rows2 } = await customerRequest(context, `customers?${params}`, { method: "GET" });
|
|
962
|
+
if (!Array.isArray(rows2)) throw new CustomerServiceError("server", "unexpected response");
|
|
963
|
+
const row = rows2.find((raw) => String(raw.id) === id);
|
|
964
|
+
if (!row) throw new CustomerServiceError("invalid", "Unknown customer");
|
|
965
|
+
const uuid = row.meta_data?.find((entry) => entry.key === "_woocommerce_pos_uuid")?.value;
|
|
966
|
+
if (typeof uuid !== "string" || !uuid) throw new CustomerServiceError("server", "unexpected response");
|
|
967
|
+
const payload = toWooPatch(patch);
|
|
968
|
+
if (Object.keys(payload).length === 0) return toWooCustomer(row);
|
|
969
|
+
const { status, body } = await customerRequest(context, "push/customers", {
|
|
970
|
+
method: "POST",
|
|
971
|
+
headers: { "Content-Type": "application/json" },
|
|
972
|
+
body: JSON.stringify({
|
|
973
|
+
mutationId: newId(),
|
|
974
|
+
operation: "update",
|
|
975
|
+
collection: "customers",
|
|
976
|
+
recordId: uuid,
|
|
977
|
+
baseRevision: typeof row._rxdb_revision === "string" ? row._rxdb_revision : null,
|
|
978
|
+
payload
|
|
979
|
+
})
|
|
980
|
+
});
|
|
981
|
+
if (status !== 200 || !body?.document) throw new CustomerServiceError("server", "unexpected response");
|
|
982
|
+
return toWooCustomer(body.document);
|
|
983
|
+
},
|
|
917
984
|
async emailReceipt(context, orderId, email, options2) {
|
|
918
985
|
if (!/^[1-9]\d*$/.test(orderId)) throw new CustomerServiceError("invalid", "Invalid order ID");
|
|
919
986
|
const { status, body } = await customerRequest(context, `orders/${encodeURIComponent(orderId)}/email`, {
|
|
@@ -1485,7 +1552,7 @@ async function readWooCapabilities(context) {
|
|
|
1485
1552
|
}
|
|
1486
1553
|
|
|
1487
1554
|
// package.json
|
|
1488
|
-
var version = "3.
|
|
1555
|
+
var version = "3.13.0";
|
|
1489
1556
|
|
|
1490
1557
|
// src/index.ts
|
|
1491
1558
|
import { ConnectorUnauthorizedError as ConnectorUnauthorizedError4 } from "@tallyui/core";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tallyui/connector-woocommerce",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.13.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "WooCommerce connector for Tally UI",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -27,10 +27,10 @@
|
|
|
27
27
|
"directory": "connectors/woocommerce"
|
|
28
28
|
},
|
|
29
29
|
"peerDependencies": {
|
|
30
|
-
"@tallyui/core": "^3.
|
|
30
|
+
"@tallyui/core": "^3.13.0"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
|
-
"@tallyui/database": "3.
|
|
33
|
+
"@tallyui/database": "3.13.0",
|
|
34
34
|
"rxdb": "17.5.0"
|
|
35
35
|
},
|
|
36
36
|
"scripts": {
|
package/src/customers.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
CustomerServiceError, type Customer, type CustomerAddress, type CustomerInput, type CustomerPatch, type SyncContext,
|
|
3
|
+
} from '@tallyui/core';
|
|
2
4
|
import { checkResponse } from './replication/products';
|
|
3
5
|
|
|
4
6
|
type WooCustomerRow = {
|
|
@@ -10,8 +12,40 @@ type WooCustomerRow = {
|
|
|
10
12
|
first_name?: string | null; last_name?: string | null; email?: string | null;
|
|
11
13
|
phone?: string | null; company?: string | null;
|
|
12
14
|
} | null;
|
|
15
|
+
meta_data?: { key?: string; value?: unknown }[];
|
|
16
|
+
_rxdb_revision?: unknown;
|
|
13
17
|
};
|
|
14
18
|
|
|
19
|
+
// Woo's address keys and the CustomerAddress field each comes from; shipping has no email.
|
|
20
|
+
const WOO_ADDRESS = [
|
|
21
|
+
['first_name', 'firstName'], ['last_name', 'lastName'], ['company', 'company'], ['address_1', 'address1'],
|
|
22
|
+
['address_2', 'address2'], ['city', 'city'], ['postcode', 'postcode'], ['country', 'country'], ['state', 'state'],
|
|
23
|
+
['email', 'email'], ['phone', 'phone'],
|
|
24
|
+
] as const;
|
|
25
|
+
|
|
26
|
+
/** The whole Woo address, '' for an absent field, so a patch replaces the stored address rather than merging into it. */
|
|
27
|
+
function toWooAddress(address: CustomerAddress, kind: 'billing' | 'shipping'): Record<string, string> {
|
|
28
|
+
return Object.fromEntries(WOO_ADDRESS.filter(([key]) => kind === 'billing' || key !== 'email')
|
|
29
|
+
.map(([key, field]) => [key, address[field] ?? '']));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function toWooPatch(patch: CustomerPatch): Record<string, unknown> {
|
|
33
|
+
const contact = {
|
|
34
|
+
...(patch.phone !== undefined ? { phone: patch.phone } : {}), ...(patch.company !== undefined ? { company: patch.company } : {}),
|
|
35
|
+
};
|
|
36
|
+
const billing = patch.billing ? { ...toWooAddress(patch.billing, 'billing'), ...contact } : contact;
|
|
37
|
+
return {
|
|
38
|
+
...(patch.firstName !== undefined ? { first_name: patch.firstName } : {}),
|
|
39
|
+
...(patch.lastName !== undefined ? { last_name: patch.lastName } : {}),
|
|
40
|
+
...(patch.email !== undefined ? { email: patch.email } : {}),
|
|
41
|
+
...(Object.keys(billing).length > 0 ? { billing } : {}),
|
|
42
|
+
...(patch.shipping ? { shipping: toWooAddress(patch.shipping, 'shipping') } : {}),
|
|
43
|
+
...(patch.taxIds ? { tax_ids: patch.taxIds.map(({ type, value, country, label }) => ({
|
|
44
|
+
type, value, ...(country !== undefined ? { country } : {}), ...(label !== undefined ? { label } : {}),
|
|
45
|
+
})) } : {}),
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
15
49
|
export function toWooCustomer(raw: WooCustomerRow): Customer {
|
|
16
50
|
const customer: Customer = { id: String(raw.id), name: String(raw.id) };
|
|
17
51
|
for (const [key, value] of [
|
|
@@ -48,7 +82,7 @@ export async function customerRequest(context: SyncContext, route: string, init:
|
|
|
48
82
|
const body = await response.json().catch(() => undefined);
|
|
49
83
|
if (!response.ok) {
|
|
50
84
|
const invalid = [400, 404, 422].includes(response.status);
|
|
51
|
-
throw new CustomerServiceError(invalid ? 'invalid' : 'server', invalid ? body?.message ?? `HTTP ${response.status}` : `HTTP ${response.status}
|
|
85
|
+
throw new CustomerServiceError(invalid ? 'invalid' : 'server', invalid ? body?.message ?? `HTTP ${response.status}` : `HTTP ${response.status}`, { status: response.status });
|
|
52
86
|
}
|
|
53
87
|
return { status: response.status, body, headers: response.headers };
|
|
54
88
|
}
|
|
@@ -95,6 +129,31 @@ export function createWooCustomers(options?: { newId?: () => string }) {
|
|
|
95
129
|
return toWooCustomer(body.document);
|
|
96
130
|
},
|
|
97
131
|
|
|
132
|
+
// WCPOS's own write surface, as WCPOS v2's customer edit uses it: an `update` push of the changed Woo REST fields,
|
|
133
|
+
// keyed by the record's uuid and current revision, which only the customer read carries.
|
|
134
|
+
async updateCustomer(context: SyncContext, id: string, patch: CustomerPatch): Promise<Customer> {
|
|
135
|
+
if (!/^\d+$/.test(id)) throw new CustomerServiceError('invalid', 'Unknown customer');
|
|
136
|
+
if (patch.shipping?.email) throw new CustomerServiceError('invalid', 'WooCommerce shipping addresses have no email');
|
|
137
|
+
const params = new URLSearchParams({ include: id, per_page: '1', role: 'all' });
|
|
138
|
+
const { body: rows } = await customerRequest(context, `customers?${params}`, { method: 'GET' });
|
|
139
|
+
if (!Array.isArray(rows)) throw new CustomerServiceError('server', 'unexpected response');
|
|
140
|
+
const row: WooCustomerRow | undefined = rows.find((raw: WooCustomerRow) => String(raw.id) === id);
|
|
141
|
+
if (!row) throw new CustomerServiceError('invalid', 'Unknown customer');
|
|
142
|
+
const uuid = row.meta_data?.find((entry) => entry.key === '_woocommerce_pos_uuid')?.value;
|
|
143
|
+
if (typeof uuid !== 'string' || !uuid) throw new CustomerServiceError('server', 'unexpected response');
|
|
144
|
+
const payload = toWooPatch(patch);
|
|
145
|
+
if (Object.keys(payload).length === 0) return toWooCustomer(row);
|
|
146
|
+
const { status, body } = await customerRequest(context, 'push/customers', {
|
|
147
|
+
method: 'POST', headers: { 'Content-Type': 'application/json' },
|
|
148
|
+
body: JSON.stringify({
|
|
149
|
+
mutationId: newId(), operation: 'update', collection: 'customers', recordId: uuid,
|
|
150
|
+
baseRevision: typeof row._rxdb_revision === 'string' ? row._rxdb_revision : null, payload,
|
|
151
|
+
}),
|
|
152
|
+
});
|
|
153
|
+
if (status !== 200 || !body?.document) throw new CustomerServiceError('server', 'unexpected response');
|
|
154
|
+
return toWooCustomer(body.document);
|
|
155
|
+
},
|
|
156
|
+
|
|
98
157
|
async emailReceipt(context: SyncContext, orderId: string, email: string, options?: { saveToBilling?: boolean }): Promise<void> {
|
|
99
158
|
if (!/^[1-9]\d*$/.test(orderId)) throw new CustomerServiceError('invalid', 'Invalid order ID');
|
|
100
159
|
const { status, body } = await customerRequest(context, `orders/${encodeURIComponent(orderId)}/email`, {
|
|
@@ -62,11 +62,12 @@ const toEntries = (rows: any[]) => rows.filter((row) => Number.isInteger(row?.id
|
|
|
62
62
|
* fast-path request where the store offers it (#313), else page by page, with no date filter, and hands differences
|
|
63
63
|
* to `feed`, which reaches the collection only through the pull. `confirmGone` is the deletion proof: a by-id re-read.
|
|
64
64
|
*/
|
|
65
|
-
export function wooCatalogueReconcile(feed: Pick<ReconcileFeed<any>, 'enqueue'>): CatalogueReconcileAdapter<any, number> {
|
|
65
|
+
export function wooCatalogueReconcile(feed: Pick<ReconcileFeed<any>, 'enqueue'>): CatalogueReconcileAdapter<any, number | undefined> {
|
|
66
66
|
return {
|
|
67
|
-
async *fetchPages(context, from
|
|
67
|
+
async *fetchPages(context, from) {
|
|
68
68
|
// One status read per pass, yielded as an empty page so that it takes its own budget slot.
|
|
69
69
|
const fast = await wooHasIdFastPath(context);
|
|
70
|
+
// A fresh pass carries no cursor, so a failure before the first listing page restarts it uninterrupted (#376).
|
|
70
71
|
yield { entries: [], cursor: from };
|
|
71
72
|
if (fast) {
|
|
72
73
|
// The whole catalogue in one request, so `from` is moot. A store that refuses it (a store scope answers 400)
|
|
@@ -91,7 +92,7 @@ export function wooCatalogueReconcile(feed: Pick<ReconcileFeed<any>, 'enqueue'>)
|
|
|
91
92
|
}
|
|
92
93
|
// status=publish: a product that is not published is absent, so it becomes a deletion candidate and
|
|
93
94
|
// confirmGone decides; drafts cost no daily refetch.
|
|
94
|
-
for (let page = from; ; page++) {
|
|
95
|
+
for (let page = from ?? 1; ; page++) {
|
|
95
96
|
const params = new URLSearchParams({
|
|
96
97
|
per_page: String(PAGE_SIZE), page: String(page), orderby: 'id', order: 'asc', status: 'publish', _fields: LISTING_FIELDS,
|
|
97
98
|
});
|