@@ -4,6 +4,7 @@ import { assetDocument, createAssetDocument } from '../clubsx/assets'
4
4
import {
5
5
ErrorOr ,
6
6
isNotError ,
7
+ whenDefined ,
7
8
whenNotError ,
8
9
whenNotErrorAll ,
9
10
} from '@devprotocol/util-ts'
@@ -13,7 +14,8 @@ import {
13
14
ClubsOffering ,
14
15
} from '@devprotocol/clubs-core'
15
16
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'
17
19
18
20
type BundlePassportOffering = Omit < PassportOffering , 'bundle' > & {
19
21
bundle : NonNullable < PassportOffering [ 'bundle' ] >
@@ -22,17 +24,24 @@ type BundlePassportOffering = Omit<PassportOffering, 'bundle'> & {
22
24
const hasBundle = has ( 'bundle' )
23
25
const hasNoBundle = complement ( hasBundle )
24
26
25
- const findOffering = (
27
+ const findOffering = < T > (
26
28
payload : ClubsOffering [ 'payload' ] ,
27
- offerings ?: ReadonlyArray < ClubsOffering > ,
28
- ) : ErrorOr < ClubsOffering > => {
29
+ offerings : ReadonlyArray < ClubsOffering > ,
30
+ data : T ,
31
+ ) : ErrorOr < ClubsOffering & { __data : T } > => {
29
32
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 } ) ,
32
38
) ?? new Error ( 'Offering not found' )
33
39
)
34
40
}
35
41
42
+ const duplicateCartItems = ( offers : ( ClubsOffering & { __data : CartItem } ) [ ] ) =>
43
+ offers . map ( ( o ) => repeat ( o , o . __data . quantity ) ) . flat ( )
44
+
36
45
export const complete = async ( {
37
46
scope,
38
47
eoa,
@@ -53,34 +62,38 @@ export const complete = async ({
53
62
} )
54
63
const singleOfferings = whenNotError ( orderCompletionResult , ( result ) => {
55
64
const resolvedOfferings = result
56
- . map ( ( res ) => findOffering ( res . payload , offerings ) )
65
+ . map ( ( res ) => findOffering ( res . payload , offerings ?? [ ] , res ) )
57
66
. filter ( hasNoBundle )
58
67
return resolvedOfferings . every ( isNotError )
59
- ? resolvedOfferings
68
+ ? duplicateCartItems ( resolvedOfferings )
60
69
: new Error ( 'Failed to process single offerings' )
61
70
} )
71
+ type B = { payload : PassportOffering [ 'payload' ] ; cartItem : CartItem } [ ]
62
72
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 ) ,
80
93
)
81
94
82
95
return resolvedOfferings . every ( isNotError )
83
- ? resolvedOfferings
96
+ ? duplicateCartItems ( resolvedOfferings )
84
97
: new Error ( 'Failed to process bundled offerings' )
85
98
} )
86
99
const offeringsToProcess = whenNotErrorAll (
0 commit comments