Add mark all as old API endpoint for notifications

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
rzuasti
2026-05-27 11:55:30 -04:00
co-authored by Claude Sonnet 4.6
parent a647e0d061
commit bda7b87cc7
3 changed files with 74 additions and 0 deletions
+57
View File
@@ -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<Notification> {
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;
+4
View File
@@ -33,6 +33,10 @@ pub async fn serve() -> Result<(), Box<dyn Error>> {
.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",
+13
View File
@@ -45,6 +45,19 @@ pub async fn mark_as_new(Path(id): Path<i64>) -> 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<HashMap<String, String>>,
) -> Result<Json<Vec<Notification>>, StatusCode> {