@thorprovider/storefront 0.7.0 → 0.10.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.
Files changed (164) hide show
  1. package/README.md +22 -2
  2. package/assets/templates/AGENTS.md +14 -2
  3. package/assets/templates/app/app/[[...slug]]/page.tsx +9 -2
  4. package/assets/templates/app/lib/__STOREFRONT__/account-address.ts +129 -0
  5. package/assets/templates/app/lib/__STOREFRONT__/account-lifecycle.client.tsx +2 -2
  6. package/assets/templates/app/lib/__STOREFRONT__/account-private.core.ts +143 -0
  7. package/assets/templates/app/lib/__STOREFRONT__/account-profile.ts +220 -0
  8. package/assets/templates/app/lib/__STOREFRONT__/actions.ts +677 -80
  9. package/assets/templates/app/lib/__STOREFRONT__/addresses-port.ts +107 -0
  10. package/assets/templates/app/lib/__STOREFRONT__/commerce.ts +477 -38
  11. package/assets/templates/app/lib/__STOREFRONT__/confirmation.core.ts +240 -0
  12. package/assets/templates/app/lib/__STOREFRONT__/customer-profile-port.ts +103 -0
  13. package/assets/templates/app/lib/__STOREFRONT__/fulfillment-evidence-port.ts +31 -0
  14. package/assets/templates/app/lib/__STOREFRONT__/handlers.core.ts +21 -4
  15. package/assets/templates/app/lib/__STOREFRONT__/mappers.ts +6 -2
  16. package/assets/templates/app/lib/__STOREFRONT__/order-detail-mapper.ts +642 -0
  17. package/assets/templates/app/lib/__STOREFRONT__/order-detail-port.ts +25 -0
  18. package/assets/templates/app/lib/__STOREFRONT__/orders-port.ts +30 -0
  19. package/assets/templates/app/lib/__STOREFRONT__/ports.ts +28 -0
  20. package/assets/templates/app/lib/__STOREFRONT__/projection-ownership.client.tsx +4 -4
  21. package/assets/templates/app/lib/__STOREFRONT__/session.ts +24 -6
  22. package/assets/templates/app/lib/__STOREFRONT__/state.core.ts +21 -0
  23. package/assets/templates/app/lib/__STOREFRONT__/views/sections/address-book-advanced.client.tsx +29 -0
  24. package/assets/templates/app/lib/__STOREFRONT__/views/sections/checkout-commerce-v1.client.tsx +19 -0
  25. package/assets/templates/app/lib/__STOREFRONT__/views/sections/checkout-commerce-v1.tsx +16 -0
  26. package/assets/templates/app/lib/__STOREFRONT__/views/sections/customer-profile.client.tsx +27 -0
  27. package/assets/templates/app/lib/__STOREFRONT__/views/sections/index.ts +4 -1
  28. package/assets/templates/app/lib/__STOREFRONT__/views/sections/mega-menu-universal.client.tsx +19 -0
  29. package/assets/templates/app/lib/__STOREFRONT__/views/sections/mega-menu-universal.tsx +16 -0
  30. package/assets/templates/app/lib/__STOREFRONT__/views/sections/schema.address-book-advanced.ts +437 -0
  31. package/assets/templates/app/lib/__STOREFRONT__/views/sections/schema.checkout-commerce-v1.ts +398 -0
  32. package/assets/templates/app/lib/__STOREFRONT__/views/sections/schema.customer-profile.ts +138 -0
  33. package/assets/templates/app/lib/__STOREFRONT__/views/sections/schema.mega-menu-universal.ts +76 -0
  34. package/assets/templates/app/lib/__STOREFRONT__/views/sections/schema.universal-banner.ts +217 -0
  35. package/assets/templates/app/lib/__STOREFRONT__/views/sections/universal-banner.client.tsx +24 -0
  36. package/assets/templates/app/lib/__STOREFRONT__/views/sections/universal-banner.tsx +16 -0
  37. package/assets/templates/features/checkout/handlers.ts +24 -4
  38. package/assets/templates/features/customer-addresses/handlers.ts +11 -0
  39. package/assets/templates/features/customer-addresses/state.ts +33 -0
  40. package/assets/templates/features/customer-auth/state.ts +10 -0
  41. package/assets/templates/features/customer-profile/handlers.ts +11 -0
  42. package/assets/templates/features/customer-profile/state.ts +18 -0
  43. package/assets/templates/views/account/address-book-advanced.defaults.ts +534 -0
  44. package/assets/templates/views/account/assembly/assembly.json +36 -0
  45. package/assets/templates/views/account/assembly/index.skeleton.tsx +277 -0
  46. package/assets/templates/views/account/assembly/registry.skeleton.tsx +1057 -0
  47. package/assets/templates/views/account/assembly/slots/addresses/AddressBookAdvanced/binding.json +5 -0
  48. package/assets/templates/views/account/assembly/slots/addresses/AddressBookAdvanced/catalog.part +4 -0
  49. package/assets/templates/views/account/assembly/slots/addresses/AddressBookAdvanced/element.part +5 -0
  50. package/assets/templates/views/account/assembly/slots/addresses/AddressBookAdvanced/registry.part +1 -0
  51. package/assets/templates/views/account/assembly/slots/addresses/cards/catalog.part +5 -0
  52. package/assets/templates/views/account/assembly/slots/addresses/cards/element.part +10 -0
  53. package/assets/templates/views/account/assembly/slots/addresses/cards/registry.part +1 -0
  54. package/assets/templates/views/account/assembly/slots/login/card/catalog.part +4 -0
  55. package/assets/templates/views/account/assembly/slots/login/card/element.part +13 -0
  56. package/assets/templates/views/account/assembly/slots/login/card/registry.part +1 -0
  57. package/assets/templates/views/account/assembly/slots/orders/OrderHistory/catalog.part +5 -0
  58. package/assets/templates/views/account/assembly/slots/orders/OrderHistory/element.part +5 -0
  59. package/assets/templates/views/account/assembly/slots/orders/OrderHistory/registry.part +2 -0
  60. package/assets/templates/views/account/assembly/slots/orders/OrderHistoryAdvanced/binding.json +5 -0
  61. package/assets/templates/views/account/assembly/slots/orders/OrderHistoryAdvanced/catalog.part +5 -0
  62. package/assets/templates/views/account/assembly/slots/orders/OrderHistoryAdvanced/element.part +5 -0
  63. package/assets/templates/views/account/assembly/slots/orders/OrderHistoryAdvanced/registry.part +1 -0
  64. package/assets/templates/views/account/assembly/slots/profile/CustomerProfileAdvanced/binding.json +5 -0
  65. package/assets/templates/views/account/assembly/slots/profile/CustomerProfileAdvanced/catalog.part +4 -0
  66. package/assets/templates/views/account/assembly/slots/profile/CustomerProfileAdvanced/element.part +5 -0
  67. package/assets/templates/views/account/assembly/slots/profile/CustomerProfileAdvanced/registry.part +1 -0
  68. package/assets/templates/views/account/assembly/slots/profile/form/catalog.part +5 -0
  69. package/assets/templates/views/account/assembly/slots/profile/form/element.part +10 -0
  70. package/assets/templates/views/account/assembly/slots/profile/form/registry.part +1 -0
  71. package/assets/templates/views/account/assembly/slots/register/SignUpPage1/catalog.part +4 -0
  72. package/assets/templates/views/account/assembly/slots/register/SignUpPage1/element.part +13 -0
  73. package/assets/templates/views/account/assembly/slots/register/SignUpPage1/registry.part +1 -0
  74. package/assets/templates/views/account/assembly/slots/summary/stack/catalog.part +5 -0
  75. package/assets/templates/views/account/assembly/slots/summary/stack/element.part +5 -0
  76. package/assets/templates/views/account/assembly/slots/summary/stack/registry.part +1 -0
  77. package/assets/templates/views/account/assembly/slots/summary/tabs/catalog.part +5 -0
  78. package/assets/templates/views/account/assembly/slots/summary/tabs/element.part +5 -0
  79. package/assets/templates/views/account/assembly/slots/summary/tabs/registry.part +1 -0
  80. package/assets/templates/views/account/content.ts +326 -0
  81. package/assets/templates/views/account/customer-profile.defaults.ts +136 -0
  82. package/assets/templates/views/account/fulfillment-evidence.client.tsx +34 -0
  83. package/assets/templates/views/account/fulfillment-evidence.defaults.ts +145 -0
  84. package/assets/templates/views/account/fulfillment-evidence.schema.ts +15 -0
  85. package/assets/templates/views/account/index.tsx +259 -38
  86. package/assets/templates/views/account/order-detail.client.tsx +35 -0
  87. package/assets/templates/views/account/order-detail.defaults.ts +457 -0
  88. package/assets/templates/views/account/orders-advanced.client.tsx +77 -0
  89. package/assets/templates/views/account/orders-advanced.defaults.ts +230 -0
  90. package/assets/templates/views/account/registry.tsx +935 -420
  91. package/assets/templates/views/account/schema.ts +50 -0
  92. package/assets/templates/views/account/sections-data.ts +2 -0
  93. package/assets/templates/views/cart/content.ts +1 -0
  94. package/assets/templates/views/category/assembly/index.skeleton.tsx +1 -0
  95. package/assets/templates/views/category/assembly/slots/filter/CategoryListing/element.part +1 -0
  96. package/assets/templates/views/category/content.ts +2 -0
  97. package/assets/templates/views/category/index.tsx +2 -0
  98. package/assets/templates/views/category/sections-data.ts +2 -0
  99. package/assets/templates/views/checkout/checkout-commerce-hosted.tsx +1089 -0
  100. package/assets/templates/views/checkout/content.ts +51 -0
  101. package/assets/templates/views/checkout/index.tsx +1 -1
  102. package/assets/templates/views/checkout/props.ts +10 -2
  103. package/assets/templates/views/checkout/registry.tsx +104 -42
  104. package/assets/templates/views/checkout/schema.ts +76 -1
  105. package/assets/templates/views/home/assembly/assembly.json +23 -2
  106. package/assets/templates/views/home/assembly/index.skeleton.tsx +4 -1
  107. package/assets/templates/views/home/assembly/registry.skeleton.tsx +1 -0
  108. package/assets/templates/views/home/assembly/slots/hero/UniversalBanner/binding.json +5 -0
  109. package/assets/templates/views/home/assembly/slots/hero/UniversalBanner/catalog.part +4 -0
  110. package/assets/templates/views/home/assembly/slots/hero/UniversalBanner/element.part +4 -0
  111. package/assets/templates/views/home/assembly/slots/hero/UniversalBanner/registry.part +10 -0
  112. package/assets/templates/views/home/content.ts +247 -1
  113. package/assets/templates/views/home/data.ts +10 -1
  114. package/assets/templates/views/home/index.tsx +8 -6
  115. package/assets/templates/views/home/props.ts +7 -1
  116. package/assets/templates/views/home/registry.tsx +11 -28
  117. package/assets/templates/views/home/schema.ts +163 -0
  118. package/assets/templates/views/home/sections-data.ts +2 -0
  119. package/assets/templates/views/product/content.ts +1 -0
  120. package/assets/templates/views/static/content.ts +1 -0
  121. package/bin/storefront.js +356 -27
  122. package/package.json +2 -1
  123. package/recipes/adapter-surface.json +159 -0
  124. package/recipes/archetypes.json +43 -4
  125. package/recipes/catalog.json +147 -2
  126. package/recipes/feature.schema.json +9 -0
  127. package/recipes/features.json +72 -1
  128. package/recipes/manifest.schema.json +5 -0
  129. package/recipes/recipe.schema.json +13 -1
  130. package/recipes/recipes.json +37 -7
  131. package/recipes/section.schema.json +112 -0
  132. package/recipes/sections.json +281 -4
  133. package/recipes/validate.mjs +83 -1
  134. package/scripts/adapter-surface.mjs +41 -0
  135. package/scripts/agent-blocks.mjs +700 -0
  136. package/scripts/block-validator.mjs +45 -0
  137. package/scripts/candidate.mjs +56 -14
  138. package/scripts/composers.mjs +7 -2
  139. package/scripts/default-sections.mjs +78 -0
  140. package/scripts/file-classes.mjs +18 -0
  141. package/scripts/generate-adapter-surface.mjs +148 -0
  142. package/scripts/operations.mjs +100 -0
  143. package/scripts/reconcile.mjs +67 -39
  144. package/scripts/section-configure.mjs +316 -0
  145. package/scripts/sync-assets.mjs +1 -0
  146. package/scripts/view-assembly.mjs +101 -14
  147. package/skills/sf-feature/SKILL.md +19 -0
  148. package/skills/sf-view-gen/SKILL.md +73 -7
  149. package/skills/sf-view-gen/assets/recipes/archetypes.json +43 -4
  150. package/skills/sf-view-gen/assets/recipes/catalog.json +147 -2
  151. package/skills/sf-view-gen/assets/recipes/features.json +72 -1
  152. package/skills/sf-view-gen/assets/recipes/recipes.json +37 -7
  153. package/skills/sf-view-gen/assets/recipes/section.schema.json +112 -0
  154. package/skills/sf-view-gen/assets/recipes/sections.json +281 -4
  155. package/skills/sf-view-gen/assets/templates/views/sections/checkout-commerce-v1.client.tsx +19 -0
  156. package/skills/sf-view-gen/assets/templates/views/sections/checkout-commerce-v1.tsx +16 -0
  157. package/skills/sf-view-gen/assets/templates/views/sections/mega-menu-universal.client.tsx +19 -0
  158. package/skills/sf-view-gen/assets/templates/views/sections/mega-menu-universal.tsx +16 -0
  159. package/skills/sf-view-gen/assets/templates/views/sections/schema.checkout-commerce-v1.ts +398 -0
  160. package/skills/sf-view-gen/assets/templates/views/sections/schema.mega-menu-universal.ts +76 -0
  161. package/skills/sf-view-gen/assets/templates/views/sections/schema.universal-banner.ts +217 -0
  162. package/skills/sf-view-gen/assets/templates/views/sections/universal-banner.client.tsx +24 -0
  163. package/skills/sf-view-gen/assets/templates/views/sections/universal-banner.tsx +16 -0
  164. package/skills/sf-view-gen/scripts/validate.mjs +83 -1
