1
- use shopify_function:: prelude:: * ;
2
- use shopify_function:: Result ;
1
+ use crate :: input:: InputCart as Cart ;
2
+ use crate :: input:: InputCartLinesMerchandise :: ProductVariant ;
3
+ use crate :: input:: InputCartLinesMerchandiseOnProductVariant ;
3
4
use crate :: output:: CartLineInput ;
4
- use crate :: output:: PriceAdjustment ;
5
- use crate :: output:: PriceAdjustmentValue ;
6
5
use crate :: output:: CartOperation ;
7
6
use crate :: output:: ExpandOperation ;
8
7
use crate :: output:: ExpandedItem ;
9
8
use crate :: output:: MergeOperation ;
10
- use crate :: input:: InputCart as Cart ;
11
- use crate :: input:: InputCartLinesMerchandiseOnProductVariant ;
12
- use crate :: input:: InputCartLinesMerchandise :: ProductVariant as ProductVariant ;
9
+ use crate :: output:: PriceAdjustment ;
10
+ use crate :: output:: PriceAdjustmentValue ;
13
11
use serde:: { Deserialize , Serialize } ;
12
+ use shopify_function:: prelude:: * ;
13
+ use shopify_function:: Result ;
14
14
15
15
generate_types ! (
16
16
query_path = "./input.graphql" ,
@@ -55,10 +55,12 @@ fn function(input: input::ResponseData) -> Result<output::FunctionResult> {
55
55
let mut cart_operations: Vec < CartOperation > = get_merge_cart_operations ( & input. cart ) ;
56
56
cart_operations. extend ( get_expand_cart_operations ( & input. cart ) ) ;
57
57
58
- Ok ( output:: FunctionResult { operations : Some ( cart_operations) } )
59
- }
58
+ Ok ( output:: FunctionResult {
59
+ operations : Some ( cart_operations) ,
60
+ } )
61
+ }
60
62
61
- // merge operation logic
63
+ // merge operation logic
62
64
63
65
fn get_merge_cart_operations ( cart : & Cart ) -> Vec < CartOperation > {
64
66
let merge_parent_defintions: Vec < ComponentParent > = get_merge_parent_definitions ( cart) ;
@@ -80,8 +82,8 @@ fn get_merge_cart_operations(cart: &Cart) -> Vec<CartOperation> {
80
82
if let Some ( price_adjustment) = & definition. price_adjustment {
81
83
price = Some ( PriceAdjustment {
82
84
percentage_decrease : Some ( PriceAdjustmentValue {
83
- value : ( * price_adjustment) . to_string ( )
84
- } )
85
+ value : ( * price_adjustment) . to_string ( ) ,
86
+ } ) ,
85
87
} ) ;
86
88
}
87
89
@@ -93,17 +95,17 @@ fn get_merge_cart_operations(cart: &Cart) -> Vec<CartOperation> {
93
95
price : price,
94
96
} ;
95
97
96
- result. push ( CartOperation { merge : Some ( merge_operation) , expand : None } ) ;
98
+ result. push ( CartOperation {
99
+ merge : Some ( merge_operation) ,
100
+ expand : None ,
101
+ } ) ;
97
102
}
98
103
}
99
104
100
105
return result;
101
106
}
102
107
103
- fn get_components_in_cart (
104
- cart : & Cart ,
105
- definition : & ComponentParent ,
106
- ) -> Vec < CartLineInput > {
108
+ fn get_components_in_cart ( cart : & Cart , definition : & ComponentParent ) -> Vec < CartLineInput > {
107
109
let mut line_results: Vec < CartLineInput > = Vec :: new ( ) ;
108
110
for ( reference, quantity) in definition
109
111
. component_reference
@@ -120,13 +122,13 @@ fn get_components_in_cart(
120
122
}
121
123
122
124
if let Some ( merchandise) = & variant {
123
- if reference == & merchandise. id && & line. quantity >= quantity {
124
- line_results. push ( CartLineInput {
125
- cart_line_id : line. id . clone ( ) ,
126
- quantity : quantity. clone ( ) ,
127
- } ) ;
128
- break ;
129
- }
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
+ }
130
132
}
131
133
}
132
134
}
@@ -154,7 +156,9 @@ fn get_merge_parent_definitions(cart: &Cart) -> Vec<ComponentParent> {
154
156
return merge_parent_defintions;
155
157
}
156
158
157
- fn get_component_parents ( variant : & InputCartLinesMerchandiseOnProductVariant ) -> Vec < ComponentParent > {
159
+ fn get_component_parents (
160
+ variant : & InputCartLinesMerchandiseOnProductVariant ,
161
+ ) -> Vec < ComponentParent > {
158
162
let mut component_parents: Vec < ComponentParent > = Vec :: new ( ) ;
159
163
if let Some ( component_parents_metafield) = & variant. component_parents {
160
164
let value: Vec < ComponentParentMetafield > =
@@ -196,13 +200,17 @@ fn get_expand_cart_operations(cart: &Cart) -> Vec<CartOperation> {
196
200
let component_references: Vec < ID > = get_component_references ( & merchandise) ;
197
201
let component_quantities: Vec < i64 > = get_component_quantities ( & merchandise) ;
198
202
199
- if component_references. is_empty ( ) || component_references. len ( ) != component_quantities. len ( ) {
203
+ if component_references. is_empty ( )
204
+ || component_references. len ( ) != component_quantities. len ( )
205
+ {
200
206
continue ;
201
207
}
202
208
203
209
let mut expand_relationships: Vec < ExpandedItem > = Vec :: new ( ) ;
204
210
205
- for ( reference, quantity) in component_references. iter ( ) . zip ( component_quantities. iter ( ) ) {
211
+ for ( reference, quantity) in
212
+ component_references. iter ( ) . zip ( component_quantities. iter ( ) )
213
+ {
206
214
let expand_relationship: ExpandedItem = ExpandedItem {
207
215
merchandise_id : reference. clone ( ) ,
208
216
quantity : quantity. clone ( ) ,
@@ -213,13 +221,16 @@ fn get_expand_cart_operations(cart: &Cart) -> Vec<CartOperation> {
213
221
214
222
let price: Option < PriceAdjustment > = get_price_adjustment ( & merchandise) ;
215
223
216
- let expand_operation: ExpandOperation = ExpandOperation {
224
+ let expand_operation: ExpandOperation = ExpandOperation {
217
225
cart_line_id : line. id . clone ( ) ,
218
226
expanded_cart_items : expand_relationships,
219
227
price : price,
220
228
} ;
221
229
222
- result. push ( CartOperation { expand : Some ( expand_operation) , merge : None } ) ;
230
+ result. push ( CartOperation {
231
+ expand : Some ( expand_operation) ,
232
+ merge : None ,
233
+ } ) ;
223
234
}
224
235
}
225
236
@@ -242,13 +253,15 @@ fn get_component_references(variant: &InputCartLinesMerchandiseOnProductVariant)
242
253
return Vec :: new ( ) ;
243
254
}
244
255
245
- fn get_price_adjustment ( variant : & InputCartLinesMerchandiseOnProductVariant ) -> Option < PriceAdjustment > {
256
+ fn get_price_adjustment (
257
+ variant : & InputCartLinesMerchandiseOnProductVariant ,
258
+ ) -> Option < PriceAdjustment > {
246
259
if let Some ( price_adjustment) = & variant. price_adjustment {
247
- return Some ( PriceAdjustment {
248
- percentage_decrease : Some ( PriceAdjustmentValue {
249
- value : price_adjustment. value . parse ( ) . unwrap ( )
250
- } )
251
- } ) ;
260
+ return Some ( PriceAdjustment {
261
+ percentage_decrease : Some ( PriceAdjustmentValue {
262
+ value : price_adjustment. value . parse ( ) . unwrap ( ) ,
263
+ } ) ,
264
+ } ) ;
252
265
}
253
266
254
267
return None ;
0 commit comments