webhooks
This commit is contained in:
parent
37475460e2
commit
02966c0605
9 changed files with 340 additions and 13 deletions
|
@ -4,12 +4,15 @@ use serde::Deserialize;
|
|||
pub struct Config {
|
||||
/// Allowed tokens for access
|
||||
pub allowed_tokens: Vec<String>,
|
||||
/// Webhook Config
|
||||
pub webhook: Option<Webhook>,
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
allowed_tokens: Vec::new(),
|
||||
webhook: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -21,3 +24,17 @@ pub fn get_config() -> Config {
|
|||
|
||||
Config::default()
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct Webhook {
|
||||
pub item_below_minimum: Option<String>,
|
||||
pub transaction_added: Option<String>,
|
||||
pub transaction_consumed: Option<String>,
|
||||
}
|
||||
|
||||
impl Webhook {
|
||||
pub async fn send(url: &str, data: &serde_json::Value) {
|
||||
let client = reqwest::Client::new();
|
||||
client.post(url).json(data).send().await.unwrap();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
use mongod::{Model, ToAPI};
|
||||
use rocket::serde::json::Json;
|
||||
use rocket::{get, post, State};
|
||||
use serde::Deserialize;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::check_auth;
|
||||
use crate::config::Config;
|
||||
use crate::config::{Config, Webhook};
|
||||
use crate::item::Item;
|
||||
use crate::routes::Token;
|
||||
use crate::variant::Variant;
|
||||
use crate::{
|
||||
|
@ -26,7 +28,7 @@ pub struct DemandForm {
|
|||
pub async fn demand_route(f: Json<DemandForm>, t: Token, c: &State<Config>) -> FallibleApiResponse {
|
||||
check_auth!(t, c);
|
||||
|
||||
Variant::demand(
|
||||
let transaction = Variant::demand(
|
||||
&f.uuid,
|
||||
f.price
|
||||
.clone()
|
||||
|
@ -37,6 +39,33 @@ pub async fn demand_route(f: Json<DemandForm>, t: Token, c: &State<Config>) -> F
|
|||
.await
|
||||
.ok_or_else(|| api_error("Demand failed"))?;
|
||||
|
||||
if let Some(hook) = &c.webhook {
|
||||
if let Some(url) = &hook.transaction_consumed {
|
||||
Webhook::send(url, &transaction.api().await).await;
|
||||
}
|
||||
|
||||
if let Some(url) = &hook.item_below_minimum {
|
||||
let variant = Item::get(&transaction.item)
|
||||
.await
|
||||
.unwrap()
|
||||
.variant(&transaction.variant)
|
||||
.unwrap();
|
||||
let min_res = variant.is_below_min().await;
|
||||
if min_res.0 {
|
||||
Webhook::send(
|
||||
url,
|
||||
&json!({
|
||||
"item": transaction.item,
|
||||
"variant": transaction.variant,
|
||||
"minimum": variant.min,
|
||||
"current_amount": variant.min.unwrap() - min_res.1
|
||||
}),
|
||||
)
|
||||
.await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(json!({"ok": 1}))
|
||||
}
|
||||
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
use mongod::ToAPI;
|
||||
use rocket::serde::json::Json;
|
||||
use rocket::{get, post, State};
|
||||
use serde::Deserialize;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::check_auth;
|
||||
use crate::config::Config;
|
||||
use crate::config::{Config, Webhook};
|
||||
use crate::routes::Token;
|
||||
use crate::{
|
||||
db::ItemDB,
|
||||
|
@ -38,7 +39,7 @@ pub async fn supply_route(
|
|||
.variant(&form.variant)
|
||||
.ok_or_else(variant_does_not_exist_error)?;
|
||||
|
||||
let transaction_id = variant
|
||||
let transaction = variant
|
||||
.supply(
|
||||
form.price
|
||||
.clone()
|
||||
|
@ -49,7 +50,13 @@ pub async fn supply_route(
|
|||
)
|
||||
.await;
|
||||
|
||||
Ok(json!({"uuid": transaction_id}))
|
||||
if let Some(hook) = &c.webhook {
|
||||
if let Some(url) = &hook.transaction_added {
|
||||
Webhook::send(url, &transaction.api().await).await;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(json!({"uuid": transaction._id}))
|
||||
}
|
||||
|
||||
/// Returns a list of Transaction UUIDs for the Item Variant
|
||||
|
|
|
@ -77,7 +77,7 @@ impl Transaction {
|
|||
}
|
||||
|
||||
/// Consumes the Item with `price` and `destination`
|
||||
pub async fn consume(self, price: Price, destination: &str) {
|
||||
pub async fn consume(self, price: Price, destination: &str) -> Self {
|
||||
self.change()
|
||||
.consumed(Some(Consumed {
|
||||
destination: destination.to_string(),
|
||||
|
@ -86,7 +86,7 @@ impl Transaction {
|
|||
}))
|
||||
.update()
|
||||
.await
|
||||
.unwrap();
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
pub async fn is_expired(&self) -> bool {
|
||||
|
|
|
@ -125,11 +125,11 @@ impl Variant {
|
|||
ret
|
||||
}
|
||||
|
||||
pub async fn demand(uuid: &str, price: Price, destination: &str) -> Option<()> {
|
||||
pub async fn demand(uuid: &str, price: Price, destination: &str) -> Option<Transaction> {
|
||||
// check if transaction exists
|
||||
let mut t = Transaction::get(uuid).await?;
|
||||
t.consume(price, destination).await;
|
||||
Some(())
|
||||
t = t.consume(price, destination).await;
|
||||
Some(t)
|
||||
}
|
||||
|
||||
/// Records a supply transaction in the database.
|
||||
|
@ -147,12 +147,12 @@ impl Variant {
|
|||
price: Price,
|
||||
origin: Option<&str>,
|
||||
location: Option<&str>,
|
||||
) -> String {
|
||||
) -> Transaction {
|
||||
let t = Transaction::new(&self.item, &self.variant, price, origin, location).await;
|
||||
|
||||
t.insert().await.unwrap();
|
||||
|
||||
t._id
|
||||
t
|
||||
}
|
||||
|
||||
pub async fn get_all_transactions(&self) -> Vec<Transaction> {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue