Skip to content

Commit

Permalink
This is better
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles committed Jul 24, 2024
1 parent 0ab452e commit 8b0927c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
4 changes: 4 additions & 0 deletions backend/canisters/user_index/impl/src/model/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ pub struct User {
pub chit_updated: TimestampMillis,
#[serde(rename = "lc", default)]
pub latest_chit_event: TimestampMillis,
#[serde(rename = "lcp", default)]
pub latest_chit_event_previous_month: TimestampMillis,
#[serde(rename = "uh", default, skip_serializing_if = "Option::is_none")]
pub unique_person_proof: Option<UniquePersonProof>,
}
Expand Down Expand Up @@ -125,6 +127,7 @@ impl User {
streak: 0,
streak_ends: 0,
latest_chit_event: 0,
latest_chit_event_previous_month: 0,
unique_person_proof: None,
}
}
Expand Down Expand Up @@ -246,6 +249,7 @@ impl Default for User {
streak_ends: 0,
chit_updated: 0,
latest_chit_event: 0,
latest_chit_event_previous_month: 0,
unique_person_proof: None,
}
}
Expand Down
22 changes: 13 additions & 9 deletions backend/canisters/user_index/impl/src/model/user_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,19 +210,23 @@ impl UserMap {
};

let chit_event_month = MonthKey::from_timestamp(chit_event_timestamp);
let now_month = MonthKey::from_timestamp(now);

if chit_event_month == now_month && chit_event_timestamp <= user.latest_chit_event {
return false;
if chit_event_timestamp >= user.latest_chit_event {
user.latest_chit_event = chit_event_timestamp;
user.chit_balance = chit_balance;
user.streak = streak;
user.streak_ends = streak_ends;
user.chit_updated = now;
} else {
let previous_month = MonthKey::from_timestamp(now).previous();
if chit_event_month == previous_month && chit_event_timestamp >= user.latest_chit_event_previous_month {
user.latest_chit_event_previous_month = chit_event_timestamp;
} else {
return false;
}
}

user.chit_per_month.insert(chit_event_month, chit_balance);
user.latest_chit_event = chit_event_timestamp;
user.chit_balance = chit_balance;
user.streak = streak;
user.streak_ends = streak_ends;
user.chit_updated = now;

true
}

Expand Down
14 changes: 14 additions & 0 deletions backend/libraries/utils/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,20 @@ impl MonthKey {
}
}

pub fn previous(self) -> MonthKey {
if self.month == 1 {
MonthKey {
year: self.year.saturating_sub(1),
month: 12,
}
} else {
MonthKey {
year: self.year,
month: self.month.saturating_sub(1),
}
}
}

pub fn timestamp_range(&self) -> Range<TimestampMillis> {
let start = self.start_timestamp();
let end = self.next().start_timestamp();
Expand Down

0 comments on commit 8b0927c

Please sign in to comment.