@tallyui/connector-medusa 3.20.0 → 4.0.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.
@@ -1,5 +1,5 @@
1
1
  import { ConnectorUnauthorizedError, StoreSettingsError } from '@tallyui/core';
2
- import type { StoreSettings, StoreSettingsChoice, SyncContext } from '@tallyui/core';
2
+ import type { StoreSettings, StoreSettingsChoice, StoreTaxRate, SyncContext } from '@tallyui/core';
3
3
 
4
4
  type Store = { id: string; default_region_id: string | null };
5
5
  type Country = { iso_2: string };
@@ -7,8 +7,19 @@ type Region = { id: string; name: string; currency_code: string; countries?: Cou
7
7
  type PricePreference = { attribute: string; value: string; is_tax_inclusive: boolean };
8
8
  type SalesChannel = { id: string; name: string };
9
9
  type ApiKey = { id: string; title: string; token: string; revoked_at: string | null; sales_channels?: SalesChannel[] };
10
- type TaxRate = { rate: number | null; is_default: boolean; rules?: unknown[] };
11
- type TaxRegion = { country_code: string; province_code: string | null; tax_rates?: TaxRate[] };
10
+ type TaxRate = {
11
+ id: string;
12
+ name: string | null;
13
+ code: string | null;
14
+ rate: number | null;
15
+ is_default: boolean;
16
+ rules?: { reference: string; reference_id: string }[];
17
+ };
18
+ type TaxRegion = { id: string; country_code: string; province_code: string | null; tax_rates?: TaxRate[] };
19
+
20
+ const TAX_REGIONS_PAGE_SIZE = 100;
21
+ // As Woo's rate list: at most 100 pages, so a misbehaving count can never loop for ever.
22
+ const MAX_TAX_REGION_PAGES = 100;
12
23
 
13
24
  /** Except for 401/403 (`ConnectorUnauthorizedError`), a non-OK response becomes `StoreSettingsError('failed')`, carrying the HTTP status and Medusa's own `message`. */
14
25
  async function get(context: SyncContext, path: string): Promise<any> {
@@ -42,8 +53,37 @@ function channelName(key: ApiKey): string {
42
53
  }
43
54
 
44
55
  /**
45
- * Reads the store's currency, tax inclusivity and location tax rate, and the
46
- * publishable-key channel, from Medusa's admin API only (TV4b). Read-only
56
+ * Every tax region's rates, in Medusa's order, for an app's "View all tax rates" table (#673).
57
+ * Display only: the cart's rate stays `taxRatesPpm`. A rate with no percentage is left out.
58
+ */
59
+ async function allMedusaTaxRates(context: SyncContext): Promise<StoreTaxRate[]> {
60
+ const rates: StoreTaxRate[] = [];
61
+ for (let page = 0; page < MAX_TAX_REGION_PAGES; page++) {
62
+ const offset = page * TAX_REGIONS_PAGE_SIZE;
63
+ const res = await get(context, `/admin/tax-regions?limit=${TAX_REGIONS_PAGE_SIZE}&offset=${offset}&fields=id,country_code,province_code,parent_id,*tax_rates,*tax_rates.rules`);
64
+ const regions: TaxRegion[] = res.tax_regions ?? [];
65
+ for (const region of regions) {
66
+ for (const rate of region.tax_rates ?? []) {
67
+ if (rate.rate == null) continue;
68
+ rates.push({
69
+ id: rate.id,
70
+ name: rate.name || rate.code || rate.id,
71
+ rate: String(rate.rate),
72
+ // Codes as Medusa stores them (lower case); the app formats them.
73
+ location: region.province_code !== null ? { country: region.country_code, region: region.province_code } : { country: region.country_code },
74
+ ...(rate.rules?.length ? { rules: rate.rules.map((r) => ({ reference: r.reference, referenceId: r.reference_id })) } : {}),
75
+ });
76
+ }
77
+ }
78
+ if (regions.length < TAX_REGIONS_PAGE_SIZE || offset + regions.length >= res.count) break;
79
+ }
80
+ return rates;
81
+ }
82
+
83
+ /**
84
+ * Reads the store's currency, tax inclusivity and location tax rate, the
85
+ * publishable-key channel, and once every choice is made, every tax region's
86
+ * rates (`allTaxRates`), from Medusa's admin API only (TV4b). Read-only
47
87
  * (ADR-048). Region, then country, then channel: the first ambiguity that
48
88
  * needs a choice reports every choice known at that point, so the app asks
49
89
  * once.
@@ -100,6 +140,9 @@ export const medusaStoreSettings = async (context: SyncContext, choice?: StoreSe
100
140
  // No tax region, or no default rate: Medusa's system provider adds no tax line, so default is 0 — never a silent guess.
101
141
  const taxRatesPpm = { default: defaultRate?.rate != null ? Math.round(defaultRate.rate * 10_000) : 0 };
102
142
 
143
+ // Only once every choice is made, so a choice_required path makes no extra request.
144
+ const allTaxRates = await allMedusaTaxRates(context);
145
+
103
146
  return {
104
147
  currency: region.currency_code.toUpperCase(),
105
148
  pricesIncludeTax,
@@ -111,5 +154,6 @@ export const medusaStoreSettings = async (context: SyncContext, choice?: StoreSe
111
154
  currency_code: region.currency_code.toLowerCase(),
112
155
  publishable_key: channelKey.token,
113
156
  },
157
+ allTaxRates,
114
158
  };
115
159
  };