@thorprovider/storefront 0.9.0 → 0.11.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/README.md +8 -5
- package/assets/templates/app/app/globals.css +0 -1
- package/assets/templates/app/lib/__STOREFRONT__/account-session.core.ts +4 -2
- package/assets/templates/app/lib/__STOREFRONT__/account-user.core.ts +2 -1
- package/assets/templates/app/lib/__STOREFRONT__/actions.ts +130 -12
- package/assets/templates/app/lib/__STOREFRONT__/commerce.ts +156 -9
- package/assets/templates/app/lib/__STOREFRONT__/confirmation.core.ts +38 -3
- package/assets/templates/app/lib/__STOREFRONT__/fulfillment-evidence-port.ts +31 -0
- package/assets/templates/app/lib/__STOREFRONT__/handlers.core.ts +21 -4
- package/assets/templates/app/lib/__STOREFRONT__/mappers.ts +6 -2
- package/assets/templates/app/lib/__STOREFRONT__/order-detail-aftersales.ts +55 -0
- package/assets/templates/app/lib/__STOREFRONT__/order-detail-capabilities.ts +79 -0
- package/assets/templates/app/lib/__STOREFRONT__/order-detail-mapper.ts +653 -0
- package/assets/templates/app/lib/__STOREFRONT__/order-detail-money.ts +39 -0
- package/assets/templates/app/lib/__STOREFRONT__/order-detail-port.ts +6 -7
- package/assets/templates/app/lib/__STOREFRONT__/order-detail-reorder.ts +62 -0
- package/assets/templates/app/lib/__STOREFRONT__/ports.ts +13 -3
- package/assets/templates/views/account/assembly/assembly.json +0 -5
- package/assets/templates/views/account/assembly/index.skeleton.tsx +51 -0
- package/assets/templates/views/account/assembly/registry.skeleton.tsx +120 -68
- package/assets/templates/views/account/content.ts +13 -7
- package/assets/templates/views/account/fulfillment-evidence.client.tsx +34 -0
- package/assets/templates/views/account/fulfillment-evidence.defaults.ts +145 -0
- package/assets/templates/views/account/fulfillment-evidence.schema.ts +15 -0
- package/assets/templates/views/account/index.tsx +44 -3
- package/assets/templates/views/account/order-detail-flows.defaults.ts +106 -0
- package/assets/templates/views/account/order-detail-invoice.ts +136 -0
- package/assets/templates/views/account/order-detail-sheet.tsx +100 -0
- package/assets/templates/views/account/order-detail-sheets.tsx +122 -0
- package/assets/templates/views/account/order-detail.client.tsx +401 -1
- package/assets/templates/views/account/orders-advanced.client.tsx +10 -99
- package/assets/templates/views/account/orders-advanced.defaults.ts +7 -7
- package/assets/templates/views/account/registry.tsx +120 -69
- package/assets/templates/views/account/schema.ts +2 -0
- package/assets/templates/views/home/assembly/assembly.json +1 -1
- package/assets/templates/views/home/assembly/index.skeleton.tsx +3 -1
- package/assets/templates/views/home/assembly/registry.skeleton.tsx +1 -0
- package/assets/templates/views/home/assembly/slots/hero/UniversalBanner/binding.json +5 -0
- package/assets/templates/views/home/assembly/slots/hero/UniversalBanner/catalog.part +4 -0
- package/assets/templates/views/home/assembly/slots/hero/UniversalBanner/element.part +4 -0
- package/assets/templates/views/home/assembly/slots/hero/UniversalBanner/registry.part +10 -0
- package/assets/templates/views/home/content.ts +248 -2
- package/assets/templates/views/home/data.ts +10 -1
- package/assets/templates/views/home/index.tsx +7 -6
- package/assets/templates/views/home/props.ts +7 -1
- package/assets/templates/views/home/registry.tsx +11 -28
- package/assets/templates/views/home/schema.ts +163 -0
- package/assets/templates/views/product/data.ts +13 -6
- package/assets/templates/views/product/index.tsx +3 -3
- package/bin/storefront.js +18 -18
- package/package.json +1 -1
- package/recipes/adapter-surface.json +1 -0
- package/recipes/archetypes.json +5 -1
- package/recipes/catalog.json +9 -1
- package/recipes/recipes.json +5 -4
- package/scripts/candidate.mjs +59 -14
- package/scripts/file-classes.mjs +4 -0
- package/scripts/mock-capabilities.mjs +22 -0
- package/scripts/reconcile.mjs +5 -2
- package/scripts/render-templates.mjs +126 -43
- package/skills/sf-view-gen/assets/recipes/archetypes.json +5 -1
- package/skills/sf-view-gen/assets/recipes/catalog.json +9 -1
- package/skills/sf-view-gen/assets/recipes/recipes.json +5 -4
- package/assets/templates/views/account/assembly/slots/boot/AccountBoot/catalog.part +0 -5
- package/assets/templates/views/account/assembly/slots/boot/AccountBoot/element.part +0 -5
- package/assets/templates/views/account/assembly/slots/boot/AccountBoot/registry.part +0 -1
|
@@ -0,0 +1,653 @@
|
|
|
1
|
+
// Generated by Thor Commerce storefront (template: assets/templates/app/lib/__STOREFRONT__/order-detail-mapper.ts)
|
|
2
|
+
//
|
|
3
|
+
// The host-side projection from the universal `Order` the provider reports to
|
|
4
|
+
// the order-detail surface's snapshot contract. It is pure and server-safe:
|
|
5
|
+
// no React, no Next.js, no transport. Every displayed amount comes from the
|
|
6
|
+
// order the provider returned; the mapper only converts the decimal money
|
|
7
|
+
// envelope to integer minor units and groups the existing fields.
|
|
8
|
+
//
|
|
9
|
+
// Honest limits: the universal order envelope does not carry per-shipment
|
|
10
|
+
// allocations, transactions or after-sales cases. A provider that has a richer
|
|
11
|
+
// detail contract replaces this factory later; until then the mapper derives
|
|
12
|
+
// only what the envelope states (aggregate fulfilment, the single payment
|
|
13
|
+
// status, the order's own lines/addresses/totals) and never invents a line, a
|
|
14
|
+
// charge or a section.
|
|
15
|
+
|
|
16
|
+
import type {
|
|
17
|
+
Address as CommerceAddress,
|
|
18
|
+
Money as CommerceMoney,
|
|
19
|
+
Order,
|
|
20
|
+
} from '@thorprovider/types'
|
|
21
|
+
import type {
|
|
22
|
+
Adjustment,
|
|
23
|
+
AfterSalesCase,
|
|
24
|
+
Fulfillment,
|
|
25
|
+
OrderDetailFulfillmentStatus,
|
|
26
|
+
LogisticsGroup,
|
|
27
|
+
OrderDetailMoney,
|
|
28
|
+
OrderDetailActionId,
|
|
29
|
+
OrderDetailOrder,
|
|
30
|
+
OrderDetailSnapshot,
|
|
31
|
+
OrderLine,
|
|
32
|
+
TaxLine,
|
|
33
|
+
Transaction,
|
|
34
|
+
} from '@thorprovider/blocks/ports'
|
|
35
|
+
import { orderDetailDemoDocuments, projectHostActions } from './order-detail-capabilities'
|
|
36
|
+
|
|
37
|
+
type Money = OrderDetailMoney
|
|
38
|
+
type FulfillmentStatus = OrderDetailFulfillmentStatus
|
|
39
|
+
type OrderActionId = OrderDetailActionId
|
|
40
|
+
|
|
41
|
+
const MOCK_TRACKING_CARRIER = 'Thor Express'
|
|
42
|
+
const MOCK_PICKUP = {
|
|
43
|
+
locationName: 'Thor Store · Centro',
|
|
44
|
+
address: 'Av. Reforma 100, Ciudad de México',
|
|
45
|
+
schedule: 'Lun–Sáb · 10:00–19:00',
|
|
46
|
+
} as const
|
|
47
|
+
|
|
48
|
+
type LifecycleHint = {
|
|
49
|
+
fulfilment?: 'shipped' | 'delayed' | 'ready_for_pickup' | 'partially_delivered'
|
|
50
|
+
returns?: 'none' | 'requested' | 'in_progress' | 'completed'
|
|
51
|
+
payment?: 'partially_paid'
|
|
52
|
+
/** Mock fixture: captured amount for a partially paid order (major units). */
|
|
53
|
+
paidAmount?: number
|
|
54
|
+
/** Mock fixture: refunded amount for a partially refunded order (major units). */
|
|
55
|
+
refundAmount?: number
|
|
56
|
+
creditTerms?: boolean
|
|
57
|
+
partiallyCancelled?: boolean
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function readLifecycleHint(order: Order): LifecycleHint {
|
|
61
|
+
const raw = order.metadata?.orderHistory
|
|
62
|
+
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) return {}
|
|
63
|
+
return raw as LifecycleHint
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function currencyDigits(currency: string): number {
|
|
67
|
+
try {
|
|
68
|
+
return (
|
|
69
|
+
new Intl.NumberFormat('en', { style: 'currency', currency }).resolvedOptions()
|
|
70
|
+
.maximumFractionDigits ?? 2
|
|
71
|
+
)
|
|
72
|
+
} catch {
|
|
73
|
+
return 2
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** The backend decimal string as integer minor units, the block's money shape. */
|
|
78
|
+
export function toDetailMoney(
|
|
79
|
+
value: CommerceMoney | undefined,
|
|
80
|
+
currency: string,
|
|
81
|
+
): Money {
|
|
82
|
+
const code = value?.currencyCode ?? currency
|
|
83
|
+
const amount = Number(value?.amount ?? 0)
|
|
84
|
+
return {
|
|
85
|
+
amount: Number.isFinite(amount) ? Math.round(amount * 10 ** currencyDigits(code)) : 0,
|
|
86
|
+
currency: code,
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function zero(currency: string): Money {
|
|
91
|
+
return { amount: 0, currency }
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function toDetailAddress(address: CommerceAddress | undefined): OrderDetailOrder['shippingAddress'] {
|
|
95
|
+
if (!address) return undefined
|
|
96
|
+
const name = [address.firstName, address.lastName].filter(Boolean).join(' ').trim()
|
|
97
|
+
if (!name || !address.address1 || !address.city || !address.countryCode) return undefined
|
|
98
|
+
return {
|
|
99
|
+
name,
|
|
100
|
+
line1: address.address1,
|
|
101
|
+
...(address.address2 ? { line2: address.address2 } : {}),
|
|
102
|
+
city: address.city,
|
|
103
|
+
...(address.province ? { region: address.province } : {}),
|
|
104
|
+
postalCode: address.postalCode ?? '',
|
|
105
|
+
country: address.countryCode,
|
|
106
|
+
...(address.phone ? { phone: address.phone } : {}),
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function toCommercial(order: Order): OrderDetailOrder['commercial'] {
|
|
111
|
+
switch (order.status) {
|
|
112
|
+
case 'pending':
|
|
113
|
+
case 'processing':
|
|
114
|
+
case 'completed':
|
|
115
|
+
case 'cancelled':
|
|
116
|
+
return order.status
|
|
117
|
+
// A refunded order is finished; the refund is carried by its transactions.
|
|
118
|
+
case 'refunded':
|
|
119
|
+
return 'completed'
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function toFulfillmentStatus(order: Order, hint: LifecycleHint): FulfillmentStatus {
|
|
124
|
+
if (order.status === 'cancelled') return 'cancelled'
|
|
125
|
+
switch (order.fulfillmentStatus) {
|
|
126
|
+
case 'unfulfilled':
|
|
127
|
+
if (hint.fulfilment === 'ready_for_pickup') return 'ready_for_pickup'
|
|
128
|
+
return order.status === 'processing' ? 'preparing' : 'pending'
|
|
129
|
+
case 'partially_fulfilled':
|
|
130
|
+
return 'partially_delivered'
|
|
131
|
+
case 'fulfilled':
|
|
132
|
+
return order.completedAt ? 'delivered' : 'shipped'
|
|
133
|
+
case 'returned':
|
|
134
|
+
return 'delivered'
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
type GroupSource = {
|
|
139
|
+
id: string
|
|
140
|
+
mode: 'shipping' | 'pickup'
|
|
141
|
+
methodName: string
|
|
142
|
+
fee: CommerceMoney | undefined
|
|
143
|
+
lineIds: string[]
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function resolveGroupSources(order: Order): GroupSource[] {
|
|
147
|
+
const allLineIds = order.items.map((item) => item.id)
|
|
148
|
+
if (Array.isArray(order.deliveryGroups) && order.deliveryGroups.length > 0) {
|
|
149
|
+
return order.deliveryGroups.map((group, index) => ({
|
|
150
|
+
id: group.id,
|
|
151
|
+
mode: group.mode === 'pickup' ? 'pickup' : 'shipping',
|
|
152
|
+
methodName:
|
|
153
|
+
group.methodLabel ?? (group.mode === 'pickup' ? 'Recogida en tienda' : 'Envío estándar'),
|
|
154
|
+
fee: group.fee ?? (index === 0 ? order.shipping : undefined),
|
|
155
|
+
lineIds:
|
|
156
|
+
Array.isArray(group.lineIds) && group.lineIds.length > 0 ? group.lineIds : allLineIds,
|
|
157
|
+
}))
|
|
158
|
+
}
|
|
159
|
+
return [
|
|
160
|
+
{
|
|
161
|
+
id: `${order.id}-group-default`,
|
|
162
|
+
mode: order.shippingMethod?.mode === 'pickup' ? 'pickup' : 'shipping',
|
|
163
|
+
methodName: order.shippingMethod?.label ?? 'Envío estándar',
|
|
164
|
+
fee: order.shipping,
|
|
165
|
+
lineIds: allLineIds,
|
|
166
|
+
},
|
|
167
|
+
]
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function toOrderLine(
|
|
171
|
+
item: Order['items'][number],
|
|
172
|
+
currency: string,
|
|
173
|
+
cancelledQuantity: number,
|
|
174
|
+
): OrderLine {
|
|
175
|
+
const chargeable = Math.max(0, item.quantity - cancelledQuantity)
|
|
176
|
+
const unit = toDetailMoney(item.price, currency)
|
|
177
|
+
const lineTotal: Money = { amount: unit.amount * chargeable, currency }
|
|
178
|
+
return {
|
|
179
|
+
id: item.id,
|
|
180
|
+
...(item.productId ? { productId: item.productId } : {}),
|
|
181
|
+
name: item.title,
|
|
182
|
+
sku: item.sku ?? item.variantId ?? item.id,
|
|
183
|
+
...(item.image?.url ? { image: item.image.url } : {}),
|
|
184
|
+
...(item.variantTitle ? { variant: item.variantTitle } : {}),
|
|
185
|
+
quantity: item.quantity,
|
|
186
|
+
cancelledQuantity,
|
|
187
|
+
unitPrice: { original: unit, applied: unit, kind: 'list' },
|
|
188
|
+
subtotal: lineTotal,
|
|
189
|
+
discounts: [],
|
|
190
|
+
taxes: [] as TaxLine[],
|
|
191
|
+
total: lineTotal,
|
|
192
|
+
attributes: [],
|
|
193
|
+
kind: 'physical',
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function calculateTax(order: Order, currency: string, orderDiscount: number): number {
|
|
198
|
+
if (order.tax) return toDetailMoney(order.tax, currency).amount
|
|
199
|
+
const subtotal = toDetailMoney(order.subtotal, currency).amount
|
|
200
|
+
const shipping = toDetailMoney(order.shipping, currency).amount
|
|
201
|
+
const total = toDetailMoney(order.total, currency).amount
|
|
202
|
+
return Math.max(0, total - subtotal - shipping + orderDiscount)
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function allocateLines(
|
|
206
|
+
lines: OrderLine[],
|
|
207
|
+
group: GroupSource,
|
|
208
|
+
status: FulfillmentStatus,
|
|
209
|
+
): { lineId: string; quantity: number }[] {
|
|
210
|
+
const groupLines = lines.filter((line) => group.lineIds.includes(line.id))
|
|
211
|
+
const chargeable = (line: OrderLine) => Math.max(0, line.quantity - line.cancelledQuantity)
|
|
212
|
+
if (status === 'pending' || status === 'preparing' || status === 'ready_for_pickup' || status === 'cancelled') {
|
|
213
|
+
return []
|
|
214
|
+
}
|
|
215
|
+
if (status === 'partially_delivered') {
|
|
216
|
+
const first = groupLines.find((line) => chargeable(line) > 0)
|
|
217
|
+
return first ? [{ lineId: first.id, quantity: chargeable(first) }] : []
|
|
218
|
+
}
|
|
219
|
+
return groupLines
|
|
220
|
+
.filter((line) => chargeable(line) > 0)
|
|
221
|
+
.map((line) => ({ lineId: line.id, quantity: chargeable(line) }))
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function toFulfillment(
|
|
225
|
+
order: Order,
|
|
226
|
+
group: GroupSource,
|
|
227
|
+
status: FulfillmentStatus,
|
|
228
|
+
lines: OrderLine[],
|
|
229
|
+
isMock: boolean,
|
|
230
|
+
): Fulfillment {
|
|
231
|
+
const allocations = allocateLines(lines, group, status)
|
|
232
|
+
const sent = status !== 'pending' && status !== 'preparing' && status !== 'ready_for_pickup' && status !== 'cancelled'
|
|
233
|
+
const shippedAt = sent ? order.updatedAt : undefined
|
|
234
|
+
const deliveredAt = status === 'delivered' || status === 'picked_up' ? order.completedAt : undefined
|
|
235
|
+
const trackingNumber = isMock && (status === 'shipped' || status === 'delivered')
|
|
236
|
+
? `THOR-${order.orderNumber}-${group.mode === 'pickup' ? 'P' : 'S'}`
|
|
237
|
+
: undefined
|
|
238
|
+
return {
|
|
239
|
+
id: `${group.id}-fulfillment`,
|
|
240
|
+
groupId: group.id,
|
|
241
|
+
status,
|
|
242
|
+
...(isMock && status !== 'cancelled' && group.mode === 'shipping'
|
|
243
|
+
? { carrier: MOCK_TRACKING_CARRIER }
|
|
244
|
+
: {}),
|
|
245
|
+
...(trackingNumber ? { trackingNumber } : {}),
|
|
246
|
+
...(shippedAt ? { shippedAt } : {}),
|
|
247
|
+
...(deliveredAt ? { deliveredAt } : {}),
|
|
248
|
+
packages:
|
|
249
|
+
isMock && sent && group.mode === 'shipping'
|
|
250
|
+
? [{ id: `${group.id}-package`, label: 'Paquete 1' }]
|
|
251
|
+
: [],
|
|
252
|
+
events:
|
|
253
|
+
isMock && sent
|
|
254
|
+
? [
|
|
255
|
+
{
|
|
256
|
+
id: `${group.id}-event-confirmed`,
|
|
257
|
+
at: order.createdAt,
|
|
258
|
+
label: 'Pedido confirmado',
|
|
259
|
+
},
|
|
260
|
+
...(shippedAt
|
|
261
|
+
? [
|
|
262
|
+
{
|
|
263
|
+
id: `${group.id}-event-shipped`,
|
|
264
|
+
at: shippedAt,
|
|
265
|
+
label: group.mode === 'pickup' ? 'Listo para recoger' : 'En camino',
|
|
266
|
+
},
|
|
267
|
+
]
|
|
268
|
+
: []),
|
|
269
|
+
...(deliveredAt
|
|
270
|
+
? [
|
|
271
|
+
{
|
|
272
|
+
id: `${group.id}-event-delivered`,
|
|
273
|
+
at: deliveredAt,
|
|
274
|
+
label: group.mode === 'pickup' ? 'Recogido' : 'Entregado',
|
|
275
|
+
},
|
|
276
|
+
]
|
|
277
|
+
: []),
|
|
278
|
+
]
|
|
279
|
+
: [],
|
|
280
|
+
allocations,
|
|
281
|
+
...(isMock && group.mode === 'pickup'
|
|
282
|
+
? {
|
|
283
|
+
pickup: {
|
|
284
|
+
...MOCK_PICKUP,
|
|
285
|
+
status:
|
|
286
|
+
status === 'picked_up' || status === 'delivered'
|
|
287
|
+
? ('picked_up' as const)
|
|
288
|
+
: status === 'ready_for_pickup'
|
|
289
|
+
? ('ready' as const)
|
|
290
|
+
: ('scheduled' as const),
|
|
291
|
+
code: order.orderNumber,
|
|
292
|
+
},
|
|
293
|
+
}
|
|
294
|
+
: {}),
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
function resolveTransactions(
|
|
299
|
+
order: Order,
|
|
300
|
+
total: Money,
|
|
301
|
+
refund: Money,
|
|
302
|
+
isMock: boolean,
|
|
303
|
+
hintPaidAmount = 0,
|
|
304
|
+
): Transaction[] {
|
|
305
|
+
if (!isMock) {
|
|
306
|
+
// A live provider that reaches this mapper without a transaction contract
|
|
307
|
+
// reports only the aggregate status; no movement is invented.
|
|
308
|
+
return []
|
|
309
|
+
}
|
|
310
|
+
const method = 'card' as const
|
|
311
|
+
const at = order.completedAt ?? order.updatedAt ?? order.createdAt
|
|
312
|
+
const entries: Transaction[] = []
|
|
313
|
+
switch (order.paymentStatus) {
|
|
314
|
+
case 'paid':
|
|
315
|
+
entries.push({
|
|
316
|
+
id: `${order.id}-payment`,
|
|
317
|
+
kind: 'payment',
|
|
318
|
+
status: 'captured',
|
|
319
|
+
amount: total,
|
|
320
|
+
at,
|
|
321
|
+
method,
|
|
322
|
+
})
|
|
323
|
+
break
|
|
324
|
+
case 'partially_refunded':
|
|
325
|
+
entries.push({
|
|
326
|
+
id: `${order.id}-payment`,
|
|
327
|
+
kind: 'payment',
|
|
328
|
+
status: 'captured',
|
|
329
|
+
amount: total,
|
|
330
|
+
at,
|
|
331
|
+
method,
|
|
332
|
+
})
|
|
333
|
+
if (refund.amount > 0) {
|
|
334
|
+
entries.push({
|
|
335
|
+
id: `${order.id}-refund`,
|
|
336
|
+
kind: 'refund',
|
|
337
|
+
status: 'refunded',
|
|
338
|
+
amount: refund,
|
|
339
|
+
at: order.updatedAt,
|
|
340
|
+
method,
|
|
341
|
+
})
|
|
342
|
+
}
|
|
343
|
+
break
|
|
344
|
+
case 'refunded':
|
|
345
|
+
entries.push({
|
|
346
|
+
id: `${order.id}-payment`,
|
|
347
|
+
kind: 'payment',
|
|
348
|
+
status: 'captured',
|
|
349
|
+
amount: total,
|
|
350
|
+
at,
|
|
351
|
+
method,
|
|
352
|
+
})
|
|
353
|
+
entries.push({
|
|
354
|
+
id: `${order.id}-refund`,
|
|
355
|
+
kind: 'refund',
|
|
356
|
+
status: 'refunded',
|
|
357
|
+
amount: total,
|
|
358
|
+
at: order.updatedAt ?? at,
|
|
359
|
+
method,
|
|
360
|
+
})
|
|
361
|
+
break
|
|
362
|
+
case 'authorized':
|
|
363
|
+
entries.push({
|
|
364
|
+
id: `${order.id}-authorization`,
|
|
365
|
+
kind: 'authorization',
|
|
366
|
+
status: 'authorized',
|
|
367
|
+
amount: total,
|
|
368
|
+
at,
|
|
369
|
+
method,
|
|
370
|
+
})
|
|
371
|
+
break
|
|
372
|
+
case 'pending': {
|
|
373
|
+
const paid = isMock && hintPaidAmount > 0
|
|
374
|
+
? { amount: Math.min(hintPaidAmount, total.amount), currency: total.currency }
|
|
375
|
+
: undefined
|
|
376
|
+
if (paid) {
|
|
377
|
+
entries.push({
|
|
378
|
+
id: `${order.id}-payment`,
|
|
379
|
+
kind: 'payment',
|
|
380
|
+
status: 'captured',
|
|
381
|
+
amount: paid,
|
|
382
|
+
at,
|
|
383
|
+
method,
|
|
384
|
+
})
|
|
385
|
+
entries.push({
|
|
386
|
+
id: `${order.id}-balance`,
|
|
387
|
+
kind: 'payment',
|
|
388
|
+
status: 'pending',
|
|
389
|
+
amount: { amount: total.amount - paid.amount, currency: total.currency },
|
|
390
|
+
at,
|
|
391
|
+
method,
|
|
392
|
+
})
|
|
393
|
+
} else {
|
|
394
|
+
entries.push({
|
|
395
|
+
id: `${order.id}-payment`,
|
|
396
|
+
kind: 'payment',
|
|
397
|
+
status: 'pending',
|
|
398
|
+
amount: total,
|
|
399
|
+
at,
|
|
400
|
+
method,
|
|
401
|
+
})
|
|
402
|
+
}
|
|
403
|
+
break
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
return entries
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
type DetailPaymentStatus = 'unpaid' | 'authorized' | 'partially_paid' | 'paid'
|
|
410
|
+
type DetailReturnStatus = 'none' | 'requested' | 'in_progress' | 'completed'
|
|
411
|
+
|
|
412
|
+
function toActionSet(input: {
|
|
413
|
+
commercial: OrderDetailOrder['commercial']
|
|
414
|
+
paymentStatus: DetailPaymentStatus
|
|
415
|
+
logistics: FulfillmentStatus
|
|
416
|
+
returns: DetailReturnStatus
|
|
417
|
+
}): OrderActionId[] {
|
|
418
|
+
const actions: OrderActionId[] = []
|
|
419
|
+
switch (input.commercial) {
|
|
420
|
+
case 'completed':
|
|
421
|
+
actions.push('reorder', 'invoice')
|
|
422
|
+
break
|
|
423
|
+
case 'cancelled':
|
|
424
|
+
actions.push('invoice')
|
|
425
|
+
break
|
|
426
|
+
case 'processing':
|
|
427
|
+
if (input.paymentStatus === 'paid') actions.push('invoice')
|
|
428
|
+
break
|
|
429
|
+
case 'pending':
|
|
430
|
+
actions.push('cancel')
|
|
431
|
+
break
|
|
432
|
+
}
|
|
433
|
+
if (input.paymentStatus === 'unpaid' || input.paymentStatus === 'partially_paid') {
|
|
434
|
+
actions.push('pay-balance')
|
|
435
|
+
}
|
|
436
|
+
if (
|
|
437
|
+
input.logistics === 'shipped' ||
|
|
438
|
+
input.logistics === 'partially_delivered' ||
|
|
439
|
+
input.logistics === 'ready_for_pickup'
|
|
440
|
+
) {
|
|
441
|
+
actions.push('track')
|
|
442
|
+
}
|
|
443
|
+
if (input.logistics === 'delivered' && input.returns === 'none') {
|
|
444
|
+
actions.push('return', 'exchange')
|
|
445
|
+
}
|
|
446
|
+
if (input.returns === 'requested' || input.returns === 'in_progress') actions.push('problem')
|
|
447
|
+
if (
|
|
448
|
+
input.commercial !== 'completed' &&
|
|
449
|
+
input.commercial !== 'cancelled' &&
|
|
450
|
+
(input.logistics === 'pending' || input.logistics === 'preparing')
|
|
451
|
+
) {
|
|
452
|
+
actions.push('cancel')
|
|
453
|
+
}
|
|
454
|
+
actions.push('support')
|
|
455
|
+
return [...new Set(actions)]
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/**
|
|
459
|
+
* Project one universal order into the order-detail snapshot.
|
|
460
|
+
*
|
|
461
|
+
* `orderId` is the requested id; the provider has already enforced ownership.
|
|
462
|
+
* The mapper never changes the order identity or the authoritative total.
|
|
463
|
+
*/
|
|
464
|
+
export function toOrderDetailSnapshot(order: Order): OrderDetailSnapshot {
|
|
465
|
+
const currency = order.total.currencyCode
|
|
466
|
+
const hint = readLifecycleHint(order)
|
|
467
|
+
const isMock = order.metadata?.provider === 'mock'
|
|
468
|
+
const orderDiscount = toDetailMoney(order.discount, currency).amount
|
|
469
|
+
const taxAmount = calculateTax(order, currency, orderDiscount)
|
|
470
|
+
const lines = order.items.map((item, index) =>
|
|
471
|
+
toOrderLine(
|
|
472
|
+
item,
|
|
473
|
+
currency,
|
|
474
|
+
hint.partiallyCancelled && index === order.items.length - 1 ? 1 : 0,
|
|
475
|
+
),
|
|
476
|
+
)
|
|
477
|
+
if (taxAmount > 0 && lines[0]) {
|
|
478
|
+
lines[0].taxes = [
|
|
479
|
+
{
|
|
480
|
+
id: `${order.id}-tax`,
|
|
481
|
+
label: 'Impuestos',
|
|
482
|
+
amount: { amount: taxAmount, currency },
|
|
483
|
+
},
|
|
484
|
+
]
|
|
485
|
+
// The line total is the authoritative charged amount (taxes included), so
|
|
486
|
+
// the order total reconciles without re-applying tax in the view.
|
|
487
|
+
lines[0].total = {
|
|
488
|
+
amount: lines[0].total.amount + taxAmount,
|
|
489
|
+
currency,
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
const groupSources = resolveGroupSources(order)
|
|
494
|
+
const groups: LogisticsGroup[] = groupSources.map((group) => {
|
|
495
|
+
const fee = toDetailMoney(group.fee, currency)
|
|
496
|
+
return {
|
|
497
|
+
id: group.id,
|
|
498
|
+
mode: group.mode,
|
|
499
|
+
method: { id: `${group.id}-method`, name: group.methodName },
|
|
500
|
+
shippingSubtotal: fee,
|
|
501
|
+
discounts: [],
|
|
502
|
+
taxes: [],
|
|
503
|
+
total: fee,
|
|
504
|
+
...(group.mode === 'pickup' && isMock
|
|
505
|
+
? {
|
|
506
|
+
pickup: {
|
|
507
|
+
...MOCK_PICKUP,
|
|
508
|
+
status: hint.fulfilment === 'ready_for_pickup' ? 'ready' : 'scheduled',
|
|
509
|
+
code: order.orderNumber,
|
|
510
|
+
},
|
|
511
|
+
}
|
|
512
|
+
: {}),
|
|
513
|
+
}
|
|
514
|
+
})
|
|
515
|
+
|
|
516
|
+
const logistics = toFulfillmentStatus(order, hint)
|
|
517
|
+
const fulfillments = groupSources.map((group) => {
|
|
518
|
+
const status =
|
|
519
|
+
groupSources.length > 1 && group.mode === 'pickup' && logistics === 'partially_delivered'
|
|
520
|
+
? 'preparing'
|
|
521
|
+
: logistics
|
|
522
|
+
return toFulfillment(order, group, status, lines, isMock)
|
|
523
|
+
})
|
|
524
|
+
|
|
525
|
+
const refund =
|
|
526
|
+
hint.refundAmount !== undefined
|
|
527
|
+
? toDetailMoney({ amount: String(hint.refundAmount), currencyCode: currency }, currency)
|
|
528
|
+
: toDetailMoney(order.discount, currency)
|
|
529
|
+
const hintPaid = hint.paidAmount !== undefined
|
|
530
|
+
? Math.round(hint.paidAmount * 10 ** currencyDigits(currency))
|
|
531
|
+
: 0
|
|
532
|
+
const transactions = resolveTransactions(
|
|
533
|
+
order,
|
|
534
|
+
toDetailMoney(order.total, currency),
|
|
535
|
+
refund,
|
|
536
|
+
isMock,
|
|
537
|
+
hintPaid,
|
|
538
|
+
)
|
|
539
|
+
|
|
540
|
+
const cases: AfterSalesCase[] =
|
|
541
|
+
hint.returns && hint.returns !== 'none'
|
|
542
|
+
? [
|
|
543
|
+
{
|
|
544
|
+
id: `${order.id}-return`,
|
|
545
|
+
type: 'return',
|
|
546
|
+
status:
|
|
547
|
+
hint.returns === 'requested'
|
|
548
|
+
? 'requested'
|
|
549
|
+
: hint.returns === 'in_progress'
|
|
550
|
+
? 'in_transit'
|
|
551
|
+
: 'completed',
|
|
552
|
+
createdAt: order.updatedAt,
|
|
553
|
+
reason: 'Solicitud de devolución del cliente',
|
|
554
|
+
lines: lines.map((line) => ({
|
|
555
|
+
lineId: line.id,
|
|
556
|
+
quantity: Math.max(0, line.quantity - line.cancelledQuantity),
|
|
557
|
+
})),
|
|
558
|
+
...(hint.returns === 'completed' ? { refund } : {}),
|
|
559
|
+
availableActions: [],
|
|
560
|
+
},
|
|
561
|
+
]
|
|
562
|
+
: order.fulfillmentStatus === 'returned'
|
|
563
|
+
? [
|
|
564
|
+
{
|
|
565
|
+
id: `${order.id}-return`,
|
|
566
|
+
type: 'return',
|
|
567
|
+
status: 'completed',
|
|
568
|
+
createdAt: order.updatedAt,
|
|
569
|
+
reason: 'Devolución completada',
|
|
570
|
+
lines: lines.map((line) => ({
|
|
571
|
+
lineId: line.id,
|
|
572
|
+
quantity: Math.max(0, line.quantity - line.cancelledQuantity),
|
|
573
|
+
})),
|
|
574
|
+
refund: toDetailMoney(order.total, currency),
|
|
575
|
+
availableActions: [],
|
|
576
|
+
},
|
|
577
|
+
]
|
|
578
|
+
: []
|
|
579
|
+
|
|
580
|
+
const paymentStatus: DetailPaymentStatus =
|
|
581
|
+
hint.payment ??
|
|
582
|
+
(order.paymentStatus === 'pending'
|
|
583
|
+
? 'unpaid'
|
|
584
|
+
: order.paymentStatus === 'authorized'
|
|
585
|
+
? 'authorized'
|
|
586
|
+
: 'paid')
|
|
587
|
+
const returns: DetailReturnStatus =
|
|
588
|
+
hint.returns ?? (order.fulfillmentStatus === 'returned' ? 'completed' : 'none')
|
|
589
|
+
const commercial = toCommercial(order)
|
|
590
|
+
const adjustments: Adjustment[] =
|
|
591
|
+
orderDiscount > 0
|
|
592
|
+
? [
|
|
593
|
+
{
|
|
594
|
+
id: `${order.id}-discount`,
|
|
595
|
+
label: 'Descuento',
|
|
596
|
+
scope: 'order',
|
|
597
|
+
direction: 'discount',
|
|
598
|
+
amount: { amount: orderDiscount, currency },
|
|
599
|
+
source: 'manual',
|
|
600
|
+
},
|
|
601
|
+
]
|
|
602
|
+
: []
|
|
603
|
+
|
|
604
|
+
const subtotal = toDetailMoney(order.subtotal, currency)
|
|
605
|
+
const shipping = toDetailMoney(order.shipping, currency)
|
|
606
|
+
const total = toDetailMoney(order.total, currency)
|
|
607
|
+
|
|
608
|
+
const shippingAddress = toDetailAddress(order.shippingAddress)
|
|
609
|
+
const billingAddress = toDetailAddress(order.billingAddress)
|
|
610
|
+
const documents = orderDetailDemoDocuments(
|
|
611
|
+
{ number: order.orderNumber, createdAt: order.createdAt },
|
|
612
|
+
isMock,
|
|
613
|
+
)
|
|
614
|
+
const detail: OrderDetailOrder = {
|
|
615
|
+
id: order.id,
|
|
616
|
+
number: order.orderNumber,
|
|
617
|
+
createdAt: order.createdAt,
|
|
618
|
+
updatedAt: order.updatedAt,
|
|
619
|
+
currency,
|
|
620
|
+
commercial,
|
|
621
|
+
logistics: { status: logistics, ...(hint.fulfilment === 'delayed' ? { delayed: true } : {}) },
|
|
622
|
+
...(shippingAddress ? { shippingAddress } : {}),
|
|
623
|
+
...(billingAddress ? { billingAddress } : {}),
|
|
624
|
+
lines,
|
|
625
|
+
groups,
|
|
626
|
+
fulfillments,
|
|
627
|
+
promotions: [],
|
|
628
|
+
adjustments,
|
|
629
|
+
transactions,
|
|
630
|
+
cases,
|
|
631
|
+
documents,
|
|
632
|
+
revisions: [],
|
|
633
|
+
totals: {
|
|
634
|
+
itemsSubtotal: subtotal,
|
|
635
|
+
itemDiscounts: zero(currency),
|
|
636
|
+
orderDiscounts: { amount: orderDiscount, currency },
|
|
637
|
+
shippingSubtotal: shipping,
|
|
638
|
+
shippingDiscounts: zero(currency),
|
|
639
|
+
tax: { amount: taxAmount, currency },
|
|
640
|
+
adjustments: zero(currency),
|
|
641
|
+
total,
|
|
642
|
+
},
|
|
643
|
+
availableActions: projectHostActions(
|
|
644
|
+
toActionSet({ commercial, paymentStatus, logistics, returns }).filter(
|
|
645
|
+
(action) =>
|
|
646
|
+
action !== 'invoice' ||
|
|
647
|
+
documents.some((document) => document.kind === 'invoice' && document.available),
|
|
648
|
+
),
|
|
649
|
+
),
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
return { order: detail }
|
|
653
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Currency-aware conversion between the snapshot's integer minor units and the
|
|
3
|
+
* canonical decimal `Money` the blocks speak.
|
|
4
|
+
*
|
|
5
|
+
* Dividing by 100 is only right for the currencies that happen to have two
|
|
6
|
+
* fraction digits. JPY has none and KWD has three, so the exponent comes from
|
|
7
|
+
* `Intl.NumberFormat`, the same source the platform uses to render the symbol.
|
|
8
|
+
*/
|
|
9
|
+
export function currencyDigits(currency: string): number {
|
|
10
|
+
try {
|
|
11
|
+
return (
|
|
12
|
+
new Intl.NumberFormat('en-US', {
|
|
13
|
+
style: 'currency',
|
|
14
|
+
currency,
|
|
15
|
+
}).resolvedOptions().maximumFractionDigits ?? 2
|
|
16
|
+
)
|
|
17
|
+
} catch {
|
|
18
|
+
return 2
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const scale = (currency: string): number => 10 ** currencyDigits(currency)
|
|
23
|
+
|
|
24
|
+
/** Minor units → canonical decimal string, e.g. `12600, 'USD' → '126.00'`. */
|
|
25
|
+
export function fromMinor(amount: number, currency: string): string {
|
|
26
|
+
const digits = currencyDigits(currency)
|
|
27
|
+
return (amount / scale(currency)).toFixed(digits)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Minor units → locale-formatted amount. */
|
|
31
|
+
export function formatMinor(amount: number, currency: string, locale: string): string {
|
|
32
|
+
try {
|
|
33
|
+
return new Intl.NumberFormat(locale, { style: 'currency', currency }).format(
|
|
34
|
+
amount / scale(currency),
|
|
35
|
+
)
|
|
36
|
+
} catch {
|
|
37
|
+
return `${fromMinor(amount, currency)} ${currency}`
|
|
38
|
+
}
|
|
39
|
+
}
|