diff --git a/server/presentation/src/notification_handler.rs b/server/presentation/src/notification_handler.rs index 7e2404be..fc8d6b86 100644 --- a/server/presentation/src/notification_handler.rs +++ b/server/presentation/src/notification_handler.rs @@ -1,7 +1,5 @@ use axum::{extract::State, http::StatusCode, response::IntoResponse, Extension, Json}; -use domain::{ - notification::models::NotificationSource, repository::Repositories, user::models::User, -}; +use domain::{repository::Repositories, user::models::User}; use itertools::Itertools; use resource::repository::RealInfrastructureRepository; use serde_json::json; @@ -30,20 +28,9 @@ pub async fn fetch_by_request_user( notifications .into_iter() .map(|notification| { - notification.try_read(&user).map(|notification| { - let (source_type, source_id) = match notification.source() { - NotificationSource::Message(message_id) => { - ("MESSAGE".to_string(), message_id.to_string()) - } - }; - - NotificationResponse { - id: notification.id().to_owned(), - source_type, - source_id, - is_read: notification.is_read().to_owned(), - } - }) + notification + .try_read(&user) + .map(NotificationResponse::from_notification_ref) }) .collect::, _>>() .map_err(Into::into) @@ -78,20 +65,9 @@ pub async fn update_read_state( updated_notifications .into_iter() .map(|notification| { - notification.try_read(&user).map(|notification| { - let (source_type, source_id) = match notification.source() { - NotificationSource::Message(message_id) => { - ("MESSAGE".to_string(), message_id.to_string()) - } - }; - - NotificationResponse { - id: notification.id().to_owned(), - source_type, - source_id, - is_read: notification.is_read().to_owned(), - } - }) + notification + .try_read(&user) + .map(NotificationResponse::from_notification_ref) }) .collect::, _>>() .map_err(Into::into) diff --git a/server/presentation/src/schemas/notification/notification_response_schemas.rs b/server/presentation/src/schemas/notification/notification_response_schemas.rs index 9dac444f..fd5e81fc 100644 --- a/server/presentation/src/schemas/notification/notification_response_schemas.rs +++ b/server/presentation/src/schemas/notification/notification_response_schemas.rs @@ -1,4 +1,4 @@ -use domain::notification::models::NotificationId; +use domain::notification::models::{Notification, NotificationId, NotificationSource}; use serde::Serialize; #[derive(Serialize, Debug)] @@ -8,3 +8,20 @@ pub struct NotificationResponse { pub source_id: String, pub is_read: bool, } + +impl NotificationResponse { + pub fn from_notification_ref(notification: &Notification) -> Self { + let (source_type, source_id) = match notification.source() { + NotificationSource::Message(message_id) => { + ("MESSAGE".to_string(), message_id.to_string()) + } + }; + + Self { + id: notification.id().to_owned(), + source_type, + source_id, + is_read: notification.is_read().to_owned(), + } + } +}