Skip to content

fix: make validation checks on payout entity #374

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/api/server/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,10 @@ impl From<ApplicationError> for tonic::Status {
ApplicationError::CouldNotParseIncomingPsbt(_) => {
tonic::Status::invalid_argument(err.to_string())
}
ApplicationError::PayoutAlreadyCommitted => {
ApplicationError::PayoutError(PayoutError::PayoutAlreadyCommitted) => {
tonic::Status::failed_precondition(err.to_string())
}
ApplicationError::PayoutError(PayoutError::PayoutAlreadyCancelled) => {
tonic::Status::failed_precondition(err.to_string())
}
_ => tonic::Status::internal(err.to_string()),
Expand Down
2 changes: 0 additions & 2 deletions src/app/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ pub enum ApplicationError {
SigningSessionNotFoundForXPubId(crate::primitives::XPubId),
#[error("Could not parse incoming psbt: {0}")]
CouldNotParseIncomingPsbt(bitcoin::psbt::PsbtParseError),
#[error("Payout already committed to a batch")]
PayoutAlreadyCommitted,
#[error("Hex decode error: {0}")]
HexDecodeError(#[from] hex::FromHexError),
#[error("Could not decrypt the encrypted key: {0}")]
Expand Down
8 changes: 1 addition & 7 deletions src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -868,13 +868,7 @@ impl App {
.payouts
.find_by_id_for_cancellation(&mut tx, profile.account_id, id)
.await?;
if payout.batch_id.is_some() {
return Err(ApplicationError::PayoutAlreadyCommitted);
}
if payout.is_cancelled() {
return Ok(());
}
payout.cancel_payout(profile.id);
payout.cancel_payout(profile.id)?;
self.payouts.update(&mut tx, payout).await?;
self.ledger
.payout_cancelled(tx, LedgerTransactionId::new(), id)
Expand Down
17 changes: 15 additions & 2 deletions src/payout/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use serde::{Deserialize, Serialize};

use crate::{entity::*, primitives::*};

use super::error::PayoutError;

#[derive(Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum PayoutEvent {
Expand Down Expand Up @@ -50,10 +52,17 @@ pub struct Payout {
}

impl Payout {
pub fn cancel_payout(&mut self, profile_id: ProfileId) {
pub fn cancel_payout(&mut self, profile_id: ProfileId) -> Result<(), PayoutError> {
if self.is_cancelled() {
return Err(PayoutError::PayoutAlreadyCancelled);
}
if self.is_already_committed() {
return Err(PayoutError::PayoutAlreadyCommitted);
}
self.events.push(PayoutEvent::Cancelled {
executed_by: profile_id,
})
});
Ok(())
}

pub fn is_cancelled(&self) -> bool {
Expand All @@ -64,6 +73,10 @@ impl Payout {
}
false
}

fn is_already_committed(&self) -> bool {
self.batch_id.is_some()
}
}

#[derive(Debug, Builder, Clone)]
Expand Down
4 changes: 4 additions & 0 deletions src/payout/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ pub enum PayoutError {
PayoutIdNotFound(String),
#[error("PayoutError - External Id does not exists")]
ExternalIdNotFound,
#[error("PayoutError - Payout is already committed to batch")]
PayoutAlreadyCommitted,
#[error("PayoutError - Payout is already cancelled")]
PayoutAlreadyCancelled,
}