feat: db port to check duplicate Kot ID

This commit is contained in:
Aravinth Manivannan 2024-07-23 20:33:52 +05:30
parent 6e0364c1ee
commit 13414d34fc
Signed by: realaravinth
GPG key ID: F8F50389936984FF
6 changed files with 149 additions and 1 deletions

View file

@ -22,8 +22,10 @@ impl From<SqlxError> for OrderingDBError {
if msg.contains("cqrs_ordering_store_query_line_item_id_key") {
return Self::DuplicateLineItemID;
} else if msg.contains("cqrs_ordering_store_query_order_id_key") {
} else if msg.contains("cqrs_ordering_order_order_id_query") {
return Self::DuplicateOrderID;
} else if msg.contains("cqrs_ordering_kot_kot_id_query") {
return Self::DuplicateKotID;
} else {
println!("{msg}");
}

View file

@ -0,0 +1,83 @@
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
use uuid::Uuid;
use super::OrderingDBPostgresAdapter;
use crate::ordering::application::port::output::db::{errors::*, kot_id_exists::*};
#[async_trait::async_trait]
impl KotIDExistsDBPort for OrderingDBPostgresAdapter {
async fn kot_id_exists(&self, kot_id: &Uuid) -> OrderingDBResult<bool> {
let res = sqlx::query!(
"SELECT EXISTS (
SELECT 1
FROM cqrs_ordering_kot_query
WHERE
kot_id = $1
);",
kot_id
)
.fetch_one(&self.pool)
.await?;
if let Some(x) = res.exists {
Ok(x)
} else {
Ok(false)
}
}
}
#[cfg(test)]
pub mod tests {
use super::*;
// use crate::ordering::domain::add_product_command::tests::get_customizations;
use crate::ordering::domain::kot_aggregate::*;
async fn create_dummy_kot(kot: &Kot, db: &OrderingDBPostgresAdapter) {
sqlx::query!(
"INSERT INTO cqrs_ordering_kot_query (
version,
order_id,
kot_id,
created_time,
deleted
) VALUES (
$1, $2, $3, $4, $5
);",
1,
kot.order_id(),
kot.kot_id(),
kot.created_time(),
kot.deleted().clone(),
)
.execute(&db.pool)
.await
.unwrap();
}
#[actix_rt::test]
async fn test_postgres_product_exists() {
let settings = crate::settings::tests::get_settings().await;
settings.create_db().await;
let db = super::OrderingDBPostgresAdapter::new(
sqlx::postgres::PgPool::connect(&settings.database.url)
.await
.unwrap(),
);
let kot = Kot::default();
// state doesn't exist
assert!(!db.kot_id_exists(kot.kot_id()).await.unwrap());
create_dummy_kot(&kot, &db).await;
// state exists
assert!(db.kot_id_exists(kot.kot_id()).await.unwrap());
settings.drop_db().await;
}
}

View file

@ -14,4 +14,6 @@ pub enum OrderingDBError {
InternalError,
DuplicateOrderID,
OrderIDNotFound,
DuplicateKotID,
KotIDNotFound,
}

View file

@ -0,0 +1,53 @@
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
use mockall::predicate::*;
use mockall::*;
use uuid::Uuid;
use super::errors::*;
#[cfg(test)]
#[allow(unused_imports)]
pub use tests::*;
#[automock]
#[async_trait::async_trait]
pub trait KotIDExistsDBPort: Send + Sync {
async fn kot_id_exists(&self, kot_id: &Uuid) -> OrderingDBResult<bool>;
}
pub type KotIDExistsDBPortObj = std::sync::Arc<dyn KotIDExistsDBPort>;
#[cfg(test)]
pub mod tests {
use super::*;
use std::sync::Arc;
pub fn mock_kot_id_exists_db_port_false(times: Option<usize>) -> KotIDExistsDBPortObj {
let mut m = MockKotIDExistsDBPort::new();
if let Some(times) = times {
m.expect_kot_id_exists()
.times(times)
.returning(|_| Ok(false));
} else {
m.expect_kot_id_exists().returning(|_| Ok(false));
}
Arc::new(m)
}
pub fn mock_kot_id_exists_db_port_true(times: Option<usize>) -> KotIDExistsDBPortObj {
let mut m = MockKotIDExistsDBPort::new();
if let Some(times) = times {
m.expect_kot_id_exists()
.times(times)
.returning(|_| Ok(true));
} else {
m.expect_kot_id_exists().returning(|_| Ok(true));
}
Arc::new(m)
}
}

View file

@ -3,5 +3,6 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
pub mod errors;
pub mod kot_id_exists;
pub mod line_item_id_exists;
pub mod order_id_exists;

View file

@ -15,6 +15,7 @@ pub enum OrderingError {
LineItemIDNotFound,
InternalError,
OrderIDNotFound,
KotIDNotFound,
}
//
impl From<OrderingDBError> for OrderingError {
@ -30,6 +31,12 @@ impl From<OrderingDBError> for OrderingError {
Self::InternalError
}
OrderingDBError::OrderIDNotFound => OrderingError::OrderIDNotFound,
OrderingDBError::DuplicateKotID => {
error!("DuplicateKotID");
Self::InternalError
}
OrderingDBError::KotIDNotFound => OrderingError::KotIDNotFound,
OrderingDBError::InternalError => Self::InternalError,
}
}