Skip to content

Commit

Permalink
Simplify chit_balances responses
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles committed Jul 24, 2024
1 parent 7691047 commit 2bf5f14
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion backend/canisters/user_index/api/can.did
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ type ChitBalancesArgs = record {

type ChitBalancesResponse = variant {
Success : record {
balances : vec record { UserId; int32 };
balances : vec int32;
};
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use candid::CandidType;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use types::UserId;

#[derive(CandidType, Serialize, Deserialize, Debug)]
Expand All @@ -17,5 +16,5 @@ pub enum Response {

#[derive(CandidType, Serialize, Deserialize, Debug)]
pub struct SuccessResult {
pub balances: HashMap<UserId, i32>,
pub balances: Vec<i32>,
}
10 changes: 7 additions & 3 deletions backend/canisters/user_index/impl/src/queries/chit_balances.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{read_state, RuntimeState};
use ic_cdk::query;
use types::UserId;
use user_index_canister::chit_balances::{Response::*, *};
use utils::time::MonthKey;

Expand All @@ -14,10 +15,13 @@ fn chit_balances_impl(args: Args, state: &RuntimeState) -> Response {
let balances = args
.users
.iter()
.flat_map(|u| state.data.users.get_by_user_id(u))
.map(|u| (u.user_id, u.chit_per_month.get(&month_key).copied().unwrap_or_default()))
.filter(|(_, c)| *c > 0)
.map(|u| chit_balance_for_user(u, month_key, state).unwrap_or_default())
.collect();

Success(SuccessResult { balances })
}

fn chit_balance_for_user(user_id: &UserId, month_key: MonthKey, state: &RuntimeState) -> Option<i32> {
let user = state.data.users.get_by_user_id(user_id)?;
user.chit_per_month.get(&month_key).copied()
}

0 comments on commit 2bf5f14

Please sign in to comment.