Skip to content

Commit

Permalink
Improve charge api.
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Sep 6, 2023
1 parent daa1f28 commit 5cebec2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "walletbase"
version = "0.3.0"
version = "0.3.1"
edition = "2021"
rust-version = "1.64"
description = ""
Expand Down
28 changes: 20 additions & 8 deletions src/api/charge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ use crate::db;
#[derive(Debug, Deserialize, Validate)]
pub struct ChargeInput {
pub uid: PackObject<xid::Id>,
pub currency: String,
#[validate(range(min = 10))]
pub amount: i64,
#[validate(range(min = 10, max = 1_000_000))]
pub quantity: i64,
#[validate(length(min = 1), custom = "validate_provider")]
pub provider: String, // stripe
#[validate(range(min = 10, max = 1_000_000))]
pub quantity: i64,
pub currency: Option<String>,
#[validate(range(min = 1))]
pub amount: Option<i64>,
pub charge_id: Option<String>,
pub charge_payload: Option<PackObject<Vec<u8>>>,
}
Expand Down Expand Up @@ -103,7 +103,6 @@ pub async fn create(
) -> Result<PackObject<SuccessResponse<ChargeOutput>>, HTTPError> {
let (to, input) = to.unpack();
input.validate()?;
let cur = Currency::from_str(&input.currency)?;

let uid = input.uid.unwrap();
ctx.set_kvs(vec![
Expand All @@ -119,12 +118,20 @@ pub async fn create(
let mut doc = db::Charge {
uid,
quantity: input.quantity,
currency: cur.alpha.to_lowercase(),
amount: input.amount,
provider: input.provider,
..Default::default()
};

if let Some(amount) = input.amount {
let cur = Currency::from_str(
&input
.currency
.ok_or(HTTPError::new(400, "currency required".to_string()))?,
)?;
doc.amount = amount;
doc.currency = cur.alpha.to_lowercase();
}

if let Some(charge_id) = input.charge_id {
ctx.set("charge_id", charge_id.clone().into()).await;
doc.status = 1;
Expand Down Expand Up @@ -212,6 +219,11 @@ pub struct UpdateChargeInput {
pub current_status: i8,
#[validate(range(min = -2, max = 1))]
pub status: Option<i8>,
pub currency: Option<String>,
#[validate(range(min = 1))]
pub amount: Option<i64>,
#[validate(range(min = 1))]
pub amount_refunded: Option<i64>,
pub charge_id: Option<String>,
pub charge_payload: Option<PackObject<Vec<u8>>>,
pub failure_code: Option<String>,
Expand Down
2 changes: 2 additions & 0 deletions src/db/model_charge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ impl Charge {
) -> anyhow::Result<bool> {
let valid_fields = [
"status",
"currency",
"amount",
"amount_refunded",
"charge_id",
"charge_payload",
Expand Down

0 comments on commit 5cebec2

Please sign in to comment.