Skip to content

Commit 5f57ff2

Browse files
committed
feat: enhance offering retrieval with data handling and duplicate cart item processing
1 parent c2bd2f4 commit 5f57ff2

File tree

1 file changed

+39
-26
lines changed

1 file changed

+39
-26
lines changed

src/fixtures/fulfillment/cart.ts

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { assetDocument, createAssetDocument } from '../clubsx/assets'
44
import {
55
ErrorOr,
66
isNotError,
7+
whenDefined,
78
whenNotError,
89
whenNotErrorAll,
910
} from '@devprotocol/util-ts'
@@ -13,7 +14,8 @@ import {
1314
ClubsOffering,
1415
} from '@devprotocol/clubs-core'
1516
import type { PassportOffering } from '@devprotocol/clubs-plugin-passports/types'
16-
import { complement, has } from 'ramda'
17+
import { complement, has, repeat } from 'ramda'
18+
import { CartItem } from '../../types'
1719

1820
type BundlePassportOffering = Omit<PassportOffering, 'bundle'> & {
1921
bundle: NonNullable<PassportOffering['bundle']>
@@ -22,17 +24,24 @@ type BundlePassportOffering = Omit<PassportOffering, 'bundle'> & {
2224
const hasBundle = has('bundle')
2325
const hasNoBundle = complement(hasBundle)
2426

25-
const findOffering = (
27+
const findOffering = <T>(
2628
payload: ClubsOffering['payload'],
27-
offerings?: ReadonlyArray<ClubsOffering>,
28-
): ErrorOr<ClubsOffering> => {
29+
offerings: ReadonlyArray<ClubsOffering>,
30+
data: T,
31+
): ErrorOr<ClubsOffering & { __data: T }> => {
2932
return (
30-
(offerings ?? []).find(
31-
(offering) => bytes32Hex(offering.payload) === bytes32Hex(payload),
33+
whenDefined(
34+
offerings.find(
35+
(offering) => bytes32Hex(offering.payload) === bytes32Hex(payload),
36+
),
37+
(off) => ({ ...off, __data: data }),
3238
) ?? new Error('Offering not found')
3339
)
3440
}
3541

42+
const duplicateCartItems = (offers: (ClubsOffering & { __data: CartItem })[]) =>
43+
offers.map((o) => repeat(o, o.__data.quantity)).flat()
44+
3645
export const complete = async ({
3746
scope,
3847
eoa,
@@ -53,34 +62,38 @@ export const complete = async ({
5362
})
5463
const singleOfferings = whenNotError(orderCompletionResult, (result) => {
5564
const resolvedOfferings = result
56-
.map((res) => findOffering(res.payload, offerings))
65+
.map((res) => findOffering(res.payload, offerings ?? [], res))
5766
.filter(hasNoBundle)
5867
return resolvedOfferings.every(isNotError)
59-
? resolvedOfferings
68+
? duplicateCartItems(resolvedOfferings)
6069
: new Error('Failed to process single offerings')
6170
})
71+
type B = { payload: PassportOffering['payload']; cartItem: CartItem }[]
6272
const bundledOfferings = whenNotError(orderCompletionResult, (result) => {
63-
const payloads = result.reduce(
64-
(acc, cur) => {
65-
const offering = (offerings ?? []).find(
66-
(offering): offering is BundlePassportOffering => {
67-
return (
68-
hasBundle(offering) &&
69-
Array.isArray(offering.bundle) &&
70-
bytes32Hex(offering.payload) === bytes32Hex(cur.payload)
71-
)
72-
},
73-
)
74-
return [...acc, ...(offering?.bundle ?? [])]
75-
},
76-
[] as PassportOffering['payload'][],
77-
)
78-
const resolvedOfferings = payloads.map((payload) =>
79-
findOffering(payload, offerings),
73+
const payloads = result.reduce((acc, cur) => {
74+
const offering = (offerings ?? []).find(
75+
(offering): offering is BundlePassportOffering => {
76+
return (
77+
hasBundle(offering) &&
78+
Array.isArray(offering.bundle) &&
79+
bytes32Hex(offering.payload) === bytes32Hex(cur.payload)
80+
)
81+
},
82+
)
83+
return [
84+
...acc,
85+
...((offering?.bundle ?? []).map((p) => ({
86+
payload: p,
87+
cartItem: cur,
88+
})) ?? []),
89+
]
90+
}, [] as B)
91+
const resolvedOfferings = payloads.map((b) =>
92+
findOffering(b.payload, offerings ?? [], b.cartItem),
8093
)
8194

8295
return resolvedOfferings.every(isNotError)
83-
? resolvedOfferings
96+
? duplicateCartItems(resolvedOfferings)
8497
: new Error('Failed to process bundled offerings')
8598
})
8699
const offeringsToProcess = whenNotErrorAll(

0 commit comments

Comments
 (0)