add order request submission from customer

This commit is contained in:
creating2morrow 2023-06-24 03:41:10 -04:00
parent d8374976c9
commit 2746e7345d
2 changed files with 84 additions and 24 deletions

View file

@ -1,3 +1,5 @@
use std::error::Error;
use crate::{ use crate::{
contact, contact,
db, db,
@ -253,7 +255,7 @@ pub async fn upload_delivery_info(orid: &String, delivery_info: &Vec<u8>) {
db::Interface::async_write(&s.env, &s.handle, &m_order.orid, &Order::to_db(&m_order)).await; db::Interface::async_write(&s.env, &s.handle, &m_order.orid, &Order::to_db(&m_order)).await;
} }
/// The vendor will first search for a encrypted multisig message in the form /// The vendor will first search for an encrypted multisig message in the form
/// ///
/// txset-{order id}-{.b32.i2p} /// txset-{order id}-{.b32.i2p}
pub async fn finalize_order(orid: &String) -> reqres::FinalizeOrderResponse { pub async fn finalize_order(orid: &String) -> reqres::FinalizeOrderResponse {
@ -294,3 +296,29 @@ pub async fn finalize_order(orid: &String) -> reqres::FinalizeOrderResponse {
delivery_info, delivery_info,
} }
} }
/// Send order request to vendor and start multisig flow
pub async fn transmit_order_request(contact: String, request: reqres::OrderRequest) -> Result<Order, Box<dyn Error>> {
let host = utils::get_i2p_http_proxy();
let proxy = reqwest::Proxy::http(&host)?;
let client = reqwest::Client::builder().proxy(proxy).build();
match client?
.post(format!("http://{}/market/order/create", contact))
.json(&request)
.send()
.await
{
Ok(response) => {
let res = response.json::<Order>().await;
debug!("create order response: {:?}", res);
match res {
Ok(r) => Ok(r),
_ => Ok(Default::default()),
}
}
Err(e) => {
error!("failed to generate order due to: {:?}", e);
Ok(Default::default())
}
}
}

View file

@ -43,7 +43,10 @@ pub struct MarketApp {
new_order_shipping_address: String, new_order_shipping_address: String,
_refresh_on_delete_product_tx: Sender<bool>, _refresh_on_delete_product_tx: Sender<bool>,
_refresh_on_delete_product_rx: Receiver<bool>, _refresh_on_delete_product_rx: Receiver<bool>,
submit_order_tx: Sender<models::Order>,
submit_order_rx: Receiver<models::Order>,
s_contact: models::Contact, s_contact: models::Contact,
s_order: models::Order,
vendor_status: utils::ContactStatus, vendor_status: utils::ContactStatus,
vendors: Vec<models::Contact>, vendors: Vec<models::Contact>,
} }
@ -60,6 +63,7 @@ impl Default for MarketApp {
let (contact_info_tx, contact_info_rx) = std::sync::mpsc::channel(); let (contact_info_tx, contact_info_rx) = std::sync::mpsc::channel();
let (get_vendor_products_tx, get_vendor_products_rx) = std::sync::mpsc::channel(); let (get_vendor_products_tx, get_vendor_products_rx) = std::sync::mpsc::channel();
let (get_vendor_product_tx, get_vendor_product_rx) = std::sync::mpsc::channel(); let (get_vendor_product_tx, get_vendor_product_rx) = std::sync::mpsc::channel();
let (submit_order_tx, submit_order_rx) = std::sync::mpsc::channel();
MarketApp { MarketApp {
contact_info_rx, contact_info_rx,
contact_info_tx, contact_info_tx,
@ -104,6 +108,9 @@ impl Default for MarketApp {
_refresh_on_delete_product_tx, _refresh_on_delete_product_tx,
_refresh_on_delete_product_rx, _refresh_on_delete_product_rx,
s_contact: Default::default(), s_contact: Default::default(),
s_order: Default::default(),
submit_order_rx,
submit_order_tx,
vendor_status: Default::default(), vendor_status: Default::default(),
vendors: Vec::new(), vendors: Vec::new(),
} }
@ -115,6 +122,14 @@ impl eframe::App for MarketApp {
// Hook into async channel threads // Hook into async channel threads
//----------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------
if let Ok(submit_order) = self.submit_order_rx.try_recv() {
self.s_order = submit_order;
if self.s_order.orid != utils::empty_string() {
self.is_ordering = false;
self.is_loading = false;
}
}
if let Ok(contact_info) = self.contact_info_rx.try_recv() { if let Ok(contact_info) = self.contact_info_rx.try_recv() {
self.s_contact = contact_info; self.s_contact = contact_info;
if self.s_contact.xmr_address != utils::empty_string() { if self.s_contact.xmr_address != utils::empty_string() {
@ -190,11 +205,19 @@ impl eframe::App for MarketApp {
.labelled_by(qty_name.id); .labelled_by(qty_name.id);
}); });
ui.label(format!("price: {}", self.new_order_price)); ui.label(format!("price: {}", self.new_order_price));
if ui.button("Submit Order").clicked() {
let qty = match self.new_order_quantity.parse::<u128>() { let qty = match self.new_order_quantity.parse::<u128>() {
Ok(q) => q, Ok(q) => q,
Err(_) => 0, Err(_) => 0,
}; };
let mut p_qty: u128 = 0;
for p in &self.products {
if p.pid == self.new_order.pid {
p_qty = p.qty;
break;
}
}
if qty <= p_qty && qty > 0 {
if ui.button("Submit Order").clicked() {
let address_bytes = self.new_order_shipping_address.clone().into_bytes(); let address_bytes = self.new_order_shipping_address.clone().into_bytes();
let encrypted_shipping_address = let encrypted_shipping_address =
gpg::encrypt(self.vendor_status.i2p.clone(), &address_bytes); gpg::encrypt(self.vendor_status.i2p.clone(), &address_bytes);
@ -205,15 +228,14 @@ impl eframe::App for MarketApp {
quantity: qty, quantity: qty,
..Default::default() ..Default::default()
}; };
let _j_order = utils::order_to_json(&new_order); self.is_loading = true;
submit_order_req(self.submit_order_tx.clone(), ctx.clone(), new_order);
// create order channel
self.new_order = Default::default(); self.new_order = Default::default();
self.new_order_price = 0; self.new_order_price = 0;
self.new_order_quantity = utils::empty_string(); self.new_order_quantity = utils::empty_string();
self.new_order_shipping_address = utils::empty_string(); self.new_order_shipping_address = utils::empty_string();
} }
}
ui.label("\n"); ui.label("\n");
if ui.button("Exit").clicked() { if ui.button("Exit").clicked() {
self.is_ordering = false; self.is_ordering = false;
@ -905,3 +927,13 @@ fn vendor_status_timeout(tx: Sender<bool>, ctx: egui::Context) {
ctx.request_repaint(); ctx.request_repaint();
}); });
} }
fn submit_order_req(tx: Sender<models::Order>, ctx: egui::Context, request: reqres::OrderRequest) {
tokio::spawn(async move {
log::info!("submit order");
let contact = String::from(&request.cid);
let order = order::transmit_order_request(contact, request).await;
let _ = tx.send(order.unwrap_or(Default::default()));
ctx.request_repaint();
});
}