Skip to content

Commit c8722ff

Browse files
fix: make validation checks on payout entity
1 parent 6c45964 commit c8722ff

File tree

5 files changed

+24
-12
lines changed

5 files changed

+24
-12
lines changed

src/api/server/convert.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,10 @@ impl From<ApplicationError> for tonic::Status {
678678
ApplicationError::CouldNotParseIncomingPsbt(_) => {
679679
tonic::Status::invalid_argument(err.to_string())
680680
}
681-
ApplicationError::PayoutAlreadyCommitted => {
681+
ApplicationError::PayoutError(PayoutError::PayoutAlreadyCommitted) => {
682+
tonic::Status::failed_precondition(err.to_string())
683+
}
684+
ApplicationError::PayoutError(PayoutError::PayoutAlreadyCancelled) => {
682685
tonic::Status::failed_precondition(err.to_string())
683686
}
684687
_ => tonic::Status::internal(err.to_string()),

src/app/error.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ pub enum ApplicationError {
7272
SigningSessionNotFoundForXPubId(crate::primitives::XPubId),
7373
#[error("Could not parse incoming psbt: {0}")]
7474
CouldNotParseIncomingPsbt(bitcoin::psbt::PsbtParseError),
75-
#[error("Payout already committed to a batch")]
76-
PayoutAlreadyCommitted,
7775
#[error("Hex decode error: {0}")]
7876
HexDecodeError(#[from] hex::FromHexError),
7977
#[error("Could not decrypt the encrypted key: {0}")]

src/app/mod.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -868,13 +868,7 @@ impl App {
868868
.payouts
869869
.find_by_id_for_cancellation(&mut tx, profile.account_id, id)
870870
.await?;
871-
if payout.batch_id.is_some() {
872-
return Err(ApplicationError::PayoutAlreadyCommitted);
873-
}
874-
if payout.is_cancelled() {
875-
return Ok(());
876-
}
877-
payout.cancel_payout(profile.id);
871+
payout.cancel_payout(profile.id)?;
878872
self.payouts.update(&mut tx, payout).await?;
879873
self.ledger
880874
.payout_cancelled(tx, LedgerTransactionId::new(), id)

src/payout/entity.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use serde::{Deserialize, Serialize};
33

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

6+
use super::error::PayoutError;
7+
68
#[derive(Serialize, Deserialize)]
79
#[serde(tag = "type", rename_all = "snake_case")]
810
pub enum PayoutEvent {
@@ -50,10 +52,17 @@ pub struct Payout {
5052
}
5153

5254
impl Payout {
53-
pub fn cancel_payout(&mut self, profile_id: ProfileId) {
55+
pub fn cancel_payout(&mut self, profile_id: ProfileId) -> Result<(), PayoutError> {
56+
if self.is_cancelled() {
57+
return Err(PayoutError::PayoutAlreadyCancelled);
58+
}
59+
if self.is_already_committed() {
60+
return Err(PayoutError::PayoutAlreadyCommitted);
61+
}
5462
self.events.push(PayoutEvent::Cancelled {
5563
executed_by: profile_id,
56-
})
64+
});
65+
Ok(())
5766
}
5867

5968
pub fn is_cancelled(&self) -> bool {
@@ -64,6 +73,10 @@ impl Payout {
6473
}
6574
false
6675
}
76+
77+
fn is_already_committed(&self) -> bool {
78+
self.batch_id.is_some()
79+
}
6780
}
6881

6982
#[derive(Debug, Builder, Clone)]

src/payout/error.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,8 @@ pub enum PayoutError {
1010
PayoutIdNotFound(String),
1111
#[error("PayoutError - External Id does not exists")]
1212
ExternalIdNotFound,
13+
#[error("PayoutError - Payout is already committed to batch")]
14+
PayoutAlreadyCommitted,
15+
#[error("PayoutError - Payout is already cancelled")]
16+
PayoutAlreadyCancelled,
1317
}

0 commit comments

Comments
 (0)