Changed emitter amounts to negatives values to ease tallies.

This commit is contained in:
didier amyot committed 2023-04-06 16:56:05 -04:00
1 parent 4810603dd7
commit a980b1a9b1
2 files changed
+35 -18

No files matched your search

+9 -9
View File
@@ -303,6 +303,7 @@ pub fn is_owner(tx: &Connection, account_id: &AccountId, user_id: &UserId) -> Cu
pub struct AccountChangeReceipt { pub struct AccountChangeReceipt {
pub account_id: AccountId, pub account_id: AccountId,
pub security_id: SecurityId, pub security_id: SecurityId,
pub account_type: AccountType,
pub version: i64, pub version: i64,
pub timestamp: i64, pub timestamp: i64,
pub amount: i64, pub amount: i64,
@@ -316,6 +317,7 @@ impl AccountChangeReceipt {
amount: row.get("amount")?, amount: row.get("amount")?,
timestamp: row.get("timestamp")?, timestamp: row.get("timestamp")?,
security_id: SecurityId::from_id(row.get("security_id")?), security_id: SecurityId::from_id(row.get("security_id")?),
account_type: AccountType::from_row(row)?
}) })
} }
} }
@@ -341,18 +343,16 @@ pub fn add_to_account(
amount: i64, amount: i64,
) -> CustomResult<AccountChangeReceipt> { ) -> CustomResult<AccountChangeReceipt> {
let q = " let q = "
UPDATE accounts SET amount = CASE account_type UPDATE accounts SET amount = amount + ?1,
WHEN 'security_emitter'
THEN amount
ELSE amount + ?1 END,
version = version + 1, version = version + 1,
timestamp = CURRENT_TIMESTAMP timestamp = CURRENT_TIMESTAMP
WHERE id = ?2 WHERE id = ?2
RETURNING id, RETURNING id,
version, version,
amount, amount,
UNIXEPOCH(CURRENT_TIMESTAMP) as timestamp, UNIXEPOCH(CURRENT_TIMESTAMP) AS timestamp,
security as security_id account_type AS acc_account_type,
security AS security_id
"; ";
let params = params![amount, account_id]; let params = params![amount, account_id];
let ret = tx let ret = tx
@@ -516,16 +516,16 @@ mod test_accounts {
let user = users.get(0).unwrap().id(); let user = users.get(0).unwrap().id();
let acc = create_security_emitter_account(&tx, &sec, &user)?; let acc = create_security_emitter_account(&tx, &sec, &user)?;
let receipt = add_to_account(&tx, &acc.id(), 33)?; let receipt = add_to_account(&tx, &acc.id(), -33)?;
assert_eq!(receipt.account_id, acc.id()); assert_eq!(receipt.account_id, acc.id());
assert_eq!(receipt.amount, 0); assert_eq!(receipt.amount, -33);
assert_eq!(receipt.version, 1); assert_eq!(receipt.version, 1);
tx.commit().unwrap(); tx.commit().unwrap();
let tx2 = conn.transaction().unwrap(); let tx2 = conn.transaction().unwrap();
let acc = get_account_by_id(&tx2, &acc.id()).unwrap(); 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, 0); assert_eq!(acc.amount, -33);
assert_eq!(get_account_history(&tx2, &acc.id())?.len(), 2); assert_eq!(get_account_history(&tx2, &acc.id())?.len(), 2);
Ok(()) Ok(())
+26 -9
View File
@@ -1,6 +1,7 @@
use rusqlite::{ffi, params, Connection, Savepoint, Transaction}; use rusqlite::{ffi, params, Connection, Savepoint, Transaction};
use crate::db::accounts; use crate::db::accounts;
use crate::db::accounts::AccountType;
use crate::db::cheques::{Cheque, WireTransferRequest}; use crate::db::cheques::{Cheque, WireTransferRequest};
use crate::db::external_id::AccountId; use crate::db::external_id::AccountId;
@@ -17,9 +18,9 @@ fn _transfer_securities_from_one_account_to_another(
)> { )> {
let drawer_receipt = accounts::add_to_account(tx, drawer_account_id, -amount)?; let drawer_receipt = accounts::add_to_account(tx, drawer_account_id, -amount)?;
let payee_receipt = accounts::add_to_account(tx, payee_account_id, amount)?; let payee_receipt = accounts::add_to_account(tx, payee_account_id, amount)?;
if drawer_receipt.amount < 0 { if drawer_receipt.amount < 0 && drawer_receipt.account_type != AccountType::SecurityEmitter {
let m = format!( let m = format!(
"Drawer of {:?} does not have sufficient amount in account.", "Drawer of {:} does not have sufficient amount in account.",
drawer_account_id drawer_account_id
); );
return Err(Error::InvalidAmount(m)); return Err(Error::InvalidAmount(m));
@@ -177,7 +178,7 @@ fn cash_cheque_unsafe(
if !accounts::is_owner(&savepoint, payee_account_id, &cheque.payee_user_id)? { if !accounts::is_owner(&savepoint, payee_account_id, &cheque.payee_user_id)? {
return Err(Error::InvalidPayee(format!( return Err(Error::InvalidPayee(format!(
"Trying to cash the cheque {:?} in \ "Trying to cash the cheque {:?} in \
an account {:?} that is not owned by the payee.", an account {:} that is not owned by the payee.",
cheque, payee_account_id cheque, payee_account_id
))); )));
} }
@@ -207,12 +208,12 @@ fn cash_wire_transfer_request_unsafe(
savepoint: &Savepoint, savepoint: &Savepoint,
wire_transfer: &WireTransferRequest, wire_transfer: &WireTransferRequest,
) -> CustomResult<()> { ) -> CustomResult<()> {
if wire_transfer.amount < 0 { // if wire_transfer.amount < 0 {
return Err(Error::InvalidAmount(format!( // return Err(Error::InvalidAmount(format!(
"The cheque {:?} has a negative amount.", // "The cheque {:?} has a negative amount.",
wire_transfer // wire_transfer
))); // )));
} // }
let (drawer_receipt, payee_receipt) = _transfer_wire_transfer_securities(&savepoint, wire_transfer)?; let (drawer_receipt, payee_receipt) = _transfer_wire_transfer_securities(&savepoint, wire_transfer)?;
_add_wire_transfer_request_to_wire_transfers_log(&savepoint, wire_transfer, &drawer_receipt, &payee_receipt)?; _add_wire_transfer_request_to_wire_transfers_log(&savepoint, wire_transfer, &drawer_receipt, &payee_receipt)?;
Ok(()) Ok(())
@@ -407,6 +408,22 @@ mod test_caching_cheques {
} }
} }
#[test]
fn test_unfounded_cheque() -> CustomResult<()> {
let (mut conn, drawer, payee, payee_account) = setup_basic();
let mut tx = conn.transaction().unwrap();
let cheque = Cheque::new(drawer, payee, 1_000_000, 99);
let ret = cash_cheque(&mut tx, &cheque, &payee_account);
match ret {
Err(Error::InvalidAmount(_)) => Ok(()),
_ => Err(Error::Testing(
"Expected invalid amount error here.".to_string(),
)),
}
}
#[test] #[test]
fn test_negative_security_emitter_cheque() -> CustomResult<()> { fn test_negative_security_emitter_cheque() -> CustomResult<()> {
let (mut conn, security_emitter, payee, payee_account) = setup_security_emitter(); let (mut conn, security_emitter, payee, payee_account) = setup_security_emitter();