Merge pull request 'feat: update Category aggregate cmd, event and service' (#52) from update-category into master
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

Reviewed-on: #52
This commit is contained in:
Aravinth Manivannan 2024-07-16 22:40:42 +05:30
commit f8a4cc5a82
10 changed files with 422 additions and 15 deletions

View file

@ -215,6 +215,7 @@ mod tests {
add_customization_service::tests::mock_add_customization_service,
add_product_service::tests::mock_add_product_service,
add_store_service::AddStoreServiceBuilder,
update_category_service::tests::mock_update_category_service,
update_customization_service::tests::mock_update_customization_service,
update_product_service::tests::mock_update_product_service,
InventoryServicesBuilder,
@ -223,6 +224,7 @@ mod tests {
add_category_command::AddCategoryCommand, add_customization_command,
add_product_command::tests::get_command, add_store_command::AddStoreCommand,
commands::InventoryCommand,
update_category_command::tests::get_update_category_command,
update_customization_command::tests::get_update_customization_command,
update_product_command,
},
@ -273,6 +275,10 @@ mod tests {
IS_NEVER_CALLED,
get_update_customization_command(),
))
.update_category(mock_update_category_service(
IS_NEVER_CALLED,
get_update_category_command(),
))
.build()
.unwrap();

View file

@ -13,6 +13,7 @@ pub mod add_category_service;
pub mod add_customization_service;
pub mod add_product_service;
pub mod add_store_service;
pub mod update_category_service;
pub mod update_customization_service;
pub mod update_product_service;
@ -24,6 +25,7 @@ pub trait InventoryServicesInterface: Send + Sync {
fn add_customization(&self) -> add_customization_service::AddCustomizationServiceObj;
fn update_product(&self) -> update_product_service::UpdateProductServiceObj;
fn update_customization(&self) -> update_customization_service::UpdateCustomizationServiceObj;
fn update_category(&self) -> update_category_service::UpdateCategoryServiceObj;
}
#[derive(Clone, Builder)]
@ -34,6 +36,7 @@ pub struct InventoryServices {
add_customization: add_customization_service::AddCustomizationServiceObj,
update_product: update_product_service::UpdateProductServiceObj,
update_customization: update_customization_service::UpdateCustomizationServiceObj,
update_category: update_category_service::UpdateCategoryServiceObj,
}
impl InventoryServicesInterface for InventoryServices {
@ -58,4 +61,8 @@ impl InventoryServicesInterface for InventoryServices {
fn update_customization(&self) -> update_customization_service::UpdateCustomizationServiceObj {
self.update_customization.clone()
}
fn update_category(&self) -> update_category_service::UpdateCategoryServiceObj {
self.update_category.clone()
}
}

View file

@ -1,3 +1,197 @@
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
/// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
use std::sync::Arc;
use derive_builder::Builder;
use mockall::predicate::*;
use mockall::*;
use super::errors::*;
use crate::inventory::{
application::port::output::db::{
category_id_exists::*, category_name_exists_for_store::*, store_id_exists::*,
},
domain::{category_aggregate::*, category_updated_event::*, update_category_command::*},
};
use crate::utils::uuid::*;
#[automock]
#[async_trait::async_trait]
pub trait UpdateCategoryUseCase: Send + Sync {
async fn update_category(
&self,
cmd: UpdateCategoryCommand,
) -> InventoryResult<CategoryUpdatedEvent>;
}
pub type UpdateCategoryServiceObj = Arc<dyn UpdateCategoryUseCase>;
#[derive(Clone, Builder)]
pub struct UpdateCategoryService {
// TODO: check if store ID exists
db_category_name_exists_for_store: CategoryNameExistsForStoreDBPortObj,
db_category_id_exists: CategoryIDExistsDBPortObj,
db_store_id_exists: StoreIDExistsDBPortObj,
}
#[async_trait::async_trait]
impl UpdateCategoryUseCase for UpdateCategoryService {
async fn update_category(
&self,
cmd: UpdateCategoryCommand,
) -> InventoryResult<CategoryUpdatedEvent> {
if !self
.db_category_id_exists
.category_id_exists(cmd.old_category().category_id())
.await?
{
return Err(InventoryError::CategoryIDNotFound);
}
if !self
.db_store_id_exists
.store_id_exists(cmd.old_category().store_id())
.await?
{
return Err(InventoryError::StoreIDNotFound);
}
let updated_category = CategoryBuilder::default()
.name(cmd.name().into())
.description(cmd.description().as_ref().map(|s| s.to_string()))
.category_id(*cmd.old_category().category_id())
.store_id(*cmd.old_category().store_id())
.build()
.unwrap();
if updated_category.name() != cmd.old_category().name() {
if self
.db_category_name_exists_for_store
.category_name_exists_for_store(&updated_category)
.await?
{
return Err(InventoryError::DuplicateCategoryName);
}
}
Ok(CategoryUpdatedEventBuilder::default()
.added_by_user(*cmd.adding_by())
.old_category(cmd.old_category().clone())
.new_category(updated_category)
.build()
.unwrap())
}
}
#[cfg(test)]
pub mod tests {
use super::*;
use crate::inventory::domain::category_updated_event;
use crate::inventory::domain::update_category_command::tests::get_update_category_command;
use crate::utils::uuid::tests::UUID;
use crate::{tests::bdd::*, utils::uuid::tests::mock_get_uuid};
pub fn mock_update_category_service(
times: Option<usize>,
cmd: UpdateCategoryCommand,
) -> UpdateCategoryServiceObj {
let mut m = MockUpdateCategoryUseCase::new();
let res = category_updated_event::tests::get_category_updated_event_from_command(&cmd);
if let Some(times) = times {
m.expect_update_category()
.times(times)
.returning(move |_| Ok(res.clone()));
} else {
m.expect_update_category()
.returning(move |_| Ok(res.clone()));
}
Arc::new(m)
}
#[actix_rt::test]
async fn test_service() {
let cmd = get_update_category_command();
let s = UpdateCategoryServiceBuilder::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,
))
.db_category_id_exists(mock_category_id_exists_db_port_true(IS_CALLED_ONLY_ONCE))
.build()
.unwrap();
let res = s.update_category(cmd.clone()).await.unwrap();
assert_eq!(res.new_category().name(), cmd.name());
assert_eq!(res.new_category().description(), cmd.description());
assert_eq!(res.new_category().store_id(), cmd.old_category().store_id());
assert_eq!(
res.new_category().category_id(),
cmd.old_category().category_id()
);
assert_eq!(res.old_category(), cmd.old_category());
assert_eq!(res.added_by_user(), cmd.adding_by());
}
#[actix_rt::test]
async fn test_service_store_doesnt_exist() {
let cmd = get_update_category_command();
let s = UpdateCategoryServiceBuilder::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_true(IS_CALLED_ONLY_ONCE))
.build()
.unwrap();
assert_eq!(
s.update_category(cmd.clone()).await,
Err(InventoryError::StoreIDNotFound)
);
}
#[actix_rt::test]
async fn test_category_id_not_found() {
let cmd = get_update_category_command();
let s = UpdateCategoryServiceBuilder::default()
.db_store_id_exists(mock_store_id_exists_db_port_true(IS_NEVER_CALLED))
.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_CALLED_ONLY_ONCE))
.build()
.unwrap();
assert_eq!(
s.update_category(cmd.clone()).await,
Err(InventoryError::CategoryIDNotFound)
);
}
#[actix_rt::test]
async fn test_duplicate_new_name() {
let cmd = get_update_category_command();
let s = UpdateCategoryServiceBuilder::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,
))
.db_category_id_exists(mock_category_id_exists_db_port_true(IS_CALLED_ONLY_ONCE))
.build()
.unwrap();
assert_eq!(
s.update_category(cmd.clone()).await,
Err(InventoryError::DuplicateCategoryName)
);
}
}

