feat: update Customization aggregate cmd, event and service #51

Merged
realaravinth merged 2 commits from update-customization into master 2024-07-16 21:47:18 +05:30
11 changed files with 425 additions and 6 deletions
Showing only changes of commit 987b48a3df - Show all commits

View file

@ -60,7 +60,6 @@ impl View<Customization> for CustomizationView {
} }
} }
#[async_trait] #[async_trait]
impl ViewRepository<CustomizationView, Customization> for InventoryDBPostgresAdapter { impl ViewRepository<CustomizationView, Customization> for InventoryDBPostgresAdapter {
async fn load( async fn load(

View file

@ -215,13 +215,16 @@ mod tests {
add_customization_service::tests::mock_add_customization_service, add_customization_service::tests::mock_add_customization_service,
add_product_service::tests::mock_add_product_service, add_product_service::tests::mock_add_product_service,
add_store_service::AddStoreServiceBuilder, add_store_service::AddStoreServiceBuilder,
update_customization_service::tests::mock_update_customization_service,
update_product_service::tests::mock_update_product_service, update_product_service::tests::mock_update_product_service,
InventoryServicesBuilder, InventoryServicesBuilder,
}, },
domain::{ domain::{
add_category_command::AddCategoryCommand, add_customization_command, add_category_command::AddCategoryCommand, add_customization_command,
add_product_command::tests::get_command, add_store_command::AddStoreCommand, add_product_command::tests::get_command, add_store_command::AddStoreCommand,
commands::InventoryCommand, update_product_command, commands::InventoryCommand,
update_customization_command::tests::get_update_customization_command,
update_product_command,
}, },
}, },
tests::bdd::IS_NEVER_CALLED, tests::bdd::IS_NEVER_CALLED,
@ -266,6 +269,10 @@ mod tests {
IS_NEVER_CALLED, IS_NEVER_CALLED,
update_product_command::tests::get_command(), update_product_command::tests::get_command(),
)) ))
.update_customization(mock_update_customization_service(
IS_NEVER_CALLED,
get_update_customization_command(),
))
.build() .build()
.unwrap(); .unwrap();

View file

@ -18,6 +18,7 @@ pub enum InventoryError {
DuplicateCustomizationName, DuplicateCustomizationName,
ProductIDNotFound, ProductIDNotFound,
CategoryIDNotFound, CategoryIDNotFound,
CustomizationIDNotFound,
StoreIDNotFound, StoreIDNotFound,
InternalError, InternalError,
} }

View file

