I should be working by now.
This commit is contained in:
1 parent
2715dc20fa
commit
77d0442a3a
3 files changed
+89
-33
No files matched your search
+37
-15
@@ -5,7 +5,7 @@ use crate::db::_external_id::{AccountId, Id, UserId};
|
||||
use crate::db::_securities::Security;
|
||||
use crate::db::_users::User;
|
||||
use crate::db::external_id::SecurityId;
|
||||
use crate::util::{CustomResult, TransactionHelper};
|
||||
use crate::util::{CustomResult, Error, TransactionHelper};
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
pub enum AccountType {
|
||||
@@ -73,7 +73,7 @@ pub fn list_accounts<'a>(conn: &Connection) -> Result<Vec<Account>> {
|
||||
return Ok(ret);
|
||||
}
|
||||
|
||||
pub fn get_account_by_id(tx: &Transaction, id: &AccountId) -> Result<Account> {
|
||||
pub fn get_account_by_id(tx: &Connection, id: &AccountId) -> Result<Account> {
|
||||
let q = "SELECT acc.id AS acc_id,
|
||||
acc.amount AS acc_amount,
|
||||
UNIXEPOCH(acc.timestamp) AS acc_timestamp,
|
||||
@@ -88,7 +88,7 @@ pub fn get_account_by_id(tx: &Transaction, id: &AccountId) -> Result<Account> {
|
||||
return tx.query_row(q, params![id], Account::from_row);
|
||||
}
|
||||
|
||||
fn create_account(tx: &Transaction, sec: &SecurityId, user: &UserId, account_type: AccountType) -> Result<Account> {
|
||||
fn create_account(tx: &Connection, sec: &SecurityId, user: &UserId, account_type: AccountType) -> Result<Account> {
|
||||
let q = "INSERT INTO accounts(security, amount, timestamp, account_type)
|
||||
VALUES (?1, 0, CURRENT_TIMESTAMP, ?2)";
|
||||
let account_type = match account_type {
|
||||
@@ -104,15 +104,15 @@ fn create_account(tx: &Transaction, sec: &SecurityId, user: &UserId, account_typ
|
||||
return get_account_by_id(tx, &id);
|
||||
}
|
||||
|
||||
pub fn create_checking_account(tx: &Transaction, sec: &SecurityId, user: &UserId) -> Result<Account> {
|
||||
pub fn create_checking_account(tx: &Connection, sec: &SecurityId, user: &UserId) -> Result<Account> {
|
||||
create_account(tx, sec, user, AccountType::Checking)
|
||||
}
|
||||
|
||||
pub fn create_security_emitter_account(tx: &Transaction, sec: &SecurityId, user: &UserId) -> Result<Account> {
|
||||
pub fn create_security_emitter_account(tx: &Connection, sec: &SecurityId, user: &UserId) -> Result<Account> {
|
||||
create_account(tx, sec, user, AccountType::SecurityEmitter)
|
||||
}
|
||||
|
||||
pub fn is_owner(tx: &Transaction, account_id: &AccountId, user_id: &UserId) -> Result<bool> {
|
||||
pub fn is_owner(tx: &Connection, 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 params = params![account_id, user_id];
|
||||
let ite = |row: &Row| row.get::<usize, i64>(0);
|
||||
@@ -121,9 +121,9 @@ pub fn is_owner(tx: &Transaction, account_id: &AccountId, user_id: &UserId) -> R
|
||||
}
|
||||
|
||||
pub struct Receipt {
|
||||
account_id: AccountId,
|
||||
timestamp: i64,
|
||||
amount: i64,
|
||||
pub account_id: AccountId,
|
||||
pub timestamp: i64,
|
||||
pub amount: i64,
|
||||
}
|
||||
|
||||
impl Receipt {
|
||||
@@ -136,17 +136,20 @@ impl Receipt {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_to_account(tx: &TransactionHelper,
|
||||
pub fn add_to_account(tx: &Connection,
|
||||
account_id: &AccountId,
|
||||
amount: i64) -> CustomResult<Receipt> {
|
||||
let q = "
|
||||
UPDATE accounts SET amount = amount + ?1,
|
||||
UPDATE accounts SET amount = CASE account_type
|
||||
WHEN 'security_emitter'
|
||||
THEN amount
|
||||
ELSE amount + ?1 END,
|
||||
timestamp = CURRENT_TIMESTAMP
|
||||
WHERE id = ?2
|
||||
RETURNING id, amount, UNIXEPOCH(CURRENT_TIMESTAMP) as timestamp
|
||||
";
|
||||
let params = params![amount, account_id];
|
||||
tx.query_row(q, params, Receipt::from_row)
|
||||
tx.query_row(q, params, Receipt::from_row).map_err(Error::SqlError)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -254,14 +257,13 @@ mod test_accounts {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_add_to_account() -> CustomResult<()> {
|
||||
fn test_add_to_checking_account() -> CustomResult<()> {
|
||||
let (mut conn, users, secs) = setup();
|
||||
let tx = conn.transaction().unwrap();
|
||||
let mtx = TransactionHelper::new(&tx);
|
||||
let sec = secs.get(0).unwrap().id();
|
||||
let user = users.get(0).unwrap().id();
|
||||
let acc = create_checking_account(&tx, &sec, &user).unwrap();
|
||||
let receipt = add_to_account(&mtx, &acc.id(), 33)?;
|
||||
let receipt = add_to_account(&tx, &acc.id(), 33)?;
|
||||
assert_eq!(receipt.account_id, acc.id());
|
||||
assert_eq!(receipt.amount, 33);
|
||||
tx.commit().unwrap();
|
||||
@@ -273,5 +275,25 @@ mod test_accounts {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_add_to_currency_emitter_account() -> CustomResult<()> {
|
||||
let (mut conn, users, secs) = setup();
|
||||
let tx = conn.transaction().unwrap();
|
||||
let sec = secs.get(0).unwrap().id();
|
||||
let user = users.get(0).unwrap().id();
|
||||
let acc = create_security_emitter_account(&tx, &sec, &user).unwrap();
|
||||
let receipt = add_to_account(&tx, &acc.id(), 33)?;
|
||||
assert_eq!(receipt.account_id, acc.id());
|
||||
assert_eq!(receipt.amount, 0);
|
||||
tx.commit().unwrap();
|
||||
|
||||
let tx2 = conn.transaction().unwrap();
|
||||
let acc = get_account_by_id(&tx2, &acc.id()).unwrap();
|
||||
assert_eq!(acc.timestamp, receipt.timestamp);
|
||||
assert_eq!(acc.amount, 0);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user