From 8b0927c63191cede7a90459cdb0aa89c5f0e57e6 Mon Sep 17 00:00:00 2001 From: Hamish Peebles Date: Wed, 24 Jul 2024 11:03:58 +0100 Subject: [PATCH] This is better --- .../user_index/impl/src/model/user.rs | 4 ++++ .../user_index/impl/src/model/user_map.rs | 22 +++++++++++-------- backend/libraries/utils/src/time.rs | 14 ++++++++++++ 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/backend/canisters/user_index/impl/src/model/user.rs b/backend/canisters/user_index/impl/src/model/user.rs index 2cdef62a98..5534e10538 100644 --- a/backend/canisters/user_index/impl/src/model/user.rs +++ b/backend/canisters/user_index/impl/src/model/user.rs @@ -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, } @@ -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, } } @@ -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, } } diff --git a/backend/canisters/user_index/impl/src/model/user_map.rs b/backend/canisters/user_index/impl/src/model/user_map.rs index 18d43cf38f..9d240039d4 100644 --- a/backend/canisters/user_index/impl/src/model/user_map.rs +++ b/backend/canisters/user_index/impl/src/model/user_map.rs @@ -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 } diff --git a/backend/libraries/utils/src/time.rs b/backend/libraries/utils/src/time.rs index 443f181509..38e31dbaba 100644 --- a/backend/libraries/utils/src/time.rs +++ b/backend/libraries/utils/src/time.rs @@ -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 { let start = self.start_timestamp(); let end = self.next().start_timestamp();