@ -13,6 +13,7 @@ pub mod add_category_service;
pub mod add_customization_service; pub mod add_customization_service;
pub mod add_product_service; pub mod add_product_service;
pub mod add_store_service; pub mod add_store_service;
pub mod update_customization_service;
pub mod update_product_service; pub mod update_product_service;
#[automock] #[automock]
@ -22,6 +23,7 @@ pub trait InventoryServicesInterface: Send + Sync {
fn add_product(&self) -> add_product_service::AddProductServiceObj; fn add_product(&self) -> add_product_service::AddProductServiceObj;
fn add_customization(&self) -> add_customization_service::AddCustomizationServiceObj; fn add_customization(&self) -> add_customization_service::AddCustomizationServiceObj;
fn update_product(&self) -> update_product_service::UpdateProductServiceObj; fn update_product(&self) -> update_product_service::UpdateProductServiceObj;
fn update_customization(&self) -> update_customization_service::UpdateCustomizationServiceObj;
} }
#[derive(Clone, Builder)] #[derive(Clone, Builder)]
@ -31,6 +33,7 @@ pub struct InventoryServices {
add_product: add_product_service::AddProductServiceObj, add_product: add_product_service::AddProductServiceObj,
add_customization: add_customization_service::AddCustomizationServiceObj, add_customization: add_customization_service::AddCustomizationServiceObj,
update_product: update_product_service::UpdateProductServiceObj, update_product: update_product_service::UpdateProductServiceObj,
update_customization: update_customization_service::UpdateCustomizationServiceObj,
} }
impl InventoryServicesInterface for InventoryServices { impl InventoryServicesInterface for InventoryServices {
@ -47,7 +50,12 @@ impl InventoryServicesInterface for InventoryServices {
fn add_customization(&self) -> add_customization_service::AddCustomizationServiceObj { fn add_customization(&self) -> add_customization_service::AddCustomizationServiceObj {
self.add_customization.clone() self.add_customization.clone()
} }
fn update_product(&self) -> update_product_service::UpdateProductServiceObj { fn update_product(&self) -> update_product_service::UpdateProductServiceObj {
self.update_product().clone() self.update_product.clone()
}
fn update_customization(&self) -> update_customization_service::UpdateCustomizationServiceObj {
self.update_customization.clone()
} }
} }

View file

@ -0,0 +1,209 @@
/// 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::{
customization_id_exists::*, customization_name_exists_for_product::*, product_id_exists::*,
},
domain::{
customization_aggregate::*, customization_updated_event::*, update_customization_command::*,
},
};
use crate::utils::uuid::*;
#[automock]
#[async_trait::async_trait]
pub trait UpdateCustomizationUseCase: Send + Sync {
async fn update_customization(
&self,
cmd: UpdateCustomizationCommand,
) -> InventoryResult<CustomizationUpdatedEvent>;
}
pub type UpdateCustomizationServiceObj = Arc<dyn UpdateCustomizationUseCase>;
#[derive(Clone, Builder)]
pub struct UpdateCustomizationService {
// TODO: check if product ID exists
db_customization_name_exists_for_product: CustomizationNameExistsForProductDBPortObj,
db_customization_id_exists: CustomizationIDExistsDBPortObj,
db_product_id_exists: ProductIDExistsDBPortObj,
}
#[async_trait::async_trait]
impl UpdateCustomizationUseCase for UpdateCustomizationService {
async fn update_customization(
&self,
cmd: UpdateCustomizationCommand,
) -> InventoryResult<CustomizationUpdatedEvent> {
if !self
.db_customization_id_exists
.customization_id_exists(cmd.old_customization().customization_id())
.await?
{
return Err(InventoryError::CustomizationIDNotFound);
}
if !self
.db_product_id_exists
.product_id_exists(cmd.old_customization().product_id())
.await?
{
return Err(InventoryError::ProductIDNotFound);
}
let updated_customization = CustomizationBuilder::default()
.name(cmd.name().into())
.product_id(*cmd.old_customization().product_id())
.customization_id(*cmd.old_customization().customization_id())
.deleted(*cmd.old_customization().deleted())
.build()
.unwrap();
if updated_customization.name() != cmd.old_customization().name() {
if self
.db_customization_name_exists_for_product
.customization_name_exists_for_product(&updated_customization)
.await?
{
return Err(InventoryError::DuplicateCustomizationName);
}
}
Ok(CustomizationUpdatedEventBuilder::default()
.added_by_user(*cmd.adding_by())
.old_customization(cmd.old_customization().clone())
.new_customization(updated_customization)
.build()
.unwrap())
}
}
#[cfg(test)]
pub mod tests {
use super::*;
use crate::inventory::domain::customization_updated_event;
use crate::inventory::domain::update_customization_command::tests::get_update_customization_command;
use crate::tests::bdd::*;
pub fn mock_update_customization_service(
times: Option<usize>,
cmd: UpdateCustomizationCommand,
) -> UpdateCustomizationServiceObj {
let mut m = MockUpdateCustomizationUseCase::new();
let res =
customization_updated_event::tests::get_customization_updated_event_from_command(&cmd);
if let Some(times) = times {
m.expect_update_customization()
.times(times)
.returning(move |_| Ok(res.clone()));
} else {
m.expect_update_customization()
.returning(move |_| Ok(res.clone()));
}
Arc::new(m)
}
#[actix_rt::test]
async fn test_service() {
let cmd = get_update_customization_command();
let s = UpdateCustomizationServiceBuilder::default()
.db_product_id_exists(mock_product_id_exists_db_port_true(IS_CALLED_ONLY_ONCE))
.db_customization_name_exists_for_product(
mock_customization_name_exists_for_product_db_port_false(IS_CALLED_ONLY_ONCE),
)
.db_customization_id_exists(mock_customization_id_exists_db_port_true(
IS_CALLED_ONLY_ONCE,
))
.build()
.unwrap();
let res = s.update_customization(cmd.clone()).await.unwrap();
assert_eq!(res.new_customization().name(), cmd.name());
assert_eq!(
res.new_customization().product_id(),
cmd.old_customization().product_id()
);
assert_eq!(
res.new_customization().customization_id(),
cmd.old_customization().customization_id()
);
assert_eq!(res.old_customization(), cmd.old_customization());
assert_eq!(res.added_by_user(), cmd.adding_by());
}
#[actix_rt::test]
async fn test_service_product_doesnt_exist() {
let cmd = get_update_customization_command();
let s = UpdateCustomizationServiceBuilder::default()
.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_NEVER_CALLED),
)
.db_customization_id_exists(mock_customization_id_exists_db_port_true(
IS_CALLED_ONLY_ONCE,
))
.build()
.unwrap();
assert_eq!(
s.update_customization(cmd.clone()).await,
Err(InventoryError::ProductIDNotFound)
);
}
#[actix_rt::test]
async fn test_customization_id_not_found() {
let cmd = get_update_customization_command();
let s = UpdateCustomizationServiceBuilder::default()
.db_product_id_exists(mock_product_id_exists_db_port_true(IS_NEVER_CALLED))
.db_customization_name_exists_for_product(
mock_customization_name_exists_for_product_db_port_false(IS_NEVER_CALLED),
)
.db_customization_id_exists(mock_customization_id_exists_db_port_false(
IS_CALLED_ONLY_ONCE,
))
.build()
.unwrap();
assert_eq!(
s.update_customization(cmd.clone()).await,
Err(InventoryError::CustomizationIDNotFound)
);
}
#[actix_rt::test]
async fn test_duplicate_new_name() {
let cmd = get_update_customization_command();
let s = UpdateCustomizationServiceBuilder::default()
.db_product_id_exists(mock_product_id_exists_db_port_true(IS_CALLED_ONLY_ONCE))
.db_customization_name_exists_for_product(
mock_customization_name_exists_for_product_db_port_true(IS_CALLED_ONLY_ONCE),
)
.db_customization_id_exists(mock_customization_id_exists_db_port_true(
IS_CALLED_ONLY_ONCE,
))
.build()
.unwrap();
assert_eq!(
s.update_customization(cmd.clone()).await,
Err(InventoryError::DuplicateCustomizationName)
);
}
}

