File tree Expand file tree Collapse file tree 5 files changed +24
-12
lines changed Expand file tree Collapse file tree 5 files changed +24
-12
lines changed Original file line number Diff line number Diff line change @@ -678,7 +678,10 @@ impl From<ApplicationError> for tonic::Status {
678
678
ApplicationError :: CouldNotParseIncomingPsbt ( _) => {
679
679
tonic:: Status :: invalid_argument ( err. to_string ( ) )
680
680
}
681
- ApplicationError :: PayoutAlreadyCommitted => {
681
+ ApplicationError :: PayoutError ( PayoutError :: PayoutAlreadyCommitted ) => {
682
+ tonic:: Status :: failed_precondition ( err. to_string ( ) )
683
+ }
684
+ ApplicationError :: PayoutError ( PayoutError :: PayoutAlreadyCancelled ) => {
682
685
tonic:: Status :: failed_precondition ( err. to_string ( ) )
683
686
}
684
687
_ => tonic:: Status :: internal ( err. to_string ( ) ) ,
Original file line number Diff line number Diff line change @@ -72,8 +72,6 @@ pub enum ApplicationError {
72
72
SigningSessionNotFoundForXPubId ( crate :: primitives:: XPubId ) ,
73
73
#[ error( "Could not parse incoming psbt: {0}" ) ]
74
74
CouldNotParseIncomingPsbt ( bitcoin:: psbt:: PsbtParseError ) ,
75
- #[ error( "Payout already committed to a batch" ) ]
76
- PayoutAlreadyCommitted ,
77
75
#[ error( "Hex decode error: {0}" ) ]
78
76
HexDecodeError ( #[ from] hex:: FromHexError ) ,
79
77
#[ error( "Could not decrypt the encrypted key: {0}" ) ]
Original file line number Diff line number Diff line change @@ -868,13 +868,7 @@ impl App {
868
868
. payouts
869
869
. find_by_id_for_cancellation ( & mut tx, profile. account_id , id)
870
870
. 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 ) ?;
878
872
self . payouts . update ( & mut tx, payout) . await ?;
879
873
self . ledger
880
874
. payout_cancelled ( tx, LedgerTransactionId :: new ( ) , id)
Original file line number Diff line number Diff line change @@ -3,6 +3,8 @@ use serde::{Deserialize, Serialize};
3
3
4
4
use crate :: { entity:: * , primitives:: * } ;
5
5
6
+ use super :: error:: PayoutError ;
7
+
6
8
#[ derive( Serialize , Deserialize ) ]
7
9
#[ serde( tag = "type" , rename_all = "snake_case" ) ]
8
10
pub enum PayoutEvent {
@@ -50,10 +52,17 @@ pub struct Payout {
50
52
}
51
53
52
54
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
+ }
54
62
self . events . push ( PayoutEvent :: Cancelled {
55
63
executed_by : profile_id,
56
- } )
64
+ } ) ;
65
+ Ok ( ( ) )
57
66
}
58
67
59
68
pub fn is_cancelled ( & self ) -> bool {
@@ -64,6 +73,10 @@ impl Payout {
64
73
}
65
74
false
66
75
}
76
+
77
+ fn is_already_committed ( & self ) -> bool {
78
+ self . batch_id . is_some ( )
79
+ }
67
80
}
68
81
69
82
#[ derive( Debug , Builder , Clone ) ]
Original file line number Diff line number Diff line change @@ -10,4 +10,8 @@ pub enum PayoutError {
10
10
PayoutIdNotFound ( String ) ,
11
11
#[ error( "PayoutError - External Id does not exists" ) ]
12
12
ExternalIdNotFound ,
13
+ #[ error( "PayoutError - Payout is already committed to batch" ) ]
14
+ PayoutAlreadyCommitted ,
15
+ #[ error( "PayoutError - Payout is already cancelled" ) ]
16
+ PayoutAlreadyCancelled ,
13
17
}
You can’t perform that action at this time.
0 commit comments