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