bazzar/crates/channels/src/carts.rs
2022-12-20 15:34:20 +01:00

164 lines
4.5 KiB
Rust

pub static CLIENT_NAME: &str = "carts";
pub enum Topic {}
#[derive(Debug, Clone, thiserror::Error, serde::Serialize, serde::Deserialize)]
pub enum Error {
#[error("Internal server error")]
InternalServerError,
#[error("Failed to load account shopping carts")]
NoCarts,
#[error("Account does not have active shopping cart")]
NoActiveCart,
#[error("Failed to delete item {0:?}")]
DeleteItem(model::ShoppingCartItemId),
#[error("Failed to modify item {0:?}")]
ModifyItem(model::ShoppingCartItemId),
#[error("Failed to create item")]
CreateItem,
#[error("Failed to modify cart {0:?}")]
ModifyCart(model::ShoppingCartId),
#[error("Failed to load cart {0:?} items")]
LoadItems(model::ShoppingCartId),
}
pub mod remove_product {
use super::Error;
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Input {
pub shopping_cart_id: model::ShoppingCartId,
pub shopping_cart_item_id: model::ShoppingCartItemId,
}
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Details {
pub item: model::ShoppingCartItem,
}
pub type Output = Result<Details, Error>;
}
pub mod modify_item {
use model::v2::ProductVariantId;
use model::ProductId;
use super::Error;
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Input {
pub buyer_id: model::AccountId,
pub product_variant_id: ProductVariantId,
pub product_id: ProductId,
pub quantity: model::Quantity,
pub quantity_unit: model::QuantityUnit,
}
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Details {
pub item: model::ShoppingCartItem,
}
pub type Output = Result<Details, Error>;
}
pub mod modify_cart {
use super::{modify_item, Error};
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Input {
pub buyer_id: model::AccountId,
pub items: Vec<modify_item::Input>,
pub checkout_notes: String,
pub payment_method: Option<model::PaymentMethod>,
}
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct CartDetails {
pub cart_id: model::ShoppingCartId,
pub items: Vec<model::ShoppingCartItem>,
pub checkout_notes: String,
pub payment_method: model::PaymentMethod,
}
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Details {
pub cart: CartDetails,
}
pub type Output = Result<Details, Error>;
}
pub mod active_shopping_cart {
use super::Error;
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Input {
pub buyer_id: model::AccountId,
}
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Details {
pub cart: model::ShoppingCart,
}
pub type Output = Result<Details, Error>;
}
pub mod rpc {
use config::SharedAppConfig;
use super::{active_shopping_cart, modify_cart, modify_item, remove_product};
#[tarpc::service]
pub trait Carts {
/// Change shopping cart item.
async fn modify_item(input: modify_item::Input) -> modify_item::Output;
/// Change entire shopping cart content.
async fn modify_cart(input: modify_cart::Input) -> modify_cart::Output;
/// Remove product from shopping cart.
async fn remove_product(input: remove_product::Input) -> remove_product::Output;
async fn active_shopping_cart(
input: active_shopping_cart::Input,
) -> active_shopping_cart::Output;
}
pub async fn create_client(config: SharedAppConfig) -> CartsClient {
use tarpc::client;
use tarpc::tokio_serde::formats::Bincode;
let addr = {
let l = config.lock();
(
l.account_manager().rpc_bind.clone(),
l.account_manager().rpc_port,
)
};
let transport = tarpc::serde_transport::tcp::connect(addr, Bincode::default);
let client = CartsClient::new(
client::Config::default(),
transport.await.expect("Failed to connect to server"),
)
.spawn();
client
}
}
pub mod mqtt {
use config::SharedAppConfig;
use rumqttc::EventLoop;
use super::CLIENT_NAME;
use crate::AsyncClient;
pub fn create_client(config: SharedAppConfig) -> (AsyncClient, EventLoop) {
crate::mqtt::create_client(CLIENT_NAME, config.lock().cart_manager().mqtt_addr())
}
}