View file

@ -50,19 +50,28 @@ impl Aggregate for Category {
let res = services.add_category().add_category(cmd).await?;
Ok(vec![InventoryEvent::CategoryAdded(res)])
}
InventoryCommand::UpdateCategory(cmd) => {
let res = services.update_category().update_category(cmd).await?;
Ok(vec![InventoryEvent::CategoryUpdated(res)])
}
_ => Ok(Vec::default()),
}
}
fn apply(&mut self, event: Self::Event) {
if let InventoryEvent::CategoryAdded(e) = event {
*self = CategoryBuilder::default()
.name(e.name().into())
.category_id(*e.category_id())
.description(e.description().clone())
.store_id(*e.store_id())
.build()
.unwrap();
match event {
InventoryEvent::CategoryAdded(e) => {
*self = CategoryBuilder::default()
.name(e.name().into())
.category_id(*e.category_id())
.description(e.description().clone())
.store_id(*e.store_id())
.build()
.unwrap()
}
InventoryEvent::CategoryUpdated(e) => *self = e.new_category().clone(),
_ => (),
}
}
}
@ -72,14 +81,17 @@ mod aggregate_tests {
use std::sync::Arc;
use cqrs_es::test::TestFramework;
use update_category_service::tests::mock_update_category_service;
use uuid::Uuid;
use super::*;
use crate::inventory::{
application::services::{add_category_service::tests::*, *},
domain::{
add_category_command::*, category_added_event::*, commands::InventoryCommand,
events::InventoryEvent,
add_category_command::*, category_added_event::*,
category_updated_event::tests::get_category_updated_event_from_command,
commands::InventoryCommand, events::InventoryEvent,
update_category_command::tests::get_update_category_command,
},
};
use crate::tests::bdd::*;
@ -119,4 +131,26 @@ mod aggregate_tests {
.when(InventoryCommand::AddCategory(cmd))
.then_expect_events(vec![expected]);
}
#[test]
fn test_update_category() {
let cmd = get_update_category_command();
let expected = get_category_updated_event_from_command(&cmd);
let expected = InventoryEvent::CategoryUpdated(expected);
let mut services = MockInventoryServicesInterface::new();
services
.expect_update_category()
.times(IS_CALLED_ONLY_ONCE.unwrap())
.return_const(mock_update_category_service(
IS_CALLED_ONLY_ONCE,
cmd.clone(),
));
CategoryTestFramework::with(Arc::new(services))
.given_no_previous_events()
.when(InventoryCommand::UpdateCategory(cmd))
.then_expect_events(vec![expected]);
}
}

