diff --git a/backend/src/db/notifications.rs b/backend/src/db/notifications.rs index 80e7fdf..892ec95 100644 --- a/backend/src/db/notifications.rs +++ b/backend/src/db/notifications.rs @@ -108,6 +108,21 @@ pub fn mark_as_new(id: i64) -> Result<(), DbError> { } } +pub fn mark_all_as_old() -> Result<(), DbError> { + let conn = db::get_db_connection(); + + match conn.execute("UPDATE notifications SET is_new=0 WHERE is_new=1", []) { + Ok(_) => { + debug!("All notifications flagged as old"); + Ok(()) + } + Err(error) => { + error!("Error marking all notifications as old: {error}"); + Err(DbError::from(error)) + } + } +} + pub fn read(id: i64) -> Option { let conn = db::get_db_connection(); @@ -207,6 +222,48 @@ mod tests { ); } + #[tokio::test] + async fn test_mark_all_as_old() { + tests_common::setup().await; + + // Insert two new notifications + let id1 = insert(Notification::new( + Utc::now(), + NotificationType::Other, + "New notification 1".to_string(), + "Body 1".to_string(), + true, + )) + .unwrap(); + + let id2 = insert(Notification::new( + Utc::now(), + NotificationType::Other, + "New notification 2".to_string(), + "Body 2".to_string(), + true, + )) + .unwrap(); + + // Mark all as old + mark_all_as_old().unwrap(); + + // Both should now have is_new=false + assert!( + !read(id1).unwrap().is_new, + "Notification id={id1} should have is_new=0 after mark_all_as_old" + ); + assert!( + !read(id2).unwrap().is_new, + "Notification id={id2} should have is_new=0 after mark_all_as_old" + ); + + // Restore seeded test notifications so other tests are not affected + mark_as_new(1).unwrap(); + mark_as_new(3).unwrap(); + mark_as_new(5).unwrap(); + } + #[tokio::test] async fn test_insert() { tests_common::setup().await; diff --git a/backend/src/web_server.rs b/backend/src/web_server.rs index 8a95024..9b1ee09 100644 --- a/backend/src/web_server.rs +++ b/backend/src/web_server.rs @@ -33,6 +33,10 @@ pub async fn serve() -> Result<(), Box> { .route("/api/devices/{mac_address}", delete(devices::unregister)) .route("/api/devices/{mac_address}", get(devices::read)) .route("/api/notifications", get(notifications::list)) + .route( + "/api/notifications/mark_all_as_old", + post(notifications::mark_all_as_old), + ) .route("/api/notifications/{id}", get(notifications::read)) .route( "/api/notifications/{id}/read_without_flagging", diff --git a/backend/src/web_server/notifications.rs b/backend/src/web_server/notifications.rs index 73f77df..f021f17 100644 --- a/backend/src/web_server/notifications.rs +++ b/backend/src/web_server/notifications.rs @@ -45,6 +45,19 @@ pub async fn mark_as_new(Path(id): Path) -> impl IntoResponse { } } +pub async fn mark_all_as_old() -> impl IntoResponse { + match db::notifications::mark_all_as_old() { + Ok(_) => (StatusCode::OK, "All notifications marked as old"), + Err(err) => { + error!("Error marking all notifications as old: {}", err); + ( + StatusCode::INTERNAL_SERVER_ERROR, + "Error updating notifications in the server, check your logs", + ) + } + } +} + pub async fn list( Query(params): Query>, ) -> Result>, StatusCode> {