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_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()
}
}

View file

@ -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),
}

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 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()

View file

@ -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]);
}
}

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_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;