Files
AstralRinth/apps/labrinth/src/database/models/payout_item.rs
T
aecsocket 17f395ee55 Mural Pay integration (#4520)
* wip: muralpay integration

* Basic Mural Pay API bindings

* Fix clippy

* use dotenvy in muralpay example

* Refactor payout creation code

* wip: muralpay payout requests

* Mural Pay payouts work

* Fix clippy

* add mural pay fees API

* Work on payout fee API

* Fees API for more payment methods

* Fix CI

* Temporarily disable Venmo and PayPal methods from frontend

* wip: counterparties

* Start on counterparties and payment methods API

* Mural Pay multiple methods when fetching

* Don't send supported_countries to frontend

* Add countries to muralpay fiat methods

* Compile fix

* Add exchange rate info to fees endpoint

* Add fees to premium Tremendous options

* Add delivery email field to Tremendous payouts

* Add Tremendous product category to payout methods

* Add bank details API to muralpay

* Fix CI

* Fix CI

* Remove prepaid visa, compute fees properly for Tremendous methods

* Add more details to Tremendous errors

* Add fees to Mural

* Payout history route and bank details

* Re-add legacy PayPal/Venmo options for US

* move the mural bank details route

* Add utoipa support to payout endpoints

* address some PR comments

* add CORS to new utoipa routes

* Immediately approve mural payouts

* Add currency support to Tremendous payouts

* Currency forex

* add forex to tremendous fee request

* Add Mural balance to bank balance info

* Add more Tremendous currencies support

* Transaction payouts available use the correct date

* Address my own review comment

* Address PR comments

* Change Mural withdrawal limit to 3k

* maybe fix tremendous gift cards

* Change how Mural minimum withdrawals are calculated

* Tweak min/max withdrawal values

---------

Co-authored-by: Calum H. <contact@cal.engineer>
Co-authored-by: Alejandro González <me@alegon.dev>
2025-11-03 14:19:46 -08:00

119 lines
3.2 KiB
Rust

use crate::models::payouts::{PayoutMethodType, PayoutStatus};
use chrono::{DateTime, Utc};
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use super::{DBPayoutId, DBUserId, DatabaseError};
#[derive(Deserialize, Serialize, Clone, Debug)]
pub struct DBPayout {
pub id: DBPayoutId,
pub user_id: DBUserId,
pub created: DateTime<Utc>,
pub status: PayoutStatus,
pub amount: Decimal,
pub fee: Option<Decimal>,
pub method: Option<PayoutMethodType>,
pub method_address: Option<String>,
pub platform_id: Option<String>,
}
impl DBPayout {
pub async fn insert(
&self,
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
) -> Result<(), DatabaseError> {
sqlx::query!(
"
INSERT INTO payouts (
id, amount, fee, user_id, status, method, method_address, platform_id
)
VALUES (
$1, $2, $3, $4, $5, $6, $7, $8
)
",
self.id.0,
self.amount,
self.fee,
self.user_id.0,
self.status.as_str(),
self.method.as_ref().map(|x| x.as_str()),
self.method_address,
self.platform_id,
)
.execute(&mut **transaction)
.await?;
Ok(())
}
pub async fn get<'a, 'b, E>(
id: DBPayoutId,
executor: E,
) -> Result<Option<DBPayout>, DatabaseError>
where
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
{
DBPayout::get_many(&[id], executor)
.await
.map(|x| x.into_iter().next())
}
pub async fn get_many<'a, E>(
payout_ids: &[DBPayoutId],
exec: E,
) -> Result<Vec<DBPayout>, DatabaseError>
where
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
{
use futures::TryStreamExt;
let results = sqlx::query!(
"
SELECT id, user_id, created, amount, status, method, method_address, platform_id, fee
FROM payouts
WHERE id = ANY($1)
",
&payout_ids.into_iter().map(|x| x.0).collect::<Vec<_>>()
)
.fetch(exec)
.map_ok(|r| DBPayout {
id: DBPayoutId(r.id),
user_id: DBUserId(r.user_id),
created: r.created,
status: PayoutStatus::from_string(&r.status),
amount: r.amount,
method: r.method.and_then(|x| PayoutMethodType::from_string(&x)),
method_address: r.method_address,
platform_id: r.platform_id,
fee: r.fee,
})
.try_collect::<Vec<DBPayout>>()
.await?;
Ok(results)
}
pub async fn get_all_for_user(
user_id: DBUserId,
exec: impl sqlx::Executor<'_, Database = sqlx::Postgres>,
) -> Result<Vec<DBPayoutId>, DatabaseError> {
let results = sqlx::query!(
"
SELECT id
FROM payouts
WHERE user_id = $1
",
user_id.0
)
.fetch_all(exec)
.await?;
Ok(results
.into_iter()
.map(|r| DBPayoutId(r.id))
.collect::<Vec<_>>())
}
}