diff --git a/src/inventory/application/services/add_category_service.rs b/src/inventory/application/services/add_category_service.rs index 4fe8c78..8e16448 100644 --- a/src/inventory/application/services/add_category_service.rs +++ b/src/inventory/application/services/add_category_service.rs @@ -10,7 +10,9 @@ use mockall::*; use super::errors::*; use crate::inventory::{ - application::port::output::db::{category_id_exists::*, category_name_exists_for_store::*}, + application::port::output::db::{ + category_id_exists::*, category_name_exists_for_store::*, store_id_exists::*, + }, domain::{ add_category_command::AddCategoryCommand, category_added_event::{CategoryAddedEvent, CategoryAddedEventBuilder}, @@ -29,7 +31,7 @@ pub type AddCategoryServiceObj = Arc; #[derive(Clone, Builder)] pub struct AddCategoryService { - // TODO: check if store ID exists + db_store_id_exists: StoreIDExistsDBPortObj, db_category_name_exists_for_store: CategoryNameExistsForStoreDBPortObj, db_category_id_exists: CategoryIDExistsDBPortObj, get_uuid: GetUUIDInterfaceObj, @@ -38,6 +40,14 @@ pub struct AddCategoryService { #[async_trait::async_trait] impl AddCategoryUseCase for AddCategoryService { async fn add_category(&self, cmd: AddCategoryCommand) -> InventoryResult { + if !self + .db_store_id_exists + .store_id_exists(cmd.store_id()) + .await? + { + return Err(InventoryError::StoreIDNotFound); + } + let mut category_id = self.get_uuid.get_uuid(); loop { @@ -128,6 +138,7 @@ pub mod tests { .unwrap(); let s = AddCategoryServiceBuilder::default() + .db_store_id_exists(mock_store_id_exists_db_port_true(IS_CALLED_ONLY_ONCE)) .db_category_name_exists_for_store(mock_category_name_exists_for_store_db_port_false( IS_CALLED_ONLY_ONCE, )) @@ -156,6 +167,7 @@ pub mod tests { .unwrap(); let s = AddCategoryServiceBuilder::default() + .db_store_id_exists(mock_store_id_exists_db_port_true(IS_CALLED_ONLY_ONCE)) .db_category_name_exists_for_store(mock_category_name_exists_for_store_db_port_true( IS_CALLED_ONLY_ONCE, )) @@ -169,4 +181,31 @@ pub mod tests { Err(InventoryError::DuplicateCategoryName) ) } + + #[actix_rt::test] + async fn test_service_store_doesnt_exist() { + let name = "foo"; + let description = "bar"; + let user_id = UUID; + let store_id = Uuid::new_v4(); + + // description = None + let cmd = AddCategoryCommand::new(name.into(), Some(description.into()), store_id, user_id) + .unwrap(); + + let s = AddCategoryServiceBuilder::default() + .db_store_id_exists(mock_store_id_exists_db_port_false(IS_CALLED_ONLY_ONCE)) + .db_category_name_exists_for_store(mock_category_name_exists_for_store_db_port_false( + IS_NEVER_CALLED, + )) + .db_category_id_exists(mock_category_id_exists_db_port_false(IS_NEVER_CALLED)) + .get_uuid(mock_get_uuid(IS_NEVER_CALLED)) + .build() + .unwrap(); + + assert_eq!( + s.add_category(cmd.clone()).await, + Err(InventoryError::StoreIDNotFound) + ) + } }