From e9921db55bfd694ec23cbfbe50a57c68b15a6b45 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Thu, 26 May 2022 19:31:36 +0530 Subject: [PATCH] feat: def traits for adding, getting notifications and marking them read --- db/db-core/src/errors.rs | 3 +++ db/db-core/src/lib.rs | 40 ++++++++++++++++++++++++++++++++++++++++ db/db-core/src/tests.rs | 24 ++++++++++++++++++++++++ 3 files changed, 67 insertions(+) diff --git a/db/db-core/src/errors.rs b/db/db-core/src/errors.rs index 702bb0f4..3c0c8289 100644 --- a/db/db-core/src/errors.rs +++ b/db/db-core/src/errors.rs @@ -47,6 +47,9 @@ pub enum DBError { /// Traffic pattern not found #[error("Traffic pattern not found")] TrafficPatternNotFound, + + #[error("Notification not found")] + NotificationNotFound, } /// Convenience type alias for grouping driver-specific errors diff --git a/db/db-core/src/lib.rs b/db/db-core/src/lib.rs index a3d6b871..9ec6b2e9 100644 --- a/db/db-core/src/lib.rs +++ b/db/db-core/src/lib.rs @@ -208,6 +208,46 @@ pub trait MCDatabase: std::marker::Send + std::marker::Sync + CloneSPDatabase { username: &str, captcha_key: &str, ) -> DBResult<()>; + + /// create new notification + async fn create_notification(&self, p: &AddNotification) -> DBResult<()>; + + /// get all unread notifications + async fn get_all_unread_notifications( + &self, + username: &str, + ) -> DBResult>; + + /// mark a notification read + async fn mark_notification_read(&self, username: &str, id: i32) -> DBResult<()>; +} + +#[derive(Debug, Clone, Default, Deserialize, Serialize, PartialEq)] +/// Represents notification +pub struct Notification { + /// receiver name of the notification + pub name: Option, + /// heading of the notification + pub heading: Option, + /// message of the notification + pub message: Option, + /// when notification was received + pub received: Option, + /// db assigned ID of the notification + pub id: Option, +} + +#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)] +/// Data required to add notification +pub struct AddNotification<'a> { + /// who is the notification addressed to? + pub to: &'a str, + /// notification sender + pub from: &'a str, + /// heading of the notification + pub heading: &'a str, + /// mesage of the notification + pub message: &'a str, } #[derive(Default, PartialEq, Serialize, Deserialize, Clone, Debug)] diff --git a/db/db-core/src/tests.rs b/db/db-core/src/tests.rs index f8888912..34cfb576 100644 --- a/db/db-core/src/tests.rs +++ b/db/db-core/src/tests.rs @@ -25,6 +25,7 @@ pub async fn database_works<'a, T: MCDatabase>( c: &CreateCaptcha<'a>, l: &[Level], tp: &TrafficPattern, + an: &AddNotification<'a>, ) { assert!(db.ping().await, "ping test"); if db.username_exists(p.username).await.unwrap() { @@ -133,6 +134,29 @@ pub async fn database_works<'a, T: MCDatabase>( "user was with empty email but email is set; so email should exsit" ); + /* + * test notification workflows + * 1. Add notifications: a minimum of two, to mark as read and test if it has affected it + * 2. Get unread notifications + * 3. Mark a notification read, check if it has affected Step #2 + */ + + // 1. add notification + db.create_notification(an).await.unwrap(); + db.create_notification(an).await.unwrap(); + + // 2. Get notifications + let notifications = db.get_all_unread_notifications(&an.to).await.unwrap(); + assert_eq!(notifications.len(), 2); + assert_eq!(notifications[0].heading.as_ref().unwrap(), an.heading); + + // 3. mark a notification read + db.mark_notification_read(an.to, notifications[0].id.unwrap()) + .await + .unwrap(); + let new_notifications = db.get_all_unread_notifications(&an.to).await.unwrap(); + assert_eq!(new_notifications.len(), 1); + // create captcha db.create_captcha(p.username, c).await.unwrap(); assert!(db.captcha_exists(None, c.key).await.unwrap());