View file

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

View file

@ -51,6 +51,14 @@ impl Aggregate for Customization {
let res = services.add_customization().add_customization(cmd).await?; let res = services.add_customization().add_customization(cmd).await?;
Ok(vec![InventoryEvent::CustomizationAdded(res)]) Ok(vec![InventoryEvent::CustomizationAdded(res)])
} }
InventoryCommand::UpdateCustomization(cmd) => {
let res = services
.update_customization()
.update_customization(cmd)
.await?;
Ok(vec![InventoryEvent::CustomizationUpdated(res)])
}
_ => Ok(Vec::default()), _ => Ok(Vec::default()),
} }
} }
@ -60,6 +68,9 @@ impl Aggregate for Customization {
InventoryEvent::CustomizationAdded(e) => { InventoryEvent::CustomizationAdded(e) => {
*self = e.customization().clone(); *self = e.customization().clone();
} }
InventoryEvent::CustomizationUpdated(e) => {
*self = e.new_customization().clone();
}
_ => (), _ => (),
} }
} }
@ -73,11 +84,15 @@ mod aggregate_tests {
use super::*; use super::*;
use crate::inventory::{ use crate::inventory::{
application::services::{add_customization_service::tests::*, *}, application::services::{
add_customization_service::tests::*, update_customization_service::tests::*, *,
},
domain::{ domain::{
add_customization_command, commands::InventoryCommand, add_customization_command, commands::InventoryCommand,
customization_added_event::tests::get_customization_added_event_from_cmd, customization_added_event::tests::get_customization_added_event_from_cmd,
customization_updated_event::tests::get_customization_updated_event_from_command,
events::InventoryEvent, events::InventoryEvent,
update_customization_command::tests::get_update_customization_command,
}, },
}; };
use crate::tests::bdd::*; use crate::tests::bdd::*;
@ -104,4 +119,25 @@ mod aggregate_tests {
.when(InventoryCommand::AddCustomization(cmd)) .when(InventoryCommand::AddCustomization(cmd))
.then_expect_events(vec![expected]); .then_expect_events(vec![expected]);
} }
#[test]
fn test_update_customization() {
let cmd = get_update_customization_command();
let expected = get_customization_updated_event_from_command(&cmd);
let expected = InventoryEvent::CustomizationUpdated(expected);
let mut services = MockInventoryServicesInterface::new();
services
.expect_update_customization()
.times(IS_CALLED_ONLY_ONCE.unwrap())
.return_const(mock_update_customization_service(
IS_CALLED_ONLY_ONCE,
cmd.clone(),
));
CustomizationTestFramework::with(Arc::new(services))
.given_no_previous_events()
.when(InventoryCommand::UpdateCustomization(cmd))
.then_expect_events(vec![expected]);
}
} }

View file

@ -0,0 +1,52 @@
// 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::customization_aggregate::Customization;
#[derive(
Clone, Debug, Builder, Serialize, Deserialize, Getters, Eq, PartialEq, Ord, PartialOrd,
)]
pub struct CustomizationUpdatedEvent {
added_by_user: Uuid,
old_customization: Customization,
new_customization: Customization,
}
#[cfg(test)]
pub mod tests {
use crate::inventory::domain::{
customization_aggregate::*, update_customization_command::UpdateCustomizationCommand,
};
use crate::utils::uuid::tests::UUID;
use super::*;
#[test]
fn test_name() {}
pub fn get_customization_updated_event_from_command(
cmd: &UpdateCustomizationCommand,
) -> CustomizationUpdatedEvent {
let customization = CustomizationBuilder::default()
.name(cmd.name().into())
.deleted(false)
.customization_id(UUID.clone())
.product_id(UUID.clone())
.build()
.unwrap();
CustomizationUpdatedEventBuilder::default()
.added_by_user(cmd.adding_by().clone())
.new_customization(customization)
.old_customization(cmd.old_customization().clone())
.build()
.unwrap()
}
}

