I need to fucking work.

This commit is contained in:
didier amyot committed 2023-01-25 12:50:16 -05:00
1 parent e1e5d5dbf3
commit f99513a8c8
7 files changed
+230 -53

No files matched your search

+18 -10
View File
@@ -8,35 +8,40 @@ use crate::util::*;
fn _move_securities(tx: &Savepoint,
cheque: &Cheque,
account_id: &AccountId) -> CustomResult<()> {
account_id: &AccountId) -> CustomResult<(accounts::Receipt, accounts::Receipt)> {
let drawer_receipt = accounts::add_to_account(tx, &cheque.drawer_account_id, -cheque.amount)?;
let _payee_receipt = accounts::add_to_account(tx, account_id, cheque.amount)?;
let payee_receipt = accounts::add_to_account(tx, account_id, cheque.amount)?;
if drawer_receipt.amount < 0 {
let m = format!("Drawer of {:?} does not have sufficient amount in account.", cheque);
return Err(Error::InvalidAmount(m));
}
Ok(())
Ok((drawer_receipt, payee_receipt))
}
fn _add_cheque_to_cashed_cheques_log(tx: &Connection,
cheque: &Cheque,
payee_account_id: &AccountId) -> Result<(), Error> {
drawer_receipt: &accounts::Receipt,
payee_receipt: &accounts::Receipt) -> Result<(), Error> {
let q = "
INSERT INTO cashed_cheques(
drawer_user_id,
drawer_account_id,
drawer_account_version,
payee_user_id,
payee_account_id,
payee_account_version,
cheque_number,
amount,
underlying_security
)
VALUES(?1, ?2, ?3, ?4, ?5, ?6, ?7)
VALUES(?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)
";
let params = params![cheque.drawer_user_id,
cheque.drawer_account_id,
drawer_receipt.version,
cheque.payee_user_id,
payee_account_id,
payee_receipt.account_id,
payee_receipt.version,
cheque.cheque_number,
cheque.amount,
cheque.security_id];
@@ -55,16 +60,19 @@ fn cash_cheque_unsafe(savepoint: &Savepoint,
if cheque.amount < 0 {
return Err(Error::InvalidAmount(format!("The cheque {:?} has a negative amount.", cheque)));
}
if !accounts::is_owner(&savepoint, &cheque.drawer_account_id, &cheque.drawer_user_id).map_err(Error::SqlError)? {
if !accounts::is_owner(&savepoint, &cheque.drawer_account_id, &cheque.drawer_user_id)? {
return Err(Error::InvalidDrawer(format!("The drawer is the security owner: {:?}", cheque)));
}
if !accounts::is_owner(&savepoint, account_id, &cheque.payee_user_id).map_err(Error::SqlError)? {
if !accounts::is_owner(&savepoint, account_id, &cheque.payee_user_id)? {
return Err(Error::InvalidPayee(format!("Trying to cash the cheque {:?} in \
an account {:?} that is not owned by the payee.", cheque, account_id)));
}
_move_securities(&savepoint, cheque, account_id)?;
_add_cheque_to_cashed_cheques_log(&savepoint, cheque, account_id)?;
let (drawer_receipt, payee_receipt) = _move_securities(&savepoint, cheque, account_id)?;
_add_cheque_to_cashed_cheques_log(&savepoint,
cheque,
&drawer_receipt,
&payee_receipt)?;
Ok(())
}