Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
Fix gift cards (#877)
Browse files Browse the repository at this point in the history
* Fix gift card cashout

* Fix mutex locks
  • Loading branch information
Geometrically authored Feb 5, 2024
1 parent a02aa75 commit ce3b024
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 22 deletions.
24 changes: 10 additions & 14 deletions src/queue/payouts.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::models::ids::UserId;
use crate::models::payouts::{
PayoutDecimal, PayoutInterval, PayoutMethod, PayoutMethodFee, PayoutMethodType,
};
Expand All @@ -7,7 +6,6 @@ use crate::util::env::parse_var;
use crate::{database::redis::RedisPool, models::projects::MonetizationStatus};
use base64::Engine;
use chrono::{DateTime, Datelike, Duration, Utc, Weekday};
use dashmap::DashMap;
use reqwest::Method;
use rust_decimal::Decimal;
use serde::de::DeserializeOwned;
Expand All @@ -16,13 +14,12 @@ use serde_json::Value;
use sqlx::postgres::PgQueryResult;
use sqlx::PgPool;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::{Mutex, RwLock};

pub struct PayoutsQueue {
credential: RwLock<Option<PayPalCredentials>>,
payout_options: RwLock<Option<PayoutMethods>>,
payouts_locks: DashMap<UserId, Arc<Mutex<()>>>,
pub payouts_locks: Mutex<()>,
}

#[derive(Clone)]
Expand All @@ -49,7 +46,7 @@ impl PayoutsQueue {
PayoutsQueue {
credential: RwLock::new(None),
payout_options: RwLock::new(None),
payouts_locks: DashMap::new(),
payouts_locks: Mutex::new(()),
}
}

Expand Down Expand Up @@ -346,8 +343,14 @@ impl PayoutsQueue {
"OEFTMSBA5ELH",
"A3CQK6UHNV27",
];
const SUPPORTED_METHODS: &[&str] =
&["merchant_cards", "visa", "bank", "ach", "visa_card"];
const SUPPORTED_METHODS: &[&str] = &[
"merchant_cards",
"merchant_card",
"visa",
"bank",
"ach",
"visa_card",
];

if !SUPPORTED_METHODS.contains(&&*product.category)
|| BLACKLISTED_IDS.contains(&&*product.id)
Expand Down Expand Up @@ -506,13 +509,6 @@ impl PayoutsQueue {

Ok(options.options)
}

pub fn lock_user_payouts(&self, user_id: UserId) -> Arc<Mutex<()>> {
self.payouts_locks
.entry(user_id)
.or_insert_with(|| Arc::new(Mutex::new(())))
.clone()
}
}

pub async fn process_payout(
Expand Down
11 changes: 3 additions & 8 deletions src/routes/v3/payouts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,7 @@ pub async fn paypal_webhook(
.await?;

if let Some(result) = result {
let mtx =
payouts.lock_user_payouts(crate::models::ids::UserId(result.user_id as u64));
let _guard = mtx.lock().await;
let _guard = payouts.payouts_locks.lock().await;

sqlx::query!(
"
Expand Down Expand Up @@ -249,9 +247,7 @@ pub async fn tremendous_webhook(
.await?;

if let Some(result) = result {
let mtx =
payouts.lock_user_payouts(crate::models::ids::UserId(result.user_id as u64));
let _guard = mtx.lock().await;
let _guard = payouts.payouts_locks.lock().await;

sqlx::query!(
"
Expand Down Expand Up @@ -371,8 +367,7 @@ pub async fn create_payout(
));
}

let mtx = payouts_queue.lock_user_payouts(user.id.into());
let _guard = mtx.lock().await;
let _guard = payouts_queue.payouts_locks.lock().await;

if user.balance < body.amount || body.amount < Decimal::ZERO {
return Err(ApiError::InvalidInput(
Expand Down

0 comments on commit ce3b024

Please sign in to comment.