@pradip1995/segment-primitives 0.4.15 → 0.4.20

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.
@@ -1,151 +1,168 @@
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
- }
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
+ /** Replacement/outbound line that the customer has already received. */
58
+ export function isReceivedOutboundItem(item: OrderLineLike): boolean {
59
+ return (
60
+ lineQty(item, "delivered_quantity") > 0 ||
61
+ lineQty(item, "fulfilled_quantity") > 0
62
+ )
63
+ }
64
+
65
+ export function outboundExchangeItemIds(
66
+ items: OrderLineLike[],
67
+ exchanges?: OrderExchangeLike[] | null
68
+ ): Set<string> {
69
+ const ids = new Set<string>()
70
+ for (const exchange of exchanges || []) {
71
+ if (isCanceled(exchange.canceled_at)) continue
72
+ for (const extra of exchange.additional_items || []) {
73
+ const id = extra.item_id || extra.id
74
+ if (id) ids.add(id)
75
+ }
76
+ }
77
+ if (ids.size > 0) return ids
78
+
79
+ const inbound = items.filter(isInboundExchangeItem)
80
+ if (!inbound.length) return ids
81
+
82
+ const inboundIds = new Set(inbound.map((item) => item.id))
83
+ const inboundByProduct = new Map<string, OrderLineLike[]>()
84
+ for (const item of inbound) {
85
+ if (!item.product_id) continue
86
+ const list = inboundByProduct.get(item.product_id) || []
87
+ list.push(item)
88
+ inboundByProduct.set(item.product_id, list)
89
+ }
90
+
91
+ for (const item of items) {
92
+ if (inboundIds.has(item.id) || !item.product_id) continue
93
+ const matches = inboundByProduct.get(item.product_id)
94
+ if (!matches?.length) continue
95
+
96
+ const laterThanInbound = matches.some((origin) => {
97
+ const originAt = itemCreatedAt(origin)
98
+ const candidateAt = itemCreatedAt(item)
99
+ return originAt > 0 && candidateAt > originAt + 1000
100
+ })
101
+ const pendingReplacement =
102
+ lineQty(item, "fulfilled_quantity") === 0 &&
103
+ lineQty(item, "delivered_quantity") === 0
104
+
105
+ if (laterThanInbound || pendingReplacement) {
106
+ ids.add(item.id)
107
+ }
108
+ }
109
+
110
+ return ids
111
+ }
112
+
113
+ export function presentOrderLines<T extends OrderLineLike>(order: {
114
+ items?: T[] | null
115
+ shipping_total?: number | null
116
+ total?: number | null
117
+ exchanges?: OrderExchangeLike[] | null
118
+ }): {
119
+ purchasedItems: T[]
120
+ outboundItems: T[]
121
+ inboundItems: T[]
122
+ /**
123
+ * Lines the customer may return/exchange again:
124
+ * original purchased lines + delivered/fulfilled replacement lines.
125
+ * Pending (not yet shipped) replacements stay excluded.
126
+ */
127
+ returnExchangeItems: T[]
128
+ reorderItems: T[]
129
+ paidSubtotal: number
130
+ paidTotal: number
131
+ hasActiveExchange: boolean
132
+ originalFulfillmentIsComplete: boolean
133
+ } {
134
+ const items = (order.items || []) as T[]
135
+ const outboundIds = outboundExchangeItemIds(items, order.exchanges)
136
+ const outboundItems = items.filter((item) => outboundIds.has(item.id))
137
+ const purchasedItems = items.filter((item) => !outboundIds.has(item.id))
138
+ const inboundItems = purchasedItems.filter(isInboundExchangeItem)
139
+ const receivedOutboundItems = outboundItems.filter(isReceivedOutboundItem)
140
+ const returnExchangeItems = [...purchasedItems, ...receivedOutboundItems]
141
+ const paidSubtotal = purchasedItems.reduce((sum, item) => {
142
+ if (typeof item.total === "number") return sum + item.total
143
+ return sum + qty(item.unit_price) * qty(item.quantity)
144
+ }, 0)
145
+ const shipping = qty(order.shipping_total)
146
+ const hasActiveExchange = inboundItems.some(
147
+ (item) => lineQty(item, "return_requested_quantity") > 0
148
+ )
149
+ const originalFulfillmentIsComplete =
150
+ purchasedItems.length > 0 &&
151
+ purchasedItems.every(
152
+ (item) =>
153
+ lineQty(item, "delivered_quantity") > 0 ||
154
+ lineQty(item, "fulfilled_quantity") > 0
155
+ )
156
+
157
+ return {
158
+ purchasedItems,
159
+ outboundItems,
160
+ inboundItems,
161
+ returnExchangeItems,
162
+ reorderItems: purchasedItems,
163
+ paidSubtotal,
164
+ paidTotal: paidSubtotal + shipping,
165
+ hasActiveExchange,
166
+ originalFulfillmentIsComplete,
167
+ }
168
+ }
@@ -1,56 +1,56 @@
1
- "use client"
2
-
3
- import {
4
- createContext,
5
- useContext,
6
- useMemo,
7
- useState,
8
- type Dispatch,
9
- type ReactNode,
10
- type SetStateAction,
11
- } from "react"
12
- import type { ProductOptionsMap } from "./product-variants"
13
-
14
- type ProductSelectionContextValue = {
15
- selectedOptions: ProductOptionsMap
16
- setSelectedOptions: Dispatch<SetStateAction<ProductOptionsMap>>
17
- selectedVariantId: string | undefined
18
- setSelectedVariantId: Dispatch<SetStateAction<string | undefined>>
19
- }
20
-
21
- const ProductSelectionContext = createContext<ProductSelectionContextValue | undefined>(
22
- undefined
23
- )
24
-
25
- export function ProductSelectionProvider({
26
- children,
27
- initialOptions = {},
28
- initialVariantId,
29
- }: {
30
- children: ReactNode
31
- initialOptions?: ProductOptionsMap
32
- initialVariantId?: string
33
- }) {
34
- const [selectedOptions, setSelectedOptions] = useState<ProductOptionsMap>(initialOptions)
35
- const [selectedVariantId, setSelectedVariantId] = useState<string | undefined>(
36
- initialVariantId
37
- )
38
-
39
- const value = useMemo(
40
- () => ({
41
- selectedOptions,
42
- setSelectedOptions,
43
- selectedVariantId,
44
- setSelectedVariantId,
45
- }),
46
- [selectedOptions, selectedVariantId]
47
- )
48
-
49
- return (
50
- <ProductSelectionContext.Provider value={value}>{children}</ProductSelectionContext.Provider>
51
- )
52
- }
53
-
54
- export function useProductSelection() {
55
- return useContext(ProductSelectionContext)
56
- }
1
+ "use client"
2
+
3
+ import {
4
+ createContext,
5
+ useContext,
6
+ useMemo,
7
+ useState,
8
+ type Dispatch,
9
+ type ReactNode,
10
+ type SetStateAction,
11
+ } from "react"
12
+ import type { ProductOptionsMap } from "./product-variants"
13
+
14
+ type ProductSelectionContextValue = {
15
+ selectedOptions: ProductOptionsMap
16
+ setSelectedOptions: Dispatch<SetStateAction<ProductOptionsMap>>
17
+ selectedVariantId: string | undefined
18
+ setSelectedVariantId: Dispatch<SetStateAction<string | undefined>>
19
+ }
20
+
21
+ const ProductSelectionContext = createContext<ProductSelectionContextValue | undefined>(
22
+ undefined
23
+ )
24
+
25
+ export function ProductSelectionProvider({
26
+ children,
27
+ initialOptions = {},
28
+ initialVariantId,
29
+ }: {
30
+ children: ReactNode
31
+ initialOptions?: ProductOptionsMap
32
+ initialVariantId?: string
33
+ }) {
34
+ const [selectedOptions, setSelectedOptions] = useState<ProductOptionsMap>(initialOptions)
35
+ const [selectedVariantId, setSelectedVariantId] = useState<string | undefined>(
36
+ initialVariantId
37
+ )
38
+
39
+ const value = useMemo(
40
+ () => ({
41
+ selectedOptions,
42
+ setSelectedOptions,
43
+ selectedVariantId,
44
+ setSelectedVariantId,
45
+ }),
46
+ [selectedOptions, selectedVariantId]
47
+ )
48
+
49
+ return (
50
+ <ProductSelectionContext.Provider value={value}>{children}</ProductSelectionContext.Provider>
51
+ )
52
+ }
53
+
54
+ export function useProductSelection() {
55
+ return useContext(ProductSelectionContext)
56
+ }