mirror of
https://github.com/modrinth/code.git
synced 2026-09-03 13:36:48 +00:00
more logging to index billing
This commit is contained in:
@@ -481,7 +481,7 @@ impl PayoutsQueue {
|
|||||||
Ok(options.options)
|
Ok(options.options)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_brex_balance() -> eyre::Result<Option<AccountBalance>> {
|
pub async fn get_brex_balance() -> eyre::Result<AccountBalance> {
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct BrexBalance {
|
struct BrexBalance {
|
||||||
pub amount: i64,
|
pub amount: i64,
|
||||||
@@ -510,7 +510,7 @@ impl PayoutsQueue {
|
|||||||
.await
|
.await
|
||||||
.wrap_request_err("reading `accounts/cash` request")?;
|
.wrap_request_err("reading `accounts/cash` request")?;
|
||||||
|
|
||||||
Ok(Some(AccountBalance {
|
Ok(AccountBalance {
|
||||||
available: Decimal::from(
|
available: Decimal::from(
|
||||||
res.items
|
res.items
|
||||||
.iter()
|
.iter()
|
||||||
@@ -525,10 +525,10 @@ impl PayoutsQueue {
|
|||||||
})
|
})
|
||||||
.sum::<i64>(),
|
.sum::<i64>(),
|
||||||
) / Decimal::from(100),
|
) / Decimal::from(100),
|
||||||
}))
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_paypal_balance() -> eyre::Result<Option<AccountBalance>> {
|
pub async fn get_paypal_balance() -> eyre::Result<AccountBalance> {
|
||||||
let api_username = &ENV.PAYPAL_NVP_USERNAME;
|
let api_username = &ENV.PAYPAL_NVP_USERNAME;
|
||||||
let api_password = &ENV.PAYPAL_NVP_PASSWORD;
|
let api_password = &ENV.PAYPAL_NVP_PASSWORD;
|
||||||
let api_signature = &ENV.PAYPAL_NVP_SIGNATURE;
|
let api_signature = &ENV.PAYPAL_NVP_SIGNATURE;
|
||||||
@@ -560,28 +560,23 @@ impl PayoutsQueue {
|
|||||||
let mut key_value_map = HashMap::new();
|
let mut key_value_map = HashMap::new();
|
||||||
|
|
||||||
for pair in body.split('&') {
|
for pair in body.split('&') {
|
||||||
let mut iter = pair.splitn(2, '=');
|
if let Some((key, value)) = pair.split_once('=') {
|
||||||
if let (Some(key), Some(value)) = (iter.next(), iter.next()) {
|
|
||||||
key_value_map.insert(key.to_string(), value.to_string());
|
key_value_map.insert(key.to_string(), value.to_string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(amount) = key_value_map
|
let amount =
|
||||||
.get("L_AMT0")
|
key_value_map.get("L_AMT0").wrap_err("missing `L_AMT0`")?;
|
||||||
.and_then(|x| Decimal::from_str_exact(x).ok())
|
let amount = Decimal::from_str_exact(amount)
|
||||||
{
|
.wrap_err("cannot parse `L_AMT0` as decimal")?;
|
||||||
Ok(Some(AccountBalance {
|
|
||||||
available: amount,
|
Ok(AccountBalance {
|
||||||
pending: Decimal::ZERO,
|
available: amount,
|
||||||
}))
|
pending: Decimal::ZERO,
|
||||||
} else {
|
})
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_tremendous_balance(
|
pub async fn get_tremendous_balance(&self) -> eyre::Result<AccountBalance> {
|
||||||
&self,
|
|
||||||
) -> eyre::Result<Option<AccountBalance>> {
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct FundingSourceMeta {
|
struct FundingSourceMeta {
|
||||||
available_cents: Option<u64>,
|
available_cents: Option<u64>,
|
||||||
@@ -606,18 +601,22 @@ impl PayoutsQueue {
|
|||||||
None,
|
None,
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.wrap_request_err("fetching funding sources")?;
|
.wrap_err("fetching funding sources")?;
|
||||||
|
|
||||||
Ok(val
|
let funding_source = val
|
||||||
.funding_sources
|
.funding_sources
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.find(|x| x.method == "balance")
|
.find(|x| x.method == "balance")
|
||||||
.map(|x| AccountBalance {
|
.wrap_err("no balance funding source")?;
|
||||||
available: Decimal::from(x.meta.available_cents.unwrap_or(0))
|
|
||||||
/ Decimal::from(100),
|
Ok(AccountBalance {
|
||||||
pending: Decimal::from(x.meta.pending_cents.unwrap_or(0))
|
available: Decimal::from(
|
||||||
/ Decimal::from(100),
|
funding_source.meta.available_cents.unwrap_or(0),
|
||||||
}))
|
) / Decimal::from(100),
|
||||||
|
pending: Decimal::from(
|
||||||
|
funding_source.meta.pending_cents.unwrap_or(0),
|
||||||
|
) / Decimal::from(100),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1282,30 +1281,30 @@ pub async fn insert_bank_balances_and_webhook(
|
|||||||
let now = Utc::now();
|
let now = Utc::now();
|
||||||
let today = now.date_naive().and_time(NaiveTime::MIN).and_utc();
|
let today = now.date_naive().and_time(NaiveTime::MIN).and_utc();
|
||||||
|
|
||||||
let mut add_balance = |account_type: &str, balance: &AccountBalance| {
|
let mut add_balance =
|
||||||
insert_account_types.push(account_type.to_string());
|
|account_type: &str,
|
||||||
insert_amounts.push(balance.available);
|
balance: Result<&AccountBalance, &eyre::Report>| match balance
|
||||||
insert_pending.push(false);
|
{
|
||||||
insert_recorded.push(today);
|
Ok(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_account_types.push(account_type.to_string());
|
||||||
insert_amounts.push(balance.pending);
|
insert_amounts.push(balance.pending);
|
||||||
insert_pending.push(true);
|
insert_pending.push(true);
|
||||||
insert_recorded.push(today);
|
insert_recorded.push(today);
|
||||||
};
|
}
|
||||||
|
Err(err) => {
|
||||||
|
warn!("Failed to check balance for '{account_type}': {err:?}");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
if let Ok(Some(ref paypal)) = paypal_result {
|
add_balance("paypal", paypal_result.as_ref());
|
||||||
add_balance("paypal", paypal);
|
add_balance("brex", brex_result.as_ref());
|
||||||
}
|
add_balance("tremendous", tremendous_result.as_ref());
|
||||||
if let Ok(Some(ref brex)) = brex_result {
|
add_balance("mural", mural_result.as_ref());
|
||||||
add_balance("brex", brex);
|
|
||||||
}
|
|
||||||
if let Ok(Some(ref tremendous)) = tremendous_result {
|
|
||||||
add_balance("tremendous", tremendous);
|
|
||||||
}
|
|
||||||
if let Ok(Some(ref mural)) = mural_result {
|
|
||||||
add_balance("mural", mural);
|
|
||||||
}
|
|
||||||
|
|
||||||
let inserted = sqlx::query_scalar!(
|
let inserted = sqlx::query_scalar!(
|
||||||
r#"
|
r#"
|
||||||
@@ -1362,13 +1361,13 @@ pub async fn insert_bank_balances_and_webhook(
|
|||||||
async fn check_balance_with_webhook(
|
async fn check_balance_with_webhook(
|
||||||
source: &str,
|
source: &str,
|
||||||
threshold: u64,
|
threshold: u64,
|
||||||
result: eyre::Result<Option<AccountBalance>>,
|
result: eyre::Result<AccountBalance>,
|
||||||
) -> eyre::Result<Option<AccountBalance>> {
|
) -> eyre::Result<()> {
|
||||||
let maybe_threshold = if threshold > 0 { Some(threshold) } else { None };
|
let maybe_threshold = if threshold > 0 { Some(threshold) } else { None };
|
||||||
let payout_alert_webhook = &ENV.PAYOUT_ALERT_SLACK_WEBHOOK;
|
let payout_alert_webhook = &ENV.PAYOUT_ALERT_SLACK_WEBHOOK;
|
||||||
|
|
||||||
match &result {
|
match &result {
|
||||||
Ok(Some(account_balance)) => {
|
Ok(account_balance) => {
|
||||||
if let Some(threshold) = maybe_threshold
|
if let Some(threshold) = maybe_threshold
|
||||||
&& let Some(available) =
|
&& let Some(available) =
|
||||||
account_balance.available.trunc().to_u64()
|
account_balance.available.trunc().to_u64()
|
||||||
@@ -1385,7 +1384,6 @@ async fn check_balance_with_webhook(
|
|||||||
.await?;
|
.await?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
// use compact single-line error repr here
|
// use compact single-line error repr here
|
||||||
error!(
|
error!(
|
||||||
@@ -1405,11 +1403,9 @@ async fn check_balance_with_webhook(
|
|||||||
.await?;
|
.await?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(result.ok().flatten())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
@@ -104,9 +104,7 @@ impl PayoutsQueue {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_mural_balance(
|
pub async fn get_mural_balance(&self) -> eyre::Result<AccountBalance> {
|
||||||
&self,
|
|
||||||
) -> eyre::Result<Option<AccountBalance>> {
|
|
||||||
let muralpay = self.muralpay.load();
|
let muralpay = self.muralpay.load();
|
||||||
let muralpay = muralpay
|
let muralpay = muralpay
|
||||||
.as_ref()
|
.as_ref()
|
||||||
@@ -131,10 +129,10 @@ impl PayoutsQueue {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
.sum::<Decimal>();
|
.sum::<Decimal>();
|
||||||
Ok(Some(AccountBalance {
|
Ok(AccountBalance {
|
||||||
available,
|
available,
|
||||||
pending: Decimal::ZERO,
|
pending: Decimal::ZERO,
|
||||||
}))
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user