Skip to content

Commit

Permalink
Merge pull request #36 from mainmatter/accept-company-name
Browse files Browse the repository at this point in the history
accept optional company
  • Loading branch information
marcoow authored Aug 6, 2024
2 parents 86d927a + 5f349c8 commit a643473
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub struct Payload {
pub email: String,
pub message: String,
pub service: Option<String>,
pub company: Option<String>,
}

#[event(fetch, respond_with_errors)]
Expand Down Expand Up @@ -74,12 +75,18 @@ where
} else {
"".to_owned()
};
let subject = if !service.is_empty() && service.to_lowercase() != "other" {
let mut subject = if !service.is_empty() && service.to_lowercase() != "other" {
format!("Mainmatter inquiry for {service}")
} else {
"Mainmatter inquiry".to_owned()
};

subject = if let Some(company) = payload.company {
format!("{} from {}", subject, company)
} else {
subject
};

let data = json!({
"personalizations": [{
"to": [
Expand Down
44 changes: 44 additions & 0 deletions tests/send_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ async fn it_works_for_the_happy_path() {
email: String::from("[email protected]"),
message: String::from("Hi!"),
service: Some(String::from("Digital Products & Design")),
company: None,
};
let result = send_message(payload, "api_key", "[email protected]", &request_sendgrid).await;

Expand Down Expand Up @@ -52,6 +53,7 @@ async fn it_sends_the_right_payload_to_sendgrid() {
email: String::from("[email protected]"),
message: String::from("Hi!"),
service: Some(String::from("Digital Products & Design")),
company: None,
};
let _result = send_message(payload, "api_key", "[email protected]", &request_sendgrid).await;
}
Expand Down Expand Up @@ -87,6 +89,7 @@ async fn it_sends_an_empty_message_if_none_is_provided() {
email: String::from("[email protected]"),
message: String::from(""),
service: Some(String::from("Digital Products & Design")),
company: None,
};
let _result = send_message(payload, "api_key", "[email protected]", &request_sendgrid).await;
}
Expand Down Expand Up @@ -122,6 +125,7 @@ async fn it_leaves_out_the_service_if_an_empty_one_is_provided() {
email: String::from("[email protected]"),
message: String::from("Hi!"),
service: Some(String::from("")),
company: None,
};
let _result = send_message(payload, "api_key", "[email protected]", &request_sendgrid).await;
}
Expand Down Expand Up @@ -157,6 +161,7 @@ async fn it_leaves_out_the_service_if_none_is_provided() {
email: String::from("[email protected]"),
message: String::from("Hi!"),
service: None,
company: None,
};
let _result = send_message(payload, "api_key", "[email protected]", &request_sendgrid).await;
}
Expand Down Expand Up @@ -192,6 +197,43 @@ async fn it_leaves_out_the_service_if_other_is_provided() {
email: String::from("[email protected]"),
message: String::from("Hi!"),
service: Some(String::from("Other")),
company: None,
};
let _result = send_message(payload, "api_key", "[email protected]", &request_sendgrid).await;
}

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

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: Some(String::from("Other")),
company: Some(String::from("Company")),
};
let _result = send_message(payload, "api_key", "[email protected]", &request_sendgrid).await;
}
Expand All @@ -207,6 +249,7 @@ async fn it_responds_with_502_if_sendgrid_errors() {
email: String::from("[email protected]"),
message: String::from("Hi!"),
service: Some(String::from("")),
company: None,
};
let result = send_message(payload, "api_key", "[email protected]", &request_sendgrid).await;

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

Expand Down

0 comments on commit a643473

Please sign in to comment.