You've already forked AstralRinth
forked from didirus/AstralRinth
Add bank balances to DB (#3860)
This commit is contained in:
@@ -5,7 +5,7 @@ use crate::models::payouts::{
|
||||
use crate::models::projects::MonetizationStatus;
|
||||
use crate::routes::ApiError;
|
||||
use base64::Engine;
|
||||
use chrono::{DateTime, Datelike, Duration, TimeZone, Utc};
|
||||
use chrono::{DateTime, Datelike, Duration, NaiveTime, TimeZone, Utc};
|
||||
use dashmap::DashMap;
|
||||
use futures::TryStreamExt;
|
||||
use reqwest::Method;
|
||||
@@ -1072,3 +1072,61 @@ pub async fn insert_payouts(
|
||||
.execute(&mut **transaction)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn insert_bank_balances(
|
||||
payouts: &PayoutsQueue,
|
||||
pool: &PgPool,
|
||||
) -> Result<(), ApiError> {
|
||||
let mut transaction = pool.begin().await?;
|
||||
|
||||
let (paypal, brex, tremendous) = futures::future::try_join3(
|
||||
PayoutsQueue::get_paypal_balance(),
|
||||
PayoutsQueue::get_brex_balance(),
|
||||
payouts.get_tremendous_balance(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
let mut insert_account_types = Vec::new();
|
||||
let mut insert_amounts = Vec::new();
|
||||
let mut insert_pending = Vec::new();
|
||||
let mut insert_recorded = Vec::new();
|
||||
|
||||
let now = Utc::now();
|
||||
let today = now.date_naive().and_time(NaiveTime::MIN).and_utc();
|
||||
|
||||
let mut add_balance =
|
||||
|account_type: &str, balance: Option<AccountBalance>| {
|
||||
if let Some(balance) = balance {
|
||||
insert_account_types.push(account_type.to_string());
|
||||
insert_amounts.push(balance.available);
|
||||
insert_pending.push(false);
|
||||
insert_recorded.push(today);
|
||||
|
||||
insert_account_types.push(account_type.to_string());
|
||||
insert_amounts.push(balance.pending);
|
||||
insert_pending.push(true);
|
||||
insert_recorded.push(today);
|
||||
}
|
||||
};
|
||||
|
||||
add_balance("paypal", paypal);
|
||||
add_balance("brex", brex);
|
||||
add_balance("tremendous", tremendous);
|
||||
|
||||
sqlx::query!(
|
||||
"
|
||||
INSERT INTO payout_sources_balance (account_type, amount, pending, recorded)
|
||||
SELECT * FROM UNNEST ($1::text[], $2::numeric[], $3::boolean[], $4::timestamptz[])
|
||||
ON CONFLICT (recorded, account_type, pending)
|
||||
DO UPDATE SET amount = EXCLUDED.amount
|
||||
",
|
||||
&insert_account_types[..],
|
||||
&insert_amounts[..],
|
||||
&insert_pending[..],
|
||||
&insert_recorded[..],
|
||||
)
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user