mirror of
https://github.com/modrinth/code.git
synced 2026-08-31 12:05:53 +00:00
serialize values as floats
This commit is contained in:
@@ -80,19 +80,25 @@ const PLATFORM_REVENUE_SPLIT: Decimal = dec!(0.25);
|
|||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct DayDistribution {
|
pub struct DayDistribution {
|
||||||
/// Amount of revenue input into the algorithm.
|
/// Amount of revenue input into the algorithm.
|
||||||
|
#[serde(with = "rust_decimal::serde::float")]
|
||||||
pub raw_revenue_usd: Decimal,
|
pub raw_revenue_usd: Decimal,
|
||||||
/// Operational fees to subtract.
|
/// Operational fees to subtract.
|
||||||
|
#[serde(with = "rust_decimal::serde::float")]
|
||||||
pub fees_usd: Decimal,
|
pub fees_usd: Decimal,
|
||||||
/// Estimation variance to subtract.
|
/// Estimation variance to subtract.
|
||||||
///
|
///
|
||||||
/// For non-estimates (actual revenue values), this is zero.
|
/// For non-estimates (actual revenue values), this is zero.
|
||||||
|
#[serde(with = "rust_decimal::serde::float")]
|
||||||
pub variance_usd: Decimal,
|
pub variance_usd: Decimal,
|
||||||
/// Total net revenue that we earned;
|
/// Total net revenue that we earned;
|
||||||
/// `raw_revenue - fees - variance`.
|
/// `raw_revenue - fees - variance`.
|
||||||
|
#[serde(with = "rust_decimal::serde::float")]
|
||||||
pub net_revenue_usd: Decimal,
|
pub net_revenue_usd: Decimal,
|
||||||
/// How much of the net revenue goes to the platform.
|
/// How much of the net revenue goes to the platform.
|
||||||
|
#[serde(with = "rust_decimal::serde::float")]
|
||||||
pub platform_net_revenue_usd: Decimal,
|
pub platform_net_revenue_usd: Decimal,
|
||||||
/// How much of the net revenue goes to creators.
|
/// How much of the net revenue goes to creators.
|
||||||
|
#[serde(with = "rust_decimal::serde::float")]
|
||||||
pub creator_net_revenue_usd: Decimal,
|
pub creator_net_revenue_usd: Decimal,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use actix_web::{get, web};
|
use actix_web::{HttpRequest, get, web};
|
||||||
use chrono::{Months, NaiveDate, Utc};
|
use chrono::{Months, NaiveDate, Utc};
|
||||||
use rust_decimal::Decimal;
|
use rust_decimal::Decimal;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use xredis::RedisPool;
|
use xredis::RedisPool;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
auth::get_user_from_headers,
|
||||||
database::{
|
database::{
|
||||||
PgPool,
|
PgPool,
|
||||||
models::{
|
models::{
|
||||||
@@ -14,9 +15,13 @@ use crate::{
|
|||||||
payout_variance_item::DBPayoutVariance,
|
payout_variance_item::DBPayoutVariance,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
queue::payout_run::{
|
models::pats::Scopes,
|
||||||
DayDistribution, PayoutVariance, PayoutVariances,
|
queue::{
|
||||||
compute_actual_distribution_flow, distribution_for_day, estimate,
|
payout_run::{
|
||||||
|
DayDistribution, PayoutVariance, PayoutVariances,
|
||||||
|
compute_actual_distribution_flow, distribution_for_day, estimate,
|
||||||
|
},
|
||||||
|
session::AuthQueue,
|
||||||
},
|
},
|
||||||
routes::ApiError,
|
routes::ApiError,
|
||||||
util::{
|
util::{
|
||||||
@@ -69,19 +74,32 @@ pub struct PayoutRunDay {
|
|||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
pub struct PayoutRunAdjustment {
|
pub struct PayoutRunAdjustment {
|
||||||
/// Total value of the adjustment.
|
/// Total value of the adjustment.
|
||||||
|
#[serde(with = "rust_decimal::serde::float")]
|
||||||
pub amount_usd: Decimal,
|
pub amount_usd: Decimal,
|
||||||
/// Why this adjustment was applied.
|
/// Why this adjustment was applied.
|
||||||
///
|
///
|
||||||
/// Only visible to admins.
|
/// Only visible to admins.
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub description: Option<String>,
|
pub description: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("")]
|
#[get("")]
|
||||||
pub async fn get_runs(
|
pub async fn get_runs(
|
||||||
|
req: HttpRequest,
|
||||||
pool: web::Data<PgPool>,
|
pool: web::Data<PgPool>,
|
||||||
redis: web::Data<RedisPool>,
|
redis: web::Data<RedisPool>,
|
||||||
aditude: web::Data<aditude::Client>,
|
aditude: web::Data<aditude::Client>,
|
||||||
|
session_queue: web::Data<AuthQueue>,
|
||||||
) -> Result<web::Json<PayoutRuns>, ApiError> {
|
) -> Result<web::Json<PayoutRuns>, ApiError> {
|
||||||
|
let show_adjustment_descriptions = get_user_from_headers(
|
||||||
|
&req,
|
||||||
|
&**pool,
|
||||||
|
&redis,
|
||||||
|
&session_queue,
|
||||||
|
Scopes::SESSION_ACCESS,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.is_ok_and(|(_, user)| user.role.is_admin());
|
||||||
let now = Utc::now();
|
let now = Utc::now();
|
||||||
let latest_payout_value = sqlx::query_scalar!(
|
let latest_payout_value = sqlx::query_scalar!(
|
||||||
r#"
|
r#"
|
||||||
@@ -93,7 +111,7 @@ pub async fn get_runs(
|
|||||||
.await
|
.await
|
||||||
.wrap_internal_err("fetching latest payout value")?
|
.wrap_internal_err("fetching latest payout value")?
|
||||||
.unwrap_or(now)
|
.unwrap_or(now)
|
||||||
.max(now);
|
.min(now);
|
||||||
|
|
||||||
let current_period = YearMonth::from_day1(now.date_naive());
|
let current_period = YearMonth::from_day1(now.date_naive());
|
||||||
let mut period = YearMonth::from_day1(latest_payout_value.date_naive());
|
let mut period = YearMonth::from_day1(latest_payout_value.date_naive());
|
||||||
@@ -146,10 +164,16 @@ pub async fn get_runs(
|
|||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|estimate| -> Result<_, ApiError> {
|
.map(|estimate| -> Result<_, ApiError> {
|
||||||
if let Some(period) = stored_periods.get(&estimate.period.date()) {
|
if let Some(period) = stored_periods.get(&estimate.period.date()) {
|
||||||
let adjustments = serde_json::from_value::<
|
let mut adjustments =
|
||||||
Vec<PayoutRunAdjustment>,
|
serde_json::from_value::<Vec<PayoutRunAdjustment>>(
|
||||||
>(period.adjustments.clone())
|
period.adjustments.clone(),
|
||||||
.wrap_internal_err("deserializing payout adjustments")?;
|
)
|
||||||
|
.wrap_internal_err("deserializing payout adjustments")?;
|
||||||
|
if !show_adjustment_descriptions {
|
||||||
|
for adjustment in &mut adjustments {
|
||||||
|
adjustment.description = None;
|
||||||
|
}
|
||||||
|
}
|
||||||
let total_estimated_revenue_usd = period
|
let total_estimated_revenue_usd = period
|
||||||
.days
|
.days
|
||||||
.iter()
|
.iter()
|
||||||
|
|||||||
Reference in New Issue
Block a user