View file

@ -0,0 +1,45 @@
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
use derive_builder::Builder;
use derive_getters::Getters;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use super::category_aggregate::*;
#[derive(
Clone, Debug, Builder, Serialize, Deserialize, Getters, Eq, PartialEq, Ord, PartialOrd,
)]
pub struct CategoryUpdatedEvent {
added_by_user: Uuid,
new_category: Category,
old_category: Category,
}
#[cfg(test)]
pub mod tests {
use crate::inventory::domain::update_category_command::UpdateCategoryCommand;
use super::*;
pub fn get_category_updated_event_from_command(
cmd: &UpdateCategoryCommand,
) -> CategoryUpdatedEvent {
let category = CategoryBuilder::default()
.name(cmd.name().into())
.description(cmd.description().as_ref().map(|s| s.to_string()))
.category_id(*cmd.old_category().category_id())
.store_id(*cmd.old_category().store_id())
.build()
.unwrap();
CategoryUpdatedEventBuilder::default()
.new_category(category)
.old_category(cmd.old_category().clone())
.added_by_user(*cmd.adding_by())
.build()
.unwrap()
}
}

View file

@ -8,6 +8,7 @@ use serde::{Deserialize, Serialize};
use super::{
add_category_command::AddCategoryCommand, add_customization_command::AddCustomizationCommand,
add_product_command::AddProductCommand, add_store_command::AddStoreCommand,
update_category_command::UpdateCategoryCommand,
update_customization_command::UpdateCustomizationCommand,
update_product_command::UpdateProductCommand,
};
@ -20,4 +21,5 @@ pub enum InventoryCommand {
AddCustomization(AddCustomizationCommand),
UpdateProduct(UpdateProductCommand),
UpdateCustomization(UpdateCustomizationCommand),
UpdateCategory(UpdateCategoryCommand),
}

View file

