show notification badge in menu
Some checks are pending
CI/CD Pipeline / test (push) Waiting to run
CI/CD Pipeline / deploy-staging (push) Blocked by required conditions
CI/CD Pipeline / deploy-main (push) Blocked by required conditions

This commit is contained in:
2024-04-17 13:51:47 +02:00
parent e2746d5105
commit 7c71ce59bd
17 changed files with 101 additions and 48 deletions

View File

@ -47,16 +47,18 @@ pub struct User {
}
#[derive(Debug, Serialize, Deserialize)]
pub struct UserWithRoles {
pub struct UserWithRolesAndNotificationCount {
#[serde(flatten)]
pub user: User,
pub amount_unread_notifications: i32,
pub roles: Vec<String>,
}
impl UserWithRoles {
impl UserWithRolesAndNotificationCount {
pub async fn from_user(user: User, db: &SqlitePool) -> Self {
Self {
roles: user.roles(db).await,
amount_unread_notifications: user.amount_unread_notifications(db).await,
user,
}
}
@ -237,6 +239,17 @@ impl User {
.count
}
pub async fn amount_unread_notifications(&self, db: &SqlitePool) -> i32 {
sqlx::query!(
"SELECT COUNT(*) as count FROM notification WHERE user_id = ? AND read_at IS NULL",
self.id
)
.fetch_one(db)
.await
.unwrap()
.count
}
pub async fn has_role(&self, db: &SqlitePool, role: &str) -> bool {
if sqlx::query!(
"SELECT * FROM user_role WHERE user_id=? AND role_id = (SELECT id FROM role WHERE name = ?)",