@tallyui/connector-medusa 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 +17 -2
- package/dist/index.js +61 -6
- package/package.json +3 -3
- package/src/customers.ts +68 -8
- package/src/index.ts +6 -4
- package/src/reconcile/calculated-prices.ts +3 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as _tallyui_core from '@tallyui/core';
|
|
2
|
-
import { ProductTraits, CollectionSync, ReplicationAdapter, StockReconcileAdapter, SyncContext, StoreSettingsChoice, StoreSettings, CustomerInput, Customer, TallyConnector, ConnectorAuth, ServerCapabilities } from '@tallyui/core';
|
|
2
|
+
import { ProductTraits, CollectionSync, ReplicationAdapter, StockReconcileAdapter, SyncContext, StoreSettingsChoice, StoreSettings, CustomerInput, Customer, CustomerPatch, TallyConnector, ConnectorAuth, ServerCapabilities } from '@tallyui/core';
|
|
3
3
|
export { ConnectorUnauthorizedError } from '@tallyui/core';
|
|
4
4
|
import { RxJsonSchema } from 'rxdb';
|
|
5
5
|
|
|
@@ -130,9 +130,15 @@ declare const medusaStockReconcile: StockReconcileAdapter;
|
|
|
130
130
|
*/
|
|
131
131
|
declare const medusaStoreSettings: (context: SyncContext, choice?: StoreSettingsChoice) => Promise<StoreSettings>;
|
|
132
132
|
|
|
133
|
+
type MedusaAddress = {
|
|
134
|
+
id: string;
|
|
135
|
+
is_default_billing?: boolean;
|
|
136
|
+
is_default_shipping?: boolean;
|
|
137
|
+
} & Record<string, unknown>;
|
|
133
138
|
type MedusaCustomer = {
|
|
134
139
|
id: string;
|
|
135
140
|
deleted_at?: string | null;
|
|
141
|
+
addresses?: MedusaAddress[];
|
|
136
142
|
} & Record<string, unknown>;
|
|
137
143
|
type CustomerInit = {
|
|
138
144
|
signal?: AbortSignal;
|
|
@@ -144,6 +150,14 @@ declare function searchMedusaCustomers(baseUrl: string, headers: Record<string,
|
|
|
144
150
|
}): Promise<Customer[]>;
|
|
145
151
|
declare function createMedusaCustomer(baseUrl: string, headers: Record<string, string>, input: CustomerInput, init?: CustomerInit): Promise<Customer>;
|
|
146
152
|
declare function getMedusaCustomer(baseUrl: string, headers: Record<string, string>, id: string, init?: CustomerInit): Promise<Customer | null>;
|
|
153
|
+
/**
|
|
154
|
+
* Medusa's own way: `POST /admin/customers/{id}` for the customer's fields, then the address book for billing and
|
|
155
|
+
* shipping (the addresses flagged `is_default_billing` / `is_default_shipping`), each replaced whole. An address that
|
|
156
|
+
* is both defaults is split rather than edited for one of them. `taxIds` is not sent: Medusa customers have no tax
|
|
157
|
+
* ids, and createMedusaCustomer drops them too. The customer POST runs first, so an unknown id or a refused field
|
|
158
|
+
* changes nothing; an address refused after it leaves the customer's fields saved.
|
|
159
|
+
*/
|
|
160
|
+
declare function updateMedusaCustomer(baseUrl: string, headers: Record<string, string>, id: string, patch: CustomerPatch, init?: CustomerInit): Promise<Customer>;
|
|
147
161
|
|
|
148
162
|
/**
|
|
149
163
|
* Fills each variant's `calculated_price` from the store API, priced with
|
|
@@ -170,6 +184,7 @@ declare const medusaSearchCustomers: (context: SyncContext, query: string, optio
|
|
|
170
184
|
}) => Promise<_tallyui_core.Customer[]>;
|
|
171
185
|
declare const medusaCreateCustomer: (context: SyncContext, input: CustomerInput) => Promise<_tallyui_core.Customer>;
|
|
172
186
|
declare const medusaGetCustomer: (context: SyncContext, id: string) => Promise<_tallyui_core.Customer | null>;
|
|
187
|
+
declare const medusaUpdateCustomer: (context: SyncContext, id: string, patch: CustomerPatch) => Promise<_tallyui_core.Customer>;
|
|
173
188
|
declare const medusaAdminUserAuth: ConnectorAuth;
|
|
174
189
|
/**
|
|
175
190
|
* MedusaJS v2 connector for Tally UI, with a secret API key. Build one per store session, anew on each
|
|
@@ -202,4 +217,4 @@ declare const medusaConnector: TallyConnector;
|
|
|
202
217
|
*/
|
|
203
218
|
declare const medusaAdminUserConnector: TallyConnector;
|
|
204
219
|
|
|
205
|
-
export { MEDUSA_CALCULATED_PRICE_RECONCILE_INTERVAL_MS, type MedusaCalculatedPrice, type MedusaProductDocument, type MedusaVariantDocument, createMedusaAdminUserConnector, createMedusaConnector, createMedusaCustomer, createMedusaVariantFeedReplication, getMedusaCustomer, medusaAdminUserAuth, medusaAdminUserConnector, medusaCapabilities, medusaConnector, medusaCreateCustomer, medusaGetCustomer, medusaProductReplication, medusaProductSchema, medusaProductSync, medusaProductTraits, medusaSearchCustomers, medusaSecretKeyAuth, medusaSignIn, medusaStockReconcile, medusaStoreSettings, searchMedusaCustomers, toCustomer, withCalculatedPrices };
|
|
220
|
+
export { MEDUSA_CALCULATED_PRICE_RECONCILE_INTERVAL_MS, type MedusaCalculatedPrice, type MedusaProductDocument, type MedusaVariantDocument, createMedusaAdminUserConnector, createMedusaConnector, createMedusaCustomer, createMedusaVariantFeedReplication, getMedusaCustomer, medusaAdminUserAuth, medusaAdminUserConnector, medusaCapabilities, medusaConnector, medusaCreateCustomer, medusaGetCustomer, medusaProductReplication, medusaProductSchema, medusaProductSync, medusaProductTraits, medusaSearchCustomers, medusaSecretKeyAuth, medusaSignIn, medusaStockReconcile, medusaStoreSettings, medusaUpdateCustomer, searchMedusaCustomers, toCustomer, updateMedusaCustomer, withCalculatedPrices };
|
package/dist/index.js
CHANGED
|
@@ -26,6 +26,26 @@ async function readCapabilities(baseUrl, headers, init = {}) {
|
|
|
26
26
|
|
|
27
27
|
// src/customers.ts
|
|
28
28
|
import { ConnectorUnauthorizedError, CustomerServiceError } from "@tallyui/core";
|
|
29
|
+
var ADDRESS_FIELDS = [
|
|
30
|
+
["firstName", "first_name"],
|
|
31
|
+
["lastName", "last_name"],
|
|
32
|
+
["company", "company"],
|
|
33
|
+
["address1", "address_1"],
|
|
34
|
+
["address2", "address_2"],
|
|
35
|
+
["city", "city"],
|
|
36
|
+
["state", "province"],
|
|
37
|
+
["postcode", "postal_code"],
|
|
38
|
+
["country", "country_code"],
|
|
39
|
+
["phone", "phone"]
|
|
40
|
+
];
|
|
41
|
+
function toAddress(raw) {
|
|
42
|
+
const address = {};
|
|
43
|
+
for (const [key, field] of ADDRESS_FIELDS) {
|
|
44
|
+
const value = raw[field];
|
|
45
|
+
if (typeof value === "string" && value.length > 0) address[key] = key === "country" ? value.toUpperCase() : value;
|
|
46
|
+
}
|
|
47
|
+
return address;
|
|
48
|
+
}
|
|
29
49
|
function toCustomer(raw) {
|
|
30
50
|
const customer = { id: raw.id, name: raw.id };
|
|
31
51
|
for (const [key, value] of [
|
|
@@ -38,6 +58,10 @@ function toCustomer(raw) {
|
|
|
38
58
|
if (typeof value === "string" && value.length > 0) customer[key] = value;
|
|
39
59
|
}
|
|
40
60
|
customer.name = [customer.firstName, customer.lastName].filter(Boolean).join(" ") || customer.company || customer.email || customer.id;
|
|
61
|
+
const billing = raw.addresses?.find((address) => address.is_default_billing);
|
|
62
|
+
const shipping = raw.addresses?.find((address) => address.is_default_shipping);
|
|
63
|
+
if (billing) customer.billing = toAddress(billing);
|
|
64
|
+
if (shipping) customer.shipping = toAddress(shipping);
|
|
41
65
|
return customer;
|
|
42
66
|
}
|
|
43
67
|
function errorMessage(error) {
|
|
@@ -47,7 +71,7 @@ function errorMessage(error) {
|
|
|
47
71
|
return String(error);
|
|
48
72
|
}
|
|
49
73
|
async function customerRequest(url, key, init) {
|
|
50
|
-
const { fetch: doFetch = fetch, ...requestInit } = init;
|
|
74
|
+
const { fetch: doFetch = fetch, missing, ...requestInit } = init;
|
|
51
75
|
let res;
|
|
52
76
|
try {
|
|
53
77
|
res = await doFetch(url, requestInit);
|
|
@@ -56,15 +80,16 @@ async function customerRequest(url, key, init) {
|
|
|
56
80
|
}
|
|
57
81
|
if (res.status === 401 || res.status === 403) throw new ConnectorUnauthorizedError(`Medusa rejected the credentials (HTTP ${res.status})`, res.status);
|
|
58
82
|
if (res.status === 404 && key === "customer" && init.method === "GET") return null;
|
|
59
|
-
|
|
83
|
+
const refused = res.status === 400 || res.status === 404 && missing === "invalid";
|
|
84
|
+
if (!res.ok && !refused) throw new CustomerServiceError("server", `HTTP ${res.status}`, { status: res.status });
|
|
60
85
|
let body;
|
|
61
86
|
try {
|
|
62
87
|
body = await res.json();
|
|
63
88
|
} catch {
|
|
64
|
-
throw new CustomerServiceError(
|
|
89
|
+
throw new CustomerServiceError(refused ? "invalid" : "server", refused ? `HTTP ${res.status}` : "unexpected response", { status: res.status });
|
|
65
90
|
}
|
|
66
|
-
if (
|
|
67
|
-
if (key === "customers" ? !Array.isArray(body?.[key]) : !body?.[key]) throw new CustomerServiceError("server", "unexpected response");
|
|
91
|
+
if (refused) throw new CustomerServiceError("invalid", body?.message ?? `HTTP ${res.status}`, { status: res.status });
|
|
92
|
+
if (key === "customers" ? !Array.isArray(body?.[key]) : !body?.[key]) throw new CustomerServiceError("server", "unexpected response", { status: res.status });
|
|
68
93
|
return body[key];
|
|
69
94
|
}
|
|
70
95
|
async function searchMedusaCustomers(baseUrl, headers, query, init = {}) {
|
|
@@ -88,6 +113,32 @@ async function getMedusaCustomer(baseUrl, headers, id, init = {}) {
|
|
|
88
113
|
const customer = await customerRequest(`${baseUrl}/admin/customers/${encodeURIComponent(id)}`, "customer", { ...init, method: "GET", headers });
|
|
89
114
|
return customer === null || customer.deleted_at != null ? null : toCustomer(customer);
|
|
90
115
|
}
|
|
116
|
+
var blankToNull = (value) => value ? value : null;
|
|
117
|
+
function toMedusaAddress(address) {
|
|
118
|
+
return Object.fromEntries(ADDRESS_FIELDS.map(([key, field]) => [field, blankToNull(key === "country" ? address.country?.toLowerCase() : address[key])]));
|
|
119
|
+
}
|
|
120
|
+
async function updateMedusaCustomer(baseUrl, headers, id, patch, init = {}) {
|
|
121
|
+
const url = `${baseUrl}/admin/customers/${encodeURIComponent(id)}`;
|
|
122
|
+
const post = (path, body) => customerRequest(`${url}${path}?fields=*addresses`, "customer", {
|
|
123
|
+
...init,
|
|
124
|
+
method: "POST",
|
|
125
|
+
headers: { ...headers, "Content-Type": "application/json" },
|
|
126
|
+
body: JSON.stringify(body),
|
|
127
|
+
missing: "invalid"
|
|
128
|
+
});
|
|
129
|
+
const fields = [["email", "email"], ["firstName", "first_name"], ["lastName", "last_name"], ["phone", "phone"], ["company", "company_name"]];
|
|
130
|
+
let customer = await post("", Object.fromEntries(fields.filter(([key]) => patch[key] !== void 0).map(([key, field]) => [field, blankToNull(patch[key])])));
|
|
131
|
+
const billing = customer.addresses?.find((address) => address.is_default_billing);
|
|
132
|
+
const shipping = customer.addresses?.find((address) => address.is_default_shipping);
|
|
133
|
+
const shared = billing !== void 0 && billing.id === shipping?.id;
|
|
134
|
+
if (patch.billing) {
|
|
135
|
+
customer = billing && (!shared || patch.shipping) ? await post(`/addresses/${encodeURIComponent(billing.id)}`, toMedusaAddress(patch.billing)) : await post("/addresses", { ...toMedusaAddress(patch.billing), is_default_billing: true });
|
|
136
|
+
}
|
|
137
|
+
if (patch.shipping) {
|
|
138
|
+
customer = shipping && !shared ? await post(`/addresses/${encodeURIComponent(shipping.id)}`, toMedusaAddress(patch.shipping)) : await post("/addresses", { ...toMedusaAddress(patch.shipping), is_default_shipping: true });
|
|
139
|
+
}
|
|
140
|
+
return toCustomer(customer);
|
|
141
|
+
}
|
|
91
142
|
|
|
92
143
|
// src/schemas/products.ts
|
|
93
144
|
var medusaProductSchema = {
|
|
@@ -1027,6 +1078,7 @@ var medusaCapabilities = (context) => readCapabilities(context.baseUrl, context.
|
|
|
1027
1078
|
var medusaSearchCustomers = (context, query, options) => searchMedusaCustomers(context.baseUrl, context.headers, query, { ...options, signal: context.signal });
|
|
1028
1079
|
var medusaCreateCustomer = (context, input) => createMedusaCustomer(context.baseUrl, context.headers, input, { signal: context.signal });
|
|
1029
1080
|
var medusaGetCustomer = (context, id) => getMedusaCustomer(context.baseUrl, context.headers, id, { signal: context.signal });
|
|
1081
|
+
var medusaUpdateCustomer = (context, id, patch) => updateMedusaCustomer(context.baseUrl, context.headers, id, patch, { signal: context.signal });
|
|
1030
1082
|
var medusaAdminUserAuth = {
|
|
1031
1083
|
type: "Medusa admin user (Bearer JWT)",
|
|
1032
1084
|
fields: [
|
|
@@ -1086,7 +1138,8 @@ var createMedusaAdminUserConnector = () => ({
|
|
|
1086
1138
|
capabilities: medusaCapabilities,
|
|
1087
1139
|
searchCustomers: medusaSearchCustomers,
|
|
1088
1140
|
createCustomer: medusaCreateCustomer,
|
|
1089
|
-
getCustomer: medusaGetCustomer
|
|
1141
|
+
getCustomer: medusaGetCustomer,
|
|
1142
|
+
updateCustomer: medusaUpdateCustomer
|
|
1090
1143
|
});
|
|
1091
1144
|
var medusaConnector = createMedusaConnector();
|
|
1092
1145
|
var medusaAdminUserConnector = createMedusaAdminUserConnector();
|
|
@@ -1113,7 +1166,9 @@ export {
|
|
|
1113
1166
|
medusaSignIn,
|
|
1114
1167
|
medusaStockReconcile,
|
|
1115
1168
|
medusaStoreSettings,
|
|
1169
|
+
medusaUpdateCustomer,
|
|
1116
1170
|
searchMedusaCustomers,
|
|
1117
1171
|
toCustomer,
|
|
1172
|
+
updateMedusaCustomer,
|
|
1118
1173
|
withCalculatedPrices
|
|
1119
1174
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tallyui/connector-medusa",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.13.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "MedusaJS v2 connector for Tally UI",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -28,10 +28,10 @@
|
|
|
28
28
|
"directory": "connectors/medusa"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
31
|
-
"@tallyui/core": "^3.
|
|
31
|
+
"@tallyui/core": "^3.13.0"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
|
-
"@tallyui/database": "3.
|
|
34
|
+
"@tallyui/database": "3.13.0",
|
|
35
35
|
"rxdb": "17.5.0"
|
|
36
36
|
},
|
|
37
37
|
"scripts": {
|
package/src/customers.ts
CHANGED
|
@@ -1,8 +1,25 @@
|
|
|
1
|
-
import { ConnectorUnauthorizedError, CustomerServiceError, type Customer, type CustomerInput } from '@tallyui/core';
|
|
1
|
+
import { ConnectorUnauthorizedError, CustomerServiceError, type Customer, type CustomerAddress, type CustomerInput, type CustomerPatch } from '@tallyui/core';
|
|
2
2
|
|
|
3
|
-
type
|
|
3
|
+
type MedusaAddress = { id: string; is_default_billing?: boolean; is_default_shipping?: boolean } & Record<string, unknown>;
|
|
4
|
+
type MedusaCustomer = { id: string; deleted_at?: string | null; addresses?: MedusaAddress[] } & Record<string, unknown>;
|
|
4
5
|
type CustomerInit = { signal?: AbortSignal; fetch?: typeof fetch };
|
|
5
6
|
|
|
7
|
+
/** CustomerAddress key → Medusa customer-address field (Medusa addresses have no email). */
|
|
8
|
+
const ADDRESS_FIELDS = [
|
|
9
|
+
['firstName', 'first_name'], ['lastName', 'last_name'], ['company', 'company'], ['address1', 'address_1'],
|
|
10
|
+
['address2', 'address_2'], ['city', 'city'], ['state', 'province'], ['postcode', 'postal_code'],
|
|
11
|
+
['country', 'country_code'], ['phone', 'phone'],
|
|
12
|
+
] as const;
|
|
13
|
+
|
|
14
|
+
function toAddress(raw: MedusaAddress): CustomerAddress {
|
|
15
|
+
const address: CustomerAddress = {};
|
|
16
|
+
for (const [key, field] of ADDRESS_FIELDS) {
|
|
17
|
+
const value = raw[field];
|
|
18
|
+
if (typeof value === 'string' && value.length > 0) address[key] = key === 'country' ? value.toUpperCase() : value;
|
|
19
|
+
}
|
|
20
|
+
return address;
|
|
21
|
+
}
|
|
22
|
+
|
|
6
23
|
export function toCustomer(raw: MedusaCustomer): Customer {
|
|
7
24
|
const customer: Customer = { id: raw.id, name: raw.id };
|
|
8
25
|
for (const [key, value] of [
|
|
@@ -12,6 +29,11 @@ export function toCustomer(raw: MedusaCustomer): Customer {
|
|
|
12
29
|
if (typeof value === 'string' && value.length > 0) customer[key] = value;
|
|
13
30
|
}
|
|
14
31
|
customer.name = [customer.firstName, customer.lastName].filter(Boolean).join(' ') || customer.company || customer.email || customer.id;
|
|
32
|
+
// Medusa's billing and shipping are the address-book entries flagged as defaults; only present when asked for (?fields=*addresses).
|
|
33
|
+
const billing = raw.addresses?.find((address) => address.is_default_billing);
|
|
34
|
+
const shipping = raw.addresses?.find((address) => address.is_default_shipping);
|
|
35
|
+
if (billing) customer.billing = toAddress(billing);
|
|
36
|
+
if (shipping) customer.shipping = toAddress(shipping);
|
|
15
37
|
return customer;
|
|
16
38
|
}
|
|
17
39
|
|
|
@@ -22,8 +44,9 @@ function errorMessage(error: unknown): string {
|
|
|
22
44
|
return String(error);
|
|
23
45
|
}
|
|
24
46
|
|
|
25
|
-
|
|
26
|
-
|
|
47
|
+
/** `missing: 'invalid'` refuses a 404 like a 400 (update: an id the store doesn't have). */
|
|
48
|
+
async function customerRequest(url: string, key: 'customers' | 'customer', init: RequestInit & CustomerInit & { missing?: 'invalid' }) {
|
|
49
|
+
const { fetch: doFetch = fetch, missing, ...requestInit } = init;
|
|
27
50
|
let res: Response;
|
|
28
51
|
try {
|
|
29
52
|
res = await doFetch(url, requestInit);
|
|
@@ -32,15 +55,16 @@ async function customerRequest(url: string, key: 'customers' | 'customer', init:
|
|
|
32
55
|
}
|
|
33
56
|
if (res.status === 401 || res.status === 403) throw new ConnectorUnauthorizedError(`Medusa rejected the credentials (HTTP ${res.status})`, res.status as 401 | 403);
|
|
34
57
|
if (res.status === 404 && key === 'customer' && init.method === 'GET') return null;
|
|
35
|
-
|
|
58
|
+
const refused = res.status === 400 || (res.status === 404 && missing === 'invalid');
|
|
59
|
+
if (!res.ok && !refused) throw new CustomerServiceError('server', `HTTP ${res.status}`, { status: res.status });
|
|
36
60
|
let body;
|
|
37
61
|
try {
|
|
38
62
|
body = await res.json();
|
|
39
63
|
} catch {
|
|
40
|
-
throw new CustomerServiceError(
|
|
64
|
+
throw new CustomerServiceError(refused ? 'invalid' : 'server', refused ? `HTTP ${res.status}` : 'unexpected response', { status: res.status });
|
|
41
65
|
}
|
|
42
|
-
if (
|
|
43
|
-
if (key === 'customers' ? !Array.isArray(body?.[key]) : !body?.[key]) throw new CustomerServiceError('server', 'unexpected response');
|
|
66
|
+
if (refused) throw new CustomerServiceError('invalid', body?.message ?? `HTTP ${res.status}`, { status: res.status });
|
|
67
|
+
if (key === 'customers' ? !Array.isArray(body?.[key]) : !body?.[key]) throw new CustomerServiceError('server', 'unexpected response', { status: res.status });
|
|
44
68
|
return body[key];
|
|
45
69
|
}
|
|
46
70
|
|
|
@@ -67,3 +91,39 @@ export async function getMedusaCustomer(baseUrl: string, headers: Record<string,
|
|
|
67
91
|
const customer: MedusaCustomer | null = await customerRequest(`${baseUrl}/admin/customers/${encodeURIComponent(id)}`, 'customer', { ...init, method: 'GET', headers });
|
|
68
92
|
return customer === null || customer.deleted_at != null ? null : toCustomer(customer);
|
|
69
93
|
}
|
|
94
|
+
|
|
95
|
+
const blankToNull = (value: string | undefined) => (value ? value : null);
|
|
96
|
+
|
|
97
|
+
function toMedusaAddress(address: CustomerAddress): Record<string, string | null> {
|
|
98
|
+
return Object.fromEntries(ADDRESS_FIELDS.map(([key, field]) => [field, blankToNull(key === 'country' ? address.country?.toLowerCase() : address[key])]));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Medusa's own way: `POST /admin/customers/{id}` for the customer's fields, then the address book for billing and
|
|
103
|
+
* shipping (the addresses flagged `is_default_billing` / `is_default_shipping`), each replaced whole. An address that
|
|
104
|
+
* is both defaults is split rather than edited for one of them. `taxIds` is not sent: Medusa customers have no tax
|
|
105
|
+
* ids, and createMedusaCustomer drops them too. The customer POST runs first, so an unknown id or a refused field
|
|
106
|
+
* changes nothing; an address refused after it leaves the customer's fields saved.
|
|
107
|
+
*/
|
|
108
|
+
export async function updateMedusaCustomer(baseUrl: string, headers: Record<string, string>, id: string, patch: CustomerPatch, init: CustomerInit = {}): Promise<Customer> {
|
|
109
|
+
const url = `${baseUrl}/admin/customers/${encodeURIComponent(id)}`;
|
|
110
|
+
const post = (path: string, body: object): Promise<MedusaCustomer> => customerRequest(`${url}${path}?fields=*addresses`, 'customer', {
|
|
111
|
+
...init, method: 'POST', headers: { ...headers, 'Content-Type': 'application/json' }, body: JSON.stringify(body), missing: 'invalid',
|
|
112
|
+
});
|
|
113
|
+
const fields = [['email', 'email'], ['firstName', 'first_name'], ['lastName', 'last_name'], ['phone', 'phone'], ['company', 'company_name']] as const;
|
|
114
|
+
let customer = await post('', Object.fromEntries(fields.filter(([key]) => patch[key] !== undefined).map(([key, field]) => [field, blankToNull(patch[key])])));
|
|
115
|
+
const billing = customer.addresses?.find((address) => address.is_default_billing);
|
|
116
|
+
const shipping = customer.addresses?.find((address) => address.is_default_shipping);
|
|
117
|
+
const shared = billing !== undefined && billing.id === shipping?.id;
|
|
118
|
+
if (patch.billing) {
|
|
119
|
+
customer = billing && (!shared || patch.shipping)
|
|
120
|
+
? await post(`/addresses/${encodeURIComponent(billing.id)}`, toMedusaAddress(patch.billing))
|
|
121
|
+
: await post('/addresses', { ...toMedusaAddress(patch.billing), is_default_billing: true });
|
|
122
|
+
}
|
|
123
|
+
if (patch.shipping) {
|
|
124
|
+
customer = shipping && !shared
|
|
125
|
+
? await post(`/addresses/${encodeURIComponent(shipping.id)}`, toMedusaAddress(patch.shipping))
|
|
126
|
+
: await post('/addresses', { ...toMedusaAddress(patch.shipping), is_default_shipping: true });
|
|
127
|
+
}
|
|
128
|
+
return toCustomer(customer);
|
|
129
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { combinePullAdapters, createReconcileFeed, SignInError, type ConnectorAuth, type CustomerInput, type ServerCapabilities, type SyncContext, type TallyConnector } from '@tallyui/core';
|
|
1
|
+
import { combinePullAdapters, createReconcileFeed, SignInError, type ConnectorAuth, type CustomerInput, type CustomerPatch, type ServerCapabilities, type SyncContext, type TallyConnector } from '@tallyui/core';
|
|
2
2
|
|
|
3
3
|
import { readCapabilities } from './capabilities';
|
|
4
|
-
import { searchMedusaCustomers, createMedusaCustomer, getMedusaCustomer } from './customers';
|
|
4
|
+
import { searchMedusaCustomers, createMedusaCustomer, getMedusaCustomer, updateMedusaCustomer } from './customers';
|
|
5
5
|
import { medusaProductSchema } from './schemas/products';
|
|
6
6
|
import { medusaProductTraits } from './traits/product';
|
|
7
7
|
import { medusaProductSync } from './sync/products';
|
|
@@ -76,6 +76,8 @@ export const medusaCreateCustomer = (context: SyncContext, input: CustomerInput)
|
|
|
76
76
|
createMedusaCustomer(context.baseUrl, context.headers, input, { signal: context.signal });
|
|
77
77
|
export const medusaGetCustomer = (context: SyncContext, id: string) =>
|
|
78
78
|
getMedusaCustomer(context.baseUrl, context.headers, id, { signal: context.signal });
|
|
79
|
+
export const medusaUpdateCustomer = (context: SyncContext, id: string, patch: CustomerPatch) =>
|
|
80
|
+
updateMedusaCustomer(context.baseUrl, context.headers, id, patch, { signal: context.signal });
|
|
79
81
|
|
|
80
82
|
// The JWT from Medusa's emailpass sign-in (POST /auth/user/emailpass) is stored as token.
|
|
81
83
|
// email and password are only sign-in form fields and are never sent as headers.
|
|
@@ -164,7 +166,7 @@ const medusaConnectorParts = {
|
|
|
164
166
|
/** Medusa connector using an admin user's Bearer JWT; like createMedusaConnector(), one per store session with its own feed. */
|
|
165
167
|
export const createMedusaAdminUserConnector = (): TallyConnector => ({
|
|
166
168
|
...createMedusaConnector(), auth: medusaAdminUserAuth, capabilities: medusaCapabilities,
|
|
167
|
-
searchCustomers: medusaSearchCustomers, createCustomer: medusaCreateCustomer, getCustomer: medusaGetCustomer,
|
|
169
|
+
searchCustomers: medusaSearchCustomers, createCustomer: medusaCreateCustomer, getCustomer: medusaGetCustomer, updateCustomer: medusaUpdateCustomer,
|
|
168
170
|
});
|
|
169
171
|
|
|
170
172
|
/**
|
|
@@ -187,7 +189,7 @@ export { medusaProductReplication } from './replication/products';
|
|
|
187
189
|
export { createMedusaVariantFeedReplication } from './replication/variant-feed';
|
|
188
190
|
export { medusaStockReconcile } from './reconcile/stock';
|
|
189
191
|
export { medusaStoreSettings } from './store-settings';
|
|
190
|
-
export { searchMedusaCustomers, createMedusaCustomer, getMedusaCustomer, toCustomer } from './customers';
|
|
192
|
+
export { searchMedusaCustomers, createMedusaCustomer, getMedusaCustomer, updateMedusaCustomer, toCustomer } from './customers';
|
|
191
193
|
export { withCalculatedPrices } from './pricing/calculated';
|
|
192
194
|
export { MEDUSA_CALCULATED_PRICE_RECONCILE_INTERVAL_MS } from './reconcile/calculated-prices';
|
|
193
195
|
export type { MedusaCalculatedPrice, MedusaProductDocument, MedusaVariantDocument } from './schemas/products';
|
|
@@ -18,8 +18,9 @@ function fingerprintOf(variants: Variant[]): string {
|
|
|
18
18
|
* Every product the store API lists for the pricing context, with its
|
|
19
19
|
* calculated-price fingerprint, one `/store/products` request per page of
|
|
20
20
|
* 100 (ADR-060 amendment 8, D2b). Without `context.pricingContext` it yields
|
|
21
|
-
*
|
|
22
|
-
* does not list are not reported; the runner
|
|
21
|
+
* no page, so the pass is not complete and runs again at the next check.
|
|
22
|
+
* Products the sales channel does not list are not reported; the runner
|
|
23
|
+
* counts them as `unreported`.
|
|
23
24
|
*
|
|
24
25
|
* The app runs it as `reconcile.calculatedPrices` with
|
|
25
26
|
* `startFingerprintReconcile({ …, intervalMs: MEDUSA_CALCULATED_PRICE_RECONCILE_INTERVAL_MS, maxPages: 1000 })`:
|