2022-04-20 16:09:09 +02:00
|
|
|
use actix::Message;
|
2022-05-06 11:47:18 +02:00
|
|
|
use config::SharedAppConfig;
|
|
|
|
use database_manager::{query_db, SharedDatabase};
|
|
|
|
use model::{AccountId, AccountOrder, OrderStatus, ShoppingCart, ShoppingCartId, ShoppingCartItem};
|
2022-04-20 14:30:59 +02:00
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! order_async_handler {
|
|
|
|
($msg: ty, $async: ident, $res: ty) => {
|
|
|
|
impl actix::Handler<$msg> for OrderManager {
|
|
|
|
type Result = actix::ResponseActFuture<Self, Result<$res>>;
|
|
|
|
|
|
|
|
fn handle(&mut self, msg: $msg, _ctx: &mut Self::Context) -> Self::Result {
|
|
|
|
use actix::WrapFuture;
|
|
|
|
let db = self.db.clone();
|
2022-04-28 15:54:18 +02:00
|
|
|
let config = self.config.clone();
|
|
|
|
Box::pin(async { $async(msg, db, config).await }.into_actor(self))
|
2022-04-20 14:30:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
2022-04-20 16:09:09 +02:00
|
|
|
pub enum Error {
|
|
|
|
#[error("Database actor failed")]
|
|
|
|
DatabaseInternal,
|
|
|
|
#[error("Shopping cart does not exists")]
|
|
|
|
ShoppingCart,
|
|
|
|
#[error("Failed to create account order")]
|
|
|
|
CreateAccountOrder,
|
|
|
|
}
|
2022-04-20 14:30:59 +02:00
|
|
|
|
|
|
|
pub type Result<T> = std::result::Result<T, Error>;
|
|
|
|
|
|
|
|
pub struct OrderManager {
|
|
|
|
db: SharedDatabase,
|
2022-04-28 15:54:18 +02:00
|
|
|
config: SharedAppConfig,
|
2022-04-20 14:30:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl actix::Actor for OrderManager {
|
|
|
|
type Context = actix::Context<Self>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl OrderManager {
|
2022-04-28 15:54:18 +02:00
|
|
|
pub fn new(config: SharedAppConfig, db: SharedDatabase) -> Self {
|
|
|
|
Self { db, config }
|
2022-04-20 14:30:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Message, Debug)]
|
|
|
|
#[rtype(result = "Result<AccountOrder>")]
|
|
|
|
pub struct CreateOrder {
|
2022-04-20 16:09:09 +02:00
|
|
|
pub account_id: AccountId,
|
2022-04-20 14:30:59 +02:00
|
|
|
pub shopping_cart_id: ShoppingCartId,
|
|
|
|
}
|
|
|
|
|
2022-04-20 16:09:09 +02:00
|
|
|
order_async_handler!(CreateOrder, create_order, AccountOrder);
|
|
|
|
|
2022-04-28 15:54:18 +02:00
|
|
|
pub(crate) async fn create_order(
|
|
|
|
msg: CreateOrder,
|
|
|
|
db: SharedDatabase,
|
|
|
|
_config: SharedAppConfig,
|
|
|
|
) -> Result<AccountOrder> {
|
2022-05-06 11:47:18 +02:00
|
|
|
let cart: ShoppingCart = query_db!(
|
|
|
|
db,
|
|
|
|
database_manager::FindShoppingCart {
|
2022-04-20 16:09:09 +02:00
|
|
|
id: msg.shopping_cart_id,
|
2022-05-06 11:47:18 +02:00
|
|
|
},
|
|
|
|
Error::ShoppingCart,
|
|
|
|
Error::DatabaseInternal
|
|
|
|
);
|
|
|
|
let items: Vec<ShoppingCartItem> = query_db!(
|
|
|
|
db,
|
|
|
|
database_manager::AccountShoppingCartItems {
|
2022-04-20 16:09:09 +02:00
|
|
|
account_id: cart.buyer_id,
|
|
|
|
shopping_cart_id: Some(cart.id),
|
2022-05-06 11:47:18 +02:00
|
|
|
},
|
|
|
|
Error::ShoppingCart,
|
|
|
|
Error::DatabaseInternal
|
|
|
|
);
|
|
|
|
let order = query_db!(
|
|
|
|
db,
|
|
|
|
database_manager::CreateAccountOrder {
|
2022-04-20 16:09:09 +02:00
|
|
|
shopping_cart_id: cart.id,
|
|
|
|
buyer_id: msg.account_id,
|
|
|
|
items: items
|
|
|
|
.into_iter()
|
2022-05-06 11:47:18 +02:00
|
|
|
.map(|item| database_manager::create_order::OrderItem {
|
2022-04-20 16:09:09 +02:00
|
|
|
product_id: item.product_id,
|
|
|
|
quantity: item.quantity,
|
|
|
|
quantity_unit: item.quantity_unit,
|
|
|
|
})
|
|
|
|
.collect(),
|
2022-05-06 11:47:18 +02:00
|
|
|
},
|
|
|
|
Error::CreateAccountOrder,
|
|
|
|
Error::DatabaseInternal
|
|
|
|
);
|
2022-04-20 16:09:09 +02:00
|
|
|
|
|
|
|
Ok(order)
|
2022-04-20 14:30:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn change(current: OrderStatus, next: OrderStatus) -> Option<OrderStatus> {
|
|
|
|
match (current, next) {
|
|
|
|
// paying
|
|
|
|
(OrderStatus::Confirmed, OrderStatus::Payed) => Some(OrderStatus::Payed),
|
|
|
|
|
|
|
|
// delivering
|
|
|
|
(OrderStatus::Confirmed | OrderStatus::Payed, OrderStatus::Delivered) => {
|
|
|
|
Some(OrderStatus::Delivered)
|
|
|
|
}
|
|
|
|
|
|
|
|
// cancelling
|
|
|
|
(OrderStatus::Confirmed, OrderStatus::Cancelled) => Some(OrderStatus::Cancelled),
|
|
|
|
(OrderStatus::Payed, OrderStatus::Cancelled) => Some(OrderStatus::RequireRefund),
|
|
|
|
(OrderStatus::Payed, OrderStatus::RequireRefund) => Some(OrderStatus::RequireRefund),
|
|
|
|
(OrderStatus::RequireRefund, OrderStatus::Refunded) => Some(OrderStatus::Refunded),
|
|
|
|
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|