feat: check for Customization constraint violation in add_product_service
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/pr/woodpecker Pipeline was successful

This commit is contained in:
Aravinth Manivannan 2024-07-16 11:19:59 +05:30
parent 66464b33f1
commit 38485e572f
Signed by: realaravinth
GPG key ID: F8F50389936984FF

View file

@ -10,7 +10,12 @@ use mockall::*;
use super::errors::*;
use crate::inventory::{
application::port::output::db::{product_id_exists::*, product_name_exists_for_category::*},
application::port::output::db::{
customization_id_exists::{self, *},
customization_name_exists_for_product::*,
product_id_exists::*,
product_name_exists_for_category::*,
},
domain::{
add_product_command::AddProductCommand,
product_added_event::{ProductAddedEvent, ProductAddedEventBuilder},
@ -31,6 +36,8 @@ pub type AddProductServiceObj = Arc<dyn AddProductUseCase>;
pub struct AddProductService {
db_product_name_exists_for_category: ProductNameExistsForCategoryDBPortObj,
db_product_id_exists: ProductIDExistsDBPortObj,
db_customization_id_exists: CustomizationIDExistsDBPortObj,
db_customization_name_exists_for_product: CustomizationNameExistsForProductDBPortObj,
get_uuid: GetUUIDInterfaceObj,
}
@ -55,19 +62,35 @@ impl AddProductUseCase for AddProductService {
let mut customizations = Vec::with_capacity(cmd.customizations().len());
for c in cmd.customizations().iter() {
let mut customization_id = self.get_uuid.get_uuid();
loop {
if self
.db_customization_id_exists
.customization_id_exists(&customization_id)
.await?
{
customization_id = self.get_uuid.get_uuid();
continue;
} else {
break;
}
}
// TODO:
// 1. check if customization.name exists for product
// 2. check customization.customization_id duplicate in query table
let customization = CustomizationBuilder::default()
.name(c.name().into())
.deleted(false)
.customization_id(customization_id)
.build()
.unwrap();
customizations.push(
CustomizationBuilder::default()
.name(c.name().into())
.deleted(false)
.customization_id(customization_id)
.build()
.unwrap(),
);
if self
.db_customization_name_exists_for_product
.customization_name_exists_for_product(&customization)
.await?
{
return Err(InventoryError::DuplicateCustomizationName);
}
customizations.push(customization);
}
let product = ProductBuilder::default()
@ -168,7 +191,13 @@ pub mod tests {
.db_product_name_exists_for_category(
mock_product_name_exists_for_category_db_port_false(IS_CALLED_ONLY_ONCE),
)
.db_customization_id_exists(mock_customization_id_exists_db_port_false(
IS_CALLED_ONLY_THRICE,
))
.db_product_id_exists(mock_product_id_exists_db_port_false(IS_CALLED_ONLY_ONCE))
.db_customization_name_exists_for_product(
mock_customization_name_exists_for_product_db_port_false(IS_CALLED_ONLY_THRICE),
)
.get_uuid(mock_get_uuid(IS_CALLED_ONLY_FOUR_TIMES))
.build()
.unwrap();
@ -195,6 +224,12 @@ pub mod tests {
)
.get_uuid(mock_get_uuid(IS_CALLED_ONLY_FOUR_TIMES))
.db_product_id_exists(mock_product_id_exists_db_port_false(IS_CALLED_ONLY_ONCE))
.db_customization_id_exists(mock_customization_id_exists_db_port_false(
IS_CALLED_ONLY_THRICE,
))
.db_customization_name_exists_for_product(
mock_customization_name_exists_for_product_db_port_false(IS_CALLED_ONLY_THRICE),
)
.build()
.unwrap();