refactor: bal accounting data responses
All checks were successful
/ test (push) Successful in 3m21s

This commit is contained in:
Ninjdai 2025-08-17 19:07:28 +02:00
parent b551f856df
commit 810fdde741

View file

@ -1,4 +1,4 @@
use std::{collections::{HashMap, HashSet}, sync::Arc}; use std::{collections::HashSet, sync::Arc};
use axum::{extract::{Path, State}, Json}; use axum::{extract::{Path, State}, Json};
use reqwest::{StatusCode}; use reqwest::{StatusCode};
@ -315,13 +315,14 @@ pub async fn get_bals(
#[derive(Serialize, utoipa::ToSchema)] #[derive(Serialize, utoipa::ToSchema)]
pub struct OwnerAccountingData { pub struct OwnerAccountingData {
pub owner_id: u32,
pub owed_money: f32, pub owed_money: f32,
pub owed_books: Vec<book_instance::Model> pub owed_books: Vec<book_instance::Model>
} }
#[derive(Serialize, utoipa::ToSchema)] #[derive(Serialize, utoipa::ToSchema)]
pub struct BalAccounting { pub struct BalAccounting {
pub owner_map: HashMap<u32, OwnerAccountingData>, pub owners: Vec<OwnerAccountingData>,
pub total_collected_money: f32 pub total_collected_money: f32
} }
@ -350,7 +351,7 @@ pub async fn get_bal_accounting(
(StatusCode::FORBIDDEN, Json(None)) (StatusCode::FORBIDDEN, Json(None))
} else { } else {
let mut accounting_data = BalAccounting { let mut accounting_data = BalAccounting {
owner_map: HashMap::new(), owners: vec![],
total_collected_money: 0. total_collected_money: 0.
}; };
@ -364,7 +365,7 @@ pub async fn get_bal_accounting(
if owner_books.is_empty() { if owner_books.is_empty() {
continue; continue;
} }
let mut owner_accounting_data = OwnerAccountingData { owed_money: 0., owed_books: vec![] }; let mut owner_accounting_data = OwnerAccountingData { owner_id: owner.id, owed_money: 0., owed_books: vec![] };
for book_instance in owner_books { for book_instance in owner_books {
match book_instance.sold_price { match book_instance.sold_price {
Some(val) => owner_accounting_data.owed_money += val, Some(val) => owner_accounting_data.owed_money += val,
@ -372,7 +373,7 @@ pub async fn get_bal_accounting(
} }
} }
accounting_data.total_collected_money += owner_accounting_data.owed_money; accounting_data.total_collected_money += owner_accounting_data.owed_money;
accounting_data.owner_map.insert(owner.id, owner_accounting_data); accounting_data.owners.push(owner_accounting_data);
} }
(StatusCode::OK, Json(Some(accounting_data))) (StatusCode::OK, Json(Some(accounting_data)))