diff --git a/backend/canisters/translations/impl/src/model/translations.rs b/backend/canisters/translations/impl/src/model/translations.rs index b58f968cd6..f57de8dca9 100644 --- a/backend/canisters/translations/impl/src/model/translations.rs +++ b/backend/canisters/translations/impl/src/model/translations.rs @@ -14,22 +14,20 @@ impl Translations { pub fn propose(&mut self, args: ProposeArgs) -> Option { let tuple = (args.locale.clone(), args.key.clone()); - if let Some(indexes) = self.records.get(&tuple) { - // Loop backwards through translations until we reach the most recently deployed. - // If any of these translations matches the proposed value then don't add the translation. - for index in indexes.iter().rev() { - if let Some(translation) = self.translations.get(*index) { - if translation.value == args.value { - return None; - } + // Loop backwards through translations until we reach the most recently deployed. + // If any of these translations matches the proposed value then don't add the translation. + for translation in self.record_iter(&tuple).rev() { + if translation.value == args.value { + return None; + } - if matches!(translation.status, TranslationStatus::Deployed(_)) { - break; - } - } + if matches!(translation.status, TranslationStatus::Deployed(_)) { + break; } + } - // If this user has a previous proposed translation for this record then mark it as `overidden` + // If this user has a previous proposed translation for this record then mark it as `overidden` + if let Some(indexes) = self.records.get(&tuple) { for index in indexes.iter().rev() { if let Some(translation) = self.translations.get_mut(*index) { if translation.proposed.who == args.user_id && matches!(translation.status, TranslationStatus::Proposed) { @@ -69,13 +67,10 @@ impl Translations { let proposed_by = translation.proposed.who; let tuple = (translation.locale.clone(), translation.key.clone()); - let previously_approved = if let Some(indexes) = self.records.get(&tuple) { - indexes.iter().filter_map(|index| self.translations.get(*index)).any(|t| { - t.id != id && t.proposed.who == proposed_by && matches!(t.status, TranslationStatus::Approved(_)) - }) - } else { - false - }; + + let previously_approved = self + .record_iter(&tuple) + .any(|t| t.id != id && t.proposed.who == proposed_by && matches!(t.status, TranslationStatus::Approved(_))); ApproveResponse::Success(ApproveSuccess { proposed_by, @@ -174,6 +169,14 @@ impl Translations { .filter_map(|index| self.translations.get(*index)) .find(|t| matches!(t.status, TranslationStatus::Approved(_)) || matches!(t.status, TranslationStatus::Deployed(_))) } + + fn record_iter(&self, tuple_key: &(String, String)) -> impl DoubleEndedIterator + '_ { + self.records + .get(tuple_key) + .map(|indexes| indexes.iter().filter_map(|index| self.translations.get(*index))) + .into_iter() + .flatten() + } } #[derive(Clone)]