@pradip1995/segment-primitives 0.4.10 → 0.4.12
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 +3 -2
- package/src/order-lines.test.ts +246 -0
- package/src/order-lines.ts +151 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pradip1995/segment-primitives",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.12",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -20,7 +20,8 @@
|
|
|
20
20
|
"./section-variants": "./src/section-variants.ts",
|
|
21
21
|
"./product-variants": "./src/product-variants.ts",
|
|
22
22
|
"./product-selection-context": "./src/product-selection-context.tsx",
|
|
23
|
-
"./is-next-redirect": "./src/is-next-redirect.ts"
|
|
23
|
+
"./is-next-redirect": "./src/is-next-redirect.ts",
|
|
24
|
+
"./order-lines": "./src/order-lines.ts"
|
|
24
25
|
},
|
|
25
26
|
"peerDependencies": {
|
|
26
27
|
"@medusajs/types": "^2.0.0",
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import assert from "node:assert/strict"
|
|
2
|
+
import { describe, it } from "node:test"
|
|
3
|
+
import {
|
|
4
|
+
outboundExchangeItemIds,
|
|
5
|
+
presentOrderLines,
|
|
6
|
+
type OrderExchangeLike,
|
|
7
|
+
type OrderLineLike,
|
|
8
|
+
} from "./order-lines.ts"
|
|
9
|
+
|
|
10
|
+
const originalRing: OrderLineLike = {
|
|
11
|
+
id: "ordli_ring_8",
|
|
12
|
+
product_id: "prod_ring",
|
|
13
|
+
variant_id: "var_8",
|
|
14
|
+
quantity: 1,
|
|
15
|
+
total: 100,
|
|
16
|
+
created_at: "2026-08-27T12:39:21.453Z",
|
|
17
|
+
detail: {
|
|
18
|
+
fulfilled_quantity: 1,
|
|
19
|
+
delivered_quantity: 1,
|
|
20
|
+
return_requested_quantity: 1,
|
|
21
|
+
returned_quantity: 0,
|
|
22
|
+
},
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const pendant: OrderLineLike = {
|
|
26
|
+
id: "ordli_pendant",
|
|
27
|
+
product_id: "prod_pendant",
|
|
28
|
+
variant_id: "var_default",
|
|
29
|
+
quantity: 1,
|
|
30
|
+
total: 869,
|
|
31
|
+
created_at: "2026-08-27T12:39:21.453Z",
|
|
32
|
+
detail: {
|
|
33
|
+
fulfilled_quantity: 1,
|
|
34
|
+
delivered_quantity: 1,
|
|
35
|
+
return_requested_quantity: 0,
|
|
36
|
+
returned_quantity: 0,
|
|
37
|
+
},
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const replacementRing: OrderLineLike = {
|
|
41
|
+
id: "ordli_ring_7",
|
|
42
|
+
product_id: "prod_ring",
|
|
43
|
+
variant_id: "var_7",
|
|
44
|
+
quantity: 1,
|
|
45
|
+
total: 100,
|
|
46
|
+
created_at: "2026-08-27T12:41:36.022Z",
|
|
47
|
+
detail: {
|
|
48
|
+
fulfilled_quantity: 0,
|
|
49
|
+
delivered_quantity: 0,
|
|
50
|
+
return_requested_quantity: 0,
|
|
51
|
+
returned_quantity: 0,
|
|
52
|
+
},
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const originalSize7: OrderLineLike = {
|
|
56
|
+
...replacementRing,
|
|
57
|
+
id: "ordli_ring_7_original",
|
|
58
|
+
created_at: originalRing.created_at,
|
|
59
|
+
detail: {
|
|
60
|
+
fulfilled_quantity: 1,
|
|
61
|
+
delivered_quantity: 1,
|
|
62
|
+
return_requested_quantity: 0,
|
|
63
|
+
returned_quantity: 0,
|
|
64
|
+
},
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const PRESENT_CASES: {
|
|
68
|
+
name: string
|
|
69
|
+
order: {
|
|
70
|
+
items?: OrderLineLike[] | null
|
|
71
|
+
shipping_total?: number | null
|
|
72
|
+
exchanges?: OrderExchangeLike[] | null
|
|
73
|
+
}
|
|
74
|
+
want: {
|
|
75
|
+
purchasedIds: string[]
|
|
76
|
+
outboundIds: string[]
|
|
77
|
+
inboundIds: string[]
|
|
78
|
+
paidSubtotal: number
|
|
79
|
+
paidTotal: number
|
|
80
|
+
hasActiveExchange: boolean
|
|
81
|
+
originalFulfillmentIsComplete: boolean
|
|
82
|
+
reorderIds: string[]
|
|
83
|
+
}
|
|
84
|
+
}[] = [
|
|
85
|
+
{
|
|
86
|
+
name: "hides the exchange replacement from purchased items and paid total",
|
|
87
|
+
order: {
|
|
88
|
+
items: [originalRing, pendant, replacementRing],
|
|
89
|
+
shipping_total: 0,
|
|
90
|
+
},
|
|
91
|
+
want: {
|
|
92
|
+
purchasedIds: ["ordli_ring_8", "ordli_pendant"],
|
|
93
|
+
outboundIds: ["ordli_ring_7"],
|
|
94
|
+
inboundIds: ["ordli_ring_8"],
|
|
95
|
+
paidSubtotal: 969,
|
|
96
|
+
paidTotal: 969,
|
|
97
|
+
hasActiveExchange: true,
|
|
98
|
+
originalFulfillmentIsComplete: true,
|
|
99
|
+
reorderIds: ["ordli_ring_8", "ordli_pendant"],
|
|
100
|
+
},
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
name: "does not treat a second original size of the same product as the replacement",
|
|
104
|
+
order: {
|
|
105
|
+
items: [originalRing, originalSize7],
|
|
106
|
+
shipping_total: 0,
|
|
107
|
+
},
|
|
108
|
+
want: {
|
|
109
|
+
purchasedIds: ["ordli_ring_8", "ordli_ring_7_original"],
|
|
110
|
+
outboundIds: [],
|
|
111
|
+
inboundIds: ["ordli_ring_8"],
|
|
112
|
+
paidSubtotal: 200,
|
|
113
|
+
paidTotal: 200,
|
|
114
|
+
hasActiveExchange: true,
|
|
115
|
+
originalFulfillmentIsComplete: true,
|
|
116
|
+
reorderIds: ["ordli_ring_8", "ordli_ring_7_original"],
|
|
117
|
+
},
|
|
118
|
+
},
|
|
119
|
+
{
|
|
120
|
+
name: "empty order has zero totals and no lines",
|
|
121
|
+
order: { items: [], shipping_total: 40 },
|
|
122
|
+
want: {
|
|
123
|
+
purchasedIds: [],
|
|
124
|
+
outboundIds: [],
|
|
125
|
+
inboundIds: [],
|
|
126
|
+
paidSubtotal: 0,
|
|
127
|
+
paidTotal: 40,
|
|
128
|
+
hasActiveExchange: false,
|
|
129
|
+
originalFulfillmentIsComplete: false,
|
|
130
|
+
reorderIds: [],
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
{
|
|
134
|
+
name: "adds shipping to paid total",
|
|
135
|
+
order: {
|
|
136
|
+
items: [pendant],
|
|
137
|
+
shipping_total: 49,
|
|
138
|
+
},
|
|
139
|
+
want: {
|
|
140
|
+
purchasedIds: ["ordli_pendant"],
|
|
141
|
+
outboundIds: [],
|
|
142
|
+
inboundIds: [],
|
|
143
|
+
paidSubtotal: 869,
|
|
144
|
+
paidTotal: 918,
|
|
145
|
+
hasActiveExchange: false,
|
|
146
|
+
originalFulfillmentIsComplete: true,
|
|
147
|
+
reorderIds: ["ordli_pendant"],
|
|
148
|
+
},
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
name: "falls back to unit_price times quantity when total is missing",
|
|
152
|
+
order: {
|
|
153
|
+
items: [
|
|
154
|
+
{
|
|
155
|
+
id: "ordli_loose",
|
|
156
|
+
quantity: 2,
|
|
157
|
+
unit_price: 50,
|
|
158
|
+
},
|
|
159
|
+
],
|
|
160
|
+
shipping_total: 10,
|
|
161
|
+
},
|
|
162
|
+
want: {
|
|
163
|
+
purchasedIds: ["ordli_loose"],
|
|
164
|
+
outboundIds: [],
|
|
165
|
+
inboundIds: [],
|
|
166
|
+
paidSubtotal: 100,
|
|
167
|
+
paidTotal: 110,
|
|
168
|
+
hasActiveExchange: false,
|
|
169
|
+
originalFulfillmentIsComplete: false,
|
|
170
|
+
reorderIds: ["ordli_loose"],
|
|
171
|
+
},
|
|
172
|
+
},
|
|
173
|
+
]
|
|
174
|
+
|
|
175
|
+
const OUTBOUND_CASES: {
|
|
176
|
+
name: string
|
|
177
|
+
items: OrderLineLike[]
|
|
178
|
+
exchanges?: OrderExchangeLike[] | null
|
|
179
|
+
want: string[]
|
|
180
|
+
}[] = [
|
|
181
|
+
{
|
|
182
|
+
name: "uses exchange additional_items when present",
|
|
183
|
+
items: [originalRing, pendant, replacementRing],
|
|
184
|
+
exchanges: [{ additional_items: [{ item_id: "ordli_ring_7" }] }],
|
|
185
|
+
want: ["ordli_ring_7"],
|
|
186
|
+
},
|
|
187
|
+
{
|
|
188
|
+
name: "ignores canceled exchange additional_items and infers replacement",
|
|
189
|
+
items: [originalRing, pendant, replacementRing],
|
|
190
|
+
exchanges: [
|
|
191
|
+
{
|
|
192
|
+
canceled_at: "2026-08-27T12:50:00.000Z",
|
|
193
|
+
additional_items: [{ item_id: "ordli_ring_7" }],
|
|
194
|
+
},
|
|
195
|
+
],
|
|
196
|
+
want: ["ordli_ring_7"],
|
|
197
|
+
},
|
|
198
|
+
{
|
|
199
|
+
name: "returns empty when there is no inbound exchange item",
|
|
200
|
+
items: [pendant],
|
|
201
|
+
exchanges: [],
|
|
202
|
+
want: [],
|
|
203
|
+
},
|
|
204
|
+
]
|
|
205
|
+
|
|
206
|
+
describe("presentOrderLines", () => {
|
|
207
|
+
for (const tc of PRESENT_CASES) {
|
|
208
|
+
it(tc.name, () => {
|
|
209
|
+
const view = presentOrderLines(tc.order)
|
|
210
|
+
assert.deepEqual(
|
|
211
|
+
view.purchasedItems.map((item) => item.id),
|
|
212
|
+
tc.want.purchasedIds
|
|
213
|
+
)
|
|
214
|
+
assert.deepEqual(
|
|
215
|
+
view.outboundItems.map((item) => item.id),
|
|
216
|
+
tc.want.outboundIds
|
|
217
|
+
)
|
|
218
|
+
assert.deepEqual(
|
|
219
|
+
view.inboundItems.map((item) => item.id),
|
|
220
|
+
tc.want.inboundIds
|
|
221
|
+
)
|
|
222
|
+
assert.deepEqual(
|
|
223
|
+
view.reorderItems.map((item) => item.id),
|
|
224
|
+
tc.want.reorderIds
|
|
225
|
+
)
|
|
226
|
+
assert.equal(view.paidSubtotal, tc.want.paidSubtotal)
|
|
227
|
+
assert.equal(view.paidTotal, tc.want.paidTotal)
|
|
228
|
+
assert.equal(view.hasActiveExchange, tc.want.hasActiveExchange)
|
|
229
|
+
assert.equal(
|
|
230
|
+
view.originalFulfillmentIsComplete,
|
|
231
|
+
tc.want.originalFulfillmentIsComplete
|
|
232
|
+
)
|
|
233
|
+
})
|
|
234
|
+
}
|
|
235
|
+
})
|
|
236
|
+
|
|
237
|
+
describe("outboundExchangeItemIds", () => {
|
|
238
|
+
for (const tc of OUTBOUND_CASES) {
|
|
239
|
+
it(tc.name, () => {
|
|
240
|
+
assert.deepEqual(
|
|
241
|
+
[...outboundExchangeItemIds(tc.items, tc.exchanges)],
|
|
242
|
+
tc.want
|
|
243
|
+
)
|
|
244
|
+
})
|
|
245
|
+
}
|
|
246
|
+
})
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
export type OrderLineQuantities = {
|
|
2
|
+
fulfilled_quantity?: number | null
|
|
3
|
+
delivered_quantity?: number | null
|
|
4
|
+
return_requested_quantity?: number | null
|
|
5
|
+
returned_quantity?: number | null
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export type OrderLineLike = {
|
|
9
|
+
id: string
|
|
10
|
+
product_id?: string | null
|
|
11
|
+
variant_id?: string | null
|
|
12
|
+
quantity?: number | null
|
|
13
|
+
total?: number | null
|
|
14
|
+
unit_price?: number | null
|
|
15
|
+
created_at?: string | Date | null
|
|
16
|
+
detail?: OrderLineQuantities | null
|
|
17
|
+
} & OrderLineQuantities
|
|
18
|
+
|
|
19
|
+
export type OrderExchangeLike = {
|
|
20
|
+
canceled_at?: string | Date | null
|
|
21
|
+
additional_items?: Array<{ id?: string | null; item_id?: string | null }> | null
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export type OrderLike = {
|
|
25
|
+
items?: OrderLineLike[] | null
|
|
26
|
+
shipping_total?: number | null
|
|
27
|
+
total?: number | null
|
|
28
|
+
exchanges?: OrderExchangeLike[] | null
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function qty(value?: number | null): number {
|
|
32
|
+
const n = Number(value)
|
|
33
|
+
return Number.isFinite(n) ? n : 0
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function lineQty(item: OrderLineLike, key: keyof OrderLineQuantities): number {
|
|
37
|
+
return qty(item.detail?.[key] ?? item[key])
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function itemCreatedAt(item: OrderLineLike): number {
|
|
41
|
+
if (!item.created_at) return 0
|
|
42
|
+
const t = new Date(item.created_at).getTime()
|
|
43
|
+
return Number.isFinite(t) ? t : 0
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function isCanceled(value?: string | Date | null): boolean {
|
|
47
|
+
return Boolean(value)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function isInboundExchangeItem(item: OrderLineLike): boolean {
|
|
51
|
+
return (
|
|
52
|
+
lineQty(item, "return_requested_quantity") > 0 ||
|
|
53
|
+
lineQty(item, "returned_quantity") > 0
|
|
54
|
+
)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function outboundExchangeItemIds(
|
|
58
|
+
items: OrderLineLike[],
|
|
59
|
+
exchanges?: OrderExchangeLike[] | null
|
|
60
|
+
): Set<string> {
|
|
61
|
+
const ids = new Set<string>()
|
|
62
|
+
for (const exchange of exchanges || []) {
|
|
63
|
+
if (isCanceled(exchange.canceled_at)) continue
|
|
64
|
+
for (const extra of exchange.additional_items || []) {
|
|
65
|
+
const id = extra.item_id || extra.id
|
|
66
|
+
if (id) ids.add(id)
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (ids.size > 0) return ids
|
|
70
|
+
|
|
71
|
+
const inbound = items.filter(isInboundExchangeItem)
|
|
72
|
+
if (!inbound.length) return ids
|
|
73
|
+
|
|
74
|
+
const inboundIds = new Set(inbound.map((item) => item.id))
|
|
75
|
+
const inboundByProduct = new Map<string, OrderLineLike[]>()
|
|
76
|
+
for (const item of inbound) {
|
|
77
|
+
if (!item.product_id) continue
|
|
78
|
+
const list = inboundByProduct.get(item.product_id) || []
|
|
79
|
+
list.push(item)
|
|
80
|
+
inboundByProduct.set(item.product_id, list)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
for (const item of items) {
|
|
84
|
+
if (inboundIds.has(item.id) || !item.product_id) continue
|
|
85
|
+
const matches = inboundByProduct.get(item.product_id)
|
|
86
|
+
if (!matches?.length) continue
|
|
87
|
+
|
|
88
|
+
const laterThanInbound = matches.some((origin) => {
|
|
89
|
+
const originAt = itemCreatedAt(origin)
|
|
90
|
+
const candidateAt = itemCreatedAt(item)
|
|
91
|
+
return originAt > 0 && candidateAt > originAt + 1000
|
|
92
|
+
})
|
|
93
|
+
const pendingReplacement =
|
|
94
|
+
lineQty(item, "fulfilled_quantity") === 0 &&
|
|
95
|
+
lineQty(item, "delivered_quantity") === 0
|
|
96
|
+
|
|
97
|
+
if (laterThanInbound || pendingReplacement) {
|
|
98
|
+
ids.add(item.id)
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return ids
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function presentOrderLines<T extends OrderLineLike>(order: {
|
|
106
|
+
items?: T[] | null
|
|
107
|
+
shipping_total?: number | null
|
|
108
|
+
total?: number | null
|
|
109
|
+
exchanges?: OrderExchangeLike[] | null
|
|
110
|
+
}): {
|
|
111
|
+
purchasedItems: T[]
|
|
112
|
+
outboundItems: T[]
|
|
113
|
+
inboundItems: T[]
|
|
114
|
+
reorderItems: T[]
|
|
115
|
+
paidSubtotal: number
|
|
116
|
+
paidTotal: number
|
|
117
|
+
hasActiveExchange: boolean
|
|
118
|
+
originalFulfillmentIsComplete: boolean
|
|
119
|
+
} {
|
|
120
|
+
const items = (order.items || []) as T[]
|
|
121
|
+
const outboundIds = outboundExchangeItemIds(items, order.exchanges)
|
|
122
|
+
const outboundItems = items.filter((item) => outboundIds.has(item.id))
|
|
123
|
+
const purchasedItems = items.filter((item) => !outboundIds.has(item.id))
|
|
124
|
+
const inboundItems = purchasedItems.filter(isInboundExchangeItem)
|
|
125
|
+
const paidSubtotal = purchasedItems.reduce((sum, item) => {
|
|
126
|
+
if (typeof item.total === "number") return sum + item.total
|
|
127
|
+
return sum + qty(item.unit_price) * qty(item.quantity)
|
|
128
|
+
}, 0)
|
|
129
|
+
const shipping = qty(order.shipping_total)
|
|
130
|
+
const hasActiveExchange = inboundItems.some(
|
|
131
|
+
(item) => lineQty(item, "return_requested_quantity") > 0
|
|
132
|
+
)
|
|
133
|
+
const originalFulfillmentIsComplete =
|
|
134
|
+
purchasedItems.length > 0 &&
|
|
135
|
+
purchasedItems.every(
|
|
136
|
+
(item) =>
|
|
137
|
+
lineQty(item, "delivered_quantity") > 0 ||
|
|
138
|
+
lineQty(item, "fulfilled_quantity") > 0
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
return {
|
|
142
|
+
purchasedItems,
|
|
143
|
+
outboundItems,
|
|
144
|
+
inboundItems,
|
|
145
|
+
reorderItems: purchasedItems,
|
|
146
|
+
paidSubtotal,
|
|
147
|
+
paidTotal: paidSubtotal + shipping,
|
|
148
|
+
hasActiveExchange,
|
|
149
|
+
originalFulfillmentIsComplete,
|
|
150
|
+
}
|
|
151
|
+
}
|