Handler to send emails to confirmed subscribers

This commit is contained in:
Alphonse Paix
2025-08-27 12:13:58 +02:00
parent 684519f689
commit 3ae50830f4
8 changed files with 242 additions and 23 deletions

View File

@@ -1,7 +1,9 @@
mod health_check;
mod newsletters;
mod subscriptions;
mod subscriptions_confirm;
pub use health_check::*;
pub use newsletters::*;
pub use subscriptions::*;
pub use subscriptions_confirm::*;

110
src/routes/newsletters.rs Normal file
View File

@@ -0,0 +1,110 @@
use crate::{domain::SubscriberEmail, routes::error_chain_fmt, startup::AppState};
use anyhow::Context;
use axum::{
Json,
extract::State,
response::{IntoResponse, Response},
};
use reqwest::StatusCode;
use sqlx::PgPool;
#[derive(thiserror::Error)]
pub enum PublishError {
#[error(transparent)]
UnexpectedError(#[from] anyhow::Error),
}
impl std::fmt::Debug for PublishError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
error_chain_fmt(self, f)
}
}
impl IntoResponse for PublishError {
fn into_response(self) -> Response {
#[derive(serde::Serialize)]
struct ErrorResponse<'a> {
message: &'a str,
}
tracing::error!("{:?}", self);
let status = match self {
PublishError::UnexpectedError(_) => StatusCode::INTERNAL_SERVER_ERROR,
};
let message = "An internal server error occured.";
(status, Json(ErrorResponse { message })).into_response()
}
}
#[derive(serde::Deserialize)]
pub struct BodyData {
title: String,
content: Content,
}
#[derive(serde::Deserialize)]
pub struct Content {
html: String,
text: String,
}
#[tracing::instrument(
name = "Publishing a newsletter",
skip(connection_pool, email_client, body)
)]
pub async fn publish_newsletter(
State(AppState {
connection_pool,
email_client,
..
}): State<AppState>,
body: Json<BodyData>,
) -> Result<Response, PublishError> {
let subscribers = get_confirmed_subscribers(&connection_pool).await?;
for subscriber in subscribers {
match subscriber {
Ok(ConfirmedSubscriber { email, .. }) => {
email_client
.send_email(&email, &body.title, &body.content.html, &body.content.text)
.await
.with_context(|| {
format!("Failed to send newsletter issue to {}", email.as_ref())
})?;
}
Err(e) => {
tracing::warn!(
"Skipping a confirmed subscriber. Their stored contact details are invalid: {}",
e
)
}
}
}
Ok(StatusCode::OK.into_response())
}
#[allow(dead_code)]
struct ConfirmedSubscriber {
name: String,
email: SubscriberEmail,
}
#[tracing::instrument(name = "Get confirmed subscribers", skip(connection_pool))]
async fn get_confirmed_subscribers(
connection_pool: &PgPool,
) -> Result<Vec<Result<ConfirmedSubscriber, anyhow::Error>>, anyhow::Error> {
let rows = sqlx::query!("SELECT name, email FROM subscriptions WHERE status = 'confirmed'")
.fetch_all(connection_pool)
.await?;
let confirmed_subscribers = rows
.into_iter()
.map(|r| match SubscriberEmail::parse(r.email) {
Ok(email) => Ok(ConfirmedSubscriber {
name: r.name,
email,
}),
Err(e) => Err(anyhow::anyhow!(e)),
})
.collect();
Ok(confirmed_subscribers)
}

View File

@@ -24,7 +24,10 @@ fn generate_subscription_token() -> String {
.collect()
}
fn error_chain_fmt(e: &impl std::error::Error, f: &mut std::fmt::Formatter) -> std::fmt::Result {
pub fn error_chain_fmt(
e: &impl std::error::Error,
f: &mut std::fmt::Formatter,
) -> std::fmt::Result {
writeln!(f, "{}", e)?;
let mut current = e.source();
while let Some(cause) = current {
@@ -37,24 +40,12 @@ fn error_chain_fmt(e: &impl std::error::Error, f: &mut std::fmt::Formatter) -> s
Ok(())
}
// #[derive(thiserror::Error)]
// pub enum SubscribeError {
// #[error("Failed to store the confirmation token for a new subscriber.")]
// StoreToken(#[from] StoreTokenError),
// #[error("A database error occured.")]
// Database(#[from] sqlx::Error),
// #[error("Failed to send a confirmation email.")]
// SendEmail(#[from] reqwest::Error),
// #[error("{0}")]
// Validation(String),
// }
#[derive(thiserror::Error)]
pub enum SubscribeError {
#[error(transparent)]
UnexpectedError(#[from] anyhow::Error),
#[error("{0}")]
Validation(String),
ValidationError(String),
}
impl std::fmt::Debug for SubscribeError {
@@ -72,15 +63,9 @@ impl IntoResponse for SubscribeError {
tracing::error!("{:?}", self);
// let status = match self {
// SubscribeError::StoreToken(_)
// | SubscribeError::Database(_)
// | SubscribeError::SendEmail(_) => StatusCode::INTERNAL_SERVER_ERROR,
// SubscribeError::Validation(_) => StatusCode::BAD_REQUEST,
// };
let status = match self {
SubscribeError::UnexpectedError(_) => StatusCode::INTERNAL_SERVER_ERROR,
SubscribeError::Validation(_) => StatusCode::BAD_REQUEST,
SubscribeError::ValidationError(_) => StatusCode::BAD_REQUEST,
};
let message = "An internal server error occured.";
(status, Json(ErrorResponse { message })).into_response()
@@ -107,7 +92,7 @@ pub async fn subscribe(
.begin()
.await
.context("Failed to acquire a Postgres connection from the pool.")?;
let new_subscriber = form.try_into().map_err(SubscribeError::Validation)?;
let new_subscriber = form.try_into().map_err(SubscribeError::ValidationError)?;
let subscriber_id = insert_subscriber(&mut transaction, &new_subscriber)
.await
.context("Failed to insert new subscriber in the database.")?;

View File

@@ -61,6 +61,7 @@ pub fn app(connection_pool: PgPool, email_client: EmailClient, base_url: String)
.route("/health_check", get(health_check))
.route("/subscriptions", post(subscribe))
.route("/subscriptions/confirm", get(confirm))
.route("/newsletters", post(publish_newsletter))
.layer(
TraceLayer::new_for_http()
.make_span_with(|request: &Request<_>| {