@pradip1995/segment-jewelry-account-pages 0.1.4 → 0.1.5
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/package.json +1 -1
- package/src/order-history-status.test.ts +69 -1
- package/src/order-history-status.ts +105 -11
package/package.json
CHANGED
|
@@ -103,7 +103,7 @@ const CASES: Array<{
|
|
|
103
103
|
want: { label: "Delivered", tone: "done" },
|
|
104
104
|
},
|
|
105
105
|
{
|
|
106
|
-
name: "
|
|
106
|
+
name: "shows exchange requested when a return is tied to an exchange",
|
|
107
107
|
order: {
|
|
108
108
|
fulfillment_status: "delivered",
|
|
109
109
|
returns: [
|
|
@@ -111,6 +111,14 @@ const CASES: Array<{
|
|
|
111
111
|
{ status: "requested", claim_id: "claim_1" },
|
|
112
112
|
],
|
|
113
113
|
},
|
|
114
|
+
want: { label: "Exchange Requested", tone: "return" },
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
name: "ignores claim returns and keeps delivered",
|
|
118
|
+
order: {
|
|
119
|
+
fulfillment_status: "delivered",
|
|
120
|
+
returns: [{ status: "requested", claim_id: "claim_1" }],
|
|
121
|
+
},
|
|
114
122
|
want: { label: "Delivered", tone: "done" },
|
|
115
123
|
},
|
|
116
124
|
{
|
|
@@ -140,6 +148,66 @@ const CASES: Array<{
|
|
|
140
148
|
order: { fulfillment_status: "fulfilled" },
|
|
141
149
|
want: { label: "Order Confirmed", tone: "done" },
|
|
142
150
|
},
|
|
151
|
+
{
|
|
152
|
+
name: "shows return requested from item return quantities when returns are omitted",
|
|
153
|
+
order: {
|
|
154
|
+
fulfillment_status: "delivered",
|
|
155
|
+
items: [{ detail: { return_requested_quantity: 1 } }],
|
|
156
|
+
},
|
|
157
|
+
want: { label: "Return Requested", tone: "return" },
|
|
158
|
+
},
|
|
159
|
+
{
|
|
160
|
+
name: "shows exchange requested from item quantities when the order has an exchange",
|
|
161
|
+
order: {
|
|
162
|
+
fulfillment_status: "delivered",
|
|
163
|
+
exchanges: [{ id: "exch_1" }],
|
|
164
|
+
items: [{ detail: { return_requested_quantity: 1 } }],
|
|
165
|
+
},
|
|
166
|
+
want: { label: "Exchange Requested", tone: "return" },
|
|
167
|
+
},
|
|
168
|
+
{
|
|
169
|
+
name: "shows exchange requested for a same-SKU replacement line",
|
|
170
|
+
order: {
|
|
171
|
+
fulfillment_status: "delivered",
|
|
172
|
+
items: [
|
|
173
|
+
{ product_id: "prod_1", detail: { return_requested_quantity: 1 } },
|
|
174
|
+
{ product_id: "prod_1" },
|
|
175
|
+
],
|
|
176
|
+
},
|
|
177
|
+
want: { label: "Exchange Requested", tone: "return" },
|
|
178
|
+
},
|
|
179
|
+
{
|
|
180
|
+
name: "shows exchange requested when the same product appears twice after an exchange",
|
|
181
|
+
order: {
|
|
182
|
+
fulfillment_status: "delivered",
|
|
183
|
+
items: [{ product_id: "prod_1" }, { product_id: "prod_1" }],
|
|
184
|
+
},
|
|
185
|
+
want: { label: "Exchange Requested", tone: "return" },
|
|
186
|
+
},
|
|
187
|
+
{
|
|
188
|
+
name: "maps an approved exchange return to exchange approved",
|
|
189
|
+
order: {
|
|
190
|
+
fulfillment_status: "delivered",
|
|
191
|
+
returns: [{ status: "open", exchange_id: "exch_1" }],
|
|
192
|
+
},
|
|
193
|
+
want: { label: "Exchange Approved", tone: "return" },
|
|
194
|
+
},
|
|
195
|
+
{
|
|
196
|
+
name: "shows returned from received item quantities",
|
|
197
|
+
order: {
|
|
198
|
+
fulfillment_status: "delivered",
|
|
199
|
+
items: [{ detail: { return_requested_quantity: 1, return_received_quantity: 1 } }],
|
|
200
|
+
},
|
|
201
|
+
want: { label: "Returned", tone: "return" },
|
|
202
|
+
},
|
|
203
|
+
{
|
|
204
|
+
name: "shows return in progress when only some quantity is received",
|
|
205
|
+
order: {
|
|
206
|
+
fulfillment_status: "delivered",
|
|
207
|
+
items: [{ detail: { return_requested_quantity: 2, return_received_quantity: 1 } }],
|
|
208
|
+
},
|
|
209
|
+
want: { label: "Return In Progress", tone: "return" },
|
|
210
|
+
},
|
|
143
211
|
{
|
|
144
212
|
name: "shows processing as the default",
|
|
145
213
|
order: { status: "pending" },
|
|
@@ -21,6 +21,18 @@ export type OrderHistoryStatusInput = {
|
|
|
21
21
|
fulfillment_status?: string | null
|
|
22
22
|
payment_status?: string | null
|
|
23
23
|
returns?: OrderHistoryReturnLike[] | null
|
|
24
|
+
exchanges?: Array<{
|
|
25
|
+
id?: string | null
|
|
26
|
+
canceled_at?: string | Date | null
|
|
27
|
+
}> | null
|
|
28
|
+
items?: Array<{
|
|
29
|
+
product_id?: string | null
|
|
30
|
+
created_at?: string | Date | null
|
|
31
|
+
detail?: {
|
|
32
|
+
return_requested_quantity?: number | null
|
|
33
|
+
return_received_quantity?: number | null
|
|
34
|
+
} | null
|
|
35
|
+
}> | null
|
|
24
36
|
}
|
|
25
37
|
|
|
26
38
|
type ReturnLifecycle =
|
|
@@ -48,6 +60,14 @@ const RETURN_LABEL: Record<ReturnLifecycle, string> = {
|
|
|
48
60
|
refunded: "Refunded",
|
|
49
61
|
}
|
|
50
62
|
|
|
63
|
+
const EXCHANGE_LABEL: Record<ReturnLifecycle, string> = {
|
|
64
|
+
requested: "Exchange Requested",
|
|
65
|
+
approved: "Exchange Approved",
|
|
66
|
+
in_progress: "Exchange In Progress",
|
|
67
|
+
returned: "Exchanged",
|
|
68
|
+
refunded: "Refunded",
|
|
69
|
+
}
|
|
70
|
+
|
|
51
71
|
function normalize(value?: string | null) {
|
|
52
72
|
return (value || "").toLowerCase().trim()
|
|
53
73
|
}
|
|
@@ -56,6 +76,44 @@ function isStandaloneReturn(ret: OrderHistoryReturnLike) {
|
|
|
56
76
|
return !ret.exchange_id && !ret.claim_id
|
|
57
77
|
}
|
|
58
78
|
|
|
79
|
+
function isOpenExchangeReturn(ret: OrderHistoryReturnLike) {
|
|
80
|
+
return Boolean(ret.exchange_id) && !CLOSED_RETURN.has(normalize(ret.status))
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function itemCreatedAt(item: { created_at?: string | Date | null }): number {
|
|
84
|
+
if (!item.created_at) return 0
|
|
85
|
+
const t = new Date(item.created_at).getTime()
|
|
86
|
+
return Number.isFinite(t) ? t : 0
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function orderHasActiveExchange(order: OrderHistoryStatusInput): boolean {
|
|
90
|
+
if ((order.exchanges || []).some((row) => row?.id && !row.canceled_at)) {
|
|
91
|
+
return true
|
|
92
|
+
}
|
|
93
|
+
if ((order.returns || []).some(isOpenExchangeReturn)) return true
|
|
94
|
+
|
|
95
|
+
const items = order.items || []
|
|
96
|
+
const byProduct = new Map<string, number>()
|
|
97
|
+
for (const item of items) {
|
|
98
|
+
if (!item.product_id) continue
|
|
99
|
+
byProduct.set(item.product_id, (byProduct.get(item.product_id) || 0) + 1)
|
|
100
|
+
}
|
|
101
|
+
if ([...byProduct.values()].some((count) => count >= 2)) return true
|
|
102
|
+
|
|
103
|
+
for (const item of items) {
|
|
104
|
+
const createdAt = itemCreatedAt(item)
|
|
105
|
+
if (!createdAt) continue
|
|
106
|
+
const hasEarlierItem = items.some((other) => {
|
|
107
|
+
if (other === item) return false
|
|
108
|
+
const otherAt = itemCreatedAt(other)
|
|
109
|
+
return otherAt > 0 && createdAt > otherAt + 1000
|
|
110
|
+
})
|
|
111
|
+
if (hasEarlierItem) return true
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return false
|
|
115
|
+
}
|
|
116
|
+
|
|
59
117
|
function lifecycleFromReturnStatus(status: string): ReturnLifecycle | null {
|
|
60
118
|
switch (status) {
|
|
61
119
|
case "requested":
|
|
@@ -84,29 +142,60 @@ function pickHigher(
|
|
|
84
142
|
return RETURN_RANK[next] > RETURN_RANK[current] ? next : current
|
|
85
143
|
}
|
|
86
144
|
|
|
87
|
-
function
|
|
145
|
+
function requestLifecycle(
|
|
146
|
+
order: OrderHistoryStatusInput,
|
|
147
|
+
mode: "return" | "exchange"
|
|
148
|
+
): ReturnLifecycle | null {
|
|
88
149
|
const payment = normalize(order.payment_status)
|
|
89
150
|
let best: ReturnLifecycle | null = null
|
|
151
|
+
const exchange = orderHasActiveExchange(order)
|
|
90
152
|
|
|
91
|
-
if (payment === "refunded" || payment === "partially_refunded") {
|
|
153
|
+
if (mode === "return" && (payment === "refunded" || payment === "partially_refunded")) {
|
|
92
154
|
best = "refunded"
|
|
93
155
|
}
|
|
94
156
|
|
|
95
157
|
for (const ret of order.returns || []) {
|
|
96
|
-
if (!isStandaloneReturn(ret)) continue
|
|
97
158
|
const status = normalize(ret.status)
|
|
98
159
|
if (CLOSED_RETURN.has(status)) continue
|
|
160
|
+
if (mode === "exchange" && !isOpenExchangeReturn(ret)) continue
|
|
161
|
+
if (mode === "return" && !isStandaloneReturn(ret)) continue
|
|
99
162
|
const life = status
|
|
100
163
|
? lifecycleFromReturnStatus(status) ?? "requested"
|
|
101
164
|
: "requested"
|
|
102
165
|
best = pickHigher(best, life)
|
|
103
166
|
}
|
|
104
167
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
168
|
+
let requestedQty = 0
|
|
169
|
+
let receivedQty = 0
|
|
170
|
+
for (const item of order.items || []) {
|
|
171
|
+
requestedQty += Number(item.detail?.return_requested_quantity) || 0
|
|
172
|
+
receivedQty += Number(item.detail?.return_received_quantity) || 0
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const qtyApplies =
|
|
176
|
+
(mode === "exchange" && exchange) || (mode === "return" && !exchange)
|
|
177
|
+
|
|
178
|
+
if (qtyApplies) {
|
|
179
|
+
if (receivedQty > 0 && requestedQty > receivedQty) {
|
|
180
|
+
best = pickHigher(best, "in_progress")
|
|
181
|
+
} else if (receivedQty > 0) {
|
|
182
|
+
best = pickHigher(best, "returned")
|
|
183
|
+
} else if (requestedQty > 0) {
|
|
184
|
+
best = pickHigher(best, "requested")
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (mode === "return") {
|
|
189
|
+
const fulfillment = normalize(order.fulfillment_status)
|
|
190
|
+
if (fulfillment === "returned") {
|
|
191
|
+
best = pickHigher(best, "returned")
|
|
192
|
+
} else if (fulfillment === "partially_returned") {
|
|
193
|
+
best = pickHigher(best, "in_progress")
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (mode === "exchange" && exchange) {
|
|
198
|
+
return best || "requested"
|
|
110
199
|
}
|
|
111
200
|
|
|
112
201
|
return best
|
|
@@ -120,9 +209,14 @@ export function resolveOrderHistoryStatus(
|
|
|
120
209
|
return { label: "Cancelled", tone: "cancel" }
|
|
121
210
|
}
|
|
122
211
|
|
|
123
|
-
const
|
|
124
|
-
if (
|
|
125
|
-
return { label:
|
|
212
|
+
const exchangeLife = requestLifecycle(order, "exchange")
|
|
213
|
+
if (exchangeLife) {
|
|
214
|
+
return { label: EXCHANGE_LABEL[exchangeLife], tone: "return" }
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const returnLife = requestLifecycle(order, "return")
|
|
218
|
+
if (returnLife) {
|
|
219
|
+
return { label: RETURN_LABEL[returnLife], tone: "return" }
|
|
126
220
|
}
|
|
127
221
|
|
|
128
222
|
const fulfillment = normalize(order.fulfillment_status)
|