feat: inventory: customization ID is provided by caller

This commit is contained in:
Aravinth Manivannan 2024-09-20 18:09:08 +05:30
parent 2853bc5a75
commit ac6da029a5
Signed by: realaravinth
GPG key ID: F8F50389936984FF
4 changed files with 39 additions and 60 deletions

View file

@ -24,7 +24,6 @@ use crate::inventory::{
product_aggregate::*, product_aggregate::*,
}, },
}; };
use crate::utils::uuid::*;
#[automock] #[automock]
#[async_trait::async_trait] #[async_trait::async_trait]
@ -42,7 +41,6 @@ pub struct AddCustomizationService {
db_product_id_exists: ProductIDExistsDBPortObj, db_product_id_exists: ProductIDExistsDBPortObj,
db_customization_id_exists: CustomizationIDExistsDBPortObj, db_customization_id_exists: CustomizationIDExistsDBPortObj,
db_customization_name_exists_for_product: CustomizationNameExistsForProductDBPortObj, db_customization_name_exists_for_product: CustomizationNameExistsForProductDBPortObj,
get_uuid: GetUUIDInterfaceObj,
} }
#[async_trait::async_trait] #[async_trait::async_trait]
@ -59,25 +57,19 @@ impl AddCustomizationUseCase for AddCustomizationService {
return Err(InventoryError::ProductIDNotFound); return Err(InventoryError::ProductIDNotFound);
} }
let mut customization_id = self.get_uuid.get_uuid();
loop {
if self if self
.db_customization_id_exists .db_customization_id_exists
.customization_id_exists(&customization_id) .customization_id_exists(cmd.customization_id())
.await? .await?
{ {
customization_id = self.get_uuid.get_uuid(); return Err(InventoryError::DuplicateCustomizationID);
continue;
} else {
break;
}
} }
let customization = CustomizationBuilder::default() let customization = CustomizationBuilder::default()
.name(cmd.name().into()) .name(cmd.name().into())
.deleted(false) .deleted(false)
.product_id(*cmd.product_id()) .product_id(*cmd.product_id())
.customization_id(customization_id) .customization_id(*cmd.customization_id())
.build() .build()
.unwrap(); .unwrap();
@ -101,11 +93,9 @@ pub mod tests {
use super::*; use super::*;
use customization_added_event::tests::get_customization_added_event_from_cmd; use customization_added_event::tests::get_customization_added_event_from_cmd;
use uuid::Uuid;
use crate::inventory::domain::add_customization_command::tests::get_command; use crate::inventory::domain::add_customization_command::tests::get_command;
use crate::utils::uuid::tests::UUID; use crate::tests::bdd::*;
use crate::{tests::bdd::*, utils::uuid::tests::mock_get_uuid};
pub fn mock_add_customization_service( pub fn mock_add_customization_service(
times: Option<usize>, times: Option<usize>,
@ -138,12 +128,16 @@ pub mod tests {
.db_customization_name_exists_for_product( .db_customization_name_exists_for_product(
mock_customization_name_exists_for_product_db_port_false(IS_CALLED_ONLY_ONCE), mock_customization_name_exists_for_product_db_port_false(IS_CALLED_ONLY_ONCE),
) )
.get_uuid(mock_get_uuid(IS_CALLED_ONLY_ONCE))
.build() .build()
.unwrap(); .unwrap();
let res = s.add_customization(cmd.clone()).await.unwrap(); let res = s.add_customization(cmd.clone()).await.unwrap();
assert_eq!(res.customization().name(), cmd.name()); assert_eq!(res.customization().name(), cmd.name());
assert_eq!(res.customization().product_id(), cmd.product_id());
assert_eq!(
res.customization().customization_id(),
cmd.customization_id()
);
// assert_eq!(customization_added_events.len(), cmd.customizations().len()); // assert_eq!(customization_added_events.len(), cmd.customizations().len());
} }
@ -159,7 +153,6 @@ pub mod tests {
.db_customization_name_exists_for_product( .db_customization_name_exists_for_product(
mock_customization_name_exists_for_product_db_port_true(IS_CALLED_ONLY_ONCE), mock_customization_name_exists_for_product_db_port_true(IS_CALLED_ONLY_ONCE),
) )
.get_uuid(mock_get_uuid(IS_CALLED_ONLY_ONCE))
.build() .build()
.unwrap(); .unwrap();
@ -179,7 +172,6 @@ pub mod tests {
.db_customization_name_exists_for_product( .db_customization_name_exists_for_product(
mock_customization_name_exists_for_product_db_port_false(IS_NEVER_CALLED), mock_customization_name_exists_for_product_db_port_false(IS_NEVER_CALLED),
) )
.get_uuid(mock_get_uuid(IS_NEVER_CALLED))
.build() .build()
.unwrap(); .unwrap();

View file

@ -18,6 +18,7 @@ pub enum InventoryError {
DuplicateStoreName, DuplicateStoreName,
DuplicateProductName, DuplicateProductName,
DuplicateCustomizationName, DuplicateCustomizationName,
DuplicateCustomizationID,
DuplicateStoreID, DuplicateStoreID,
ProductIDNotFound, ProductIDNotFound,
CategoryIDNotFound, CategoryIDNotFound,
@ -42,10 +43,7 @@ impl From<InventoryDBError> for InventoryError {
error!("DuplicateCategoryID"); error!("DuplicateCategoryID");
Self::InternalError Self::InternalError
} }
InventoryDBError::DuplicateCustomizationID => { InventoryDBError::DuplicateCustomizationID => Self::DuplicateCustomizationID,
error!("DuplicateCustomizationID");
Self::InternalError
}
InventoryDBError::InternalError => Self::InternalError, InventoryDBError::InternalError => Self::InternalError,
InventoryDBError::ProductIDNotFound => InventoryError::ProductIDNotFound, InventoryDBError::ProductIDNotFound => InventoryError::ProductIDNotFound,
InventoryDBError::CategoryIDNotFound => InventoryError::CategoryIDNotFound, InventoryDBError::CategoryIDNotFound => InventoryError::CategoryIDNotFound,

View file

@ -16,28 +16,26 @@ pub enum AddCustomizationCommandError {
#[derive( #[derive(
Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Getters, Builder, Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Getters, Builder,
)] )]
pub struct UnvalidatedAddCustomizationCommand { #[builder(build_fn(validate = "Self::validate"))]
name: String,
product_id: Uuid,
}
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Getters)]
pub struct AddCustomizationCommand { pub struct AddCustomizationCommand {
#[builder(setter(custom))]
name: String, name: String,
product_id: Uuid, product_id: Uuid,
customization_id: Uuid,
} }
impl UnvalidatedAddCustomizationCommand { impl AddCustomizationCommandBuilder {
pub fn validate(self) -> Result<AddCustomizationCommand, AddCustomizationCommandError> { pub fn name(&mut self, name: String) -> &mut Self {
let name = self.name.trim().to_owned(); self.name = Some(name.trim().to_owned());
if name.is_empty() { self
return Err(AddCustomizationCommandError::NameIsEmpty);
} }
Ok(AddCustomizationCommand { fn validate(&self) -> Result<(), String> {
name, let name = self.name.as_ref().unwrap().trim().to_owned();
product_id: self.product_id, if name.is_empty() {
}) return Err(AddCustomizationCommandError::NameIsEmpty.to_string());
}
Ok(())
} }
} }
@ -48,13 +46,12 @@ pub mod tests {
use crate::utils::uuid::tests::UUID; use crate::utils::uuid::tests::UUID;
pub fn get_command() -> AddCustomizationCommand { pub fn get_command() -> AddCustomizationCommand {
UnvalidatedAddCustomizationCommandBuilder::default() AddCustomizationCommandBuilder::default()
.name("foo".into()) .name("foo".into())
.product_id(UUID.clone()) .product_id(UUID.clone())
.customization_id(UUID.clone())
.build() .build()
.unwrap() .unwrap()
.validate()
.unwrap()
} }
#[test] #[test]
@ -62,12 +59,11 @@ pub mod tests {
let name = "foo"; let name = "foo";
let product_id = UUID; let product_id = UUID;
let cmd = UnvalidatedAddCustomizationCommandBuilder::default() let cmd = AddCustomizationCommandBuilder::default()
.name(name.into()) .name(name.into())
.product_id(product_id.clone()) .product_id(product_id.clone())
.customization_id(UUID.clone())
.build() .build()
.unwrap()
.validate()
.unwrap(); .unwrap();
assert_eq!(cmd.name(), name); assert_eq!(cmd.name(), name);
@ -78,14 +74,11 @@ pub mod tests {
fn test_cmd_name_is_empty() { fn test_cmd_name_is_empty() {
let product_id = UUID; let product_id = UUID;
assert_eq!( assert!(AddCustomizationCommandBuilder::default()
UnvalidatedAddCustomizationCommandBuilder::default()
.name("".into()) .name("".into())
.product_id(product_id.clone()) .product_id(product_id.clone())
.customization_id(UUID.clone())
.build() .build()
.unwrap() .is_err(),);
.validate(),
Err(AddCustomizationCommandError::NameIsEmpty)
);
} }
} }

View file

@ -27,8 +27,6 @@ pub struct AddStoreCommand {
} }
impl AddStoreCommandBuilder { impl AddStoreCommandBuilder {
// pub fn custom_address(&mut self, address: Option<String>) -> &mut Self {
// fn address(&mut self, address: Option<String>) {
pub fn address(&mut self, address: Option<String>) -> &mut Self { pub fn address(&mut self, address: Option<String>) -> &mut Self {
self.address = if let Some(address) = address { self.address = if let Some(address) = address {
let address = address.trim(); let address = address.trim();
@ -43,8 +41,6 @@ impl AddStoreCommandBuilder {
self self
} }
//pub fn custom_name(&mut self, name: String) -> &mut Self {
//fn name(&mut self, name: String) {
pub fn name(&mut self, name: String) -> &mut Self { pub fn name(&mut self, name: String) -> &mut Self {
self.name = Some(name.trim().to_owned()); self.name = Some(name.trim().to_owned());
self self