From d2020a14de5a6a14415f676f3e8201e067705605 Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Tue, 23 Jul 2024 20:59:55 +0530 Subject: [PATCH] feat: delete kot service --- .../services/delete_kot_service.rs | 104 ++++++++++++++++++ src/ordering/application/services/mod.rs | 7 ++ src/ordering/domain/commands.rs | 8 +- src/ordering/domain/delete_kot_command.rs | 55 +++++++++ src/ordering/domain/events.rs | 11 +- src/ordering/domain/kot_aggregate.rs | 55 +++++---- src/ordering/domain/kot_deleted_event.rs | 41 +++++++ src/ordering/domain/mod.rs | 2 + 8 files changed, 247 insertions(+), 36 deletions(-) create mode 100644 src/ordering/application/services/delete_kot_service.rs create mode 100644 src/ordering/domain/delete_kot_command.rs create mode 100644 src/ordering/domain/kot_deleted_event.rs diff --git a/src/ordering/application/services/delete_kot_service.rs b/src/ordering/application/services/delete_kot_service.rs new file mode 100644 index 0000000..b6cce85 --- /dev/null +++ b/src/ordering/application/services/delete_kot_service.rs @@ -0,0 +1,104 @@ +// SPDX-FileCopyrightText: 2024 Aravinth Manivannan +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +use std::sync::Arc; + +use derive_builder::Builder; +use mockall::predicate::*; +use mockall::*; +use time::OffsetDateTime; + +use super::errors::*; +use crate::ordering::{ + application::port::output::db::kot_id_exists::*, + domain::{delete_kot_command::*, kot_aggregate::*, kot_deleted_event::*}, +}; + +#[automock] +#[async_trait::async_trait] +pub trait DeleteKotUseCase: Send + Sync { + async fn delete_kot(&self, cmd: DeleteKotCommand) -> OrderingResult; +} + +pub type DeleteKotServiceObj = Arc; + +#[derive(Clone, Builder)] +pub struct DeleteKotService { + db_kot_id_exists: KotIDExistsDBPortObj, +} + +#[async_trait::async_trait] +impl DeleteKotUseCase for DeleteKotService { + async fn delete_kot(&self, cmd: DeleteKotCommand) -> OrderingResult { + if !self + .db_kot_id_exists + .kot_id_exists(cmd.kot().kot_id()) + .await? + { + return Err(OrderingError::KotIDNotFound); + } + + Ok(KotDeletedEventBuilder::default() + .added_by_user(*cmd.adding_by()) + .kot(cmd.kot().clone()) + .build() + .unwrap()) + } +} + +#[cfg(test)] +pub mod tests { + use super::*; + + use crate::ordering::domain::kot_deleted_event::tests::get_deleted_kot_event_from_command; + use crate::utils::uuid::tests::UUID; + use crate::{tests::bdd::*, utils::uuid::tests::mock_get_uuid}; + + pub fn mock_delete_kot_service( + times: Option, + cmd: DeleteKotCommand, + ) -> DeleteKotServiceObj { + let mut m = MockDeleteKotUseCase::new(); + + let res = get_deleted_kot_event_from_command(&cmd); + if let Some(times) = times { + m.expect_delete_kot() + .times(times) + .returning(move |_| Ok(res.clone())); + } else { + m.expect_delete_kot().returning(move |_| Ok(res.clone())); + } + + Arc::new(m) + } + + #[actix_rt::test] + async fn test_service() { + let cmd = DeleteKotCommand::get_cmd(); + + let s = DeleteKotServiceBuilder::default() + .db_kot_id_exists(mock_kot_id_exists_db_port_true(IS_CALLED_ONLY_ONCE)) + .build() + .unwrap(); + + let res = s.delete_kot(cmd.clone()).await.unwrap(); + assert_eq!(res.added_by_user(), cmd.adding_by()); + assert_eq!(res.kot(), cmd.kot()); + } + + #[actix_rt::test] + async fn test_service_kot_id_doesnt_exist() { + let cmd = DeleteKotCommand::get_cmd(); + + let s = DeleteKotServiceBuilder::default() + .db_kot_id_exists(mock_kot_id_exists_db_port_false(IS_CALLED_ONLY_ONCE)) + .build() + .unwrap(); + + assert_eq!( + s.delete_kot(cmd.clone()).await, + Err(OrderingError::KotIDNotFound) + ); + } +} diff --git a/src/ordering/application/services/mod.rs b/src/ordering/application/services/mod.rs index ccc1afa..2d7b9cf 100644 --- a/src/ordering/application/services/mod.rs +++ b/src/ordering/application/services/mod.rs @@ -12,6 +12,7 @@ pub mod errors; pub mod add_kot_service; pub mod add_line_item_service; pub mod add_order_service; +pub mod delete_kot_service; pub mod delete_line_item_service; pub mod delete_order_service; pub mod update_kot_service; @@ -28,6 +29,7 @@ pub trait OrderingServicesInterface: Send + Sync { fn delete_order(&self) -> delete_order_service::DeleteOrderServiceObj; fn add_kot(&self) -> add_kot_service::AddKotServiceObj; fn update_kot(&self) -> update_kot_service::UpdateKotServiceObj; + fn delete_kot(&self) -> delete_kot_service::DeleteKotServiceObj; } #[derive(Clone, Builder)] @@ -40,6 +42,7 @@ pub struct OrderingServices { delete_order: delete_order_service::DeleteOrderServiceObj, add_kot: add_kot_service::AddKotServiceObj, update_kot: update_kot_service::UpdateKotServiceObj, + delete_kot: delete_kot_service::DeleteKotServiceObj, } impl OrderingServicesInterface for OrderingServices { @@ -73,4 +76,8 @@ impl OrderingServicesInterface for OrderingServices { fn update_kot(&self) -> update_kot_service::UpdateKotServiceObj { self.update_kot.clone() } + + fn delete_kot(&self) -> delete_kot_service::DeleteKotServiceObj { + self.delete_kot.clone() + } } diff --git a/src/ordering/domain/commands.rs b/src/ordering/domain/commands.rs index a22d7f9..193ddeb 100644 --- a/src/ordering/domain/commands.rs +++ b/src/ordering/domain/commands.rs @@ -7,9 +7,10 @@ use serde::{Deserialize, Serialize}; use super::{ add_kot_command::AddKotCommand, add_line_item_command::AddLineItemCommand, - add_order_command::AddOrderCommand, delete_line_item_command::DeleteLineItemCommand, - delete_order_command::DeleteOrderCommand, update_kot_command::UpdateKotCommand, - update_line_item_command::UpdateLineItemCommand, update_order_command::UpdateOrderCommand, + add_order_command::AddOrderCommand, delete_kot_command::DeleteKotCommand, + delete_line_item_command::DeleteLineItemCommand, delete_order_command::DeleteOrderCommand, + update_kot_command::UpdateKotCommand, update_line_item_command::UpdateLineItemCommand, + update_order_command::UpdateOrderCommand, }; #[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd)] @@ -22,4 +23,5 @@ pub enum OrderingCommand { DeleteOrder(DeleteOrderCommand), AddKot(AddKotCommand), UpdateKot(UpdateKotCommand), + DeleteKot(DeleteKotCommand), } diff --git a/src/ordering/domain/delete_kot_command.rs b/src/ordering/domain/delete_kot_command.rs new file mode 100644 index 0000000..aad2d36 --- /dev/null +++ b/src/ordering/domain/delete_kot_command.rs @@ -0,0 +1,55 @@ +// SPDX-FileCopyrightText: 2024 Aravinth Manivannan +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +use derive_builder::Builder; +use derive_getters::Getters; +use serde::{Deserialize, Serialize}; +use time::OffsetDateTime; +use uuid::Uuid; + +use super::kot_aggregate::*; + +#[derive( + Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Getters, Builder, +)] +pub struct DeleteKotCommand { + adding_by: Uuid, + kot: Kot, +} + +#[cfg(test)] +mod tests { + use time::macros::datetime; + + use crate::utils::uuid::tests::UUID; + + use super::*; + + impl DeleteKotCommand { + pub fn get_cmd() -> Self { + let adding_by = UUID; + + DeleteKotCommandBuilder::default() + .adding_by(adding_by) + .kot(Kot::default()) + .build() + .unwrap() + } + } + + #[test] + fn test_cmd() { + let adding_by = UUID; + let kot = Kot::default(); + + let cmd = DeleteKotCommandBuilder::default() + .adding_by(adding_by) + .kot(kot.clone()) + .build() + .unwrap(); + + assert_eq!(*cmd.adding_by(), adding_by); + assert_eq!(cmd.kot(), &kot); + } +} diff --git a/src/ordering/domain/events.rs b/src/ordering/domain/events.rs index 0ce0b0a..1049e70 100644 --- a/src/ordering/domain/events.rs +++ b/src/ordering/domain/events.rs @@ -6,10 +6,11 @@ use cqrs_es::DomainEvent; use serde::{Deserialize, Serialize}; use super::{ - kot_added_event::KotAddedEvent, kot_updated_event::KotUpdatedEvent, - line_item_added_event::LineItemAddedEvent, line_item_deleted_event::LineItemDeletedEvent, - line_item_updated_event::LineItemUpdatedEvent, order_added_event::OrderAddedEvent, - order_deleted_event::OrderDeletedEvent, order_updated_event::OrderUpdatedEvent, + kot_added_event::KotAddedEvent, kot_deleted_event::KotDeletedEvent, + kot_updated_event::KotUpdatedEvent, line_item_added_event::LineItemAddedEvent, + line_item_deleted_event::LineItemDeletedEvent, line_item_updated_event::LineItemUpdatedEvent, + order_added_event::OrderAddedEvent, order_deleted_event::OrderDeletedEvent, + order_updated_event::OrderUpdatedEvent, }; #[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd)] @@ -22,6 +23,7 @@ pub enum OrderingEvent { OrderDeleted(OrderDeletedEvent), KotAdded(KotAddedEvent), KotUpdated(KotUpdatedEvent), + KotDeleted(KotDeletedEvent), } impl DomainEvent for OrderingEvent { @@ -39,6 +41,7 @@ impl DomainEvent for OrderingEvent { OrderingEvent::OrderDeleted { .. } => "OrderingOrderDeleted", OrderingEvent::KotAdded { .. } => "OrderingKotAdded", OrderingEvent::KotUpdated { .. } => "OrderingKotUpdated", + OrderingEvent::KotDeleted { .. } => "OrderingKotDeleted", }; e.to_string() diff --git a/src/ordering/domain/kot_aggregate.rs b/src/ordering/domain/kot_aggregate.rs index a064483..9ae418a 100644 --- a/src/ordering/domain/kot_aggregate.rs +++ b/src/ordering/domain/kot_aggregate.rs @@ -87,10 +87,10 @@ impl Aggregate for Kot { let res = services.update_kot().update_kot(cmd).await?; Ok(vec![OrderingEvent::KotUpdated(res)]) } - //OrderingCommand::DeleteKot(cmd) => { - // let res = services.delete_kot().delete_kot(cmd).await?; - // Ok(vec![OrderingEvent::KotDeleted(res)]) - //} + OrderingCommand::DeleteKot(cmd) => { + let res = services.delete_kot().delete_kot(cmd).await?; + Ok(vec![OrderingEvent::KotDeleted(res)]) + } _ => Ok(Vec::default()), } } @@ -99,7 +99,7 @@ impl Aggregate for Kot { match event { OrderingEvent::KotAdded(e) => *self = e.kot().clone(), OrderingEvent::KotUpdated(e) => *self = e.new_kot().clone(), - //OrderingEvent::KotDeleted(e) => *self = e.kot().clone(), + OrderingEvent::KotDeleted(e) => *self = e.kot().clone(), _ => (), } } @@ -111,13 +111,13 @@ mod aggregate_tests { use add_kot_service::tests::mock_add_kot_service; use cqrs_es::test::TestFramework; - // use delete_kot_service::tests::mock_delete_kot_service; + use delete_kot_service::tests::mock_delete_kot_service; use update_kot_service::tests::mock_update_kot_service; use super::*; - // use crate::ordering::domain::delete_kot_command::DeleteKotCommand; - // use crate::ordering::domain::kot_deleted_event::tests::get_deleted_kot_event_from_command; + use crate::ordering::domain::delete_kot_command::DeleteKotCommand; + use crate::ordering::domain::kot_deleted_event::tests::get_deleted_kot_event_from_command; use crate::ordering::domain::kot_updated_event::tests::get_updated_kot_event_from_command; use crate::ordering::domain::update_kot_command::UpdateKotCommand; use crate::tests::bdd::*; @@ -163,25 +163,22 @@ mod aggregate_tests { .when(OrderingCommand::UpdateKot(cmd)) .then_expect_events(vec![expected]); } - // - // #[test] - // fn test_delete_kot() { - // let cmd = DeleteKotCommand::get_cmd(); - // let expected = get_deleted_kot_event_from_command(&cmd); - // let expected = OrderingEvent::KotDeleted(expected); - // - // let mut services = MockOrderingServicesInterface::new(); - // services - // .expect_delete_kot() - // .times(IS_CALLED_ONLY_ONCE.unwrap()) - // .return_const(mock_delete_kot_service( - // IS_CALLED_ONLY_ONCE, - // cmd.clone(), - // )); - // - // KotTestFramework::with(Arc::new(services)) - // .given_no_previous_events() - // .when(OrderingCommand::DeleteKot(cmd)) - // .then_expect_events(vec![expected]); - // } + + #[test] + fn test_delete_kot() { + let cmd = DeleteKotCommand::get_cmd(); + let expected = get_deleted_kot_event_from_command(&cmd); + let expected = OrderingEvent::KotDeleted(expected); + + let mut services = MockOrderingServicesInterface::new(); + services + .expect_delete_kot() + .times(IS_CALLED_ONLY_ONCE.unwrap()) + .return_const(mock_delete_kot_service(IS_CALLED_ONLY_ONCE, cmd.clone())); + + KotTestFramework::with(Arc::new(services)) + .given_no_previous_events() + .when(OrderingCommand::DeleteKot(cmd)) + .then_expect_events(vec![expected]); + } } diff --git a/src/ordering/domain/kot_deleted_event.rs b/src/ordering/domain/kot_deleted_event.rs new file mode 100644 index 0000000..b080b42 --- /dev/null +++ b/src/ordering/domain/kot_deleted_event.rs @@ -0,0 +1,41 @@ +// SPDX-FileCopyrightText: 2024 Aravinth Manivannan +// +// 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::kot_aggregate::Kot; + +#[derive( + Clone, Debug, Builder, Serialize, Deserialize, Getters, Eq, PartialEq, Ord, PartialOrd, +)] +pub struct KotDeletedEvent { + added_by_user: Uuid, + + kot: Kot, +} + +#[cfg(test)] +pub mod tests { + use crate::ordering::domain::{ + delete_kot_command::DeleteKotCommand, kot_aggregate::KotBuilder, + }; + + use super::*; + + pub fn get_deleted_kot_event_from_command(cmd: &DeleteKotCommand) -> KotDeletedEvent { + KotDeletedEventBuilder::default() + .added_by_user(cmd.adding_by().clone()) + .kot(cmd.kot().clone()) + .build() + .unwrap() + } + + #[test] + fn test_event() { + get_deleted_kot_event_from_command(&DeleteKotCommand::get_cmd()); + } +} diff --git a/src/ordering/domain/mod.rs b/src/ordering/domain/mod.rs index de30521..5901453 100644 --- a/src/ordering/domain/mod.rs +++ b/src/ordering/domain/mod.rs @@ -12,6 +12,7 @@ pub mod add_kot_command; pub mod add_line_item_command; pub mod add_order_command; pub mod commands; +pub mod delete_kot_command; pub mod delete_line_item_command; pub mod delete_order_command; pub mod update_kot_command; @@ -21,6 +22,7 @@ pub mod update_order_command; // events pub mod events; pub mod kot_added_event; +pub mod kot_deleted_event; pub mod kot_updated_event; pub mod line_item_added_event; pub mod line_item_deleted_event;