Skip to content

Commit

Permalink
fix: use deposits account for Customer account (#1231)
Browse files Browse the repository at this point in the history
* refactor: move accounting_init primitives to module

* fix: use deposit account as customer checking account

* chore: remove unused 'ledger' from Customer module

* chore: fix error message
  • Loading branch information
vindard authored Jan 9, 2025
1 parent 6968df1 commit b37a73f
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 95 deletions.
2 changes: 2 additions & 0 deletions core/deposit/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use thiserror::Error;
pub enum CoreDepositError {
#[error("CoreDepositError - Sqlx: {0}")]
Sqlx(#[from] sqlx::Error),
#[error("CoreChartOfAccountError - AuditError: {0}")]
AuditError(#[from] audit::error::AuditError),
#[error("CoreDepositError - AuthorizationError: {0}")]
AuthorizationError(#[from] authz::error::AuthorizationError),
#[error("CoreDepositError - DepositAccountError: {0}")]
Expand Down
51 changes: 49 additions & 2 deletions core/deposit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ where
.description(description.to_string())
.audit_info(audit_info.clone())
.build()
.expect("Could not build new committee");
.expect("Could not build new account");

let mut op = self.accounts.begin_op().await?;
let account = self.accounts.create_in_op(&mut op, new_account).await?;
Expand All @@ -178,6 +178,53 @@ where
Ok(account)
}

#[instrument(name = "deposit.create_account_from_system", skip(self))]
pub async fn create_account_from_system(
&self,
holder_id: impl Into<DepositAccountHolderId> + std::fmt::Debug,
name: &str,
description: &str,
) -> Result<DepositAccount, CoreDepositError> {
let mut op = self.accounts.begin_op().await?;

let audit_info = self
.authz
.audit()
.record_system_entry_in_tx(
op.tx(),
CoreDepositObject::all_deposit_accounts(),
CoreDepositAction::DEPOSIT_ACCOUNT_CREATE,
)
.await?;

let account_id = DepositAccountId::new();
let new_account = NewDepositAccount::builder()
.id(account_id)
.account_holder_id(holder_id)
.name(name.to_string())
.description(description.to_string())
.audit_info(audit_info.clone())
.build()
.expect("Could not build new account");

let account = self.accounts.create_in_op(&mut op, new_account).await?;

let mut op = self.cala.ledger_operation_from_db_op(op);
self.account_factory
.create_transaction_account_in_op(
&mut op,
account_id,
&account.name,
&account.description,
audit_info,
)
.await?;

op.commit().await?;

Ok(account)
}

#[instrument(name = "deposit.record_deposit", skip(self))]
pub async fn record_deposit(
&self,
Expand All @@ -204,7 +251,7 @@ where
.reference(reference)
.audit_info(audit_info)
.build()
.expect("Could not build new committee");
.expect("Could not build new account");

let mut op = self.deposits.begin_op().await?;
let deposit = self.deposits.create_in_op(&mut op, new_deposit).await?;
Expand Down
4 changes: 3 additions & 1 deletion lana/app/src/accounting_init/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
mod constants;
mod primitives;
mod seed;

pub mod error;

use chart_of_accounts::{ChartId, ChartOfAccountCode};

use crate::{app::primitives::*, chart_of_accounts::ChartOfAccounts};
use crate::chart_of_accounts::ChartOfAccounts;

use cala_ledger::{CalaLedger, JournalId};

use error::*;
use primitives::*;

#[derive(Clone)]
pub struct AccountingInit {
Expand Down
File renamed without changes.
4 changes: 1 addition & 3 deletions lana/app/src/app/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
mod config;
mod error;
pub mod primitives;

use sqlx::PgPool;
use tracing::instrument;
Expand Down Expand Up @@ -99,8 +98,7 @@ impl LanaApp {
String::from("OMNIBUS_ACCOUNT_ID"),
)
.await?;
let customers =
Customers::init(&pool, &config.customer, &cala, &deposits, &authz, &export).await?;
let customers = Customers::new(&pool, &config.customer, &deposits, &authz, &export);
let applicants = Applicants::new(&pool, &config.sumsub, &customers, &jobs, &export);

let collateral_factory = chart_of_accounts.transaction_account_factory(
Expand Down
2 changes: 1 addition & 1 deletion lana/app/src/credit_facility/disbursal/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ mod test {
idx: DisbursalIdx::FIRST,
amount: UsdCents::from(100_000),
account_ids: CreditFacilityAccountIds::new(),
customer_account_ids: CustomerAccountIds::new(),
customer_account_ids: CustomerAccountIds::new(DepositAccountId::new()),
audit_info: dummy_audit_info(),
}]
}
Expand Down
4 changes: 2 additions & 2 deletions lana/app/src/credit_facility/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1281,7 +1281,7 @@ mod test {
facility: default_facility(),
terms: default_terms(),
account_ids: CreditFacilityAccountIds::new(),
customer_account_ids: CustomerAccountIds::new(),
customer_account_ids: CustomerAccountIds::new(DepositAccountId::new()),
},
CreditFacilityEvent::ApprovalProcessStarted {
approval_process_id: ApprovalProcessId::new(),
Expand Down Expand Up @@ -1975,7 +1975,7 @@ mod test {
.terms(default_terms())
.facility(UsdCents::from(1_000_000_00))
.account_ids(CreditFacilityAccountIds::new())
.customer_account_ids(CustomerAccountIds::new())
.customer_account_ids(CustomerAccountIds::new(DepositAccountId::new()))
.audit_info(dummy_audit_info())
.build()
.expect("could not build new credit facility");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ pub struct CustomerAccountIds {

impl CustomerAccountIds {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
pub fn new(deposit_account_id: impl Into<LedgerAccountId>) -> Self {
Self {
deposit_account_id: LedgerAccountId::new(),
deposit_account_id: deposit_account_id.into(),
}
}
}
2 changes: 1 addition & 1 deletion lana/app/src/customer/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use es_entity::*;

use crate::{audit::AuditInfo, primitives::*};

use super::ledger::CustomerAccountIds;
use super::accounts::CustomerAccountIds;

#[derive(EsEvent, Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
Expand Down
4 changes: 0 additions & 4 deletions lana/app/src/customer/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ pub enum CustomerError {
EsEntityError(es_entity::EsEntityError),
#[error("CustomerError - CursorDestructureError: {0}")]
CursorDestructureError(#[from] es_entity::CursorDestructureError),
#[error("CustomerError - LedgerError: {0}")]
LedgerError(#[from] crate::ledger::error::LedgerError),
#[error("CustomerError - UnexpectedCurrency")]
UnexpectedCurrency,
#[error("CustomerError - KratosClientError: {0}")]
Expand All @@ -22,8 +20,6 @@ pub enum CustomerError {
JobError(#[from] crate::job::error::JobError),
#[error("CustomerError - DepositError: {0}")]
DepositError(#[from] crate::deposit::error::CoreDepositError),
#[error("CustomerError - CustomerLedgerError: {0}")]
CustomerLedgerError(#[from] super::ledger::error::CustomerLedgerError),
}

es_entity::from_es_entity_error!(CustomerError);
11 changes: 0 additions & 11 deletions lana/app/src/customer/ledger/error.rs

This file was deleted.

44 changes: 0 additions & 44 deletions lana/app/src/customer/ledger/mod.rs

This file was deleted.

50 changes: 26 additions & 24 deletions lana/app/src/customer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
pub mod accounts;
mod config;
mod entity;
pub mod error;
mod kratos;
pub mod ledger;
mod repo;

use deposit::DepositAccount;
use std::collections::HashMap;
use tracing::instrument;

use authz::PermissionCheck;
use cala_ledger::CalaLedger;

use crate::{
audit::{AuditInfo, AuditSvc},
Expand All @@ -19,42 +19,37 @@ use crate::{
primitives::{CustomerId, KycLevel, Subject},
};

pub use accounts::CustomerAccountIds;
pub use config::*;
pub use entity::*;
use error::CustomerError;
use kratos::*;
pub use ledger::CustomerAccountIds;
use ledger::CustomerLedger;
pub use repo::{customer_cursor::*, CustomerRepo, CustomersSortBy, FindManyCustomers, Sort};

#[derive(Clone)]
pub struct Customers {
repo: CustomerRepo,
ledger: CustomerLedger,
deposit: Deposits,
kratos: KratosClient,
authz: Authorization,
}

impl Customers {
pub async fn init(
pub fn new(
pool: &sqlx::PgPool,
config: &CustomerConfig,
cala: &CalaLedger,
deposits: &Deposits,
authz: &Authorization,
export: &Export,
) -> Result<Self, CustomerError> {
) -> Self {
let repo = CustomerRepo::new(pool, export);
let kratos = KratosClient::new(&config.kratos);
let ledger = CustomerLedger::init(cala).await?;
Ok(Self {
Self {
repo,
ledger,
kratos,
authz: authz.clone(),
deposit: deposits.clone(),
})
}
}

pub fn repo(&self) -> &CustomerRepo {
Expand Down Expand Up @@ -90,25 +85,26 @@ impl Customers {
.expect("audit info missing");
let customer_id: uuid::Uuid = self.kratos.create_identity(&email).await?;
let account_name = &format!("Deposit Account for Customer {}", customer_id);
self.deposit
let DepositAccount {
id: deposit_account_id,
..
} = self
.deposit
.create_account(sub, customer_id, account_name, account_name)
.await?;

let mut db = self.repo.begin_op().await?;
let new_customer = NewCustomer::builder()
.id(customer_id)
.email(email)
.telegram_id(telegram_id)
.account_ids(CustomerAccountIds::new())
.account_ids(CustomerAccountIds::new(deposit_account_id))
.audit_info(audit_info)
.build()
.expect("Could not build customer");

let mut db = self.repo.begin_op().await?;
let customer = self.repo.create_in_op(&mut db, new_customer).await?;

self.ledger
.create_accounts_for_customer(db, customer_id.into(), customer.account_ids)
.await?;
db.commit().await?;

Ok(customer)
}
Expand All @@ -130,19 +126,25 @@ impl Customers {
)
.await?;

let account_name = &format!("Deposit Account for Customer {}", id);
let DepositAccount {
id: deposit_account_id,
..
} = self
.deposit
.create_account_from_system(id, account_name, account_name)
.await?;

let new_customer = NewCustomer::builder()
.id(id)
.email(email)
.account_ids(CustomerAccountIds::new())
.account_ids(CustomerAccountIds::new(deposit_account_id))
.audit_info(audit_info.clone())
.build()
.expect("Could not build customer");

let customer = self.repo.create_in_op(&mut db, new_customer).await?;

self.ledger
.create_accounts_for_customer(db, id, customer.account_ids)
.await?;
db.commit().await?;

Ok(customer)
}
Expand Down

0 comments on commit b37a73f

Please sign in to comment.