@hartl-services/medusa-storefront 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +103 -2
- package/dist/browser/account-verification-channel.d.ts +11 -0
- package/dist/browser/account-verification-channel.js +32 -0
- package/dist/browser/actions.d.ts +53 -0
- package/dist/browser/actions.js +1 -0
- package/dist/browser/affiliate-referral.d.ts +152 -0
- package/dist/browser/affiliate-referral.js +85 -0
- package/dist/browser/browser-session.d.ts +3 -0
- package/dist/browser/browser-session.js +27 -0
- package/dist/browser/cart-mutations.d.ts +36 -0
- package/dist/browser/cart-mutations.js +137 -0
- package/dist/browser/cart-refresh.d.ts +11 -0
- package/dist/browser/cart-refresh.js +33 -0
- package/dist/browser/client.d.ts +15 -0
- package/dist/browser/client.js +20 -0
- package/dist/browser/fresh-order-marker.d.ts +11 -0
- package/dist/browser/fresh-order-marker.js +30 -0
- package/dist/browser/index.d.ts +946 -0
- package/dist/browser/index.js +70 -0
- package/dist/browser/query-invalidation.d.ts +4 -0
- package/dist/browser/query-invalidation.js +39 -0
- package/dist/browser/query-keys.d.ts +43 -0
- package/dist/browser/query-keys.js +28 -0
- package/dist/browser/query-options.d.ts +781 -0
- package/dist/browser/query-options.js +165 -0
- package/dist/browser/region.d.ts +5 -0
- package/dist/browser/region.js +15 -0
- package/dist/browser/unwrap-medusa-action.d.ts +7 -0
- package/dist/browser/unwrap-medusa-action.js +25 -0
- package/dist/core/action-result.d.ts +17 -0
- package/dist/core/action-result.js +12 -0
- package/dist/core/addresses.d.ts +46 -0
- package/dist/core/addresses.js +137 -0
- package/dist/core/affiliate/affiliate-application-errors.d.ts +2 -0
- package/dist/core/affiliate/affiliate-application-errors.js +14 -0
- package/dist/core/affiliate/affiliate-commission-presentation.d.ts +15 -0
- package/dist/core/affiliate/affiliate-commission-presentation.js +28 -0
- package/dist/core/affiliate/affiliate-coupon-code-presentation.d.ts +10 -0
- package/dist/core/affiliate/affiliate-coupon-code-presentation.js +22 -0
- package/dist/core/affiliate/affiliate-coupon-label.d.ts +3 -0
- package/dist/core/affiliate/affiliate-coupon-label.js +4 -0
- package/dist/core/affiliate/affiliate-date-range.d.ts +6 -0
- package/dist/core/affiliate/affiliate-date-range.js +16 -0
- package/dist/core/affiliate/affiliate-rate-presentation.d.ts +1 -0
- package/dist/core/affiliate/affiliate-rate-presentation.js +8 -0
- package/dist/core/affiliate/partner-company-address.d.ts +24 -0
- package/dist/core/affiliate/partner-company-address.js +31 -0
- package/dist/core/auth/customer-sign-in.d.ts +36 -0
- package/dist/core/auth/customer-sign-in.js +55 -0
- package/dist/core/auth/link-token.d.ts +8 -0
- package/dist/core/auth/link-token.js +11 -0
- package/dist/core/auth/login-code-auto-send.d.ts +7 -0
- package/dist/core/auth/login-code-auto-send.js +11 -0
- package/dist/core/auth/password-rule-label.d.ts +4 -0
- package/dist/core/auth/password-rule-label.js +13 -0
- package/dist/core/cart/cart-line-item-image-url.d.ts +9 -0
- package/dist/core/cart/cart-line-item-image-url.js +14 -0
- package/dist/core/cart/cart-normalization.d.ts +2 -0
- package/dist/core/cart/cart-normalization.js +11 -0
- package/dist/core/cart/cart-price-breakdown.d.ts +30 -0
- package/dist/core/cart/cart-price-breakdown.js +45 -0
- package/dist/core/cart/cart-tax-breakdown.d.ts +17 -0
- package/dist/core/cart/cart-tax-breakdown.js +18 -0
- package/dist/core/cart/coupon-promotions.d.ts +8 -0
- package/dist/core/cart/coupon-promotions.js +9 -0
- package/dist/core/cart/payment-session.d.ts +3 -0
- package/dist/core/cart/payment-session.js +12 -0
- package/dist/core/cart/refunded-cart.d.ts +27 -0
- package/dist/core/cart/refunded-cart.js +46 -0
- package/dist/core/catalog/category-filter.d.ts +16 -0
- package/dist/core/catalog/category-filter.js +33 -0
- package/dist/core/catalog/inventory.d.ts +10 -0
- package/dist/core/catalog/inventory.js +9 -0
- package/dist/core/catalog/medusa-product-image.d.ts +8 -0
- package/dist/core/catalog/medusa-product-image.js +13 -0
- package/dist/core/catalog/product-price.d.ts +8 -0
- package/dist/core/catalog/product-price.js +21 -0
- package/dist/core/catalog/selected-variant.d.ts +7 -0
- package/dist/core/catalog/selected-variant.js +9 -0
- package/dist/core/checkout/checkout-account-offer-state.d.ts +41 -0
- package/dist/core/checkout/checkout-account-offer-state.js +32 -0
- package/dist/core/checkout/checkout-address-draft.d.ts +39 -0
- package/dist/core/checkout/checkout-address-draft.js +78 -0
- package/dist/core/checkout/checkout-address-form.d.ts +24 -0
- package/dist/core/checkout/checkout-address-form.js +99 -0
- package/dist/core/checkout/checkout-coupon.d.ts +9 -0
- package/dist/core/checkout/checkout-coupon.js +13 -0
- package/dist/core/checkout/checkout-steps.d.ts +20 -0
- package/dist/core/checkout/checkout-steps.js +44 -0
- package/dist/core/checkout/customer-login-required.d.ts +12 -0
- package/dist/core/checkout/customer-login-required.js +14 -0
- package/dist/core/checkout/payment-authorization.d.ts +8 -0
- package/dist/core/checkout/payment-authorization.js +9 -0
- package/dist/core/checkout/stripe-redirect-return.d.ts +13 -0
- package/dist/core/checkout/stripe-redirect-return.js +29 -0
- package/dist/core/errors.d.ts +12 -0
- package/dist/core/errors.js +18 -0
- package/dist/core/fields.d.ts +18 -0
- package/dist/core/fields.js +20 -0
- package/dist/core/form-values.d.ts +162 -0
- package/dist/core/form-values.js +234 -0
- package/dist/core/format/money.d.ts +7 -0
- package/dist/core/format/money.js +6 -0
- package/dist/core/i18n.d.ts +24 -0
- package/dist/core/i18n.js +15 -0
- package/dist/core/image-url.d.ts +18 -0
- package/dist/core/image-url.js +34 -0
- package/dist/core/index.d.ts +48 -0
- package/dist/core/index.js +49 -0
- package/dist/core/order/order-payment-summary.d.ts +15 -0
- package/dist/core/order/order-payment-summary.js +48 -0
- package/dist/core/plugin-features.d.ts +21 -0
- package/dist/core/plugin-features.js +30 -0
- package/dist/core/query-string.d.ts +6 -0
- package/dist/core/query-string.js +16 -0
- package/dist/core/region.d.ts +11 -0
- package/dist/core/region.js +13 -0
- package/dist/core/report-error.d.ts +12 -0
- package/dist/core/report-error.js +1 -0
- package/dist/core/routing-locale.d.ts +1 -0
- package/dist/core/routing-locale.js +25 -0
- package/dist/core/settle-within.d.ts +8 -0
- package/dist/core/settle-within.js +14 -0
- package/dist/core/tax-compliance/address-tax-identification.d.ts +37 -0
- package/dist/core/tax-compliance/address-tax-identification.js +51 -0
- package/dist/core/tax-compliance/tax-identification.d.ts +18 -0
- package/dist/core/tax-compliance/tax-identification.js +58 -0
- package/dist/core/texts-de.d.ts +136 -0
- package/dist/core/texts-de.js +152 -0
- package/dist/react/cart-error-boundary.d.ts +24 -0
- package/dist/react/cart-error-boundary.js +57 -0
- package/dist/react/context.d.ts +17 -0
- package/dist/react/context.js +46 -0
- package/dist/react/index.d.ts +12 -0
- package/dist/react/index.js +14 -0
- package/dist/react/use-account.d.ts +214 -0
- package/dist/react/use-account.js +294 -0
- package/dist/react/use-affiliate.d.ts +566 -0
- package/dist/react/use-affiliate.js +573 -0
- package/dist/react/use-auth.d.ts +136 -0
- package/dist/react/use-auth.js +402 -0
- package/dist/react/use-cart.d.ts +57 -0
- package/dist/react/use-cart.js +137 -0
- package/dist/react/use-checkout-address-form.d.ts +156 -0
- package/dist/react/use-checkout-address-form.js +95 -0
- package/dist/react/use-checkout.d.ts +268 -0
- package/dist/react/use-checkout.js +873 -0
- package/dist/react/use-countdown.d.ts +5 -0
- package/dist/react/use-countdown.js +23 -0
- package/dist/react/use-link-token.d.ts +12 -0
- package/dist/react/use-link-token.js +17 -0
- package/dist/react/use-plugin-feature.d.ts +16 -0
- package/dist/react/use-plugin-feature.js +31 -0
- package/dist/react/use-verification-watch.d.ts +7 -0
- package/dist/react/use-verification-watch.js +49 -0
- package/dist/registry/account.json +248 -0
- package/dist/registry/affiliate.json +302 -0
- package/dist/registry/cart.json +127 -0
- package/dist/registry/checkout.json +169 -0
- package/dist/registry/form.json +145 -0
- package/dist/registry/registry.json +917 -0
- package/dist/registry/setup.json +76 -0
- package/dist/server/affiliate.d.ts +124 -0
- package/dist/server/affiliate.js +145 -0
- package/dist/server/cart.d.ts +10 -0
- package/dist/server/cart.js +113 -0
- package/dist/server/catalog.d.ts +28 -0
- package/dist/server/catalog.js +82 -0
- package/dist/server/checkout.d.ts +6 -0
- package/dist/server/checkout.js +12 -0
- package/dist/server/context.d.ts +38 -0
- package/dist/server/context.js +34 -0
- package/dist/server/customer.d.ts +84 -0
- package/dist/server/customer.js +362 -0
- package/dist/server/documents.d.ts +29 -0
- package/dist/server/documents.js +55 -0
- package/dist/server/index.d.ts +258 -0
- package/dist/server/index.js +45 -0
- package/dist/server/orders.d.ts +9 -0
- package/dist/server/orders.js +31 -0
- package/dist/server/plugin-features.d.ts +13 -0
- package/dist/server/plugin-features.js +52 -0
- package/dist/server/region.d.ts +6 -0
- package/dist/server/region.js +17 -0
- package/dist/server/route-response.d.ts +2 -0
- package/dist/server/route-response.js +17 -0
- package/dist/server/run-medusa-action.d.ts +7 -0
- package/dist/server/run-medusa-action.js +40 -0
- package/dist/server/session.d.ts +29 -0
- package/dist/server/session.js +106 -0
- package/dist/server/tax-compliance.d.ts +26 -0
- package/dist/server/tax-compliance.js +85 -0
- package/package.json +86 -3
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { queryOptions } from "@tanstack/react-query";
|
|
2
|
+
import { hasRefundedPayment } from "../core/cart/refunded-cart.js";
|
|
3
|
+
import { affiliateQueryKeys, medusaQueryKeys } from "./query-keys.js";
|
|
4
|
+
// Toggling a feature in the admin takes effect in the backend immediately;
|
|
5
|
+
// the storefront follows within a minute. The password rules likewise.
|
|
6
|
+
const PLUGIN_FEATURES_STALE_TIME_MS = 60_000;
|
|
7
|
+
const PASSWORD_POLICY_STALE_TIME_MS = 60_000;
|
|
8
|
+
/** Paths of the shop's Route Handlers the partner portal polls. */
|
|
9
|
+
export function createAffiliateApiPaths(apiBasePath) {
|
|
10
|
+
const base = `${apiBasePath.replace(/\/+$/, "")}/affiliate`;
|
|
11
|
+
return {
|
|
12
|
+
commissions: `${base}/commissions`,
|
|
13
|
+
consignmentInventory: `${base}/consignment/inventory`,
|
|
14
|
+
consignmentReports: `${base}/consignment/reports`,
|
|
15
|
+
context: `${base}/context`,
|
|
16
|
+
couponCodes: `${base}/coupon-codes`,
|
|
17
|
+
slugs: `${base}/slugs`,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
export function createQueryOptions({ actions, affiliateApiPaths, cart, referral, region, t, unwrapMedusaAction }) {
|
|
21
|
+
// Polling reads (coupon codes especially - 3s refetchInterval while
|
|
22
|
+
// provisioning) go through Route Handlers rather than Server Actions,
|
|
23
|
+
// which are POST-only and invalidate the router cache on every call - a
|
|
24
|
+
// poor fit for a tight interval. One-off mutations stay Server Actions.
|
|
25
|
+
async function fetchJson(input) {
|
|
26
|
+
const response = await fetch(input);
|
|
27
|
+
if (!response.ok) {
|
|
28
|
+
const body = await response.json().catch(() => null);
|
|
29
|
+
const message = body && typeof body === "object" && "message" in body && typeof body.message === "string" ? body.message : t("request.failed", { path: input, status: response.status });
|
|
30
|
+
throw new Error(message);
|
|
31
|
+
}
|
|
32
|
+
return (await response.json());
|
|
33
|
+
}
|
|
34
|
+
return {
|
|
35
|
+
// Every reader of the active cart goes through here, so a cart whose payment
|
|
36
|
+
// was refunded is replaced before any component can try to change or
|
|
37
|
+
// complete it.
|
|
38
|
+
activeCart: () => queryOptions({
|
|
39
|
+
queryKey: medusaQueryKeys.activeCart(),
|
|
40
|
+
queryFn: async () => {
|
|
41
|
+
const activeCart = await actions.getActiveCart();
|
|
42
|
+
return activeCart && hasRefundedPayment(activeCart) ? await cart.replaceRefundedCart(activeCart) : activeCart;
|
|
43
|
+
},
|
|
44
|
+
}),
|
|
45
|
+
customer: () => queryOptions({
|
|
46
|
+
queryKey: medusaQueryKeys.customer(),
|
|
47
|
+
queryFn: () => actions.retrieveCustomer(),
|
|
48
|
+
}),
|
|
49
|
+
shippingOptions: (cartId) => queryOptions({
|
|
50
|
+
queryKey: medusaQueryKeys.shippingOptions(cartId),
|
|
51
|
+
queryFn: () => actions.listShippingOptions(cartId),
|
|
52
|
+
}),
|
|
53
|
+
paymentProviders: (regionId) => queryOptions({
|
|
54
|
+
queryKey: medusaQueryKeys.paymentProviders(regionId),
|
|
55
|
+
queryFn: () => actions.listPaymentProviders(regionId),
|
|
56
|
+
}),
|
|
57
|
+
region: (countryCode) => {
|
|
58
|
+
const normalizedCountryCode = countryCode.toLowerCase();
|
|
59
|
+
return queryOptions({
|
|
60
|
+
queryKey: medusaQueryKeys.region(normalizedCountryCode),
|
|
61
|
+
queryFn: () => region.getRegionForCountryClient(normalizedCountryCode),
|
|
62
|
+
});
|
|
63
|
+
},
|
|
64
|
+
storeRegions: () => queryOptions({
|
|
65
|
+
queryKey: medusaQueryKeys.regions(),
|
|
66
|
+
queryFn: () => actions.listRegions(),
|
|
67
|
+
}),
|
|
68
|
+
pluginFeatures: () => queryOptions({
|
|
69
|
+
queryKey: medusaQueryKeys.pluginFeatures(),
|
|
70
|
+
queryFn: () => actions.fetchPluginFeatures(),
|
|
71
|
+
staleTime: PLUGIN_FEATURES_STALE_TIME_MS,
|
|
72
|
+
}),
|
|
73
|
+
documents: () => queryOptions({
|
|
74
|
+
queryKey: medusaQueryKeys.documents(),
|
|
75
|
+
queryFn: () => actions.listDocuments(),
|
|
76
|
+
}),
|
|
77
|
+
orders: ({ limit = 20, offset = 0 } = {}) => queryOptions({
|
|
78
|
+
queryKey: medusaQueryKeys.orders({ limit, offset }),
|
|
79
|
+
queryFn: () => actions.listOrders({ limit, offset }),
|
|
80
|
+
}),
|
|
81
|
+
/** One order with its lines and totals; needs the optional `getOrder` action. */
|
|
82
|
+
orderDetail: (orderId) => queryOptions({
|
|
83
|
+
queryKey: medusaQueryKeys.orderDetail(orderId),
|
|
84
|
+
queryFn: async () => (actions.getOrder ? await actions.getOrder(orderId) : null),
|
|
85
|
+
enabled: Boolean(actions.getOrder) && Boolean(orderId),
|
|
86
|
+
}),
|
|
87
|
+
customerPasswordPolicy: () => queryOptions({
|
|
88
|
+
queryKey: medusaQueryKeys.customerPasswordPolicy(),
|
|
89
|
+
queryFn: () => actions.fetchCustomerPasswordPolicy(),
|
|
90
|
+
retry: 1,
|
|
91
|
+
staleTime: PASSWORD_POLICY_STALE_TIME_MS,
|
|
92
|
+
}),
|
|
93
|
+
// isEnabled: logged in and tax_compliance on - the route needs both.
|
|
94
|
+
customerTaxIdentifications: (isEnabled) => queryOptions({
|
|
95
|
+
queryKey: medusaQueryKeys.taxIdentifications(),
|
|
96
|
+
queryFn: async () => unwrapMedusaAction(await actions.listCustomerTaxIdentifications()),
|
|
97
|
+
enabled: isEnabled,
|
|
98
|
+
}),
|
|
99
|
+
affiliateContext: () => queryOptions({
|
|
100
|
+
queryKey: affiliateQueryKeys.context(),
|
|
101
|
+
queryFn: () => fetchJson(affiliateApiPaths.context),
|
|
102
|
+
}),
|
|
103
|
+
// isEnabled: the affiliate plugin switch - the route answers 404 otherwise.
|
|
104
|
+
affiliateCartReferral: (cartId, isEnabled) => queryOptions({
|
|
105
|
+
queryKey: affiliateQueryKeys.cartReferral(cartId),
|
|
106
|
+
queryFn: () => referral.getAffiliateCartReferral(cartId),
|
|
107
|
+
enabled: isEnabled,
|
|
108
|
+
}),
|
|
109
|
+
// isEnabled: affiliate.features.own_order_discount - the route labels only
|
|
110
|
+
// own-order codes and answers 404 otherwise; codes are then shown as entered.
|
|
111
|
+
couponLabels: (codes, isEnabled) => queryOptions({
|
|
112
|
+
queryKey: affiliateQueryKeys.couponLabels(codes),
|
|
113
|
+
queryFn: () => referral.resolveCouponLabels(codes),
|
|
114
|
+
enabled: isEnabled && codes.length > 0,
|
|
115
|
+
}),
|
|
116
|
+
affiliateSlugs: () => queryOptions({
|
|
117
|
+
queryKey: affiliateQueryKeys.slugs(),
|
|
118
|
+
queryFn: () => fetchJson(affiliateApiPaths.slugs),
|
|
119
|
+
retry: false,
|
|
120
|
+
}),
|
|
121
|
+
affiliateCouponCodes: (query) => {
|
|
122
|
+
const params = new URLSearchParams();
|
|
123
|
+
if (query.limit !== undefined) {
|
|
124
|
+
params.set("limit", String(query.limit));
|
|
125
|
+
}
|
|
126
|
+
if (query.offset !== undefined) {
|
|
127
|
+
params.set("offset", String(query.offset));
|
|
128
|
+
}
|
|
129
|
+
if (query.status) {
|
|
130
|
+
params.set("status", query.status);
|
|
131
|
+
}
|
|
132
|
+
return queryOptions({
|
|
133
|
+
queryKey: affiliateQueryKeys.couponCodes(query),
|
|
134
|
+
queryFn: () => fetchJson(`${affiliateApiPaths.couponCodes}?${params.toString()}`),
|
|
135
|
+
});
|
|
136
|
+
},
|
|
137
|
+
affiliateCommissions: (query) => {
|
|
138
|
+
const params = new URLSearchParams();
|
|
139
|
+
if (query.from) {
|
|
140
|
+
params.set("from", query.from);
|
|
141
|
+
}
|
|
142
|
+
if (query.to) {
|
|
143
|
+
params.set("to", query.to);
|
|
144
|
+
}
|
|
145
|
+
if (query.limit !== undefined) {
|
|
146
|
+
params.set("limit", String(query.limit));
|
|
147
|
+
}
|
|
148
|
+
if (query.offset !== undefined) {
|
|
149
|
+
params.set("offset", String(query.offset));
|
|
150
|
+
}
|
|
151
|
+
return queryOptions({
|
|
152
|
+
queryKey: affiliateQueryKeys.commissions(query),
|
|
153
|
+
queryFn: () => fetchJson(`${affiliateApiPaths.commissions}?${params.toString()}`),
|
|
154
|
+
});
|
|
155
|
+
},
|
|
156
|
+
affiliateConsignmentInventory: () => queryOptions({
|
|
157
|
+
queryKey: affiliateQueryKeys.consignmentInventory(),
|
|
158
|
+
queryFn: () => fetchJson(affiliateApiPaths.consignmentInventory),
|
|
159
|
+
}),
|
|
160
|
+
affiliateConsignmentReports: () => queryOptions({
|
|
161
|
+
queryKey: affiliateQueryKeys.consignmentReports(),
|
|
162
|
+
queryFn: () => fetchJson(affiliateApiPaths.consignmentReports),
|
|
163
|
+
}),
|
|
164
|
+
};
|
|
165
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { CredentialedMedusaClient } from "./client.js";
|
|
2
|
+
export declare function createBrowserRegion(createClient: () => CredentialedMedusaClient): {
|
|
3
|
+
getRegionForCountryClient(countryCode: string): Promise<import("@medusajs/types").StoreRegion | null>;
|
|
4
|
+
};
|
|
5
|
+
export type BrowserRegion = ReturnType<typeof createBrowserRegion>;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { findRegionForCountry, REGION_FIELDS, REGION_LIST_LIMIT } from "../core/region.js";
|
|
2
|
+
// Browser-side region lookup (public data, no session cookies needed) - used
|
|
3
|
+
// by Client Components that can't reach the server-only client.
|
|
4
|
+
export function createBrowserRegion(createClient) {
|
|
5
|
+
return {
|
|
6
|
+
async getRegionForCountryClient(countryCode) {
|
|
7
|
+
const sdk = createClient();
|
|
8
|
+
const { regions } = await sdk.store.region.list({
|
|
9
|
+
fields: REGION_FIELDS,
|
|
10
|
+
limit: REGION_LIST_LIMIT,
|
|
11
|
+
});
|
|
12
|
+
return findRegionForCountry(regions, countryCode);
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { QueryClient } from "@tanstack/react-query";
|
|
2
|
+
import type { MedusaActionResult } from "../core/action-result.js";
|
|
3
|
+
export interface UnwrapMedusaActionOptions {
|
|
4
|
+
queryClient?: QueryClient;
|
|
5
|
+
}
|
|
6
|
+
export type UnwrapMedusaAction = <T>(result: MedusaActionResult<T>, options?: UnwrapMedusaActionOptions) => T;
|
|
7
|
+
export declare function createUnwrapMedusaAction(endMedusaBrowserSession: () => Promise<void>): UnwrapMedusaAction;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { MedusaActionError } from "../core/action-result.js";
|
|
2
|
+
import { resetCustomerScopedQueries } from "./query-invalidation.js";
|
|
3
|
+
// Client-safe counterpart to runMedusaAction: turns a serialisable action
|
|
4
|
+
// result back into the throw-based shape every try/catch around a server
|
|
5
|
+
// action expects. When the server side ended the customer session
|
|
6
|
+
// (runMedusaAction's endSessionOnUnauthorized on a rejected token), it also
|
|
7
|
+
// drops the Medusa host-only browser session cookie, so the storefront
|
|
8
|
+
// session and the Medusa session end together rather than leaving the browser
|
|
9
|
+
// client authenticated against a backend that no longer honours the JWT.
|
|
10
|
+
// When a queryClient is given, it also resets customer-scoped queries so
|
|
11
|
+
// gates (account, partner portal) re-render as logged out immediately.
|
|
12
|
+
export function createUnwrapMedusaAction(endMedusaBrowserSession) {
|
|
13
|
+
return function unwrapMedusaAction(result, options) {
|
|
14
|
+
if (result.ok) {
|
|
15
|
+
return result.data;
|
|
16
|
+
}
|
|
17
|
+
if (result.error.sessionEnded) {
|
|
18
|
+
void endMedusaBrowserSession().catch(() => undefined);
|
|
19
|
+
if (options?.queryClient) {
|
|
20
|
+
void resetCustomerScopedQueries(options.queryClient);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
throw new MedusaActionError(result.error.message, result.error.status);
|
|
24
|
+
};
|
|
25
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export interface MedusaActionFailure {
|
|
2
|
+
ok: false;
|
|
3
|
+
error: {
|
|
4
|
+
code?: string;
|
|
5
|
+
message: string;
|
|
6
|
+
sessionEnded?: boolean;
|
|
7
|
+
status?: number;
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
export type MedusaActionResult<T> = {
|
|
11
|
+
ok: true;
|
|
12
|
+
data: T;
|
|
13
|
+
} | MedusaActionFailure;
|
|
14
|
+
export declare class MedusaActionError extends Error {
|
|
15
|
+
status?: number;
|
|
16
|
+
constructor(message: string, status?: number);
|
|
17
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// Thrown by unwrapMedusaAction so existing catch blocks around a former
|
|
2
|
+
// throwing Server Action keep working unchanged, while still carrying the
|
|
3
|
+
// backend's HTTP status through to callers that need it (e.g. treating a 401
|
|
4
|
+
// as "not applied" instead of a hard failure).
|
|
5
|
+
export class MedusaActionError extends Error {
|
|
6
|
+
status;
|
|
7
|
+
constructor(message, status) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.name = "MedusaActionError";
|
|
10
|
+
this.status = status;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { HttpTypes } from "@medusajs/types";
|
|
2
|
+
import type { CustomerAddressValues } from "./form-values.js";
|
|
3
|
+
type AddressSnapshot = HttpTypes.StoreCartAddress | HttpTypes.StoreCustomerAddress;
|
|
4
|
+
type CountryRegion = Pick<HttpTypes.StoreRegion, "countries">;
|
|
5
|
+
type CartAddressInput = Exclude<NonNullable<HttpTypes.StoreUpdateCart["shipping_address"]>, string>;
|
|
6
|
+
export type CustomerAddressRole = "billing" | "shipping";
|
|
7
|
+
export interface CountryOption {
|
|
8
|
+
label: string;
|
|
9
|
+
value: string;
|
|
10
|
+
}
|
|
11
|
+
export declare function getCountryOptions(regions: ReadonlyArray<CountryRegion>, locale: string): CountryOption[];
|
|
12
|
+
export declare function toCartAddress(address: AddressSnapshot): CartAddressInput;
|
|
13
|
+
export declare function addressesMatch(first: AddressSnapshot | undefined, second: AddressSnapshot | undefined): boolean;
|
|
14
|
+
export declare function findMatchingCustomerAddress(addresses: HttpTypes.StoreCustomerAddress[], cartAddress: HttpTypes.StoreCartAddress | undefined): HttpTypes.StoreCustomerAddress | undefined;
|
|
15
|
+
export declare function getDefaultCustomerAddress(addresses: HttpTypes.StoreCustomerAddress[], role: CustomerAddressRole): HttpTypes.StoreCustomerAddress;
|
|
16
|
+
export declare function filterAddressesForRegion(addresses: HttpTypes.StoreCustomerAddress[], region: CountryRegion | undefined): HttpTypes.StoreCustomerAddress[];
|
|
17
|
+
/**
|
|
18
|
+
* Fields still missing on the cart, filled from the customer's region-appropriate
|
|
19
|
+
* defaults. The saved UID goes along only while B2B sales are on (`hasB2b`).
|
|
20
|
+
*/
|
|
21
|
+
export declare function getCustomerCartDefaults(cart: HttpTypes.StoreCart, customer: HttpTypes.StoreCustomer, hasB2b: boolean): HttpTypes.StoreUpdateCart;
|
|
22
|
+
interface AddressSummarySource {
|
|
23
|
+
address_1?: string | null;
|
|
24
|
+
city?: string | null;
|
|
25
|
+
company?: string | null;
|
|
26
|
+
first_name?: string | null;
|
|
27
|
+
last_name?: string | null;
|
|
28
|
+
postal_code?: string | null;
|
|
29
|
+
}
|
|
30
|
+
export declare function formatAddressName(address: AddressSummarySource): string;
|
|
31
|
+
export declare function formatAddressLocation(address: AddressSummarySource): string;
|
|
32
|
+
/** One-line summary (name, company, street, postal code and city); empty when nothing is set. */
|
|
33
|
+
export declare function formatAddressSummary(address: AddressSummarySource): string;
|
|
34
|
+
/** The UID an address carries in its metadata (empty when none). */
|
|
35
|
+
export declare function getAddressVatNumber(address: {
|
|
36
|
+
metadata?: Record<string, unknown> | null;
|
|
37
|
+
}): string;
|
|
38
|
+
/**
|
|
39
|
+
* Metadata patch for an address's UID, normalized like the backend does.
|
|
40
|
+
* Medusa merges metadata on update and removes a key sent as "", so an empty
|
|
41
|
+
* UID clears it while other keys stay untouched.
|
|
42
|
+
*/
|
|
43
|
+
export declare function vatNumberMetadataPatch(vatNumber: string): Record<string, string>;
|
|
44
|
+
/** Form values of a saved address, including its UID from metadata. */
|
|
45
|
+
export declare function toCustomerAddressValues(address: HttpTypes.StoreCustomerAddress): CustomerAddressValues;
|
|
46
|
+
export {};
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { TAX_IDENTIFICATION_METADATA_KEY } from "@hartl-services/medusa-contracts/paths/tax-compliance";
|
|
2
|
+
const comparableAddressFields = ["address_1", "address_2", "city", "company", "country_code", "first_name", "last_name", "phone", "postal_code", "province"];
|
|
3
|
+
function normalizeAddressValue(value) {
|
|
4
|
+
return value?.trim().toLowerCase() ?? "";
|
|
5
|
+
}
|
|
6
|
+
function isAddressInRegion(address, region) {
|
|
7
|
+
const countryCode = address.country_code?.toLowerCase();
|
|
8
|
+
if (!countryCode) {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
return region.countries?.some((country) => country.iso_2?.toLowerCase() === countryCode);
|
|
12
|
+
}
|
|
13
|
+
// Medusa's country display_name is English; the storefront names countries in
|
|
14
|
+
// its own language (`locale`), so the label comes from the ISO code. Region
|
|
15
|
+
// matching stays on iso_2 throughout.
|
|
16
|
+
function getCountryLabel(countryNames, isoCode, fallback) {
|
|
17
|
+
try {
|
|
18
|
+
return countryNames.of(isoCode.toUpperCase()) ?? fallback ?? isoCode.toUpperCase();
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
// Intl rejects malformed codes; fall back to what the backend sent.
|
|
22
|
+
return fallback ?? isoCode.toUpperCase();
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export function getCountryOptions(regions, locale) {
|
|
26
|
+
const countryNames = new Intl.DisplayNames([locale], { type: "region" });
|
|
27
|
+
const countries = new Map();
|
|
28
|
+
for (const country of regions.flatMap((region) => region.countries ?? [])) {
|
|
29
|
+
const value = country.iso_2?.toLowerCase();
|
|
30
|
+
if (!value || countries.has(value)) {
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
countries.set(value, {
|
|
34
|
+
label: getCountryLabel(countryNames, value, country.display_name),
|
|
35
|
+
value,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
return [...countries.values()].sort((left, right) => left.label.localeCompare(right.label, locale));
|
|
39
|
+
}
|
|
40
|
+
export function toCartAddress(address) {
|
|
41
|
+
const cartAddress = {};
|
|
42
|
+
for (const field of comparableAddressFields) {
|
|
43
|
+
const value = address[field];
|
|
44
|
+
if (typeof value !== "string" || value.length === 0) {
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
if (field === "country_code") {
|
|
48
|
+
cartAddress.country_code = value.toLowerCase();
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
cartAddress[field] = value;
|
|
52
|
+
}
|
|
53
|
+
return cartAddress;
|
|
54
|
+
}
|
|
55
|
+
export function addressesMatch(first, second) {
|
|
56
|
+
if (!first || !second) {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
return comparableAddressFields.every((field) => normalizeAddressValue(first[field]) === normalizeAddressValue(second[field]));
|
|
60
|
+
}
|
|
61
|
+
export function findMatchingCustomerAddress(addresses, cartAddress) {
|
|
62
|
+
return addresses.find((address) => addressesMatch(address, cartAddress));
|
|
63
|
+
}
|
|
64
|
+
// The address flagged as default for the role wins; without a flag the first
|
|
65
|
+
// saved address stands in, so a customer with saved addresses never starts
|
|
66
|
+
// the checkout from an empty form.
|
|
67
|
+
export function getDefaultCustomerAddress(addresses, role) {
|
|
68
|
+
const defaultField = role === "shipping" ? "is_default_shipping" : "is_default_billing";
|
|
69
|
+
return addresses.find((address) => address[defaultField]) ?? addresses[0];
|
|
70
|
+
}
|
|
71
|
+
export function filterAddressesForRegion(addresses, region) {
|
|
72
|
+
if (!region) {
|
|
73
|
+
return [];
|
|
74
|
+
}
|
|
75
|
+
return addresses.filter((address) => isAddressInRegion(address, region));
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Fields still missing on the cart, filled from the customer's region-appropriate
|
|
79
|
+
* defaults. The saved UID goes along only while B2B sales are on (`hasB2b`).
|
|
80
|
+
*/
|
|
81
|
+
export function getCustomerCartDefaults(cart, customer, hasB2b) {
|
|
82
|
+
const defaults = {};
|
|
83
|
+
const addresses = filterAddressesForRegion(customer.addresses, cart.region);
|
|
84
|
+
const defaultShippingAddress = getDefaultCustomerAddress(addresses, "shipping");
|
|
85
|
+
const defaultBillingAddress = getDefaultCustomerAddress(addresses, "billing");
|
|
86
|
+
if (!cart.email && customer.email) {
|
|
87
|
+
defaults.email = customer.email;
|
|
88
|
+
}
|
|
89
|
+
if (!cart.shipping_address && defaultShippingAddress) {
|
|
90
|
+
defaults.shipping_address = toCartAddress(defaultShippingAddress);
|
|
91
|
+
}
|
|
92
|
+
if (!cart.billing_address && defaultBillingAddress) {
|
|
93
|
+
const vatNumber = hasB2b ? getAddressVatNumber(defaultBillingAddress) : "";
|
|
94
|
+
defaults.billing_address = vatNumber ? { ...toCartAddress(defaultBillingAddress), metadata: vatNumberMetadataPatch(vatNumber) } : toCartAddress(defaultBillingAddress);
|
|
95
|
+
}
|
|
96
|
+
return defaults;
|
|
97
|
+
}
|
|
98
|
+
export function formatAddressName(address) {
|
|
99
|
+
return [address.first_name, address.last_name].filter(Boolean).join(" ");
|
|
100
|
+
}
|
|
101
|
+
export function formatAddressLocation(address) {
|
|
102
|
+
return [address.address_1, [address.postal_code, address.city].filter(Boolean).join(" ")].filter(Boolean).join(", ");
|
|
103
|
+
}
|
|
104
|
+
/** One-line summary (name, company, street, postal code and city); empty when nothing is set. */
|
|
105
|
+
export function formatAddressSummary(address) {
|
|
106
|
+
return [formatAddressName(address), address.company, formatAddressLocation(address)].filter(Boolean).join(", ");
|
|
107
|
+
}
|
|
108
|
+
/** The UID an address carries in its metadata (empty when none). */
|
|
109
|
+
export function getAddressVatNumber(address) {
|
|
110
|
+
const value = address.metadata?.[TAX_IDENTIFICATION_METADATA_KEY];
|
|
111
|
+
return typeof value === "string" ? value : "";
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Metadata patch for an address's UID, normalized like the backend does.
|
|
115
|
+
* Medusa merges metadata on update and removes a key sent as "", so an empty
|
|
116
|
+
* UID clears it while other keys stay untouched.
|
|
117
|
+
*/
|
|
118
|
+
export function vatNumberMetadataPatch(vatNumber) {
|
|
119
|
+
return { [TAX_IDENTIFICATION_METADATA_KEY]: vatNumber.replace(/\s+/g, "").toUpperCase() };
|
|
120
|
+
}
|
|
121
|
+
/** Form values of a saved address, including its UID from metadata. */
|
|
122
|
+
export function toCustomerAddressValues(address) {
|
|
123
|
+
return {
|
|
124
|
+
address_1: address.address_1 ?? "",
|
|
125
|
+
address_2: address.address_2 ?? "",
|
|
126
|
+
address_name: address.address_name ?? "",
|
|
127
|
+
city: address.city ?? "",
|
|
128
|
+
company: address.company ?? "",
|
|
129
|
+
country_code: address.country_code ?? "",
|
|
130
|
+
first_name: address.first_name ?? "",
|
|
131
|
+
last_name: address.last_name ?? "",
|
|
132
|
+
phone: address.phone ?? "",
|
|
133
|
+
postal_code: address.postal_code ?? "",
|
|
134
|
+
province: address.province ?? "",
|
|
135
|
+
vat_number: getAddressVatNumber(address),
|
|
136
|
+
};
|
|
137
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// The backend's application errors are English; the storefront shows them in its own words.
|
|
2
|
+
const APPLICATION_ERROR_CODES = {
|
|
3
|
+
"Company verification is not available in this shop.": "affiliate_application.company_verification_unavailable",
|
|
4
|
+
"Customer already has a pending affiliate application.": "affiliate_application.pending",
|
|
5
|
+
"Customer is already linked to an affiliate.": "affiliate_application.already_affiliate",
|
|
6
|
+
"Confirm that the company has no VAT number.": "affiliate_application.confirm_without_vat_number",
|
|
7
|
+
"The company address needs a company and a complete address.": "affiliate_application.company_address_incomplete",
|
|
8
|
+
"The company address was not found.": "affiliate_application.company_address_not_found",
|
|
9
|
+
"The company address needs a company and a verified VAT number.": "affiliate_application.company_address_unverified",
|
|
10
|
+
};
|
|
11
|
+
export function getAffiliateApplicationErrorMessage(message, t) {
|
|
12
|
+
const code = Object.hasOwn(APPLICATION_ERROR_CODES, message) ? APPLICATION_ERROR_CODES[message] : undefined;
|
|
13
|
+
return code ? t(code) : message;
|
|
14
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { PortalCurrencyTotals, StoreAffiliatePortalCommission } from "@hartl-services/medusa-contracts/types/affiliate-store";
|
|
2
|
+
import type { Translate } from "../i18n.js";
|
|
3
|
+
export interface AffiliateCommissionTotalRow {
|
|
4
|
+
attributedOrderCount: number;
|
|
5
|
+
commissionedOrderCount: number;
|
|
6
|
+
currencyCode: string;
|
|
7
|
+
}
|
|
8
|
+
export declare function getAffiliateCommissionLabel(type: StoreAffiliatePortalCommission["type"], t: Translate): string;
|
|
9
|
+
export interface AffiliateCommissionSumRow {
|
|
10
|
+
commissionAmount: number;
|
|
11
|
+
commissionBaseAmount: number;
|
|
12
|
+
currencyCode: string;
|
|
13
|
+
}
|
|
14
|
+
export declare function toAffiliateCommissionSumRows(totals: PortalCurrencyTotals[]): AffiliateCommissionSumRow[];
|
|
15
|
+
export declare function toAffiliateCommissionTotalRows(totals: PortalCurrencyTotals[]): AffiliateCommissionTotalRow[];
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
const affiliateCommissionLabels = {
|
|
2
|
+
affiliate_referral_bonus: "affiliate_commission.affiliate_referral_bonus",
|
|
3
|
+
affiliate_own_order_referrer_bonus: "affiliate_commission.affiliate_own_order_referrer_bonus",
|
|
4
|
+
cancellation_correction: "affiliate_commission.cancellation_correction",
|
|
5
|
+
manual_correction: "affiliate_commission.manual_correction",
|
|
6
|
+
order_commission: "affiliate_commission.order_commission",
|
|
7
|
+
refund_correction: "affiliate_commission.refund_correction",
|
|
8
|
+
};
|
|
9
|
+
export function getAffiliateCommissionLabel(type, t) {
|
|
10
|
+
return t(affiliateCommissionLabels[type]);
|
|
11
|
+
}
|
|
12
|
+
// One sum row per currency for the table footer. Taken from the backend's
|
|
13
|
+
// period totals, not summed over the visible page: the table is paginated,
|
|
14
|
+
// and the sum must always describe the whole selected period.
|
|
15
|
+
export function toAffiliateCommissionSumRows(totals) {
|
|
16
|
+
return totals.map((total) => ({
|
|
17
|
+
commissionAmount: total.net_commission_amount,
|
|
18
|
+
commissionBaseAmount: total.commission_base_amount,
|
|
19
|
+
currencyCode: total.currency_code,
|
|
20
|
+
}));
|
|
21
|
+
}
|
|
22
|
+
export function toAffiliateCommissionTotalRows(totals) {
|
|
23
|
+
return totals.map((total) => ({
|
|
24
|
+
attributedOrderCount: total.attributed_order_count,
|
|
25
|
+
commissionedOrderCount: total.commissioned_order_count,
|
|
26
|
+
currencyCode: total.currency_code,
|
|
27
|
+
}));
|
|
28
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { AffiliateCouponOrigin, AffiliateCouponWireStatus } from "@hartl-services/medusa-contracts/types/affiliate-store";
|
|
2
|
+
import type { Translate } from "../i18n.js";
|
|
3
|
+
/** How prominent a coupon status is; the shop maps it to its own chip style. */
|
|
4
|
+
export type AffiliateCouponStatusTone = "dark" | "destructive" | "outline";
|
|
5
|
+
export declare function getAffiliateCouponStatusPresentation(status: AffiliateCouponWireStatus, t: Translate): {
|
|
6
|
+
label: string;
|
|
7
|
+
tone: AffiliateCouponStatusTone;
|
|
8
|
+
};
|
|
9
|
+
export declare function getAffiliateCouponOriginLabel(origin: AffiliateCouponOrigin, t: Translate): string;
|
|
10
|
+
export declare function canManageAffiliateCoupon(status: AffiliateCouponWireStatus): status is "active" | "affiliate_inactive" | "provisioning" | "provisioning_failed";
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const couponStatusPresentation = {
|
|
2
|
+
active: { code: "affiliate_coupon.status.active", tone: "dark" },
|
|
3
|
+
affiliate_inactive: { code: "affiliate_coupon.status.affiliate_inactive", tone: "outline" },
|
|
4
|
+
provisioning: { code: "affiliate_coupon.status.provisioning", tone: "outline" },
|
|
5
|
+
provisioning_failed: { code: "affiliate_coupon.status.provisioning_failed", tone: "destructive" },
|
|
6
|
+
revoked: { code: "affiliate_coupon.status.revoked", tone: "outline" },
|
|
7
|
+
};
|
|
8
|
+
const couponOriginLabels = {
|
|
9
|
+
default: "affiliate_coupon.origin.default",
|
|
10
|
+
manual: "affiliate_coupon.origin.manual",
|
|
11
|
+
rotation: "affiliate_coupon.origin.rotation",
|
|
12
|
+
};
|
|
13
|
+
export function getAffiliateCouponStatusPresentation(status, t) {
|
|
14
|
+
const { code, tone } = couponStatusPresentation[status];
|
|
15
|
+
return { label: t(code), tone };
|
|
16
|
+
}
|
|
17
|
+
export function getAffiliateCouponOriginLabel(origin, t) {
|
|
18
|
+
return t(couponOriginLabels[origin]);
|
|
19
|
+
}
|
|
20
|
+
export function canManageAffiliateCoupon(status) {
|
|
21
|
+
return status !== "revoked";
|
|
22
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { AffiliateCouponLabel } from "@hartl-services/medusa-contracts/types/affiliate-store";
|
|
2
|
+
import type { Translate } from "../i18n.js";
|
|
3
|
+
export declare function getCouponDisplayLabel(code: string, labels: AffiliateCouponLabel[] | undefined, fallback: string, t: Translate): string;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
const apiDatePattern = /^\d{4}-\d{2}-\d{2}$/;
|
|
2
|
+
function formatUtcDate(year, month, day) {
|
|
3
|
+
return `${year}-${String(month).padStart(2, "0")}-${String(day).padStart(2, "0")}`;
|
|
4
|
+
}
|
|
5
|
+
export function isValidAffiliateDateRange(range) {
|
|
6
|
+
return apiDatePattern.test(range.from) && apiDatePattern.test(range.to) && range.from <= range.to;
|
|
7
|
+
}
|
|
8
|
+
export function getCurrentUtcMonthRange(now) {
|
|
9
|
+
const year = now.getUTCFullYear();
|
|
10
|
+
const month = now.getUTCMonth();
|
|
11
|
+
const lastDay = new Date(Date.UTC(year, month + 1, 0)).getUTCDate();
|
|
12
|
+
return {
|
|
13
|
+
from: formatUtcDate(year, month + 1, 1),
|
|
14
|
+
to: formatUtcDate(year, month + 1, lastDay),
|
|
15
|
+
};
|
|
16
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function formatAffiliateRate(rate: number, locale: string): string;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// Affiliate rates arrive from the backend as whole percents (10 means 10 %),
|
|
2
|
+
// so they are divided before Intl formats them as a percentage.
|
|
3
|
+
export function formatAffiliateRate(rate, locale) {
|
|
4
|
+
return new Intl.NumberFormat(locale, {
|
|
5
|
+
maximumFractionDigits: 2,
|
|
6
|
+
style: "percent",
|
|
7
|
+
}).format(rate / 100);
|
|
8
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { CustomerAddressTaxIdentification } from "@hartl-services/medusa-contracts/types/tax-compliance-store";
|
|
2
|
+
interface PartnerCompanyAddressSource {
|
|
3
|
+
address_1?: string | null;
|
|
4
|
+
city?: string | null;
|
|
5
|
+
company?: string | null;
|
|
6
|
+
country_code?: string | null;
|
|
7
|
+
metadata?: Record<string, unknown> | null;
|
|
8
|
+
postal_code?: string | null;
|
|
9
|
+
}
|
|
10
|
+
export type PartnerCompanyAddressKind = "verified" | "unverified" | "without_vat_number";
|
|
11
|
+
/**
|
|
12
|
+
* How a saved address can serve as the company address of a partner
|
|
13
|
+
* application, mirroring the backend's rule: company and address complete,
|
|
14
|
+
* and with a UID once that UID is verified, or while its check got no answer
|
|
15
|
+
* (`unverified`: FinanzOnline unreachable; the shop checks again before
|
|
16
|
+
* approval); without a UID the applicant confirms having none. While the
|
|
17
|
+
* shop's UID check is off (`checksVatNumber` false) a UID is taken unchecked
|
|
18
|
+
* (`unverified`). `null`: not usable yet (incomplete, not checked, or the UID
|
|
19
|
+
* is invalid).
|
|
20
|
+
*/
|
|
21
|
+
export declare function getPartnerCompanyAddressKind(address: PartnerCompanyAddressSource, identification: CustomerAddressTaxIdentification | undefined, checksVatNumber: boolean): PartnerCompanyAddressKind | null;
|
|
22
|
+
/** True when a check result lets an address with UID be used for the application. */
|
|
23
|
+
export declare function isUsablePartnerIdentification(identification: Pick<CustomerAddressTaxIdentification, "status" | "verified"> | null | undefined): boolean;
|
|
24
|
+
export {};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { getAddressVatNumber } from "../addresses.js";
|
|
2
|
+
/**
|
|
3
|
+
* How a saved address can serve as the company address of a partner
|
|
4
|
+
* application, mirroring the backend's rule: company and address complete,
|
|
5
|
+
* and with a UID once that UID is verified, or while its check got no answer
|
|
6
|
+
* (`unverified`: FinanzOnline unreachable; the shop checks again before
|
|
7
|
+
* approval); without a UID the applicant confirms having none. While the
|
|
8
|
+
* shop's UID check is off (`checksVatNumber` false) a UID is taken unchecked
|
|
9
|
+
* (`unverified`). `null`: not usable yet (incomplete, not checked, or the UID
|
|
10
|
+
* is invalid).
|
|
11
|
+
*/
|
|
12
|
+
export function getPartnerCompanyAddressKind(address, identification, checksVatNumber) {
|
|
13
|
+
const isComplete = [address.company, address.address_1, address.postal_code, address.city, address.country_code].every((value) => Boolean(value?.trim()));
|
|
14
|
+
if (!isComplete) {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
if (!getAddressVatNumber(address).trim()) {
|
|
18
|
+
return "without_vat_number";
|
|
19
|
+
}
|
|
20
|
+
if (!checksVatNumber) {
|
|
21
|
+
return "unverified";
|
|
22
|
+
}
|
|
23
|
+
if (!isUsablePartnerIdentification(identification)) {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
return identification?.verified ? "verified" : "unverified";
|
|
27
|
+
}
|
|
28
|
+
/** True when a check result lets an address with UID be used for the application. */
|
|
29
|
+
export function isUsablePartnerIdentification(identification) {
|
|
30
|
+
return Boolean(identification?.verified || identification?.status === "unavailable");
|
|
31
|
+
}
|