/
run.rs
199 lines (181 loc) · 6.27 KB
/
run.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
use shopify_function::prelude::*;
use shopify_function::Result;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, PartialEq)]
#[serde(rename_all(deserialize = "camelCase"))]
struct Configuration {
pub quantity: i64,
pub percentage: f64,
}
impl Configuration {
const DEFAULT_QUANTITY: i64 = 999;
const DEFAULT_PERCENTAGE: f64 = 0.0;
fn from_str(value: &str) -> Self {
serde_json::from_str(value).expect("Unable to parse configuration value from metafield")
}
}
impl Default for Configuration {
fn default() -> Self {
Configuration {
quantity: Self::DEFAULT_QUANTITY,
percentage: Self::DEFAULT_PERCENTAGE,
}
}
}
#[shopify_function_target(query_path = "src/run.graphql", schema_path = "schema.graphql")]
fn run(input: input::ResponseData) -> Result<output::FunctionRunResult> {
let no_discount = output::FunctionRunResult {
discounts: vec![],
discount_application_strategy: output::DiscountApplicationStrategy::FIRST,
};
let config = match input.discount_node.metafield {
Some(input::InputDiscountNodeMetafield { value }) => Configuration::from_str(&value),
None => return Ok(no_discount),
};
let targets = input
.cart
.lines
.iter()
.filter(|line| line.quantity >= config.quantity)
.filter_map(|line| match &line.merchandise {
input::InputCartLinesMerchandise::ProductVariant(variant) => Some(variant),
input::InputCartLinesMerchandise::CustomProduct => None,
})
.map(|variant| {
output::Target::ProductVariant(output::ProductVariantTarget {
id: variant.id.to_string(),
quantity: None,
})
})
.collect::<Vec<output::Target>>();
if targets.is_empty() {
eprintln!("No cart lines qualify for volume discount.");
return Ok(no_discount);
}
Ok(output::FunctionRunResult {
discounts: vec![output::Discount {
message: None,
targets,
value: output::Value::Percentage(output::Percentage {
value: Decimal(config.percentage),
}),
}],
discount_application_strategy: output::DiscountApplicationStrategy::FIRST,
})
}
#[cfg(test)]
mod tests {
use super::*;
use shopify_function::{run_function_with_input, Result};
#[test]
fn test_no_metafield_result_contains_no_discounts() -> Result<()> {
let result = run_function_with_input(
run,
r#"
{
"cart": {
"lines": [
{
"quantity": 10,
"merchandise": {
"__typename": "ProductVariant",
"id": "gid://shopify/ProductVariant/123456789"
}
}
]
},
"discountNode": {
"metafield": null
}
}
"#,
)?;
let expected = 0;
assert_eq!(result.discounts.len(), expected);
Ok(())
}
#[test]
fn test_quantity_unmet_result_contains_no_discounts() -> Result<()> {
let result = run_function_with_input(
run,
r#"
{
"cart": {
"lines": [
{
"quantity": 1,
"merchandise": {
"__typename": "ProductVariant",
"id": "gid://shopify/ProductVariant/123456789"
}
}
]
},
"discountNode": {
"metafield": {
"value": "{\"quantity\":2,\"percentage\":5}"
}
}
}
"#,
)?;
let expected = 0;
assert_eq!(result.discounts.len(), expected);
Ok(())
}
#[test]
fn test_quantity_met_discounts_variants() -> Result<()> {
use run::output::*;
let result = run_function_with_input(
run,
r#"
{
"cart": {
"lines": [
{
"quantity": 2,
"merchandise": {
"__typename": "ProductVariant",
"id": "gid://shopify/ProductVariant/123456789"
}
},
{
"quantity": 3,
"merchandise": {
"__typename": "ProductVariant",
"id": "gid://shopify/ProductVariant/987654321"
}
}
]
},
"discountNode": {
"metafield": {
"value": "{\"quantity\":2,\"percentage\":5}"
}
}
}
"#,
)?;
let expected = FunctionRunResult {
discount_application_strategy: DiscountApplicationStrategy::FIRST,
discounts: vec![Discount {
message: None,
targets: vec![
Target::ProductVariant(ProductVariantTarget {
id: "gid://shopify/ProductVariant/123456789".to_string(),
quantity: None,
}),
Target::ProductVariant(ProductVariantTarget {
id: "gid://shopify/ProductVariant/987654321".to_string(),
quantity: None,
}),
],
value: Value::Percentage(Percentage {
value: Decimal(5f64),
}),
}],
};
assert_eq!(result, expected);
Ok(())
}
}