Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
lassemand committed Sep 27, 2024
1 parent aeb97c4 commit 8fe4f56
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
46 changes: 28 additions & 18 deletions notification-server/src/bin/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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));
}
Expand All @@ -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,
Expand All @@ -258,21 +256,35 @@ async fn unsubscribe(
Json(device_token): Json<DeviceInput>,
) -> 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(
Expand All @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions notification-server/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -181,13 +181,13 @@ pub fn bitmask_to_preferences(bitmask: i32) -> HashSet<Preference> {

#[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() {
Expand Down

0 comments on commit 8fe4f56

Please sign in to comment.