diff --git a/notification-server/src/bin/api.rs b/notification-server/src/bin/api.rs index 154ad07d..90c9e3cb 100644 --- a/notification-server/src/bin/api.rs +++ b/notification-server/src/bin/api.rs @@ -22,9 +22,9 @@ use notification_server::{ google_cloud::{GoogleCloud, NotificationError}, models::device::{DeviceSubscription, Preference}, }; +use serde::{Deserialize, Serialize}; use serde_json::json; use std::{collections::HashSet, path::PathBuf, str::FromStr, sync::Arc, time::Duration}; -use serde::{Deserialize, Serialize}; use tokio_postgres::Config; use tracing::{error, info}; @@ -207,21 +207,21 @@ async fn process_device_subscription( StatusCode::INTERNAL_SERVER_ERROR, "Authentication towards the external messaging service failed".to_string(), ) - }, + } NotificationError::ClientError(msg) => { error!("Client error: {}", msg); ( StatusCode::INTERNAL_SERVER_ERROR, "Client error received from external message service".to_string(), ) - }, + } NotificationError::ServerError(msg) => { error!("Server error: {}", msg); ( StatusCode::INTERNAL_SERVER_ERROR, "Server error received from external message service".to_string(), ) - }, + } }; return Err((status, message)); } @@ -246,8 +246,6 @@ async fn process_device_subscription( Ok("Subscribed accounts to device".to_string()) } - - #[derive(Debug, Serialize, Deserialize)] pub struct DeviceInput { pub device_token: String, @@ -258,21 +256,35 @@ async fn unsubscribe( Json(device_token): Json, ) -> impl IntoResponse { info!("Unsubscribing device tokens"); - match state.db_connection.remove_subscription(device_token.device_token.as_str()).await { - Ok(0) => (StatusCode::NOT_FOUND, Json(provide_error_message("Device token not found".to_string()))), - Ok(_) => (StatusCode::OK, Json(provide_message("Device token removed".to_string()))), + match state + .db_connection + .remove_subscription(device_token.device_token.as_str()) + .await + { + Ok(0) => ( + StatusCode::NOT_FOUND, + Json(provide_error_message("Device token not found".to_string())), + ), + Ok(_) => ( + StatusCode::OK, + Json(provide_message("Device token removed".to_string())), + ), Err(err) => { error!("Database error: {}", err); - (StatusCode::INTERNAL_SERVER_ERROR, Json(provide_error_message("Internal server error occurred while removing subscriptions from database".to_string()))) - }, + ( + StatusCode::INTERNAL_SERVER_ERROR, + Json(provide_error_message( + "Internal server error occurred while removing subscriptions from database" + .to_string(), + )), + ) + } } } -fn provide_message(message: String) -> serde_json::Value { - json!({ "message": message }) -} +fn provide_message(message: String) -> serde_json::Value { json!({ "message": message }) } fn provide_error_message(message: String) -> serde_json::Value { - json!({"errorMessage": message }) + json!({ "errorMessage": message }) } async fn upsert_account_device( @@ -285,12 +297,10 @@ async fn upsert_account_device( ); match process_device_subscription(subscription, state).await { Ok(message) => (StatusCode::OK, Json(provide_message(message))), - Err((status_code, message)) => (status_code, Json(provide_error_message(message))) + Err((status_code, message)) => (status_code, Json(provide_error_message(message))), } } - - #[tokio::main] async fn main() -> anyhow::Result<()> { dotenv().ok(); diff --git a/notification-server/src/database.rs b/notification-server/src/database.rs index 3fbb7648..97877eae 100644 --- a/notification-server/src/database.rs +++ b/notification-server/src/database.rs @@ -10,7 +10,7 @@ use deadpool_postgres::{GenericClient, Manager, ManagerConfig, Pool, PoolError, use lazy_static::lazy_static; use log::error; use thiserror::Error; -use tokio_postgres::{error::SqlState, NoTls, types::ToSql}; +use tokio_postgres::{error::SqlState, types::ToSql, NoTls}; use crate::models::device::{Device, Preference}; @@ -181,13 +181,13 @@ pub fn bitmask_to_preferences(bitmask: i32) -> HashSet { #[cfg(test)] mod tests { - use std::{collections::HashSet, env, fs, path::Path, str::FromStr}; + use super::*; + use crate::models::device::Preference::{CCDTransaction, CIS2Transaction}; use dotenv::dotenv; use enum_iterator::all; use serial_test::serial; + use std::{collections::HashSet, env, fs, path::Path, str::FromStr}; use tokio_postgres::Client; - use crate::models::device::Preference::{CCDTransaction, CIS2Transaction}; - use super::*; #[test] fn test_preference_map_coverage_and_uniqueness() {