@pradip1995/segment-login-template 0.5.16 → 0.5.18

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pradip1995/segment-login-template",
3
- "version": "0.5.16",
3
+ "version": "0.5.18",
4
4
  "license": "MIT",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -34,7 +34,7 @@
34
34
  "dependencies": {
35
35
  "@pradip1995/commerce-auth": "^4.0.0",
36
36
  "@pradip1995/commerce-core": "^4.0.0",
37
- "@pradip1995/segment-primitives": "^0.4.11",
37
+ "@pradip1995/segment-primitives": "^0.4.12",
38
38
  "@pradip1995/segment-tokens": "^0.3.14"
39
39
  },
40
40
  "devDependencies": {
@@ -103,7 +103,7 @@ const CASES: Array<{
103
103
  want: { label: "Delivered", tone: "done" },
104
104
  },
105
105
  {
106
- name: "ignores exchange and claim returns",
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
  {
@@ -148,6 +156,42 @@ const CASES: Array<{
148
156
  },
149
157
  want: { label: "Return Requested", tone: "return" },
150
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
+ },
151
195
  {
152
196
  name: "shows returned from received item quantities",
153
197
  order: {
@@ -21,7 +21,13 @@ 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
24
28
  items?: Array<{
29
+ product_id?: string | null
30
+ created_at?: string | Date | null
25
31
  detail?: {
26
32
  return_requested_quantity?: number | null
27
33
  return_received_quantity?: number | null
@@ -54,6 +60,14 @@ const RETURN_LABEL: Record<ReturnLifecycle, string> = {
54
60
  refunded: "Refunded",
55
61
  }
56
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
+
57
71
  function normalize(value?: string | null) {
58
72
  return (value || "").toLowerCase().trim()
59
73
  }
@@ -62,6 +76,44 @@ function isStandaloneReturn(ret: OrderHistoryReturnLike) {
62
76
  return !ret.exchange_id && !ret.claim_id
63
77
  }
64
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
+
65
117
  function lifecycleFromReturnStatus(status: string): ReturnLifecycle | null {
66
118
  switch (status) {
67
119
  case "requested":
@@ -90,18 +142,23 @@ function pickHigher(
90
142
  return RETURN_RANK[next] > RETURN_RANK[current] ? next : current
91
143
  }
92
144
 
93
- function returnLifecycle(order: OrderHistoryStatusInput): ReturnLifecycle | null {
145
+ function requestLifecycle(
146
+ order: OrderHistoryStatusInput,
147
+ mode: "return" | "exchange"
148
+ ): ReturnLifecycle | null {
94
149
  const payment = normalize(order.payment_status)
95
150
  let best: ReturnLifecycle | null = null
151
+ const exchange = orderHasActiveExchange(order)
96
152
 
97
- if (payment === "refunded" || payment === "partially_refunded") {
153
+ if (mode === "return" && (payment === "refunded" || payment === "partially_refunded")) {
98
154
  best = "refunded"
99
155
  }
100
156
 
101
157
  for (const ret of order.returns || []) {
102
- if (!isStandaloneReturn(ret)) continue
103
158
  const status = normalize(ret.status)
104
159
  if (CLOSED_RETURN.has(status)) continue
160
+ if (mode === "exchange" && !isOpenExchangeReturn(ret)) continue
161
+ if (mode === "return" && !isStandaloneReturn(ret)) continue
105
162
  const life = status
106
163
  ? lifecycleFromReturnStatus(status) ?? "requested"
107
164
  : "requested"
@@ -114,19 +171,31 @@ function returnLifecycle(order: OrderHistoryStatusInput): ReturnLifecycle | null
114
171
  requestedQty += Number(item.detail?.return_requested_quantity) || 0
115
172
  receivedQty += Number(item.detail?.return_received_quantity) || 0
116
173
  }
117
- if (receivedQty > 0 && requestedQty > receivedQty) {
118
- best = pickHigher(best, "in_progress")
119
- } else if (receivedQty > 0) {
120
- best = pickHigher(best, "returned")
121
- } else if (requestedQty > 0) {
122
- best = pickHigher(best, "requested")
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
+ }
123
195
  }
124
196
 
125
- const fulfillment = normalize(order.fulfillment_status)
126
- if (fulfillment === "returned") {
127
- best = pickHigher(best, "returned")
128
- } else if (fulfillment === "partially_returned") {
129
- best = pickHigher(best, "in_progress")
197
+ if (mode === "exchange" && exchange) {
198
+ return best || "requested"
130
199
  }
131
200
 
132
201
  return best
@@ -140,9 +209,14 @@ export function resolveOrderHistoryStatus(
140
209
  return { label: "Cancelled", tone: "cancel" }
141
210
  }
142
211
 
143
- const life = returnLifecycle(order)
144
- if (life) {
145
- return { label: RETURN_LABEL[life], tone: "return" }
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" }
146
220
  }
147
221
 
148
222
  const fulfillment = normalize(order.fulfillment_status)