Merge pull request 'feat: update kot service' (#71) from kot-update-service into master
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

Reviewed-on: #71
This commit is contained in:
Aravinth Manivannan 2024-07-23 21:10:15 +05:30
commit 938e032114
8 changed files with 302 additions and 34 deletions

View file

@ -14,6 +14,7 @@ pub mod add_line_item_service;
pub mod add_order_service;
pub mod delete_line_item_service;
pub mod delete_order_service;
pub mod update_kot_service;
pub mod update_line_item_service;
pub mod update_order_service;
@ -26,6 +27,7 @@ pub trait OrderingServicesInterface: Send + Sync {
fn update_order(&self) -> update_order_service::UpdateOrderServiceObj;
fn delete_order(&self) -> delete_order_service::DeleteOrderServiceObj;
fn add_kot(&self) -> add_kot_service::AddKotServiceObj;
fn update_kot(&self) -> update_kot_service::UpdateKotServiceObj;
}
#[derive(Clone, Builder)]
@ -37,6 +39,7 @@ pub struct OrderingServices {
update_order: update_order_service::UpdateOrderServiceObj,
delete_order: delete_order_service::DeleteOrderServiceObj,
add_kot: add_kot_service::AddKotServiceObj,
update_kot: update_kot_service::UpdateKotServiceObj,
}
impl OrderingServicesInterface for OrderingServices {
@ -66,4 +69,8 @@ impl OrderingServicesInterface for OrderingServices {
fn add_kot(&self) -> add_kot_service::AddKotServiceObj {
self.add_kot.clone()
}
fn update_kot(&self) -> update_kot_service::UpdateKotServiceObj {
self.update_kot.clone()
}
}

View file

@ -0,0 +1,144 @@
// 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::*,
application::port::output::db::order_id_exists::*,
domain::{kot_aggregate::*, kot_updated_event::*, update_kot_command::*},
};
#[automock]
#[async_trait::async_trait]
pub trait UpdateKotUseCase: Send + Sync {
async fn update_kot(&self, cmd: UpdateKotCommand) -> OrderingResult<KotUpdatedEvent>;
}
pub type UpdateKotServiceObj = Arc<dyn UpdateKotUseCase>;
#[derive(Clone, Builder)]
pub struct UpdateKotService {
db_kot_id_exists: KotIDExistsDBPortObj,
db_order_id_exists: OrderIDExistsDBPortObj,
}
#[async_trait::async_trait]
impl UpdateKotUseCase for UpdateKotService {
async fn update_kot(&self, cmd: UpdateKotCommand) -> OrderingResult<KotUpdatedEvent> {
if !self
.db_order_id_exists
.order_id_exists(cmd.order_id())
.await?
{
return Err(OrderingError::OrderIDNotFound);
}
if !self
.db_kot_id_exists
.kot_id_exists(cmd.old_kot().kot_id())
.await?
{
return Err(OrderingError::KotIDNotFound);
}
let new_kot = KotBuilder::default()
.created_time(cmd.created_time().clone())
.order_id(*cmd.order_id())
.kot_id(*cmd.old_kot().kot_id())
.deleted(false)
.build()
.unwrap();
Ok(KotUpdatedEventBuilder::default()
.added_by_user(*cmd.adding_by())
.old_kot(cmd.old_kot().clone())
.new_kot(new_kot)
.build()
.unwrap())
}
}
#[cfg(test)]
pub mod tests {
use super::*;
use crate::ordering::domain::kot_updated_event::tests::get_updated_kot_event_from_command;
use crate::utils::uuid::tests::UUID;
use crate::{tests::bdd::*, utils::uuid::tests::mock_get_uuid};
pub fn mock_update_kot_service(
times: Option<usize>,
cmd: UpdateKotCommand,
) -> UpdateKotServiceObj {
let mut m = MockUpdateKotUseCase::new();
let res = get_updated_kot_event_from_command(&cmd);
if let Some(times) = times {
m.expect_update_kot()
.times(times)
.returning(move |_| Ok(res.clone()));
} else {
m.expect_update_kot().returning(move |_| Ok(res.clone()));
}
Arc::new(m)
}
#[actix_rt::test]
async fn test_service() {
let cmd = UpdateKotCommand::get_cmd();
let s = UpdateKotServiceBuilder::default()
.db_kot_id_exists(mock_kot_id_exists_db_port_true(IS_CALLED_ONLY_ONCE))
.db_order_id_exists(mock_order_id_exists_db_port_true(IS_CALLED_ONLY_ONCE))
.build()
.unwrap();
let res = s.update_kot(cmd.clone()).await.unwrap();
assert_eq!(res.new_kot().order_id(), cmd.order_id());
assert!(!res.new_kot().deleted());
assert_eq!(res.added_by_user(), cmd.adding_by());
assert_eq!(res.new_kot().created_time(), cmd.created_time());
assert_eq!(res.old_kot(), cmd.old_kot());
}
#[actix_rt::test]
async fn test_service_order_id_doesnt_exist() {
let cmd = UpdateKotCommand::get_cmd();
let s = UpdateKotServiceBuilder::default()
.db_kot_id_exists(mock_kot_id_exists_db_port_true(IS_NEVER_CALLED))
.db_order_id_exists(mock_order_id_exists_db_port_false(IS_CALLED_ONLY_ONCE))
.build()
.unwrap();
assert_eq!(
s.update_kot(cmd.clone()).await,
Err(OrderingError::OrderIDNotFound)
);
}
#[actix_rt::test]
async fn test_service_kot_id_doesnt_exist() {
let cmd = UpdateKotCommand::get_cmd();
let s = UpdateKotServiceBuilder::default()
.db_kot_id_exists(mock_kot_id_exists_db_port_false(IS_CALLED_ONLY_ONCE))
.db_order_id_exists(mock_order_id_exists_db_port_true(IS_CALLED_ONLY_ONCE))
.build()
.unwrap();
assert_eq!(
s.update_kot(cmd.clone()).await,
Err(OrderingError::KotIDNotFound)
);
}
}

View file

@ -8,8 +8,8 @@ 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_line_item_command::UpdateLineItemCommand,
update_order_command::UpdateOrderCommand,
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)]
@ -21,4 +21,5 @@ pub enum OrderingCommand {
UpdateOrder(UpdateOrderCommand),
DeleteOrder(DeleteOrderCommand),
AddKot(AddKotCommand),
UpdateKot(UpdateKotCommand),
}

