Compute dashboard stats
Track open rate for new post notifications (user clicked the button in the link or not). No data about the user is collected during the process, it only uses an ID inserted by the issue delivery worker.
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
use std::fmt::Display;
|
||||
|
||||
use crate::{
|
||||
authentication::AuthenticatedUser,
|
||||
idempotency::{IdempotencyKey, save_response, try_processing},
|
||||
@@ -46,23 +48,40 @@ pub async fn insert_newsletter_issue(
|
||||
Ok(newsletter_issue_id)
|
||||
}
|
||||
|
||||
pub enum EmailType {
|
||||
NewPost,
|
||||
Newsletter,
|
||||
}
|
||||
|
||||
impl Display for EmailType {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
EmailType::NewPost => write!(f, "new_post"),
|
||||
EmailType::Newsletter => write!(f, "newsletter"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip_all)]
|
||||
pub async fn enqueue_delivery_tasks(
|
||||
transaction: &mut Transaction<'static, Postgres>,
|
||||
newsletter_issue_id: Uuid,
|
||||
kind: EmailType,
|
||||
) -> Result<(), sqlx::Error> {
|
||||
let query = sqlx::query!(
|
||||
r#"
|
||||
INSERT INTO issue_delivery_queue (
|
||||
newsletter_issue_id,
|
||||
subscriber_email,
|
||||
unsubscribe_token
|
||||
unsubscribe_token,
|
||||
kind
|
||||
)
|
||||
SELECT $1, email, unsubscribe_token
|
||||
SELECT $1, email, unsubscribe_token, $2
|
||||
FROM subscriptions
|
||||
WHERE status = 'confirmed'
|
||||
"#,
|
||||
newsletter_issue_id,
|
||||
kind.to_string()
|
||||
);
|
||||
transaction.execute(query).await?;
|
||||
Ok(())
|
||||
@@ -102,7 +121,7 @@ pub async fn publish_newsletter(
|
||||
.await
|
||||
.context("Failed to store newsletter issue details.")?;
|
||||
|
||||
enqueue_delivery_tasks(&mut transaction, issue_id)
|
||||
enqueue_delivery_tasks(&mut transaction, issue_id, EmailType::Newsletter)
|
||||
.await
|
||||
.context("Failed to enqueue delivery tasks.")?;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user