From 1565e567378708bfae0225c1eb5c7eedff236615 Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Sat, 18 May 2024 19:00:58 +0530 Subject: [PATCH] chore: rename *Exists error to Dupliate* error --- .../output/db/postgres/create_verification_secret.rs | 2 +- src/identity/adapters/output/db/postgres/errors.rs | 11 ++++++++++- src/identity/application/port/output/db/errors.rs | 3 ++- src/identity/application/port/output/db/mod.rs | 1 + src/identity/application/services/errors.rs | 6 +++--- .../services/mark_user_verified/service.rs | 4 ++-- .../application/services/register_user/service.rs | 9 +++++---- 7 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/identity/adapters/output/db/postgres/create_verification_secret.rs b/src/identity/adapters/output/db/postgres/create_verification_secret.rs index c825589..08287be 100644 --- a/src/identity/adapters/output/db/postgres/create_verification_secret.rs +++ b/src/identity/adapters/output/db/postgres/create_verification_secret.rs @@ -48,7 +48,7 @@ mod tests { // duplicate: secret exists assert_eq!( db.create_verification_secret(msg).await.err(), - Some(OutDBPortError::VerificationOTPSecretExists) + Some(OutDBPortError::DuplicateVerificationOTPSecret) ); settings.drop_db().await; diff --git a/src/identity/adapters/output/db/postgres/errors.rs b/src/identity/adapters/output/db/postgres/errors.rs index d9772e9..449a278 100644 --- a/src/identity/adapters/output/db/postgres/errors.rs +++ b/src/identity/adapters/output/db/postgres/errors.rs @@ -19,7 +19,7 @@ impl From for OutDBPortError { } else if msg.contains("user_query_email_key") { return Self::InternalError; } else if msg.contains("verification_otp_secret_key") { - return Self::VerificationOTPSecretExists; + return Self::DuplicateVerificationOTPSecret; } else { println!("{msg}"); } @@ -28,3 +28,12 @@ impl From for OutDBPortError { Self::InternalError } } + +/// map custom row not found error to DB error +pub fn map_row_not_found_err(e: SqlxError, row_not_found: OutDBPortError) -> OutDBPortError { + if let SqlxError::RowNotFound = e { + row_not_found + } else { + e.into() + } +} diff --git a/src/identity/application/port/output/db/errors.rs b/src/identity/application/port/output/db/errors.rs index 09a9874..fb34cd8 100644 --- a/src/identity/application/port/output/db/errors.rs +++ b/src/identity/application/port/output/db/errors.rs @@ -10,5 +10,6 @@ pub type OutDBPortResult = Result; #[derive(Debug, Display, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)] pub enum OutDBPortError { InternalError, - VerificationOTPSecretExists, + DuplicateVerificationOTPSecret, + VerificationOTPSecretNotFound, } diff --git a/src/identity/application/port/output/db/mod.rs b/src/identity/application/port/output/db/mod.rs index 67f9db9..e4a00f7 100644 --- a/src/identity/application/port/output/db/mod.rs +++ b/src/identity/application/port/output/db/mod.rs @@ -6,5 +6,6 @@ pub mod create_verification_secret; pub mod delete_verification_secret; pub mod email_exists; pub mod errors; +pub mod get_verification_secret; pub mod username_exists; pub mod verification_secret_exists; diff --git a/src/identity/application/services/errors.rs b/src/identity/application/services/errors.rs index 50bdbb3..5454b1c 100644 --- a/src/identity/application/services/errors.rs +++ b/src/identity/application/services/errors.rs @@ -10,9 +10,9 @@ pub type IdentityResult = Result; #[derive(Debug, Display, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)] pub enum IdentityError { - UsernameExists, - VerificationOTPSecretDoesntExist, - EmailExists, + DuplicateUsername, + VerificationOTPSecretNotFound, + DuplicateEmail, } pub type IdentityCommandResult = Result; diff --git a/src/identity/application/services/mark_user_verified/service.rs b/src/identity/application/services/mark_user_verified/service.rs index b744ab1..fc9bcec 100644 --- a/src/identity/application/services/mark_user_verified/service.rs +++ b/src/identity/application/services/mark_user_verified/service.rs @@ -31,7 +31,7 @@ impl MarkUserVerifiedUseCase for MarkUserVerifiedService { .await .unwrap() { - return Err(IdentityError::VerificationOTPSecretDoesntExist); + return Err(IdentityError::VerificationOTPSecretNotFound); } self.db_delete_verification_secret_adapter @@ -86,7 +86,7 @@ mod tests { assert_eq!( s.mark_user_verified(cmd.clone()).await.err(), - Some(IdentityError::VerificationOTPSecretDoesntExist) + Some(IdentityError::VerificationOTPSecretNotFound) ); } } diff --git a/src/identity/application/services/register_user/service.rs b/src/identity/application/services/register_user/service.rs index 6fd1f64..a858e9e 100644 --- a/src/identity/application/services/register_user/service.rs +++ b/src/identity/application/services/register_user/service.rs @@ -13,6 +13,7 @@ use crate::identity::application::port::output::{ use crate::utils::random_string::*; pub const SECRET_LEN: usize = 20; +pub const REGISTRATION_SECRET_PURPOSE: &str = "account_validation"; #[derive(Builder)] pub struct RegisterUserService { @@ -35,7 +36,7 @@ impl RegisterUserUseCase for RegisterUserService { .await .unwrap() { - return Err(IdentityError::UsernameExists); + return Err(IdentityError::DuplicateUsername); } if self @@ -44,7 +45,7 @@ impl RegisterUserUseCase for RegisterUserService { .await .unwrap() { - return Err(IdentityError::EmailExists); + return Err(IdentityError::DuplicateEmail); } let secret = self.random_string_adapter.get_random(SECRET_LEN); @@ -152,7 +153,7 @@ mod tests { assert_eq!( s.register_user(cmd.clone()).await.err(), - Some(IdentityError::UsernameExists) + Some(IdentityError::DuplicateUsername) ); } @@ -182,7 +183,7 @@ mod tests { assert_eq!( s.register_user(cmd.clone()).await.err(), - Some(IdentityError::EmailExists) + Some(IdentityError::DuplicateEmail) ); } }