@@ -17,6 +17,7 @@ generate_types!(
1717 schema_path = "./schema.graphql"
1818) ;
1919
20+ #[ allow( clippy:: upper_case_acronyms) ]
2021type URL = String ;
2122
2223#[ derive( Clone , Debug , PartialEq ) ]
@@ -52,8 +53,9 @@ pub struct ComponentParentMetafieldPriceAdjustment {
5253
5354#[ shopify_function]
5455fn function ( input : input:: ResponseData ) -> Result < output:: FunctionResult > {
55- let mut cart_operations: Vec < CartOperation > = get_merge_cart_operations ( & input. cart ) ;
56- cart_operations. extend ( get_expand_cart_operations ( & input. cart ) ) ;
56+ let cart_operations: Vec < CartOperation > = get_merge_cart_operations ( & input. cart )
57+ . chain ( get_expand_cart_operations ( & input. cart ) )
58+ . collect ( ) ;
5759
5860 Ok ( output:: FunctionResult {
5961 operations : Some ( cart_operations) ,
@@ -62,207 +64,167 @@ fn function(input: input::ResponseData) -> Result<output::FunctionResult> {
6264
6365// merge operation logic
6466
65- fn get_merge_cart_operations ( cart : & Cart ) -> Vec < CartOperation > {
66- let merge_parent_defintions: Vec < ComponentParent > = get_merge_parent_definitions ( cart) ;
67- let mut result: Vec < CartOperation > = Vec :: new ( ) ;
68-
69- for definition in merge_parent_defintions. iter ( ) {
70- let components_in_cart = get_components_in_cart ( cart, definition) ;
71- if components_in_cart. len ( ) == definition. component_reference . len ( ) {
72- let cart_lines: Vec < CartLineInput > = components_in_cart
73- . iter ( )
74- . map ( |component| CartLineInput {
75- cart_line_id : component. cart_line_id . clone ( ) ,
76- quantity : component. quantity . clone ( ) ,
77- } )
78- . collect ( ) ;
79-
80- let mut price: Option < PriceAdjustment > = None ;
81-
82- if let Some ( price_adjustment) = & definition. price_adjustment {
83- price = Some ( PriceAdjustment {
84- percentage_decrease : Some ( PriceAdjustmentValue {
85- value : ( * price_adjustment) . to_string ( ) ,
86- } ) ,
87- } ) ;
88- }
89-
90- let merge_operation: MergeOperation = MergeOperation {
91- parent_variant_id : definition. id . clone ( ) ,
92- title : None ,
93- cart_lines : cart_lines. clone ( ) ,
94- image : None ,
95- price : price,
96- } ;
67+ fn get_merge_cart_operations ( cart : & Cart ) -> impl Iterator < Item = CartOperation > + ' _ {
68+ let merge_parent_defintions = get_merge_parent_definitions ( cart) ;
69+ merge_parent_defintions
70+ . into_iter ( )
71+ . filter_map ( |definition| {
72+ let components_in_cart = get_components_in_cart ( cart, & definition) ;
73+ ( components_in_cart. len ( ) == definition. component_reference . len ( ) ) . then ( || {
74+ let cart_lines: Vec < CartLineInput > = components_in_cart
75+ . into_iter ( )
76+ . map ( |component| CartLineInput {
77+ cart_line_id : component. cart_line_id ,
78+ quantity : component. quantity ,
79+ } )
80+ . collect ( ) ;
81+
82+ let price = definition
83+ . price_adjustment
84+ . map ( |price_adjustment| PriceAdjustment {
85+ percentage_decrease : Some ( PriceAdjustmentValue {
86+ value : price_adjustment. to_string ( ) ,
87+ } ) ,
88+ } ) ;
9789
98- result. push ( CartOperation {
99- merge : Some ( merge_operation) ,
100- expand : None ,
101- } ) ;
102- }
103- }
90+ let merge_operation = MergeOperation {
91+ parent_variant_id : definition. id ,
92+ title : None ,
93+ cart_lines,
94+ image : None ,
95+ price,
96+ } ;
10497
105- return result;
98+ CartOperation {
99+ merge : Some ( merge_operation) ,
100+ expand : None ,
101+ }
102+ } )
103+ } )
106104}
107105
108106fn get_components_in_cart ( cart : & Cart , definition : & ComponentParent ) -> Vec < CartLineInput > {
109- let mut line_results: Vec < CartLineInput > = Vec :: new ( ) ;
110- for ( reference, quantity) in definition
107+ definition
111108 . component_reference
112109 . iter ( )
113110 . zip ( definition. component_quantities . iter ( ) )
114- {
115- for line in cart. lines . iter ( ) {
116- let variant = match & line. merchandise {
117- ProductVariant ( variant) => Some ( variant) ,
118- _ => None ,
119- } ;
120- if variant == None {
121- continue ;
122- }
123-
124- if let Some ( merchandise) = & variant {
125- if reference == & merchandise. id && & line. quantity >= quantity {
126- line_results. push ( CartLineInput {
127- cart_line_id : line. id . clone ( ) ,
128- quantity : quantity. clone ( ) ,
129- } ) ;
130- break ;
131- }
132- }
133- }
134- }
135-
136- return line_results;
111+ . filter_map ( |( reference, & quantity) | {
112+ cart. lines . iter ( ) . find_map ( move |line| {
113+ matches ! (
114+ & line. merchandise,
115+ ProductVariant ( merchandise) if reference == & merchandise. id && line. quantity >= quantity,
116+ ) . then ( || CartLineInput { cart_line_id : line. id . clone ( ) , quantity } )
117+ } )
118+ } )
119+ . collect ( )
137120}
138121
139122fn get_merge_parent_definitions ( cart : & Cart ) -> Vec < ComponentParent > {
140123 let mut merge_parent_defintions: Vec < ComponentParent > = Vec :: new ( ) ;
141124
142125 for line in cart. lines . iter ( ) {
143- let variant = match & line. merchandise {
144- ProductVariant ( variant) => Some ( variant) ,
145- _ => None ,
146- } ;
147- if variant == None {
148- continue ;
149- }
150-
151- if let Some ( merchandise) = & variant {
152- merge_parent_defintions. append ( & mut get_component_parents ( & merchandise) ) ;
126+ if let ProductVariant ( merchandise) = & line. merchandise {
127+ merge_parent_defintions. extend ( get_component_parents ( merchandise) ) ;
153128 }
154129 }
130+
155131 merge_parent_defintions. dedup_by ( |a, b| a. id == b. id ) ;
156- return merge_parent_defintions;
132+ merge_parent_defintions
157133}
158134
159135fn get_component_parents (
160136 variant : & InputCartLinesMerchandiseOnProductVariant ,
161- ) -> Vec < ComponentParent > {
162- let mut component_parents: Vec < ComponentParent > = Vec :: new ( ) ;
163- if let Some ( component_parents_metafield) = & variant. component_parents {
164- let value: Vec < ComponentParentMetafield > =
165- serde_json:: from_str ( & component_parents_metafield. value ) . unwrap ( ) ;
166- for parent_definition in value. iter ( ) {
167- let mut price: Option < f64 > = None ;
168-
169- if let Some ( price_adjustment) = & parent_definition. price_adjustment {
170- price = Some ( price_adjustment. value . clone ( ) ) ;
171- }
172-
173- component_parents. push ( ComponentParent {
174- id : parent_definition. id . clone ( ) ,
175- component_reference : parent_definition. component_reference . value . clone ( ) ,
176- component_quantities : parent_definition. component_quantities . value . clone ( ) ,
177- price_adjustment : price,
178- } ) ;
179- }
180- }
181-
182- return component_parents;
137+ ) -> impl Iterator < Item = ComponentParent > {
138+ variant
139+ . component_parents
140+ . as_ref ( )
141+ . map ( |component_parents_metafield| {
142+ let value: Vec < ComponentParentMetafield > =
143+ serde_json:: from_str ( & component_parents_metafield. value ) . unwrap ( ) ;
144+ value. into_iter ( ) . map ( |parent_definition| {
145+ let price = parent_definition
146+ . price_adjustment
147+ . as_ref ( )
148+ . map ( |price_adjustment| price_adjustment. value ) ;
149+
150+ ComponentParent {
151+ id : parent_definition. id ,
152+ component_reference : parent_definition. component_reference . value ,
153+ component_quantities : parent_definition. component_quantities . value ,
154+ price_adjustment : price,
155+ }
156+ } )
157+ } )
158+ . into_iter ( )
159+ . flatten ( )
183160}
184161
185162// expand operation logic
186163
187- fn get_expand_cart_operations ( cart : & Cart ) -> Vec < CartOperation > {
188- let mut result: Vec < CartOperation > = Vec :: new ( ) ;
189-
190- for line in cart. lines . iter ( ) {
191- let variant = match & line. merchandise {
192- ProductVariant ( variant) => Some ( variant) ,
193- _ => None ,
194- } ;
195- if variant == None {
196- continue ;
197- }
198-
199- if let Some ( merchandise) = & variant {
200- let component_references: Vec < ID > = get_component_references ( & merchandise) ;
201- let component_quantities: Vec < i64 > = get_component_quantities ( & merchandise) ;
164+ fn get_expand_cart_operations ( cart : & Cart ) -> impl Iterator < Item = CartOperation > + ' _ {
165+ cart. lines . iter ( ) . filter_map ( |line| {
166+ if let ProductVariant ( merchandise) = & line. merchandise {
167+ let component_references: Vec < ID > = get_component_references ( merchandise) ;
168+ let component_quantities: Vec < i64 > = get_component_quantities ( merchandise) ;
202169
203170 if component_references. is_empty ( )
204171 || component_references. len ( ) != component_quantities. len ( )
205172 {
206- continue ;
207- }
208-
209- let mut expand_relationships: Vec < ExpandedItem > = Vec :: new ( ) ;
210-
211- for ( reference, quantity) in
212- component_references. iter ( ) . zip ( component_quantities. iter ( ) )
213- {
214- let expand_relationship: ExpandedItem = ExpandedItem {
215- merchandise_id : reference. clone ( ) ,
216- quantity : quantity. clone ( ) ,
173+ None
174+ } else {
175+ let expand_relationships: Vec < ExpandedItem > = component_references
176+ . into_iter ( )
177+ . zip ( component_quantities. iter ( ) )
178+ . map ( |( reference, & quantity) | ExpandedItem {
179+ merchandise_id : reference,
180+ quantity,
181+ } )
182+ . collect ( ) ;
183+
184+ let price = get_price_adjustment ( merchandise) ;
185+
186+ let expand_operation = ExpandOperation {
187+ cart_line_id : line. id . clone ( ) ,
188+ expanded_cart_items : expand_relationships,
189+ price,
217190 } ;
218191
219- expand_relationships. push ( expand_relationship) ;
192+ Some ( CartOperation {
193+ expand : Some ( expand_operation) ,
194+ merge : None ,
195+ } )
220196 }
221-
222- let price: Option < PriceAdjustment > = get_price_adjustment ( & merchandise) ;
223-
224- let expand_operation: ExpandOperation = ExpandOperation {
225- cart_line_id : line. id . clone ( ) ,
226- expanded_cart_items : expand_relationships,
227- price : price,
228- } ;
229-
230- result. push ( CartOperation {
231- expand : Some ( expand_operation) ,
232- merge : None ,
233- } ) ;
197+ } else {
198+ None
234199 }
235- }
236-
237- return result;
200+ } )
238201}
239202
240203fn get_component_quantities ( variant : & InputCartLinesMerchandiseOnProductVariant ) -> Vec < i64 > {
241204 if let Some ( component_quantities_metafield) = & variant. component_quantities {
242- return serde_json:: from_str ( & component_quantities_metafield. value ) . unwrap ( ) ;
205+ serde_json:: from_str ( & component_quantities_metafield. value ) . unwrap ( )
206+ } else {
207+ Vec :: new ( )
243208 }
244-
245- return Vec :: new ( ) ;
246209}
247210
248211fn get_component_references ( variant : & InputCartLinesMerchandiseOnProductVariant ) -> Vec < ID > {
249212 if let Some ( component_reference_metafield) = & variant. component_reference {
250- return serde_json:: from_str ( & component_reference_metafield. value ) . unwrap ( ) ;
213+ serde_json:: from_str ( & component_reference_metafield. value ) . unwrap ( )
214+ } else {
215+ Vec :: new ( )
251216 }
252-
253- return Vec :: new ( ) ;
254217}
255218
256219fn get_price_adjustment (
257220 variant : & InputCartLinesMerchandiseOnProductVariant ,
258221) -> Option < PriceAdjustment > {
259- if let Some ( price_adjustment) = & variant. price_adjustment {
260- return Some ( PriceAdjustment {
222+ variant
223+ . price_adjustment
224+ . as_ref ( )
225+ . map ( |price_adjustment| PriceAdjustment {
261226 percentage_decrease : Some ( PriceAdjustmentValue {
262227 value : price_adjustment. value . parse ( ) . unwrap ( ) ,
263228 } ) ,
264- } ) ;
265- }
266-
267- return None ;
229+ } )
268230}
0 commit comments