Skip to content

Commit a258837

Browse files
use enteredDiscountCode
1 parent d246160 commit a258837

File tree

5 files changed

+272
-181
lines changed

5 files changed

+272
-181
lines changed

discounts/rust/discounts/default/Cargo.toml.liquid

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "{{handle | replace: " ", "-" | downcase}}"
33
version = "1.0.0"
44
edition = "2021"
5-
rust-version = "1.62"
5+
rust-version = "1.84"
66

77
[dependencies]
88
serde = { version = "1.0.13", features = ["derive"] }
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
query Input {
2-
cart {
3-
buyerIdentity {
4-
email
5-
}
6-
}
2+
enteredDiscountCodes
73
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
use serde_json::json;
2+
use shopify_function::prelude::*;
3+
use shopify_function::Result;
4+
5+
use cart_fetch::output::{
6+
FunctionCartFetchResult, HttpRequest as CartHttpRequest,
7+
HttpRequestMethod as CartHttpRequestMethod, HttpRequestPolicy as CartHttpRequestPolicy,
8+
};
9+
10+
use delivery_fetch::output::FunctionDeliveryFetchResult;
11+
use delivery_fetch::output::{
12+
HttpRequest as DeliveryHttpRequest, HttpRequestMethod as DeliveryHttpRequestMethod,
13+
HttpRequestPolicy as DeliveryHttpRequestPolicy,
14+
};
15+
16+
type CartFetchResponseData = cart_fetch::input::ResponseData;
17+
type DeliveryFetchResponseData = delivery_fetch::input::ResponseData;
18+
type CartHttpRequestHeader = cart_fetch::output::HttpRequestHeader;
19+
type DeliveryHttpRequestHeader = delivery_fetch::output::HttpRequestHeader;
20+
21+
#[shopify_function_target(
22+
target = "cart_fetch",
23+
query_path = "src/fetch.graphql",
24+
schema_path = "schema.graphql"
25+
)]
26+
fn cart_fetch(input: CartFetchResponseData) -> Result<FunctionCartFetchResult> {
27+
let body = json!({
28+
"enteredDiscountCodes": input.entered_discount_codes
29+
});
30+
31+
Ok(FunctionCartFetchResult {
32+
request: Some(CartHttpRequest {
33+
body: Some(body.to_string()),
34+
headers: vec![CartHttpRequestHeader {
35+
name: "Accept".to_string(),
36+
value: "application/json; charset=utf-8".to_string(),
37+
}],
38+
json_body: Some(body.clone()),
39+
method: CartHttpRequestMethod::POST,
40+
policy: CartHttpRequestPolicy {
41+
read_timeout_ms: 2000,
42+
},
43+
url: "http://localhost:3000".to_string(),
44+
}),
45+
})
46+
}
47+
48+
#[shopify_function_target(
49+
target = "delivery_fetch",
50+
query_path = "src/fetch.graphql",
51+
schema_path = "schema.graphql"
52+
)]
53+
fn delivery_fetch(input: DeliveryFetchResponseData) -> Result<FunctionDeliveryFetchResult> {
54+
let body = json!({
55+
"enteredDiscountCodes": input.entered_discount_codes
56+
});
57+
58+
Ok(FunctionDeliveryFetchResult {
59+
request: Some(DeliveryHttpRequest {
60+
body: Some(body.to_string()),
61+
headers: vec![DeliveryHttpRequestHeader {
62+
name: "Accept".to_string(),
63+
value: "application/json; charset=utf-8".to_string(),
64+
}],
65+
json_body: Some(body.clone()),
66+
method: DeliveryHttpRequestMethod::POST,
67+
policy: DeliveryHttpRequestPolicy {
68+
read_timeout_ms: 2000,
69+
},
70+
url: "http://localhost:3000".to_string(),
71+
}),
72+
})
73+
}
74+
75+
#[cfg(test)]
76+
mod tests {
77+
use super::*;
78+
use shopify_function::{run_function_with_input, Result};
79+
80+
fn get_fetch_input_json() -> serde_json::Value {
81+
json!({
82+
"enteredDiscountCodes": ["WELCOME10"]
83+
})
84+
}
85+
86+
#[test]
87+
fn test_cart_fetch() -> Result<()> {
88+
let fetch_input = get_fetch_input_json();
89+
90+
let expected_fetch_result = FunctionCartFetchResult {
91+
request: Some(CartHttpRequest {
92+
body: Some(fetch_input.to_string()),
93+
headers: vec![CartHttpRequestHeader {
94+
name: "Accept".to_string(),
95+
value: "application/json; charset=utf-8".to_string(),
96+
}],
97+
json_body: Some(fetch_input.clone()),
98+
method: CartHttpRequestMethod::POST,
99+
policy: CartHttpRequestPolicy {
100+
read_timeout_ms: 2000,
101+
},
102+
url: "http://localhost:3000".to_string(),
103+
}),
104+
};
105+
106+
assert_eq!(
107+
run_function_with_input(cart_fetch, &fetch_input.to_string())?,
108+
expected_fetch_result
109+
);
110+
Ok(())
111+
}
112+
113+
#[test]
114+
fn test_delivery_fetch() -> Result<()> {
115+
let fetch_input = get_fetch_input_json();
116+
117+
let expected_fetch_result = FunctionDeliveryFetchResult {
118+
request: Some(DeliveryHttpRequest {
119+
body: Some(fetch_input.to_string()),
120+
headers: vec![DeliveryHttpRequestHeader {
121+
name: "Accept".to_string(),
122+
value: "application/json; charset=utf-8".to_string(),
123+
}],
124+
json_body: Some(fetch_input.clone()),
125+
method: DeliveryHttpRequestMethod::POST,
126+
policy: DeliveryHttpRequestPolicy {
127+
read_timeout_ms: 2000,
128+
},
129+
url: "http://localhost:3000".to_string(),
130+
}),
131+
};
132+
133+
assert_eq!(
134+
run_function_with_input(delivery_fetch, &fetch_input.to_string())?,
135+
expected_fetch_result
136+
);
137+
Ok(())
138+
}
139+
}

discounts/rust/discounts/default/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::process;
2+
pub mod fetch;
23
pub mod run;
34

45
fn main() {

0 commit comments

Comments
 (0)