@@ -9,7 +9,7 @@ use crate::queue::payouts::{make_aditude_request, PayoutsQueue};
9
9
use crate :: queue:: session:: AuthQueue ;
10
10
use crate :: routes:: ApiError ;
11
11
use actix_web:: { delete, get, post, web, HttpRequest , HttpResponse } ;
12
- use chrono:: { Datelike , Duration , TimeZone , Utc , Weekday } ;
12
+ use chrono:: { DateTime , Datelike , Duration , TimeZone , Utc , Weekday } ;
13
13
use hex:: ToHex ;
14
14
use hmac:: { Hmac , Mac , NewMac } ;
15
15
use reqwest:: Method ;
@@ -763,6 +763,7 @@ pub async fn payment_methods(
763
763
pub struct UserBalance {
764
764
pub available : Decimal ,
765
765
pub pending : Decimal ,
766
+ pub dates : HashMap < DateTime < Utc > , Decimal > ,
766
767
}
767
768
768
769
#[ get( "balance" ) ]
@@ -791,27 +792,27 @@ async fn get_user_balance(
791
792
user_id : crate :: database:: models:: ids:: UserId ,
792
793
pool : & PgPool ,
793
794
) -> Result < UserBalance , sqlx:: Error > {
794
- let available = sqlx:: query!(
795
+ let payouts = sqlx:: query!(
795
796
"
796
- SELECT SUM(amount)
797
+ SELECT date_available, SUM(amount) sum
797
798
FROM payouts_values
798
- WHERE user_id = $1 AND date_available <= NOW()
799
+ WHERE user_id = $1
800
+ GROUP BY date_available
801
+ ORDER BY date_available DESC
799
802
" ,
800
803
user_id. 0
801
804
)
802
- . fetch_optional ( pool)
805
+ . fetch_all ( pool)
803
806
. await ?;
804
807
805
- let pending = sqlx:: query!(
806
- "
807
- SELECT SUM(amount)
808
- FROM payouts_values
809
- WHERE user_id = $1 AND date_available > NOW()
810
- " ,
811
- user_id. 0
812
- )
813
- . fetch_optional ( pool)
814
- . await ?;
808
+ let available = payouts
809
+ . iter ( )
810
+ . filter ( |x| x. date_available <= Utc :: now ( ) )
811
+ . fold ( Decimal :: ZERO , |acc, x| acc + x. sum . unwrap_or ( Decimal :: ZERO ) ) ;
812
+ let pending = payouts
813
+ . iter ( )
814
+ . filter ( |x| x. date_available > Utc :: now ( ) )
815
+ . fold ( Decimal :: ZERO , |acc, x| acc + x. sum . unwrap_or ( Decimal :: ZERO ) ) ;
815
816
816
817
let withdrawn = sqlx:: query!(
817
818
"
@@ -824,12 +825,6 @@ async fn get_user_balance(
824
825
. fetch_optional ( pool)
825
826
. await ?;
826
827
827
- let available = available
828
- . map ( |x| x. sum . unwrap_or ( Decimal :: ZERO ) )
829
- . unwrap_or ( Decimal :: ZERO ) ;
830
- let pending = pending
831
- . map ( |x| x. sum . unwrap_or ( Decimal :: ZERO ) )
832
- . unwrap_or ( Decimal :: ZERO ) ;
833
828
let ( withdrawn, fees) = withdrawn
834
829
. map ( |x| {
835
830
(
@@ -844,6 +839,10 @@ async fn get_user_balance(
844
839
- withdrawn. round_dp ( 16 )
845
840
- fees. round_dp ( 16 ) ,
846
841
pending,
842
+ dates : payouts
843
+ . iter ( )
844
+ . map ( |x| ( x. date_available , x. sum . unwrap_or ( Decimal :: ZERO ) ) )
845
+ . collect ( ) ,
847
846
} )
848
847
}
849
848
0 commit comments