Skip to content

Commit

Permalink
Merge pull request #9 from mainmatter/handle-service
Browse files Browse the repository at this point in the history
include selected service in message
  • Loading branch information
marcoow authored Mar 17, 2023
2 parents 751de6b + 800a32d commit b63395c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub struct Payload {
pub name: String,
pub email: String,
pub message: String,
pub service: String,
}

#[event(fetch, respond_with_errors)]
Expand Down Expand Up @@ -64,6 +65,8 @@ where
{
let message = payload.message.trim();
let message = if !message.is_empty() { message } else { "–" };
let service = payload.service.trim();
let service = if !service.is_empty() { service } else { "–" };

let data = json!({
"personalizations": [{
Expand All @@ -76,7 +79,7 @@ where
"subject": "Mainmatter inquiry",
"content": [{
"type": "text/plain",
"value": message
"value": format!("Service: {service}\n\n{message}")
}]
});

Expand Down
41 changes: 39 additions & 2 deletions tests/send_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ async fn it_works_for_the_happy_path() {
name: String::from("name"),
email: String::from("[email protected]"),
message: String::from("Hi!"),
service: String::from("Digital Products & Design"),
};
let result = send_message(payload, "api_key", &request_sendgrid).await;

Expand All @@ -34,7 +35,7 @@ async fn it_sends_the_right_payload_to_sendgrid() {
"subject": "Mainmatter inquiry",
"content": [{
"type": "text/plain",
"value": "Hi!"
"value": "Service: Digital Products & Design\n\nHi!"
}]
});

Expand All @@ -47,6 +48,7 @@ async fn it_sends_the_right_payload_to_sendgrid() {
name: String::from("name"),
email: String::from("[email protected]"),
message: String::from("Hi!"),
service: String::from("Digital Products & Design"),
};
let _result = send_message(payload, "api_key", &request_sendgrid).await;
}
Expand All @@ -65,7 +67,7 @@ async fn it_sends_an_empty_message_if_none_is_provided() {
"subject": "Mainmatter inquiry",
"content": [{
"type": "text/plain",
"value": "–"
"value": "Service: Digital Products & Design\n\n–"
}]
});

Expand All @@ -78,6 +80,39 @@ async fn it_sends_an_empty_message_if_none_is_provided() {
name: String::from("name"),
email: String::from("[email protected]"),
message: String::from(""),
service: String::from("Digital Products & Design"),
};
let _result = send_message(payload, "api_key", &request_sendgrid).await;
}

#[wasm_bindgen_test]
async fn it_sends_an_empty_service_if_none_is_provided() {
async fn request_sendgrid(_api_key: &str, data: String) -> Result<u16, NetworkError> {
let expected = json!({
"personalizations": [{
"to": [
{ "email": "[email protected]", "name": "Mainmatter" }
]}
],
"from": { "email": "[email protected]", "name": "name via mainmatter.com" },
"reply_to": { "email": "[email protected]", "name": "name" },
"subject": "Mainmatter inquiry",
"content": [{
"type": "text/plain",
"value": "Service: –\n\nHi!"
}]
});

assert_eq!(data, expected.to_string());

Ok(200)
}

let payload = Payload {
name: String::from("name"),
email: String::from("[email protected]"),
message: String::from("Hi!"),
service: String::from(""),
};
let _result = send_message(payload, "api_key", &request_sendgrid).await;
}
Expand All @@ -92,6 +127,7 @@ async fn it_responds_with_502_if_sendgrid_errors() {
name: String::from("name"),
email: String::from("[email protected]"),
message: String::from("Hi!"),
service: String::from(""),
};
let result = send_message(payload, "api_key", &request_sendgrid).await;

Expand All @@ -108,6 +144,7 @@ async fn it_responds_with_500_if_calling_sendgrid_errors() {
name: String::from("name"),
email: String::from("[email protected]"),
message: String::from("Hi!"),
service: String::from(""),
};
let result = send_message(payload, "api_key", &request_sendgrid).await;

Expand Down

0 comments on commit b63395c

Please sign in to comment.