feat: delete kot service
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/pr/woodpecker Pipeline was successful
ci/woodpecker/pull_request_closed/woodpecker Pipeline was successful

This commit is contained in:
Aravinth Manivannan 2024-07-23 20:59:55 +05:30
parent ec820b4d33
commit d2020a14de
Signed by: realaravinth
GPG key ID: F8F50389936984FF
8 changed files with 247 additions and 36 deletions

View file

@ -0,0 +1,104 @@
// 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 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<KotDeletedEvent>;
}
pub type DeleteKotServiceObj = Arc<dyn DeleteKotUseCase>;
#[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<KotDeletedEvent> {
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<usize>,
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)
);
}
}

View file

@ -12,6 +12,7 @@ pub mod errors;
pub mod add_kot_service; pub mod add_kot_service;
pub mod add_line_item_service; pub mod add_line_item_service;
pub mod add_order_service; pub mod add_order_service;
pub mod delete_kot_service;
pub mod delete_line_item_service; pub mod delete_line_item_service;
pub mod delete_order_service; pub mod delete_order_service;
pub mod update_kot_service; pub mod update_kot_service;
@ -28,6 +29,7 @@ pub trait OrderingServicesInterface: Send + Sync {
fn delete_order(&self) -> delete_order_service::DeleteOrderServiceObj; fn delete_order(&self) -> delete_order_service::DeleteOrderServiceObj;
fn add_kot(&self) -> add_kot_service::AddKotServiceObj; fn add_kot(&self) -> add_kot_service::AddKotServiceObj;
fn update_kot(&self) -> update_kot_service::UpdateKotServiceObj; fn update_kot(&self) -> update_kot_service::UpdateKotServiceObj;
fn delete_kot(&self) -> delete_kot_service::DeleteKotServiceObj;
} }
#[derive(Clone, Builder)] #[derive(Clone, Builder)]
@ -40,6 +42,7 @@ pub struct OrderingServices {
delete_order: delete_order_service::DeleteOrderServiceObj, delete_order: delete_order_service::DeleteOrderServiceObj,
add_kot: add_kot_service::AddKotServiceObj, add_kot: add_kot_service::AddKotServiceObj,
update_kot: update_kot_service::UpdateKotServiceObj, update_kot: update_kot_service::UpdateKotServiceObj,
delete_kot: delete_kot_service::DeleteKotServiceObj,
} }
impl OrderingServicesInterface for OrderingServices { impl OrderingServicesInterface for OrderingServices {
@ -73,4 +76,8 @@ impl OrderingServicesInterface for OrderingServices {
fn update_kot(&self) -> update_kot_service::UpdateKotServiceObj { fn update_kot(&self) -> update_kot_service::UpdateKotServiceObj {
self.update_kot.clone() self.update_kot.clone()
} }
fn delete_kot(&self) -> delete_kot_service::DeleteKotServiceObj {
self.delete_kot.clone()
}
} }

View file

@ -7,9 +7,10 @@ use serde::{Deserialize, Serialize};
use super::{ use super::{
add_kot_command::AddKotCommand, add_line_item_command::AddLineItemCommand, add_kot_command::AddKotCommand, add_line_item_command::AddLineItemCommand,
add_order_command::AddOrderCommand, delete_line_item_command::DeleteLineItemCommand, add_order_command::AddOrderCommand, delete_kot_command::DeleteKotCommand,
delete_order_command::DeleteOrderCommand, update_kot_command::UpdateKotCommand, delete_line_item_command::DeleteLineItemCommand, delete_order_command::DeleteOrderCommand,
update_line_item_command::UpdateLineItemCommand, update_order_command::UpdateOrderCommand, update_kot_command::UpdateKotCommand, update_line_item_command::UpdateLineItemCommand,
update_order_command::UpdateOrderCommand,
}; };
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd)] #[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd)]
@ -22,4 +23,5 @@ pub enum OrderingCommand {
DeleteOrder(DeleteOrderCommand), DeleteOrder(DeleteOrderCommand),
AddKot(AddKotCommand), AddKot(AddKotCommand),
UpdateKot(UpdateKotCommand), UpdateKot(UpdateKotCommand),
DeleteKot(DeleteKotCommand),
} }

View file

@ -0,0 +1,55 @@
// 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 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);
}
}

View file