@@ -0,0 +1,240 @@
1
+ // Pure confirmation mapping: canonical Order → designer confirmation model.
2
+ // Always present, independent of whether the checkout view is installed. The
3
+ // host-confirmed order is the only source; optional fields are omitted when the
4
+ // backend does not report them. No React, Next, or view imports.
5
+
6
+ import type { Address, Money, Order } from '@thorprovider/types'
7
+ import { APPROVED_MANUAL_TEST_PROVIDER_ID } from '@thorprovider/runtime/commerce'
8
+
9
+ export interface StorefrontConfirmationAddress {
10
+ id: string
11
+ isDefault: boolean
12
+ label: string
13
+ recipient: string
14
+ line1: string
15
+ line2?: string
16
+ company?: string
17
+ city: string
18
+ province?: string
19
+ countryCode?: string
20
+ postalCode?: string
21
+ phone?: string
22
+ }
23
+
24
+ export interface StorefrontConfirmationLine {
25
+ lineId: string
26
+ title: string
27
+ variant?: string
28
+ image?: string
29
+ unitCents?: number
30
+ quantity: number
31
+ netTotalCents?: number
32
+ }
33
+
34
+ export interface StorefrontConfirmationGroup {
35
+ id: string
36
+ title: string
37
+ modeLabel: string
38
+ methodLabel: string
39
+ detailLabel: string
40
+ feeCents: number
41
+ lines: StorefrontConfirmationLine[]
42
+ }
43
+
44
+ export interface StorefrontConfirmationPromotion {
45
+ id: string
46
+ label: string
47
+ kind: string
48
+ amountCents: number
49
+ code?: string
50
+ }
51
+
52
+ export interface StorefrontConfirmation {
53
+ reference: string
54
+ totalCents: number
55
+ subtotalCents: number
56
+ unitCount?: number
57
+ promotions?: StorefrontConfirmationPromotion[]
58
+ taxCents?: number
59
+ taxRateBps?: number
60
+ shippingCents?: number
61
+ pickupCents?: number
62
+ extraCents?: number
63
+ extraLabel?: string
64
+ groups: StorefrontConfirmationGroup[]
65
+ address: StorefrontConfirmationAddress | null
66
+ paymentLabel: string
67
+ paymentDetail: string
68
+ billingLabel: string
69
+ billingDetail: string
70
+ customerName: string
71
+ customerEmail: string
72
+ locale: string
73
+ currencyCode: string
74
+ }
75
+
76
+ /** Canonical decimal Money → integer display cents; no rounding beyond cents. */
77
+ export function moneyToCents(value: Money | null | undefined): number | undefined {
78
+ if (!value || !value.currencyCode.trim()) return undefined
79
+ const match = /^(-?)(\d+)(?:\.(\d+))?$/.exec(value.amount.trim())
80
+ if (!match) return undefined
81
+ const [, sign, whole, fraction = ''] = match
82
+ let cents = BigInt(whole) * BigInt(100) + BigInt((fraction + '00').slice(0, 2))
83
+ if (fraction.length > 2 && fraction[2]! >= '5') cents += BigInt(1)
84
+ if (sign === '-') cents = -cents
85
+ if (cents > BigInt(Number.MAX_SAFE_INTEGER) || cents < BigInt(Number.MIN_SAFE_INTEGER)) {
86
+ return undefined
87
+ }
88
+ return Number(cents)
89
+ }
90
+
91
+ function addressDetail(address: Address): string {
92
+ return [
93
+ address.address1,
94
+ address.address2,
95
+ address.city,
96
+ address.province,
97
+ address.postalCode,
98
+ address.countryCode,
99
+ ]
100
+ .filter((value): value is string => typeof value === 'string' && value.trim() !== '')
101
+ .join(', ')
102
+ }
103
+
104
+ function normalizeImageUrl(url: string | null | undefined): string | undefined {
105
+ if (typeof url !== 'string') return undefined
106
+ const trimmed = url.trim()
107
+ return trimmed === '' ? undefined : trimmed
108
+ }
109
+
110
+ /**
111
+ * Build the designer confirmation from the host-confirmed order. The order is
112
+ * the single source: addresses, delivery method, payment label and totals are
113
+ * only included when the backend reports them; the view omits what is absent.
114
+ */
115
+ export function toStorefrontConfirmation(order: Order): StorefrontConfirmation {
116
+ const shippingAddress = order.shippingAddress
117
+ const recipient = shippingAddress
118
+ ? `${shippingAddress.firstName} ${shippingAddress.lastName}`.trim()
119
+ : ''
120
+ const customerName =
121
+ recipient ||
122
+ [order.customer?.firstName, order.customer?.lastName]
123
+ .filter((part): part is string => Boolean(part))
124
+ .join(' ') ||
125
+ 'Cliente'
126
+ const total = moneyToCents(order.total) ?? 0
127
+ const subtotal = moneyToCents(order.subtotal) ?? 0
128
+ const tax = moneyToCents(order.tax)
129
+ // The order's backend shipping/pickup total is authoritative; a single
130
+ // method's amount never substitutes for it.
131
+ const shippingTotal = moneyToCents(order.shipping) ?? moneyToCents(order.shippingMethod?.amount)
132
+ const discount = moneyToCents(order.discount)
133
+ const billing = order.billingAddress
134
+ const lines: StorefrontConfirmationLine[] = order.items.map((item) => {
135
+ const unitCents = moneyToCents(item.price)
136
+ const netTotalCents = moneyToCents(item.total)
137
+ return {
138
+ lineId: item.id,
139
+ title: item.title,
140
+ variant: item.variantTitle,
141
+ image: normalizeImageUrl(item.image?.url),
142
+ unitCents,
143
+ quantity: item.quantity,
144
+ netTotalCents,
145
+ }
146
+ })
147
+ const linesById = new Map(lines.map((line) => [line.lineId, line]))
148
+ const pickup =
149
+ order.deliveryGroups?.length
150
+ ? order.deliveryGroups.every((group) => group.mode === 'pickup')
151
+ : order.shippingMethod?.mode === 'pickup'
152
+ const paymentLabel =
153
+ order.payment?.providerId === APPROVED_MANUAL_TEST_PROVIDER_ID
154
+ ? 'Pago manual TEST'
155
+ : order.payment?.label ?? ''
156
+
157
+ // One confirmation entry per confirmed backend group, preserving each
158
+ // group's mode, method, detail, fee and line membership. Line membership is
159
+ // rendered only from the order's own data; membership the order does not
160
+ // expose is never invented, and a line claimed ambiguously by more than one
161
+ // group is omitted from every entry instead of assigned arbitrarily.
162
+ const membershipCounts = new Map<string, number>()
163
+ for (const group of order.deliveryGroups ?? []) {
164
+ for (const lineId of group.lineIds ?? []) {
165
+ membershipCounts.set(lineId, (membershipCounts.get(lineId) ?? 0) + 1)
166
+ }
167
+ }
168
+ const confirmedGroups = order.deliveryGroups?.length
169
+ ? order.deliveryGroups.map((group) => {
170
+ const pickupGroup = group.mode === 'pickup'
171
+ return {
172
+ id: group.id,
173
+ title: pickupGroup ? 'Recogida' : 'Entrega',
174
+ modeLabel: pickupGroup ? 'Recogida' : 'Envío a domicilio',
175
+ methodLabel: group.methodLabel ?? (pickupGroup ? 'Recogida' : 'Envío'),
176
+ detailLabel: group.methodDetail ?? '',
177
+ feeCents: moneyToCents(group.fee) ?? 0,
178
+ lines: (group.lineIds ?? [])
179
+ .filter((lineId) => membershipCounts.get(lineId) === 1)
180
+ .map((lineId) => linesById.get(lineId))
181
+ .filter((line): line is StorefrontConfirmationLine => line !== undefined),
182
+ }
183
+ })
184
+ : undefined
185
+
186
+ // Declared fallbacks for providers without group support: a single reported
187
+ // method is one group covering the whole order; without any method the
188
+ // delivery entry carries only the backend total, with no method claims.
189
+ const groups: StorefrontConfirmationGroup[] = confirmedGroups ?? [
190
+ {
191
+ id: 'delivery-1',
192
+ title: pickup ? 'Recogida' : 'Entrega',
193
+ modeLabel: pickup ? 'Recogida' : 'Envío a domicilio',
194
+ methodLabel: order.shippingMethod?.label ?? (pickup ? 'Recogida' : 'Envío'),
195
+ detailLabel: order.shippingMethod?.detail ?? '',
196
+ feeCents: moneyToCents(order.shippingMethod?.amount) ?? shippingTotal ?? 0,
197
+ lines,
198
+ },
199
+ ]
200
+ return {
201
+ reference: order.orderNumber,
202
+ totalCents: total,
203
+ subtotalCents: subtotal,
204
+ unitCount: order.items.reduce((sum, item) => sum + item.quantity, 0),
205
+ promotions:
206
+ discount && discount > 0
207
+ ? [{ id: 'discount', label: 'Descuento', kind: 'discount', amountCents: discount }]
208
+ : undefined,
209
+ taxCents: tax,
210
+ taxRateBps: order.taxRateBps,
211
+ shippingCents: shippingTotal,
212
+ groups,
213
+ address: shippingAddress
214
+ ? {
215
+ id: 'order-shipping-address',
216
+ isDefault: false,
217
+ label: shippingAddress.label || 'Dirección de entrega',
218
+ recipient,
219
+ line1: shippingAddress.address1 ?? '',
220
+ line2: shippingAddress.address2,
221
+ company: shippingAddress.company,
222
+ city: shippingAddress.city,
223
+ province: shippingAddress.province,
224
+ countryCode: shippingAddress.countryCode,
225
+ postalCode: shippingAddress.postalCode,
226
+ phone: shippingAddress.phone,
227
+ }
228
+ : null,
229
+ paymentLabel,
230
+ paymentDetail: order.payment?.detail ?? '',
231
+ billingLabel: billing ? billing.label || 'Dirección de facturación' : '',
232
+ billingDetail: billing ? addressDetail(billing) : '',
233
+ customerName,
234
+ customerEmail: order.email,
235
+ // The storefront's money presentation uses the design's en-US dollar
236
+ // format; copy stays in the store language.
237
+ locale: 'en-US',
238
+ currencyCode: order.total.currencyCode,
239
+ }
240
+ }
@@ -0,0 +1,103 @@
1
+ import { PortError } from '@thorprovider/blocks/ports'
2
+ import type {
3
+ CustomerProfilePort,
4
+ CustomerProfileSnapshot,
5
+ ProfileDraft,
6
+ VerificationChannel,
7
+ } from '@thorprovider/blocks/ports'
8
+
9
+ import {
10
+ customFieldsFromDraft,
11
+ formatPhoneNumber,
12
+ preferencesFromDraft,
13
+ personalMetadataFromDraft,
14
+ professionalFromDraft,
15
+ toCustomerProfileSnapshot,
16
+ } from './account-profile'
17
+ import {
18
+ loadAccountProfileAction,
19
+ updateAccountProfileAction,
20
+ updateAccountProfileMetadataAction,
21
+ } from './actions'
22
+
23
+ function text(draft: ProfileDraft, key: string): string {
24
+ const value = draft[key]
25
+ return typeof value === 'string' ? value.trim() : ''
26
+ }
27
+
28
+ function failed(error: string, code: 'invalid' | 'unavailable' = 'unavailable'): never {
29
+ throw new PortError(code, error)
30
+ }
31
+
32
+ /**
33
+ * Host seam for the customer-profile surface. Identity fields go through the
34
+ * existing account action; preferences, professional data and store-defined
35
+ * fields persist in `customer.metadata.profile`. Every mutation re-reads the
36
+ * snapshot so the block never renders a locally invented success state.
37
+ */
38
+ export function createCustomerProfilePort(): CustomerProfilePort {
39
+ async function loadSnapshot(): Promise<CustomerProfileSnapshot> {
40
+ const result = await loadAccountProfileAction()
41
+ if (!result.ok) failed(result.error)
42
+ return toCustomerProfileSnapshot(result.profile)
43
+ }
44
+
45
+ return {
46
+ load: loadSnapshot,
47
+
48
+ async updatePersonal(draft) {
49
+ const result = await updateAccountProfileAction({
50
+ firstName: text(draft, 'firstName'),
51
+ lastName: text(draft, 'lastName'),
52
+ profile: { person: personalMetadataFromDraft(draft) },
53
+ })
54
+ if (!result.ok) failed(result.error, 'invalid')
55
+ return loadSnapshot()
56
+ },
57
+
58
+ async updateContact(draft) {
59
+ const phone = formatPhoneNumber(
60
+ text(draft, 'phoneCountry'),
61
+ text(draft, 'phone'),
62
+ )
63
+ const result = await updateAccountProfileAction({
64
+ email: text(draft, 'email'),
65
+ phone: phone ?? '',
66
+ })
67
+ if (!result.ok) failed(result.error, 'invalid')
68
+ return loadSnapshot()
69
+ },
70
+
71
+ async updatePreferences(draft) {
72
+ const result = await updateAccountProfileMetadataAction({
73
+ preferences: preferencesFromDraft(draft),
74
+ })
75
+ if (!result.ok) failed(result.error)
76
+ return loadSnapshot()
77
+ },
78
+
79
+ async updateProfessional(draft) {
80
+ const result = await updateAccountProfileMetadataAction({
81
+ professional: professionalFromDraft(draft),
82
+ })
83
+ if (!result.ok) failed(result.error)
84
+ return loadSnapshot()
85
+ },
86
+
87
+ async updateCustomFields(draft) {
88
+ const current = await loadSnapshot()
89
+ const result = await updateAccountProfileMetadataAction({
90
+ customFields: customFieldsFromDraft(current.customFields, draft),
91
+ })
92
+ if (!result.ok) failed(result.error)
93
+ return loadSnapshot()
94
+ },
95
+
96
+ async requestVerification(channel: VerificationChannel) {
97
+ throw new PortError(
98
+ 'unavailable',
99
+ `The backend does not expose ${channel} verification yet.`,
100
+ )
101
+ },
102
+ }
103
+ }
@@ -0,0 +1,31 @@
1
+ import { PortError } from '@thorprovider/blocks/ports'
2
+ import type { FulfillmentEvidencePort } from '@thorprovider/blocks/ports'
3
+
4
+ export type FulfillmentEvidenceRunner = FulfillmentEvidencePort
5
+
6
+ /**
7
+ * Host seam for the fulfillment-evidence route. Until the evidence backend and
8
+ * signed asset resolution exist, both reads fail closed with a typed
9
+ * `unavailable` error; the landed gallery renders its own error state and never
10
+ * falls back to demo data. The backend owner supplies the runner and the route
11
+ * binding does not change.
12
+ */
13
+ export function createFulfillmentEvidencePort(
14
+ runner?: FulfillmentEvidenceRunner,
15
+ ): FulfillmentEvidencePort {
16
+ if (runner) return runner
17
+ return {
18
+ async getEvidence(fulfillmentId) {
19
+ throw new PortError(
20
+ 'unavailable',
21
+ `Fulfillment evidence is not connected to the backend yet (requested ${fulfillmentId}).`,
22
+ )
23
+ },
24
+ async resolveAssetUrl(assetId) {
25
+ throw new PortError(
26
+ 'unavailable',
27
+ `Evidence asset resolution is not connected to the backend yet (requested ${assetId}).`,
28
+ )
29
+ },
30
+ }
31
+ }
@@ -87,9 +87,18 @@ export function useCoreHandlers() {
87
87
  // A confirmed cart transition — observed only after the mutation
88
88
  // succeeded, and only when a section origin exists (default: off).
89
89
  recordConfirmedTransition('cart_add_confirmed')
90
- } else fail(result.error)
90
+ } else {
91
+ // A refused add (out of stock, no purchasable variant) must be
92
+ // visible: open the sheet so its error label renders instead of
93
+ // failing silently — which read as "the previous item replaced".
94
+ set('/cartOpen', true)
95
+ fail(result.error)
96
+ }
97
+ })
98
+ .catch((error) => {
99
+ set('/cartOpen', true)
100
+ onError('addToCart')(error)
91
101
  })
