-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
backend/canisters/user_index/api/src/queries/chit_balances.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
backend/canisters/user_index/impl/src/queries/chit_balances.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters