going somewhere.

This commit is contained in:
didier amyot committed 2022-12-21 08:35:41 -05:00
1 parent d06264401d
commit 2715dc20fa
5 files changed
+38 -22

No files matched your search

+12 -8
View File
@@ -5,6 +5,7 @@ use crate::db::_external_id::{AccountId, Id, UserId};
use crate::db::_securities::Security; use crate::db::_securities::Security;
use crate::db::_users::User; use crate::db::_users::User;
use crate::db::external_id::SecurityId; use crate::db::external_id::SecurityId;
use crate::util::{CustomResult, TransactionHelper};
#[derive(PartialEq, Debug)] #[derive(PartialEq, Debug)]
pub enum AccountType { pub enum AccountType {
@@ -114,7 +115,7 @@ pub fn create_security_emitter_account(tx: &Transaction, sec: &SecurityId, user:
pub fn is_owner(tx: &Transaction, account_id: &AccountId, user_id: &UserId) -> Result<bool> { pub fn is_owner(tx: &Transaction, account_id: &AccountId, user_id: &UserId) -> Result<bool> {
let q = "SELECT id FROM account_user_permissions WHERE account_id = ?1 AND user_id = ?2"; let q = "SELECT id FROM account_user_permissions WHERE account_id = ?1 AND user_id = ?2";
let params = params![account_id, user_id]; let params = params![account_id, user_id];
let ite = |row: &Row| row.get::<usize, u32>(0); let ite = |row: &Row| row.get::<usize, i64>(0);
let option = tx.query_row(q, params, ite).optional()?; let option = tx.query_row(q, params, ite).optional()?;
Ok(option.is_some()) Ok(option.is_some())
} }
@@ -135,7 +136,9 @@ impl Receipt {
} }
} }
pub fn add_to_account(tx: &Transaction, account_id: &AccountId, amount: i64) -> Result<Receipt> { pub fn add_to_account(tx: &TransactionHelper,
account_id: &AccountId,
amount: i64) -> CustomResult<Receipt> {
let q = " let q = "
UPDATE accounts SET amount = amount + ?1, UPDATE accounts SET amount = amount + ?1,
timestamp = CURRENT_TIMESTAMP timestamp = CURRENT_TIMESTAMP
@@ -251,19 +254,20 @@ mod test_accounts {
} }
#[test] #[test]
fn test_add_to_account() -> Result<()> { fn test_add_to_account() -> CustomResult<()> {
let (mut conn, users, secs) = setup(); let (mut conn, users, secs) = setup();
let tx = conn.transaction().unwrap(); let tx = conn.transaction().unwrap();
let mtx = TransactionHelper::new(&tx);
let sec = secs.get(0).unwrap().id(); let sec = secs.get(0).unwrap().id();
let user = users.get(0).unwrap().id(); let user = users.get(0).unwrap().id();
let acc = create_checking_account(&tx, &sec, &user)?; let acc = create_checking_account(&tx, &sec, &user).unwrap();
let receipt = add_to_account(& tx, & acc.id(), 33)?; let receipt = add_to_account(&mtx, &acc.id(), 33)?;
assert_eq!(receipt.account_id, acc.id()); assert_eq!(receipt.account_id, acc.id());
assert_eq!(receipt.amount, 33); assert_eq!(receipt.amount, 33);
tx.commit()?; tx.commit().unwrap();
let tx = conn.transaction().unwrap(); let tx2 = conn.transaction().unwrap();
let acc = get_account_by_id(&tx, & acc.id())?; let acc = get_account_by_id(&tx2, &acc.id()).unwrap();
assert_eq!(acc.timestamp, receipt.timestamp); assert_eq!(acc.timestamp, receipt.timestamp);
assert_eq!(acc.amount, 33); assert_eq!(acc.amount, 33);
+4 -4
View File
@@ -11,15 +11,15 @@ pub struct SecurityEmitterCheque {
pub drawer_account_id: AccountId, pub drawer_account_id: AccountId,
pub security_id: SecurityId, pub security_id: SecurityId,
pub payee_user_id: UserId, pub payee_user_id: UserId,
pub cheque_number: u32, pub cheque_number: i64,
pub amount: u32, pub amount: i64,
} }
impl SecurityEmitterCheque { impl SecurityEmitterCheque {
pub fn new(drawer: Drawer, pub fn new(drawer: Drawer,
payee: Payee, payee: Payee,
amount: u32, amount: i64,
cheque_number: u32) -> SecurityEmitterCheque { cheque_number: i64) -> SecurityEmitterCheque {
SecurityEmitterCheque { SecurityEmitterCheque {
drawer_user_id: drawer.0, drawer_user_id: drawer.0,
drawer_account_id: drawer.1, drawer_account_id: drawer.1,
+2 -1
View File
@@ -23,7 +23,8 @@ pub mod accounts {
create_security_emitter_account, create_security_emitter_account,
is_owner, is_owner,
get_account_by_id, get_account_by_id,
list_accounts}; list_accounts,
add_to_account};
} }
pub mod cheques { pub mod cheques {
+7 -8
View File
@@ -7,13 +7,11 @@ use crate::db::external_id::AccountId;
use crate::db::securities::get_security_by_id; use crate::db::securities::get_security_by_id;
use crate::util::*; use crate::util::*;
fn _add_securities_to_account(tx: &TransactionHelper, fn _move_securities(tx: &TransactionHelper,
cheque: &SecurityEmitterCheque, cheque: &SecurityEmitterCheque,
account_id: &AccountId) -> CustomResult<()> { account_id: &AccountId) -> CustomResult<()> {
let q = "UPDATE accounts accounts::add_to_account(tx, &cheque.drawer_account_id, -cheque.amount)?;
SET amount = amount + ?1 accounts::add_to_account(tx, account_id, cheque.amount)?;
WHERE id = ?2";
tx.execute(q, params![cheque.amount, account_id])?;
Ok(()) Ok(())
} }
@@ -54,8 +52,9 @@ fn cash_cheque(tx: &Transaction,
} }
let myx = TransactionHelper::new(tx); let myx = TransactionHelper::new(tx);
_add_securities_to_account(&myx, cheque, account_id)?; _move_securities(&myx, cheque, account_id)?;
_add_cheque_to_cashed_cheques_log(&myx, cheque, account_id)?; _add_cheque_to_cashed_cheques_log(&myx, cheque, account_id)?;
// savepoint.rollback().map_err(Error::SqlError)?; // savepoint.rollback().map_err(Error::SqlError)?;
// create transaction loggetAllPurchaseHistory // create transaction loggetAllPurchaseHistory
Ok(()) Ok(())
@@ -119,7 +118,7 @@ mod test_caching_cheques {
#[test] #[test]
fn test_double_spend() -> CustomResult<()> { fn test_double_spend() -> CustomResult<()> {
let (mut conn, drawer, payee, payee_account) = setup_basic(); let (mut conn, drawer, payee, payee_account) = setup_basic();
let amount: u32 = 1000; let amount = 1000;
let mut tx = conn.transaction().unwrap(); let mut tx = conn.transaction().unwrap();
let cheque = SecurityEmitterCheque::new( let cheque = SecurityEmitterCheque::new(
@@ -143,7 +142,7 @@ mod test_caching_cheques {
#[test] #[test]
fn test_invalid_drawer() -> CustomResult<()> { fn test_invalid_drawer() -> CustomResult<()> {
let (mut conn, drawer, payee, payee_account) = setup_basic(); let (mut conn, drawer, payee, payee_account) = setup_basic();
let amount: u32 = 1000; let amount = 1000;
let mut tx = conn.transaction().unwrap(); let mut tx = conn.transaction().unwrap();
let not_currency_owner_user_id = UserId::from_id(drawer.0.get_id() + 72623); let not_currency_owner_user_id = UserId::from_id(drawer.0.get_id() + 72623);
let not_currency_owner: Drawer = (not_currency_owner_user_id, drawer.1, drawer.2); let not_currency_owner: Drawer = (not_currency_owner_user_id, drawer.1, drawer.2);
+13 -1
View File
@@ -1,9 +1,10 @@
use rusqlite::{Params, Transaction}; use rusqlite::{Params, Row, Transaction};
#[derive(Debug)] #[derive(Debug)]
pub enum Error { pub enum Error {
SqlError(rusqlite::Error), SqlError(rusqlite::Error),
InvalidDrawer(String), InvalidDrawer(String),
InvalidAmount(String),
InvalidPayee(String), InvalidPayee(String),
DoubleSpendingError(String), DoubleSpendingError(String),
Testing(String), Testing(String),
@@ -23,4 +24,15 @@ impl TransactionHelper<'_> {
pub fn execute<P: Params>(&self, sql: &str, params: P) -> CustomResult<usize> { pub fn execute<P: Params>(&self, sql: &str, params: P) -> CustomResult<usize> {
self.tx.execute(sql, params).map_err(Error::SqlError) self.tx.execute(sql, params).map_err(Error::SqlError)
} }
pub fn query_row<T, P, F>(&self, sql: &str, params: P, f: F) -> CustomResult<T>
where
P: Params,
F: FnOnce(&Row<'_>) -> rusqlite::Result<T>, {
self.tx.query_row(sql, params, f).map_err(Error::SqlError)
}
//
// pub fn commit(&self) -> CustomResult<()> {
// self.tx.commit().map_err(Error::SqlError)
// }
} }