@ -28,9 +28,6 @@ pub mod tests {
use super::*;
#[test]
fn test_name() {}
pub fn get_customization_updated_event_from_command(
cmd: &UpdateCustomizationCommand,
) -> CustomizationUpdatedEvent {

View file

@ -6,7 +6,8 @@ use cqrs_es::DomainEvent;
use serde::{Deserialize, Serialize};
use super::{
category_added_event::*, customization_added_event::CustomizationAddedEvent,
category_added_event::*, category_updated_event::CategoryUpdatedEvent,
customization_added_event::CustomizationAddedEvent,
customization_updated_event::CustomizationUpdatedEvent, product_added_event::ProductAddedEvent,
product_updated_event::ProductUpdatedEvent, store_added_event::StoreAddedEvent,
};
@ -19,6 +20,7 @@ pub enum InventoryEvent {
CustomizationAdded(CustomizationAddedEvent),
ProductUpdated(ProductUpdatedEvent),
CustomizationUpdated(CustomizationUpdatedEvent),
CategoryUpdated(CategoryUpdatedEvent),
}
impl DomainEvent for InventoryEvent {
@ -34,6 +36,7 @@ impl DomainEvent for InventoryEvent {
InventoryEvent::CustomizationAdded { .. } => "InventoryCustomizationAdded",
InventoryEvent::ProductUpdated { .. } => "InventoryProductUpdated",
InventoryEvent::CustomizationUpdated { .. } => "InventoryCustomizationUpdated",
InventoryEvent::CategoryUpdated { .. } => "InventoryCategoryUpdated",
};
e.to_string()

View file

@ -15,11 +15,13 @@ pub mod add_customization_command;
pub mod add_product_command;
pub mod add_store_command;
pub mod commands;
pub mod update_category_command;
pub mod update_customization_command;
pub mod update_product_command;
// events
pub mod category_added_event;
pub mod category_updated_event;
pub mod customization_added_event;
pub mod customization_updated_event;
pub mod events;

View file

@ -0,0 +1,117 @@
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
use derive_getters::Getters;
use derive_more::{Display, Error};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use super::category_aggregate::*;
#[derive(Debug, Error, Display, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub enum UpdateCategoryCommandError {
NameIsEmpty,
}
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Getters)]
pub struct UpdateCategoryCommand {
name: String,
description: Option<String>,
adding_by: Uuid,
old_category: Category,
}
impl UpdateCategoryCommand {
pub fn new(
name: String,
description: Option<String>,
old_category: Category,
adding_by: Uuid,
) -> Result<Self, UpdateCategoryCommandError> {
let description: Option<String> = if let Some(description) = description {
let description = description.trim();
if description.is_empty() {
None
} else {
Some(description.to_owned())
}
} else {
None
};
let name = name.trim().to_owned();
if name.is_empty() {
return Err(UpdateCategoryCommandError::NameIsEmpty);
}
Ok(Self {
name,
description,
old_category,
adding_by,
})
}
}
#[cfg(test)]
pub mod tests {
use super::*;
use crate::utils::uuid::tests::UUID;
pub fn get_update_category_command() -> UpdateCategoryCommand {
let name = "foo";
let description = "bar";
let adding_by = UUID;
let old_category = Category::default();
UpdateCategoryCommand::new(
name.into(),
Some(description.into()),
old_category.clone(),
adding_by,
)
.unwrap()
}
#[test]
fn test_cmd() {
let name = "foo";
let description = "bar";
let adding_by = UUID;
let old_category = Category::default();
// description = None
let cmd =
UpdateCategoryCommand::new(name.into(), None, old_category.clone(), adding_by).unwrap();
assert_eq!(cmd.name(), name);
assert_eq!(cmd.description(), &None);
assert_eq!(cmd.adding_by(), &adding_by);
assert_eq!(cmd.old_category(), &old_category);
// description = Some
let cmd = UpdateCategoryCommand::new(
name.into(),
Some(description.into()),
old_category.clone(),
adding_by,
)
.unwrap();
assert_eq!(cmd.name(), name);
assert_eq!(cmd.description(), &Some(description.to_owned()));
assert_eq!(cmd.adding_by(), &adding_by);
assert_eq!(cmd.old_category(), &old_category);
// UpdateCategoryCommandError::NameIsEmpty
assert_eq!(
UpdateCategoryCommand::new(
"".into(),
Some(description.into()),
old_category,
adding_by,
),
Err(UpdateCategoryCommandError::NameIsEmpty)
)
}
}