Fix send email request body
This commit is contained in:
@@ -62,6 +62,6 @@ mod tests {
|
|||||||
|
|
||||||
#[quickcheck_macros::quickcheck]
|
#[quickcheck_macros::quickcheck]
|
||||||
fn valid_emails_are_parsed_successfully(valid_email: ValidEmailFixture) -> bool {
|
fn valid_emails_are_parsed_successfully(valid_email: ValidEmailFixture) -> bool {
|
||||||
SubscriberEmail::parse(dbg!(valid_email.0)).is_ok()
|
SubscriberEmail::parse(valid_email.0).is_ok()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,8 +32,12 @@ impl EmailClient {
|
|||||||
) -> Result<(), reqwest::Error> {
|
) -> Result<(), reqwest::Error> {
|
||||||
let url = self.base_url.join("v1/email").unwrap();
|
let url = self.base_url.join("v1/email").unwrap();
|
||||||
let request_body = SendEmailRequest {
|
let request_body = SendEmailRequest {
|
||||||
from: self.sender.as_ref(),
|
from: EmailField {
|
||||||
to: vec![recipient.as_ref()],
|
email: self.sender.as_ref(),
|
||||||
|
},
|
||||||
|
to: vec![EmailField {
|
||||||
|
email: recipient.as_ref(),
|
||||||
|
}],
|
||||||
subject,
|
subject,
|
||||||
text: text_content,
|
text: text_content,
|
||||||
html: html_content,
|
html: html_content,
|
||||||
@@ -56,13 +60,18 @@ impl EmailClient {
|
|||||||
|
|
||||||
#[derive(serde::Serialize)]
|
#[derive(serde::Serialize)]
|
||||||
struct SendEmailRequest<'a> {
|
struct SendEmailRequest<'a> {
|
||||||
from: &'a str,
|
from: EmailField<'a>,
|
||||||
to: Vec<&'a str>,
|
to: Vec<EmailField<'a>>,
|
||||||
subject: &'a str,
|
subject: &'a str,
|
||||||
text: &'a str,
|
text: &'a str,
|
||||||
html: &'a str,
|
html: &'a str,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(serde::Serialize)]
|
||||||
|
struct EmailField<'a> {
|
||||||
|
email: &'a str,
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ fn generate_subscription_token() -> String {
|
|||||||
|
|
||||||
#[tracing::instrument(
|
#[tracing::instrument(
|
||||||
name = "Adding a new subscriber",
|
name = "Adding a new subscriber",
|
||||||
skip(connection_pool, form, email_client),
|
skip(connection_pool, email_client, base_url, form),
|
||||||
fields(
|
fields(
|
||||||
subscriber_email = %form.email,
|
subscriber_email = %form.email,
|
||||||
subscriber_name = %form.name
|
subscriber_name = %form.name
|
||||||
@@ -50,18 +50,19 @@ pub async fn subscribe(
|
|||||||
{
|
{
|
||||||
return StatusCode::INTERNAL_SERVER_ERROR;
|
return StatusCode::INTERNAL_SERVER_ERROR;
|
||||||
}
|
}
|
||||||
if send_confirmation_email(
|
if let Err(e) = send_confirmation_email(
|
||||||
&email_client,
|
&email_client,
|
||||||
&new_subscriber,
|
&new_subscriber,
|
||||||
&base_url,
|
&base_url,
|
||||||
&subscription_token,
|
&subscription_token,
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.is_err()
|
|
||||||
{
|
{
|
||||||
|
tracing::error!("Could not send confirmation email: {:?}", e);
|
||||||
return StatusCode::INTERNAL_SERVER_ERROR;
|
return StatusCode::INTERNAL_SERVER_ERROR;
|
||||||
}
|
}
|
||||||
if transaction.commit().await.is_err() {
|
if let Err(e) = transaction.commit().await {
|
||||||
|
tracing::error!("Failed to commit transaction: {:?}", e);
|
||||||
return StatusCode::INTERNAL_SERVER_ERROR;
|
return StatusCode::INTERNAL_SERVER_ERROR;
|
||||||
}
|
}
|
||||||
StatusCode::OK
|
StatusCode::OK
|
||||||
|
|||||||
Reference in New Issue
Block a user