View file

@ -7,8 +7,8 @@ use serde::{Deserialize, Serialize};
use super::{ use super::{
category_added_event::*, customization_added_event::CustomizationAddedEvent, category_added_event::*, customization_added_event::CustomizationAddedEvent,
product_added_event::ProductAddedEvent, product_updated_event::ProductUpdatedEvent, customization_updated_event::CustomizationUpdatedEvent, product_added_event::ProductAddedEvent,
store_added_event::StoreAddedEvent, product_updated_event::ProductUpdatedEvent, store_added_event::StoreAddedEvent,
}; };
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd)] #[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd)]
@ -18,6 +18,7 @@ pub enum InventoryEvent {
ProductAdded(ProductAddedEvent), ProductAdded(ProductAddedEvent),
CustomizationAdded(CustomizationAddedEvent), CustomizationAdded(CustomizationAddedEvent),
ProductUpdated(ProductUpdatedEvent), ProductUpdated(ProductUpdatedEvent),
CustomizationUpdated(CustomizationUpdatedEvent),
} }
impl DomainEvent for InventoryEvent { impl DomainEvent for InventoryEvent {
@ -32,6 +33,7 @@ impl DomainEvent for InventoryEvent {
InventoryEvent::ProductAdded { .. } => "InventoryProductAdded", InventoryEvent::ProductAdded { .. } => "InventoryProductAdded",
InventoryEvent::CustomizationAdded { .. } => "InventoryCustomizationAdded", InventoryEvent::CustomizationAdded { .. } => "InventoryCustomizationAdded",
InventoryEvent::ProductUpdated { .. } => "InventoryProductUpdated", InventoryEvent::ProductUpdated { .. } => "InventoryProductUpdated",
InventoryEvent::CustomizationUpdated { .. } => "InventoryCustomizationUpdated",
}; };
e.to_string() e.to_string()

View file

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

View file

@ -0,0 +1,101 @@
// 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 derive_more::{Display, Error};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use super::customization_aggregate::Customization;
#[derive(Debug, Error, Display, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub enum UpdateCustomizationCommandError {
NameIsEmpty,
}
#[derive(
Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Getters, Builder,
)]
pub struct UnvalidatedUpdateCustomizationCommand {
name: String,
adding_by: Uuid,
old_customization: Customization,
}
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Getters)]
pub struct UpdateCustomizationCommand {
name: String,
old_customization: Customization,
adding_by: Uuid,
}
impl UnvalidatedUpdateCustomizationCommand {
pub fn validate(self) -> Result<UpdateCustomizationCommand, UpdateCustomizationCommandError> {
let name = self.name.trim().to_owned();
if name.is_empty() {
return Err(UpdateCustomizationCommandError::NameIsEmpty);
}
Ok(UpdateCustomizationCommand {
name,
old_customization: self.old_customization,
adding_by: self.adding_by,
})
}
}
#[cfg(test)]
pub mod tests {
use super::*;
use crate::{
inventory::domain::customization_aggregate::Customization, utils::uuid::tests::UUID,
};
pub fn get_update_customization_command() -> UpdateCustomizationCommand {
let customozation = Customization::default();
UnvalidatedUpdateCustomizationCommandBuilder::default()
.name("foo".into())
.old_customization(customozation)
.adding_by(UUID.clone())
.build()
.unwrap()
.validate()
.unwrap()
}
#[test]
fn test_cmd() {
let name = "foo";
let cmd = UnvalidatedUpdateCustomizationCommandBuilder::default()
.name(name.into())
.old_customization(Customization::default())
.adding_by(UUID.clone())
.build()
.unwrap()
.validate()
.unwrap();
assert_eq!(cmd.name(), name);
assert_eq!(cmd.adding_by(), &UUID);
assert_eq!(cmd.old_customization(), &Customization::default());
}
#[test]
fn test_cmd_name_is_empty() {
assert_eq!(
UnvalidatedUpdateCustomizationCommandBuilder::default()
.name("".into())
.adding_by(UUID.clone())
.old_customization(Customization::default())
.build()
.unwrap()
.validate(),
Err(UpdateCustomizationCommandError::NameIsEmpty)
);
}
}