1+ use anyhow:: Context ;
12use serde:: Deserialize ;
23
34use shopify_function:: prelude:: * ;
@@ -26,53 +27,44 @@ type CartResponseData = cart_run::input::ResponseData;
2627type DeliveryResponseData = delivery_run:: input:: ResponseData ;
2728
2829impl CartResponseData {
29- fn metafield ( & self ) -> Metafield {
30- self . discount_node
30+ fn metafield ( & self ) -> anyhow:: Result < Metafield > {
31+ let metafield = self
32+ . discount_node
3133 . metafield
3234 . as_ref ( )
33- . map ( |metafield| serde_json:: from_str ( & metafield. value ) )
34- . unwrap ( )
35- . expect ( "Missing required metafield configuration" )
35+ . context ( "Missing metafield" ) ?;
36+ serde_json:: from_str ( & metafield. value ) . context ( "Metafield value cannot be parsed" )
3637 }
37- fn valid_discount_codes ( & self ) -> Vec < String > {
38- self . fetch_result
39- . as_ref ( )
40- . unwrap ( )
41- . body
42- . as_ref ( )
43- . map ( |available_codes| serde_json:: from_str ( available_codes) )
44- . unwrap ( )
45- . expect ( "Missing required metafield configuration" )
38+ fn valid_discount_codes ( & self ) -> anyhow:: Result < Vec < String > > {
39+ let fetch_result = self . fetch_result . as_ref ( ) . context ( "Missing fetch result" ) ?;
40+ let body = fetch_result. body . as_ref ( ) . context ( "Missing body" ) ?;
41+ serde_json:: from_str ( body) . context ( "Fetch result body cannot be parsed" )
4642 }
4743}
4844
4945impl DeliveryResponseData {
50- fn metafield ( & self ) -> Metafield {
51- self . discount_node
46+ fn metafield ( & self ) -> anyhow:: Result < Metafield > {
47+ let metafield = & self
48+ . discount_node
5249 . metafield
5350 . as_ref ( )
54- . map ( |metafield| serde_json:: from_str ( & metafield. value ) )
55- . unwrap ( )
56- . expect ( "Missing required metafield configuration" )
51+ . context ( "Missing metafield" ) ?;
52+ serde_json:: from_str ( & metafield. value ) . context ( "Metafield value cannot be parsed" )
5753 }
5854
59- fn valid_discount_codes ( & self ) -> Vec < String > {
60- self . fetch_result
61- . as_ref ( )
62- . unwrap ( )
63- . body
64- . as_ref ( )
65- . map ( |available_codes| serde_json:: from_str ( available_codes) )
66- . unwrap ( )
67- . expect ( "Missing required metafield configuration" )
55+ fn valid_discount_codes ( & self ) -> anyhow:: Result < Vec < String > > {
56+ let fetch_result = self . fetch_result . as_ref ( ) . expect ( "Missing fetch result" ) ;
57+ let body = fetch_result. body . as_ref ( ) . expect ( "Missing body" ) ;
58+ serde_json:: from_str ( body) . context ( "Fetch result body cannot be parsed" )
6859 }
6960}
7061
7162#[ derive( Deserialize ) ]
63+ #[ serde( rename_all = "camelCase" ) ]
7264struct Metafield {
73- cart_percent : Option < Decimal > ,
74- product_percent : Option < Decimal > ,
75- delivery_percent : Option < Decimal > ,
65+ order_percentage : Option < Decimal > ,
66+ product_percentage : Option < Decimal > ,
67+ delivery_percentage : Option < Decimal > ,
7668}
7769
7870#[ shopify_function_target(
@@ -81,15 +73,14 @@ struct Metafield {
8173 schema_path = "schema.graphql"
8274) ]
8375fn cart_run ( input : CartResponseData ) -> Result < FunctionCartRunResult > {
84- let default = FunctionCartRunResult { operations : vec ! [ ] } ;
85- let codes = input. valid_discount_codes ( ) ;
76+ let codes = input. valid_discount_codes ( ) ?;
8677 let available_discount_code = codes. first ( ) ;
8778
8879 if available_discount_code. is_none ( ) {
89- return Ok ( default ) ;
80+ return Ok ( FunctionCartRunResult { operations : vec ! [ ] } ) ;
9081 }
9182
92- let metafield = input. metafield ( ) ;
83+ let metafield = input. metafield ( ) ? ;
9384 let mut operations: Vec < CartOperation > = vec ! [ ] ;
9485 let available_discount_code = available_discount_code. unwrap ( ) ;
9586
@@ -101,11 +92,11 @@ fn cart_run(input: CartResponseData) -> Result<FunctionCartRunResult> {
10192 } ,
10293 ) ) ;
10394
104- if metafield. cart_percent . is_some ( ) {
95+ if metafield. order_percentage . is_some ( ) {
10596 operations. push ( create_order_discount ( & metafield, available_discount_code) ) ;
10697 }
10798
108- if metafield. product_percent . is_some ( ) {
99+ if metafield. product_percentage . is_some ( ) {
109100 let highest_priced_line = input
110101 . cart
111102 . lines
@@ -132,13 +123,12 @@ fn cart_run(input: CartResponseData) -> Result<FunctionCartRunResult> {
132123 schema_path = "schema.graphql"
133124) ]
134125fn delivery_run ( input : DeliveryResponseData ) -> Result < FunctionDeliveryRunResult > {
135- let default = FunctionDeliveryRunResult { operations : vec ! [ ] } ;
136- let codes = input. valid_discount_codes ( ) ;
126+ let codes = input. valid_discount_codes ( ) ?;
137127 let available_discount_code = codes. first ( ) ;
138- let metafield = input. metafield ( ) ;
128+ let metafield = input. metafield ( ) ? ;
139129
140- if available_discount_code. is_none ( ) || metafield. delivery_percent . is_none ( ) {
141- return Ok ( default ) ;
130+ if available_discount_code. is_none ( ) || metafield. delivery_percentage . is_none ( ) {
131+ return Ok ( FunctionDeliveryRunResult { operations : vec ! [ ] } ) ;
142132 }
143133
144134 let mut operations: Vec < DeliveryOperation > = vec ! [ ] ;
@@ -183,7 +173,7 @@ fn create_order_discount(metafield: &Metafield, available_discount_code: &str) -
183173 } ) ,
184174 message: None ,
185175 value: OrderDiscountCandidateValue :: Percentage ( CartPercentage {
186- value: metafield. cart_percent . unwrap( ) ,
176+ value: metafield. order_percentage . unwrap( ) ,
187177 } ) ,
188178 conditions: None ,
189179 } ] ,
@@ -207,7 +197,7 @@ fn create_product_discount(
207197 } ) ,
208198 message: None ,
209199 value: ProductDiscountCandidateValue :: Percentage ( CartPercentage {
210- value: metafield. product_percent . unwrap( ) ,
200+ value: metafield. product_percentage . unwrap( ) ,
211201 } ) ,
212202 } ] ,
213203 } )
@@ -225,7 +215,7 @@ fn create_delivery_discount_candidate(
225215 } ,
226216 ) ] ,
227217 value : DeliveryDiscountCandidateValue :: Percentage ( DeliveryPercentage {
228- value : metafield. delivery_percent . unwrap ( ) ,
218+ value : metafield. delivery_percentage . unwrap ( ) ,
229219 } ) ,
230220 message : None ,
231221 associated_discount_code : Some ( DeliveryAssociatedDiscountCode {
@@ -271,9 +261,9 @@ mod tests {
271261 "discountNode" : {
272262 "metafield" : {
273263 "value" : json!( {
274- "cart_percent " : "10" ,
275- "product_percent " : "20" ,
276- "delivery_percent " : "30" ,
264+ "orderPercentage " : "10" ,
265+ "productPercentage " : "20" ,
266+ "deliveryPercentage " : "30" ,
277267 } ) . to_string( ) ,
278268 }
279269 } ,
0 commit comments