Skip to content

Commit

Permalink
Split out approve/reject
Browse files Browse the repository at this point in the history
  • Loading branch information
megrogan committed Feb 1, 2024
1 parent 189ab3d commit 761be2f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 31 deletions.
52 changes: 31 additions & 21 deletions backend/canisters/translations/impl/src/model/translations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,31 @@ impl Translations {
Some(new_index as u64)
}

pub fn approve(&mut self, id: u64, approve: bool, user_id: UserId, now: TimestampMillis) -> ApproveResponse {
pub fn approve(&mut self, id: u64, user_id: UserId, now: TimestampMillis) -> ApproveRejectResponse {
if let Some(translation) = self.translations.get_mut(id as usize) {
if !matches!(translation.status, TranslationStatus::Proposed) {
ApproveResponse::NotProposed
ApproveRejectResponse::NotProposed
} else {
let attribution = Attribution { who: user_id, when: now };
translation.status = if approve {
TranslationStatus::Approved(attribution)
} else {
TranslationStatus::Rejected(attribution)
};
ApproveResponse::Success
translation.status = TranslationStatus::Approved(attribution);
ApproveRejectResponse::Success
}
} else {
ApproveRejectResponse::NotFound
}
}

pub fn reject(&mut self, id: u64, user_id: UserId, now: TimestampMillis) -> ApproveRejectResponse {
if let Some(translation) = self.translations.get_mut(id as usize) {
if !matches!(translation.status, TranslationStatus::Proposed) {
ApproveRejectResponse::NotProposed
} else {
let attribution = Attribution { who: user_id, when: now };
translation.status = TranslationStatus::Rejected(attribution);
ApproveRejectResponse::Success
}
} else {
ApproveResponse::NotFound
ApproveRejectResponse::NotFound
}
}

Expand Down Expand Up @@ -188,7 +198,7 @@ pub struct Attribution {
pub when: TimestampMillis,
}

pub enum ApproveResponse {
pub enum ApproveRejectResponse {
Success,
NotProposed,
NotFound,
Expand Down Expand Up @@ -243,7 +253,7 @@ mod tests {

translations.propose(test_proposal_1());
translations.propose(test_proposal_2());
translations.approve(0, true, user_id(USER3), 2);
translations.approve(0, user_id(USER3), 2);

let results = translations.proposed();

Expand All @@ -258,8 +268,8 @@ mod tests {

translations.propose(test_proposal_1());
translations.propose(test_proposal_2());
translations.approve(0, true, user_id(USER3), 2);
translations.approve(1, false, user_id(USER3), 3);
translations.approve(0, user_id(USER3), 2);
translations.reject(1, user_id(USER3), 3);

let results = translations.proposed();

Expand Down Expand Up @@ -289,7 +299,7 @@ mod tests {

translations.propose(test_proposal_1());
translations.propose(test_proposal_2());
translations.approve(0, true, user_id(USER3), 2);
translations.approve(0, user_id(USER3), 2);

let results = translations.pending_deployment();

Expand All @@ -303,8 +313,8 @@ mod tests {

translations.propose(test_proposal_1());
translations.propose(test_proposal_2());
translations.approve(0, true, user_id(USER3), 2);
translations.approve(1, true, user_id(USER3), 3);
translations.approve(0, user_id(USER3), 2);
translations.approve(1, user_id(USER3), 3);

let results = translations.pending_deployment();

Expand All @@ -318,7 +328,7 @@ mod tests {

translations.propose(test_proposal_1());
translations.propose(test_proposal_2());
translations.approve(0, false, user_id(USER3), 2);
translations.reject(0, user_id(USER3), 2);

let results = translations.pending_deployment();

Expand All @@ -331,7 +341,7 @@ mod tests {

translations.propose(test_proposal_1());
translations.propose(test_proposal_2());
translations.approve(0, true, user_id(USER3), 2);
translations.approve(0, user_id(USER3), 2);
translations.mark_deployed(2, 3);

let results = translations.pending_deployment();
Expand All @@ -345,8 +355,8 @@ mod tests {

translations.propose(test_proposal_1());
translations.propose(test_proposal_2());
translations.approve(0, true, user_id(USER3), 2);
translations.approve(1, true, user_id(USER3), 3);
translations.approve(0, user_id(USER3), 2);
translations.approve(1, user_id(USER3), 3);
translations.mark_deployed(2, 3);

let results = translations.pending_deployment();
Expand All @@ -360,7 +370,7 @@ mod tests {

translations.propose(test_proposal_1());
translations.propose(test_proposal_2());
translations.approve(0, true, user_id(USER3), 2);
translations.approve(0, user_id(USER3), 2);
translations.mark_deployed(0, 3);

let results = translations.proposed();
Expand Down
10 changes: 5 additions & 5 deletions backend/canisters/translations/impl/src/updates/approve.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
model::{
pending_payments_queue::{PendingPayment, PendingPaymentReason},
translations::ApproveResponse,
translations::ApproveRejectResponse,
},
mutate_state, read_state,
};
Expand All @@ -23,8 +23,8 @@ async fn approve(args: Args) -> Response {
Err(LookupUserError::InternalError(error)) => return InternalError(error),
};

mutate_state(|state| match state.data.translations.approve(args.id, true, user_id, now) {
ApproveResponse::Success => {
mutate_state(|state| match state.data.translations.approve(args.id, user_id, now) {
ApproveRejectResponse::Success => {
state.data.pending_payments_queue.push(PendingPayment {
recipient_account: user_id.into(),
timestamp: now,
Expand All @@ -34,7 +34,7 @@ async fn approve(args: Args) -> Response {
});
Success
}
ApproveResponse::NotProposed => NotProposed,
ApproveResponse::NotFound => NotFound,
ApproveRejectResponse::NotProposed => NotProposed,
ApproveRejectResponse::NotFound => NotFound,
})
}
10 changes: 5 additions & 5 deletions backend/canisters/translations/impl/src/updates/reject.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{model::translations::ApproveResponse, mutate_state, read_state};
use crate::{model::translations::ApproveRejectResponse, mutate_state, read_state};
use canister_tracing_macros::trace;
use ic_cdk_macros::update;
use translations_canister::reject::{Response::*, *};
Expand All @@ -16,9 +16,9 @@ async fn reject(args: Args) -> Response {
Err(LookupUserError::InternalError(error)) => return InternalError(error),
};

mutate_state(|state| match state.data.translations.approve(args.id, false, user_id, now) {
ApproveResponse::Success => Success,
ApproveResponse::NotProposed => NotProposed,
ApproveResponse::NotFound => NotFound,
mutate_state(|state| match state.data.translations.reject(args.id, user_id, now) {
ApproveRejectResponse::Success => Success,
ApproveRejectResponse::NotProposed => NotProposed,
ApproveRejectResponse::NotFound => NotFound,
})
}

0 comments on commit 761be2f

Please sign in to comment.