Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix gift cards #877

Merged
merged 2 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading