Avec un cafe.

This commit is contained in:
didier amyot committed 2022-12-21 08:02:40 -05:00
1 parent d78b4f1d90
commit 352bb498d4
7 files changed
+86 -50

No files matched your search

+25 -9
View File
@@ -1,9 +1,9 @@
use rusqlite::{ffi, params, Params, Savepoint, Transaction};
use crate::db::{accounts, securities};
use crate::db::accounts::{Account, get_account_by_id};
use crate::db::cheques::SecurityEmitterCheque;
use crate::db::external_id::AccountId;
use crate::db::securities;
use crate::db::securities::get_security_by_id;
use crate::util::*;
@@ -44,21 +44,20 @@ fn _add_cheque_to_cashed_cheques_log(tx: &TransactionHelper,
fn cash_cheque(tx: &Transaction,
cheque: &SecurityEmitterCheque,
account_id: &AccountId) -> CustomResult<()> {
// let mut savepoint: Savepoint = tx.savepoint_with_name("bib").map_err(Error::SqlError)?;
if !securities::is_owner(&tx, &cheque.security_id, &cheque.drawer_user_id).map_err(Error::SqlError)? {
if !accounts::is_owner(&tx, &cheque.drawer_account_id, &cheque.drawer_user_id).map_err(Error::SqlError)? {
return Err(Error::InvalidDrawer(format!("The drawer is the security owner: {:?}", cheque)));
}
// todo:
// 1. payee is owner of the account.
// 2. drawer is owner of the account.
if !accounts::is_owner(&tx, account_id, &cheque.payee_user_id).map_err(Error::SqlError)? {
return Err(Error::InvalidPayee(format!("Trying to cash the cheque {:?} in \
an account {:?} that is not owned by the payee.", cheque, account_id)));
}
let myx = TransactionHelper::new(tx);
_add_securities_to_account(&myx, cheque, account_id)?;
_add_cheque_to_cashed_cheques_log(&myx, cheque, account_id)?;
// savepoint.rollback().map_err(Error::SqlError)?;
// create transaction log
// create transaction loggetAllPurchaseHistory
Ok(())
}
@@ -66,9 +65,9 @@ fn cash_cheque(tx: &Transaction,
#[cfg(test)]
mod test_caching_cheques {
use rusqlite::{Connection, params};
use crate::db::accounts::{create_checking_account, create_security_emitter_account};
use crate::db::{accounts, test_utils};
use crate::db::accounts::{create_checking_account, create_security_emitter_account};
use crate::db::cheques::{Drawer, Payee};
use crate::db::external_id::{Id, SecurityId, UserId};
use crate::db::users::User;
@@ -161,4 +160,21 @@ mod test_caching_cheques {
_ => Err(Error::Testing("Expected invalid drawer here.".to_string()))
}
}
#[test]
fn test_invalid_payee() -> CustomResult<()> {
let (mut conn, drawer, payee, payee_account) = setup_basic();
let mut tx = conn.transaction().unwrap();
// let wrong_payee_account = create_checking_account(&tx, &sec.id(), &payee.id()).unwrap();
let wrong_payee_account = &drawer.1;
let cheque = SecurityEmitterCheque::new(
drawer.clone(), payee.clone(), 1000, 99);
let ret = cash_cheque(&mut tx, &cheque, wrong_payee_account);
match ret {
Err(Error::InvalidPayee(_)) => Ok(()),
_ => Err(Error::Testing("Expected invalid payee here.".to_string()))
}
}
}