Add account type
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
//! # Project constants
|
||||
|
||||
use crate::models::accounts::AccountType;
|
||||
|
||||
/// Length of generated tokens
|
||||
pub const TOKENS_LEN: usize = 50;
|
||||
|
||||
@@ -24,3 +26,30 @@ pub const MAX_UPLOAD_FILE_SIZE: usize = 15 * 1024 * 1024;
|
||||
|
||||
/// Minimum elapsed time before a file can be deleted by garbage collector (2 hours)
|
||||
pub const MIN_FILE_LIFETIME: u64 = 3600 * 2;
|
||||
|
||||
/// Description of an account type
|
||||
#[derive(serde::Serialize, Debug)]
|
||||
pub struct AccountTypeDesc {
|
||||
label: &'static str,
|
||||
code: AccountType,
|
||||
icon: &'static str,
|
||||
}
|
||||
|
||||
/// Enumeration of accounts types
|
||||
pub const ACCOUNT_TYPES: [AccountTypeDesc; 3] = [
|
||||
AccountTypeDesc {
|
||||
label: "Cash",
|
||||
code: AccountType::Cash,
|
||||
icon: include_str!("../assets/cash.svg"),
|
||||
},
|
||||
AccountTypeDesc {
|
||||
label: "Bank",
|
||||
code: AccountType::Bank,
|
||||
icon: include_str!("../assets/credit-card.svg"),
|
||||
},
|
||||
AccountTypeDesc {
|
||||
label: "Saving",
|
||||
code: AccountType::Saving,
|
||||
icon: include_str!("../assets/saving.svg"),
|
||||
},
|
||||
];
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use crate::app_config::AppConfig;
|
||||
use crate::constants::{ACCOUNT_TYPES, AccountTypeDesc};
|
||||
use actix_web::HttpResponse;
|
||||
|
||||
/// Serve robots.txt (disallow ranking)
|
||||
@@ -57,6 +58,7 @@ impl Default for ServerConstraints {
|
||||
struct ServerConfig {
|
||||
auth_disabled: bool,
|
||||
oidc_provider_name: &'static str,
|
||||
accounts_types: &'static [AccountTypeDesc],
|
||||
constraints: ServerConstraints,
|
||||
}
|
||||
|
||||
@@ -66,6 +68,7 @@ impl Default for ServerConfig {
|
||||
auth_disabled: AppConfig::get().is_auth_disabled(),
|
||||
oidc_provider_name: AppConfig::get().openid_provider().name,
|
||||
constraints: Default::default(),
|
||||
accounts_types: &ACCOUNT_TYPES,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,31 @@
|
||||
use crate::models::users::UserID;
|
||||
use crate::schema::*;
|
||||
use diesel::prelude::*;
|
||||
use std::fmt::{Display, Formatter};
|
||||
|
||||
#[derive(Debug, Clone, Copy, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
|
||||
pub struct AccountID(pub i32);
|
||||
|
||||
#[derive(Debug, Clone, Copy, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
|
||||
pub enum AccountType {
|
||||
#[serde(rename = "C")]
|
||||
Cash,
|
||||
#[serde(rename = "B")]
|
||||
Bank,
|
||||
#[serde(rename = "S")]
|
||||
Saving,
|
||||
}
|
||||
|
||||
impl Display for AccountType {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"{}",
|
||||
serde_json::to_string(self).unwrap().replace('"', "")
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Queryable, Debug, Clone, serde::Serialize)]
|
||||
pub struct Account {
|
||||
id: i32,
|
||||
@@ -12,6 +33,7 @@ pub struct Account {
|
||||
user_id: i32,
|
||||
pub time_create: i64,
|
||||
pub time_update: i64,
|
||||
r#type: String,
|
||||
pub default_account: bool,
|
||||
}
|
||||
|
||||
@@ -23,13 +45,22 @@ impl Account {
|
||||
pub fn user_id(&self) -> UserID {
|
||||
UserID(self.user_id)
|
||||
}
|
||||
|
||||
pub fn account_type(&self) -> AccountType {
|
||||
serde_json::from_str(format!("\"{}\"", self.r#type).as_str()).unwrap()
|
||||
}
|
||||
|
||||
pub fn set_account_type(&mut self, t: AccountType) {
|
||||
self.r#type = t.to_string();
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Insertable)]
|
||||
#[derive(Insertable, Debug)]
|
||||
#[diesel(table_name = accounts)]
|
||||
pub struct NewAccount<'a> {
|
||||
pub name: &'a str,
|
||||
pub user_id: i32,
|
||||
pub time_create: i64,
|
||||
pub time_update: i64,
|
||||
pub type_: String,
|
||||
}
|
||||
|
||||
@@ -8,6 +8,9 @@ diesel::table! {
|
||||
user_id -> Int4,
|
||||
time_create -> Int8,
|
||||
time_update -> Int8,
|
||||
#[sql_name = "type"]
|
||||
#[max_length = 1]
|
||||
type_ -> Varchar,
|
||||
default_account -> Bool,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use crate::connections::db_connection::db;
|
||||
use crate::controllers::server_controller::ServerConstraints;
|
||||
use crate::models::accounts::{Account, AccountID, NewAccount};
|
||||
use crate::models::accounts::{Account, AccountID, AccountType, NewAccount};
|
||||
use crate::models::users::UserID;
|
||||
use crate::schema::accounts;
|
||||
use crate::utils::time_utils::time;
|
||||
@@ -10,6 +10,7 @@ use diesel::prelude::*;
|
||||
#[derive(serde::Deserialize)]
|
||||
pub struct UpdateAccountQuery {
|
||||
pub name: String,
|
||||
pub r#type: AccountType,
|
||||
}
|
||||
|
||||
impl UpdateAccountQuery {
|
||||
@@ -31,6 +32,7 @@ pub async fn create(user_id: UserID, query: &UpdateAccountQuery) -> anyhow::Resu
|
||||
user_id: user_id.0,
|
||||
time_create: time() as i64,
|
||||
time_update: time() as i64,
|
||||
type_: query.r#type.to_string(),
|
||||
};
|
||||
|
||||
let res: Account = diesel::insert_into(accounts::table)
|
||||
@@ -48,6 +50,7 @@ pub async fn update(id: AccountID, q: &UpdateAccountQuery) -> anyhow::Result<()>
|
||||
.set((
|
||||
accounts::dsl::time_update.eq(time() as i64),
|
||||
accounts::dsl::name.eq(&q.name),
|
||||
accounts::dsl::type_.eq(&q.r#type.to_string()),
|
||||
))
|
||||
.execute(&mut db()?)?;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user