Skip to content

Commit

Permalink
Implement chit_balances
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles committed Jul 22, 2024
1 parent f52de9b commit 792293c
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 0 deletions.
9 changes: 9 additions & 0 deletions backend/canisters/user_index/api/can.did
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ type SearchResponse = variant {
};
};

type ChitBalancesArgs = record {
users : vec UserId;
year : nat16;
month : nat8;
};

type DiamondMembershipFeesResponse = variant {
Success : vec record {
token : Cryptocurrency;
Expand Down Expand Up @@ -404,6 +410,9 @@ service : {
// Search for users matching some query
search : (SearchArgs) -> (SearchResponse) query;

// Gets the CHIT balances for multiple users for a chosen month
chit_balances : (ChitBalancesArgs) -> (ChitBalancesResponse) query;

// Retrieves the current fees to pay for Diamond membership
diamond_membership_fees : (EmptyArgs) -> (DiamondMembershipFeesResponse) query;

Expand Down
1 change: 1 addition & 0 deletions backend/canisters/user_index/api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use candid_gen::generate_candid_method;
#[allow(deprecated)]
fn main() {
generate_candid_method!(user_index, check_username, query);
generate_candid_method!(user_index, chit_balances, query);
generate_candid_method!(user_index, chit_leaderboard, query);
generate_candid_method!(user_index, current_user, query);
generate_candid_method!(user_index, diamond_membership_fees, query);
Expand Down
21 changes: 21 additions & 0 deletions backend/canisters/user_index/api/src/queries/chit_balances.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use candid::CandidType;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use types::UserId;

#[derive(CandidType, Serialize, Deserialize, Debug)]
pub struct Args {
pub users: Vec<UserId>,
pub year: u16,
pub month: u8,
}

#[derive(CandidType, Serialize, Deserialize, Debug)]
pub enum Response {
Success(SuccessResult),
}

#[derive(CandidType, Serialize, Deserialize, Debug)]
pub struct SuccessResult {
pub balances: HashMap<UserId, i32>,
}
1 change: 1 addition & 0 deletions backend/canisters/user_index/api/src/queries/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod c2c_lookup_user;
pub mod check_username;
pub mod chit_balances;
pub mod chit_leaderboard;
pub mod current_user;
pub mod diamond_membership_fees;
Expand Down
23 changes: 23 additions & 0 deletions backend/canisters/user_index/impl/src/queries/chit_balances.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use crate::{read_state, RuntimeState};
use ic_cdk::query;
use user_index_canister::chit_balances::{Response::*, *};
use utils::time::MonthKey;

#[query]
fn chit_balances(args: Args) -> Response {
read_state(|state| chit_balances_impl(args, state))
}

fn chit_balances_impl(args: Args, state: &RuntimeState) -> Response {
let month_key = MonthKey::new(args.year as u32, args.month);

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)
.collect();

Success(SuccessResult { balances })
}
1 change: 1 addition & 0 deletions backend/canisters/user_index/impl/src/queries/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod c2c_lookup_user;
pub mod check_username;
pub mod chit_balances;
pub mod chit_leaderboard;
pub mod current_user;
pub mod diamond_membership_fees;
Expand Down

0 comments on commit 792293c

Please sign in to comment.