View file

@ -6,10 +6,10 @@ use cqrs_es::DomainEvent;
use serde::{Deserialize, Serialize};
use super::{
kot_added_event::KotAddedEvent, 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_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)]
@ -21,6 +21,7 @@ pub enum OrderingEvent {
OrderUpdated(OrderUpdatedEvent),
OrderDeleted(OrderDeletedEvent),
KotAdded(KotAddedEvent),
KotUpdated(KotUpdatedEvent),
}
impl DomainEvent for OrderingEvent {
@ -37,6 +38,7 @@ impl DomainEvent for OrderingEvent {
OrderingEvent::OrderUpdated { .. } => "OrderingOrderUpdated",
OrderingEvent::OrderDeleted { .. } => "OrderingOrderDeleted",
OrderingEvent::KotAdded { .. } => "OrderingKotAdded",
OrderingEvent::KotUpdated { .. } => "OrderingKotUpdated",
};
e.to_string()

View file

@ -83,10 +83,10 @@ impl Aggregate for Kot {
let res = services.add_kot().add_kot(cmd).await?;
Ok(vec![OrderingEvent::KotAdded(res)])
}
//OrderingCommand::UpdateKot(cmd) => {
// let res = services.update_kot().update_kot(cmd).await?;
// Ok(vec![OrderingEvent::KotUpdated(res)])
//}
OrderingCommand::UpdateKot(cmd) => {
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)])
@ -98,7 +98,7 @@ impl Aggregate for Kot {
fn apply(&mut self, event: Self::Event) {
match event {
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(),
_ => (),
}
@ -112,14 +112,14 @@ 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 update_kot_service::tests::mock_update_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::kot_updated_event::tests::get_updated_kot_event_from_command;
// use crate::ordering::domain::update_kot_command::UpdateKotCommand;
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::*;
use crate::ordering::domain::{
@ -146,26 +146,23 @@ mod aggregate_tests {
.then_expect_events(vec![expected]);
}
// #[test]
// fn test_update_kot() {
// let cmd = UpdateKotCommand::get_cmd();
// let expected = get_updated_kot_event_from_command(&cmd);
// let expected = OrderingEvent::KotUpdated(expected);
//
// let mut services = MockOrderingServicesInterface::new();
// services
// .expect_update_kot()
// .times(IS_CALLED_ONLY_ONCE.unwrap())
// .return_const(mock_update_kot_service(
// IS_CALLED_ONLY_ONCE,
// cmd.clone(),
// ));
//
// KotTestFramework::with(Arc::new(services))
// .given_no_previous_events()
// .when(OrderingCommand::UpdateKot(cmd))
// .then_expect_events(vec![expected]);
// }
#[test]
fn test_update_kot() {
let cmd = UpdateKotCommand::get_cmd();
let expected = get_updated_kot_event_from_command(&cmd);
let expected = OrderingEvent::KotUpdated(expected);
let mut services = MockOrderingServicesInterface::new();
services
.expect_update_kot()
.times(IS_CALLED_ONLY_ONCE.unwrap())
.return_const(mock_update_kot_service(IS_CALLED_ONLY_ONCE, cmd.clone()));
KotTestFramework::with(Arc::new(services))
.given_no_previous_events()
.when(OrderingCommand::UpdateKot(cmd))
.then_expect_events(vec![expected]);
}
//
// #[test]
// fn test_delete_kot() {

View file

@ -0,0 +1,50 @@
// 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 KotUpdatedEvent {
added_by_user: Uuid,
new_kot: Kot,
old_kot: Kot,
}
#[cfg(test)]
pub mod tests {
use crate::ordering::domain::{
kot_aggregate::KotBuilder, update_kot_command::UpdateKotCommand,
};
use super::*;
pub fn get_updated_kot_event_from_command(cmd: &UpdateKotCommand) -> KotUpdatedEvent {
let new_kot = KotBuilder::default()
.created_time(cmd.created_time().clone())
.order_id(*cmd.order_id())
.kot_id(*cmd.old_kot().kot_id())
.build()
.unwrap();
KotUpdatedEventBuilder::default()
.added_by_user(cmd.adding_by().clone())
.old_kot(cmd.old_kot().clone())
.new_kot(new_kot)
.build()
.unwrap()
}
#[test]
fn test_event() {
get_updated_kot_event_from_command(&UpdateKotCommand::get_cmd());
}
}

View file

@ -14,12 +14,14 @@ pub mod add_order_command;
pub mod commands;
pub mod delete_line_item_command;
pub mod delete_order_command;
pub mod update_kot_command;
pub mod update_line_item_command;
pub mod update_order_command;
// events
pub mod events;
pub mod kot_added_event;
pub mod kot_updated_event;
pub mod line_item_added_event;
pub mod line_item_deleted_event;
pub mod line_item_updated_event;

View file

@ -0,0 +1,65 @@
// 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 UpdateKotCommand {
adding_by: Uuid,
#[builder(default = "OffsetDateTime::now_utc()")]
created_time: OffsetDateTime,
order_id: Uuid,
old_kot: Kot,
}
#[cfg(test)]
mod tests {
use time::macros::datetime;
use crate::utils::uuid::tests::UUID;
use super::*;
impl UpdateKotCommand {
pub fn get_cmd() -> Self {
let order_id = UUID;
let adding_by = UUID;
UpdateKotCommandBuilder::default()
.adding_by(adding_by)
.created_time(datetime!(1970-01-01 1:00 UTC))
.order_id(order_id)
.old_kot(Kot::default())
.build()
.unwrap()
}
}
#[test]
fn test_cmd() {
let order_id = UUID;
let adding_by = UUID;
let old_kot = Kot::default();
let cmd = UpdateKotCommandBuilder::default()
.adding_by(adding_by)
.order_id(order_id)
.old_kot(old_kot.clone())
.build()
.unwrap();
assert_eq!(*cmd.order_id(), order_id);
assert_eq!(*cmd.adding_by(), adding_by);
assert_eq!(cmd.old_kot(), &old_kot);
}
}