@ -6,10 +6,11 @@ use cqrs_es::DomainEvent;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use super::{ use super::{
kot_added_event::KotAddedEvent, kot_updated_event::KotUpdatedEvent, kot_added_event::KotAddedEvent, kot_deleted_event::KotDeletedEvent,
line_item_added_event::LineItemAddedEvent, line_item_deleted_event::LineItemDeletedEvent, kot_updated_event::KotUpdatedEvent, line_item_added_event::LineItemAddedEvent,
line_item_updated_event::LineItemUpdatedEvent, order_added_event::OrderAddedEvent, line_item_deleted_event::LineItemDeletedEvent, line_item_updated_event::LineItemUpdatedEvent,
order_deleted_event::OrderDeletedEvent, order_updated_event::OrderUpdatedEvent, order_added_event::OrderAddedEvent, order_deleted_event::OrderDeletedEvent,
order_updated_event::OrderUpdatedEvent,
}; };
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd)] #[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd)]
@ -22,6 +23,7 @@ pub enum OrderingEvent {
OrderDeleted(OrderDeletedEvent), OrderDeleted(OrderDeletedEvent),
KotAdded(KotAddedEvent), KotAdded(KotAddedEvent),
KotUpdated(KotUpdatedEvent), KotUpdated(KotUpdatedEvent),
KotDeleted(KotDeletedEvent),
} }
impl DomainEvent for OrderingEvent { impl DomainEvent for OrderingEvent {
@ -39,6 +41,7 @@ impl DomainEvent for OrderingEvent {
OrderingEvent::OrderDeleted { .. } => "OrderingOrderDeleted", OrderingEvent::OrderDeleted { .. } => "OrderingOrderDeleted",
OrderingEvent::KotAdded { .. } => "OrderingKotAdded", OrderingEvent::KotAdded { .. } => "OrderingKotAdded",
OrderingEvent::KotUpdated { .. } => "OrderingKotUpdated", OrderingEvent::KotUpdated { .. } => "OrderingKotUpdated",
OrderingEvent::KotDeleted { .. } => "OrderingKotDeleted",
}; };
e.to_string() e.to_string()

View file

@ -87,10 +87,10 @@ impl Aggregate for Kot {
let res = services.update_kot().update_kot(cmd).await?; let res = services.update_kot().update_kot(cmd).await?;
Ok(vec![OrderingEvent::KotUpdated(res)]) Ok(vec![OrderingEvent::KotUpdated(res)])
} }
//OrderingCommand::DeleteKot(cmd) => { OrderingCommand::DeleteKot(cmd) => {
// let res = services.delete_kot().delete_kot(cmd).await?; let res = services.delete_kot().delete_kot(cmd).await?;
// Ok(vec![OrderingEvent::KotDeleted(res)]) Ok(vec![OrderingEvent::KotDeleted(res)])
//} }
_ => Ok(Vec::default()), _ => Ok(Vec::default()),
} }
} }
@ -99,7 +99,7 @@ impl Aggregate for Kot {
match event { match event {
OrderingEvent::KotAdded(e) => *self = e.kot().clone(), OrderingEvent::KotAdded(e) => *self = e.kot().clone(),
OrderingEvent::KotUpdated(e) => *self = e.new_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 add_kot_service::tests::mock_add_kot_service;
use cqrs_es::test::TestFramework; 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 update_kot_service::tests::mock_update_kot_service;
use super::*; use super::*;
// use crate::ordering::domain::delete_kot_command::DeleteKotCommand; 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_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::kot_updated_event::tests::get_updated_kot_event_from_command;
use crate::ordering::domain::update_kot_command::UpdateKotCommand; use crate::ordering::domain::update_kot_command::UpdateKotCommand;
use crate::tests::bdd::*; use crate::tests::bdd::*;
@ -163,25 +163,22 @@ mod aggregate_tests {
.when(OrderingCommand::UpdateKot(cmd)) .when(OrderingCommand::UpdateKot(cmd))
.then_expect_events(vec![expected]); .then_expect_events(vec![expected]);
} }
//
// #[test] #[test]
// fn test_delete_kot() { fn test_delete_kot() {
// let cmd = DeleteKotCommand::get_cmd(); let cmd = DeleteKotCommand::get_cmd();
// let expected = get_deleted_kot_event_from_command(&cmd); let expected = get_deleted_kot_event_from_command(&cmd);
// let expected = OrderingEvent::KotDeleted(expected); let expected = OrderingEvent::KotDeleted(expected);
//
// let mut services = MockOrderingServicesInterface::new(); let mut services = MockOrderingServicesInterface::new();
// services services
// .expect_delete_kot() .expect_delete_kot()
// .times(IS_CALLED_ONLY_ONCE.unwrap()) .times(IS_CALLED_ONLY_ONCE.unwrap())
// .return_const(mock_delete_kot_service( .return_const(mock_delete_kot_service(IS_CALLED_ONLY_ONCE, cmd.clone()));
// IS_CALLED_ONLY_ONCE,
// cmd.clone(), KotTestFramework::with(Arc::new(services))
// )); .given_no_previous_events()
// .when(OrderingCommand::DeleteKot(cmd))
// KotTestFramework::with(Arc::new(services)) .then_expect_events(vec![expected]);
// .given_no_previous_events() }
// .when(OrderingCommand::DeleteKot(cmd))
// .then_expect_events(vec![expected]);
// }
} }

View file

@ -0,0 +1,41 @@
// 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::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());
}
}

View file

@ -12,6 +12,7 @@ pub mod add_kot_command;
pub mod add_line_item_command; pub mod add_line_item_command;
pub mod add_order_command; pub mod add_order_command;
pub mod commands; pub mod commands;
pub mod delete_kot_command;
pub mod delete_line_item_command; pub mod delete_line_item_command;
pub mod delete_order_command; pub mod delete_order_command;
pub mod update_kot_command; pub mod update_kot_command;
@ -21,6 +22,7 @@ pub mod update_order_command;
// events // events
pub mod events; pub mod events;
pub mod kot_added_event; pub mod kot_added_event;
pub mod kot_deleted_event;
pub mod kot_updated_event; pub mod kot_updated_event;
pub mod line_item_added_event; pub mod line_item_added_event;
pub mod line_item_deleted_event; pub mod line_item_deleted_event;