From 38485e572f34e4944dfdc9123b8b72f871238ec4 Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Tue, 16 Jul 2024 11:19:59 +0530 Subject: [PATCH] feat: check for Customization constraint violation in add_product_service --- .../services/add_product_service.rs | 59 +++++++++++++++---- 1 file changed, 47 insertions(+), 12 deletions(-) diff --git a/src/inventory/application/services/add_product_service.rs b/src/inventory/application/services/add_product_service.rs index e624725..6567872 100644 --- a/src/inventory/application/services/add_product_service.rs +++ b/src/inventory/application/services/add_product_service.rs @@ -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; 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();