feat: def traits for adding, getting notifications and marking them read
This commit is contained in:
parent
e4cf625d48
commit
e9921db55b
3 changed files with 67 additions and 0 deletions
|
@ -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
|
||||
|
|
|
@ -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<Vec<Notification>>;
|
||||
|
||||
/// 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<String>,
|
||||
/// heading of the notification
|
||||
pub heading: Option<String>,
|
||||
/// message of the notification
|
||||
pub message: Option<String>,
|
||||
/// when notification was received
|
||||
pub received: Option<i64>,
|
||||
/// db assigned ID of the notification
|
||||
pub id: Option<i32>,
|
||||
}
|
||||
|
||||
#[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)]
|
||||
|
|
|
@ -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());
|
||||
|
|
Loading…
Reference in a new issue