92
- .catch(onError('addToCart'))
93
102
  },
94
103
  addClickedToCart: () => {
95
104
  // Product-card shortcut: add the clicked product's first purchasable
@@ -104,9 +113,17 @@ export function useCoreHandlers() {
104
113
  applyCart(result.cart)
105
114
  set('/cartOpen', true)
106
115
  recordConfirmedTransition('cart_add_confirmed')
107
- } else fail(result.error)
116
+ } else {
117
+ // Same visibility contract as the explicit add: a refused add
118
+ // opens the sheet so the error is seen, never silent.
119
+ set('/cartOpen', true)
120
+ fail(result.error)
121
+ }
122
+ })
123
+ .catch((error) => {
124
+ set('/cartOpen', true)
125
+ onError('addClickedToCart')(error)
108
126
  })
109
- .catch(onError('addClickedToCart'))
110
127
  },
111
128
  ensureCart: () => {
112
129
  begin()
@@ -21,11 +21,15 @@ export function toFeaturedProduct(card: CardProduct): FeaturedProduct {
21
21
  price: card.price,
22
22
  // Only a real compare-at price is forwarded: the block renders a discount
23
23
  // treatment solely from an `originalPrice > price` pair, and never from a
24
- // fabricated one. Popularity/review/stock fields are omitted — the block
25
- // renders nothing for them instead of claiming `rating: 0` or `inStock`.
24
+ // fabricated one. Popularity/review fields are omitted — the block renders
25
+ // nothing for them instead of claiming `rating: 0`. Availability is
26
+ // forwarded only from real backend evidence (`inStock` on the card): an
27
+ // explicit `false` disables the hero's purchase buttons; absence makes no
28
+ // stock claim (never invented).
26
29
  originalPrice: card.originalPrice,
27
30
  image: card.image ?? '',
28
31
  tag: card.badge,
32
+ ...(card.inStock !== undefined ? { inStock: card.inStock } : {}),
29
33
  }
30
34
  }
31
35