From ce3b024fea0d0d6535eea30737d883f2d8c35e91 Mon Sep 17 00:00:00 2001 From: Geometrically <18202329+Geometrically@users.noreply.github.com> Date: Mon, 5 Feb 2024 12:02:07 -0700 Subject: [PATCH] Fix gift cards (#877) * Fix gift card cashout * Fix mutex locks --- src/queue/payouts.rs | 24 ++++++++++-------------- src/routes/v3/payouts.rs | 11 +++-------- 2 files changed, 13 insertions(+), 22 deletions(-) diff --git a/src/queue/payouts.rs b/src/queue/payouts.rs index fe341616..d16988a9 100644 --- a/src/queue/payouts.rs +++ b/src/queue/payouts.rs @@ -1,4 +1,3 @@ -use crate::models::ids::UserId; use crate::models::payouts::{ PayoutDecimal, PayoutInterval, PayoutMethod, PayoutMethodFee, PayoutMethodType, }; @@ -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; @@ -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>, payout_options: RwLock>, - payouts_locks: DashMap>>, + pub payouts_locks: Mutex<()>, } #[derive(Clone)] @@ -49,7 +46,7 @@ impl PayoutsQueue { PayoutsQueue { credential: RwLock::new(None), payout_options: RwLock::new(None), - payouts_locks: DashMap::new(), + payouts_locks: Mutex::new(()), } } @@ -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) @@ -506,13 +509,6 @@ impl PayoutsQueue { Ok(options.options) } - - pub fn lock_user_payouts(&self, user_id: UserId) -> Arc> { - self.payouts_locks - .entry(user_id) - .or_insert_with(|| Arc::new(Mutex::new(()))) - .clone() - } } pub async fn process_payout( diff --git a/src/routes/v3/payouts.rs b/src/routes/v3/payouts.rs index 981384da..dc713ee2 100644 --- a/src/routes/v3/payouts.rs +++ b/src/routes/v3/payouts.rs @@ -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!( " @@ -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!( " @@ -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(