remove users from trip after they have read the notificatoin
Some checks failed
CI/CD Pipeline / test (push) Failing after 3m50s
CI/CD Pipeline / deploy-staging (push) Has been skipped
CI/CD Pipeline / deploy-main (push) Has been skipped

This commit is contained in:
2024-05-21 19:41:00 +02:00
parent 1285c3bc28
commit b4967b54e9
12 changed files with 62 additions and 20 deletions

View File

@ -1,6 +1,7 @@
use std::ops::DerefMut;
use chrono::NaiveDateTime;
use regex::Regex;
use serde::{Deserialize, Serialize};
use sqlx::{FromRow, Sqlite, SqlitePool, Transaction};
@ -15,6 +16,7 @@ pub struct Notification {
pub created_at: NaiveDateTime,
pub category: String,
pub link: Option<String>,
pub action_after_reading: Option<String>,
}
impl Notification {
@ -30,13 +32,15 @@ impl Notification {
message: &str,
category: &str,
link: Option<&str>,
action_after_reading: Option<&str>,
) {
sqlx::query!(
"INSERT INTO notification(user_id, message, category, link) VALUES (?, ?, ?, ?)",
"INSERT INTO notification(user_id, message, category, link, action_after_reading) VALUES (?, ?, ?, ?, ?)",
user.id,
message,
category,
link
link,
action_after_reading
)
.execute(db.deref_mut())
.await
@ -49,9 +53,10 @@ impl Notification {
message: &str,
category: &str,
link: Option<&str>,
action_after_reading: Option<&str>,
) {
let mut tx = db.begin().await.unwrap();
Self::create_with_tx(&mut tx, user, message, category, link).await;
Self::create_with_tx(&mut tx, user, message, category, link, action_after_reading).await;
tx.commit().await.unwrap();
}
@ -61,11 +66,12 @@ impl Notification {
message: &str,
category: &str,
link: Option<&str>,
action_after_reading: Option<&str>,
) {
let users = User::all_with_role_tx(db, role).await;
for user in users {
Self::create_with_tx(db, &user, message, category, link).await;
Self::create_with_tx(db, &user, message, category, link, action_after_reading).await;
}
}
@ -75,16 +81,18 @@ impl Notification {
message: &str,
category: &str,
link: Option<&str>,
action_after_reading: Option<&str>,
) {
let mut tx = db.begin().await.unwrap();
Self::create_for_role_tx(&mut tx, role, message, category, link).await;
Self::create_for_role_tx(&mut tx, role, message, category, link, action_after_reading)
.await;
tx.commit().await.unwrap();
}
pub async fn for_user(db: &SqlitePool, user: &User) -> Vec<Self> {
let rows = sqlx::query!(
"
SELECT id, user_id, message, read_at, datetime(created_at, 'localtime') as created_at, category, link FROM notification
SELECT id, user_id, message, read_at, datetime(created_at, 'localtime') as created_at, category, link, action_after_reading FROM notification
WHERE
user_id = ?
AND (
@ -113,6 +121,7 @@ ORDER BY read_at DESC, created_at DESC;
.unwrap(),
category: rec.category,
link: rec.link,
action_after_reading: rec.action_after_reading,
})
.collect()
}
@ -125,5 +134,24 @@ ORDER BY read_at DESC, created_at DESC;
.execute(db)
.await
.unwrap();
if let Some(action) = self.action_after_reading.as_ref() {
// User read notification about cancelled trip/event
let re = Regex::new(r"^remove_user_trip_with_trip_details_id:(\d+)$").unwrap();
if let Some(caps) = re.captures(action) {
if let Some(matched) = caps.get(1) {
if let Ok(number) = matched.as_str().parse::<i32>() {
let _ = sqlx::query!(
"DELETE FROM user_trip WHERE user_id = ? AND trip_details_id = ?",
self.user_id,
number
)
.execute(db)
.await
.unwrap();
}
}
}
}
}
}