Skip to content

Commit

Permalink
chore: core/credit compiles
Browse files Browse the repository at this point in the history
  • Loading branch information
bodymindarts committed Feb 20, 2025
1 parent 8c4cd5b commit 19505cc
Show file tree
Hide file tree
Showing 15 changed files with 89 additions and 99 deletions.
6 changes: 3 additions & 3 deletions core/credit/src/disbursal/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub enum DisbursalEvent {
idx: DisbursalIdx,
amount: UsdCents,
account_ids: CreditFacilityAccountIds,
deposit_account_id: DepositAccountId,
deposit_account_id: uuid::Uuid,
audit_info: AuditInfo,
},
ApprovalProcessConcluded {
Expand All @@ -45,7 +45,7 @@ pub struct Disbursal {
pub idx: DisbursalIdx,
pub amount: UsdCents,
pub account_ids: CreditFacilityAccountIds,
pub deposit_account_id: DepositAccountId,
pub deposit_account_id: uuid::Uuid,
#[builder(setter(strip_option), default)]
pub concluded_tx_id: Option<LedgerTxId>,
pub(super) events: EntityEvents<DisbursalEvent>,
Expand Down Expand Up @@ -182,7 +182,7 @@ pub struct NewDisbursal {
pub(super) idx: DisbursalIdx,
pub(super) amount: UsdCents,
pub(super) account_ids: CreditFacilityAccountIds,
pub(super) deposit_account_id: DepositAccountId,
pub(super) deposit_account_id: uuid::Uuid,
#[builder(setter(into))]
pub(super) audit_info: AuditInfo,
}
Expand Down
24 changes: 12 additions & 12 deletions core/credit/src/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ use super::{
pub enum CreditFacilityEvent {
Initialized {
id: CreditFacilityId,
customer_id: CustomerId,
credit_recipient_id: CreditRecipientId,
terms: TermValues,
facility: UsdCents,
account_ids: CreditFacilityAccountIds,
deposit_account_id: DepositAccountId,
deposit_account_id: uuid::Uuid,
approval_process_id: ApprovalProcessId,
audit_info: AuditInfo,
},
Expand Down Expand Up @@ -316,10 +316,10 @@ impl From<(InterestAccrualData, CreditFacilityAccountIds)> for CreditFacilityInt
pub struct CreditFacility {
pub id: CreditFacilityId,
pub approval_process_id: ApprovalProcessId,
pub customer_id: CustomerId,
pub credit_recipient_id: CreditRecipientId,
pub terms: TermValues,
pub account_ids: CreditFacilityAccountIds,
pub deposit_account_id: DepositAccountId,
pub deposit_account_id: uuid::Uuid,
#[builder(setter(strip_option), default)]
pub activated_at: Option<DateTime<Utc>>,
#[builder(setter(strip_option), default)]
Expand Down Expand Up @@ -1114,7 +1114,7 @@ impl TryFromEvents<CreditFacilityEvent> for CreditFacility {
match event {
CreditFacilityEvent::Initialized {
id,
customer_id,
credit_recipient_id,
account_ids,
deposit_account_id,
terms: t,
Expand All @@ -1124,7 +1124,7 @@ impl TryFromEvents<CreditFacilityEvent> for CreditFacility {
terms = Some(*t);
builder = builder
.id(*id)
.customer_id(*customer_id)
.credit_recipient_id(*credit_recipient_id)
.terms(*t)
.account_ids(*account_ids)
.deposit_account_id(*deposit_account_id)
Expand Down Expand Up @@ -1160,15 +1160,15 @@ pub struct NewCreditFacility {
#[builder(setter(into))]
pub(super) approval_process_id: ApprovalProcessId,
#[builder(setter(into))]
pub(super) customer_id: CustomerId,
pub(super) credit_recipient_id: CreditRecipientId,
terms: TermValues,
facility: UsdCents,
#[builder(setter(skip), default)]
pub(super) status: CreditFacilityStatus,
#[builder(setter(skip), default)]
pub(super) collateralization_state: CollateralizationState,
account_ids: CreditFacilityAccountIds,
deposit_account_id: DepositAccountId,
deposit_account_id: uuid::Uuid,
#[builder(setter(into))]
pub(super) audit_info: AuditInfo,
}
Expand All @@ -1186,7 +1186,7 @@ impl IntoEvents<CreditFacilityEvent> for NewCreditFacility {
[CreditFacilityEvent::Initialized {
id: self.id,
audit_info: self.audit_info.clone(),
customer_id: self.customer_id,
credit_recipient_id: self.credit_recipient_id,
terms: self.terms,
facility: self.facility,
account_ids: self.account_ids,
Expand Down Expand Up @@ -1255,7 +1255,7 @@ mod test {
vec![CreditFacilityEvent::Initialized {
id: CreditFacilityId::new(),
audit_info: dummy_audit_info(),
customer_id: CustomerId::new(),
credit_recipient_id: CustomerId::new(),
facility: default_facility(),
terms: default_terms(),
account_ids: CreditFacilityAccountIds::new(),
Expand Down Expand Up @@ -1940,11 +1940,11 @@ mod test {
let new_credit_facility = NewCreditFacility::builder()
.id(id)
.approval_process_id(id)
.customer_id(CustomerId::new())
.credit_recipient_id(CreditRecipientId::new())
.terms(default_terms())
.facility(UsdCents::from(1_000_000_00))
.account_ids(CreditFacilityAccountIds::new())
.deposit_account_id(DepositAccountId::new())
.deposit_account_id(uuid::Uuid::new_v4())
.audit_info(dummy_audit_info())
.build()
.expect("could not build new credit facility");
Expand Down
16 changes: 8 additions & 8 deletions core/credit/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ pub enum CreditFacilityError {
PaymentError(#[from] super::payment::error::PaymentError),
#[error("CreditFacilityError - InterestAccrualError: {0}")]
InterestAccrualError(#[from] super::interest_accrual::error::InterestAccrualError),
#[error("CreditFacilityError - DepositAccountForHolderNotFound: {0}")]
DepositAccountForHolderNotFound(DepositAccountHolderId),
#[error("CreditFacilityError - CoreDepositError: '{0}'")]
CoreDepositError(#[from] crate::deposit::error::CoreDepositError),
// #[error("CreditFacilityError - DepositAccountForHolderNotFound: {0}")]
// DepositAccountForHolderNotFound(DepositAccountHolderId),
// #[error("CreditFacilityError - CoreDepositError: '{0}'")]
// CoreDepositError(#[from] crate::deposit::error::CoreDepositError),
#[error("CreditFacilityError - ApprovalInProgress")]
ApprovalInProgress,
#[error("CreditFacilityError - Denied")]
Expand Down Expand Up @@ -68,14 +68,14 @@ pub enum CreditFacilityError {
InterestAccrualWithInvalidFutureStartDate,
#[error("CreditFacilityError - SubjectIsNotUser")]
SubjectIsNotUser,
#[error("CreditFacilityError - SubjectIsNotCustomer")]
SubjectIsNotCustomer,
#[error("CreditFacilityError - SubjectIsNotCreditRecipientId")]
SubjectIsNotCreditRecipient,
#[error(
"CreditFacilityError - DisbursalAmountTooLarge: amount '{0}' is larger than facility balance '{1}'"
)]
DisbursalAmountTooLarge(UsdCents, UsdCents),
#[error("CreditFacilityError - CustomerMismatchForCreditFacility")]
CustomerMismatchForCreditFacility,
#[error("CreditFacilityError - CreditRecipientMismatchForCreditFacility")]
CreditRecipientMismatchForCreditFacility,
}

es_entity::from_es_entity_error!(CreditFacilityError);
12 changes: 6 additions & 6 deletions core/credit/src/for_subject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ where
Perms: PermissionCheck,
E: OutboxEventMarker<CoreCreditEvent>,
{
customer_id: CustomerId,
credit_recipient_id: CreditRecipientId,
subject: &'a <<Perms as PermissionCheck>::Audit as AuditSvc>::Subject,
authz: &'a Perms,
credit_facilities: &'a CreditFacilityRepo<E>,
Expand All @@ -26,14 +26,14 @@ where
{
pub(super) fn new(
subject: &'a <<Perms as PermissionCheck>::Audit as AuditSvc>::Subject,
customer_id: CustomerId,
credit_recipient_id: CreditRecipientId,
authz: &'a Perms,
credit_facilities: &'a CreditFacilityRepo<E>,
disbursals: &'a DisbursalRepo,
payments: &'a PaymentRepo,
) -> Self {
Self {
customer_id,
credit_recipient_id,
subject,
authz,
credit_facilities,
Expand Down Expand Up @@ -61,7 +61,7 @@ where
.await?;

self.credit_facilities
.list_for_customer_id_by_created_at(self.customer_id, query, direction)
.list_for_credit_recipient_id_by_created_at(self.credit_recipient_id, query, direction)
.await
}

Expand Down Expand Up @@ -108,12 +108,12 @@ where
object: CoreCreditObject,
action: CoreCreditAction,
) -> Result<(), CreditFacilityError> {
if credit_facility.customer_id != self.customer_id {
if credit_facility.credit_recipient_id != self.credit_recipient_id {
self.authz
.audit()
.record_entry(self.subject, object, action, false)
.await?;
return Err(CreditFacilityError::CustomerMismatchForCreditFacility);
return Err(CreditFacilityError::CreditRecipientMismatchForCreditFacility);
}

self.authz
Expand Down
49 changes: 24 additions & 25 deletions core/credit/src/ledger/credit_facility_accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

use cala_ledger::AccountId as LedgerAccountId;
use chart_of_accounts::TransactionAccountFactory;
use chart_of_accounts::{CoreChartOfAccounts, TransactionAccountFactory};

use crate::{
accounting_init::CreditFacilitiesAccountPaths,
chart_of_accounts::ChartOfAccounts,
// accounting_init::CreditFacilitiesAccountPaths,
primitives::{LedgerTxId, Satoshis, UsdCents},
terms::InterestPeriod,
};
Expand Down Expand Up @@ -47,28 +46,28 @@ pub struct CreditFacilityAccountFactories {
pub fee_income: TransactionAccountFactory,
}

impl CreditFacilityAccountFactories {
pub fn new(
chart_of_accounts: &ChartOfAccounts,
credit_facilities: CreditFacilitiesAccountPaths,
) -> Self {
Self {
facility: chart_of_accounts.transaction_account_factory(credit_facilities.facility),
facility_omnibus: chart_of_accounts
.transaction_account_factory(credit_facilities.facility_omnibus),
disbursed_receivable: chart_of_accounts
.transaction_account_factory(credit_facilities.disbursed_receivable),
collateral: chart_of_accounts.transaction_account_factory(credit_facilities.collateral),
collateral_omnibus: chart_of_accounts
.transaction_account_factory(credit_facilities.collateral_omnibus),
interest_receivable: chart_of_accounts
.transaction_account_factory(credit_facilities.interest_receivable),
interest_income: chart_of_accounts
.transaction_account_factory(credit_facilities.interest_income),
fee_income: chart_of_accounts.transaction_account_factory(credit_facilities.fee_income),
}
}
}
// impl CreditFacilityAccountFactories {
// pub fn new(
// chart_of_accounts: &ChartOfAccounts,
// credit_facilities: CreditFacilitiesAccountPaths,
// ) -> Self {
// Self {
// facility: chart_of_accounts.transaction_account_factory(credit_facilities.facility),
// facility_omnibus: chart_of_accounts
// .transaction_account_factory(credit_facilities.facility_omnibus),
// disbursed_receivable: chart_of_accounts
// .transaction_account_factory(credit_facilities.disbursed_receivable),
// collateral: chart_of_accounts.transaction_account_factory(credit_facilities.collateral),
// collateral_omnibus: chart_of_accounts
// .transaction_account_factory(credit_facilities.collateral_omnibus),
// interest_receivable: chart_of_accounts
// .transaction_account_factory(credit_facilities.interest_receivable),
// interest_income: chart_of_accounts
// .transaction_account_factory(credit_facilities.interest_income),
// fee_income: chart_of_accounts.transaction_account_factory(credit_facilities.fee_income),
// }
// }
// }
#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
pub struct CreditFacilityLedgerBalance {
pub facility: UsdCents,
Expand Down
2 changes: 1 addition & 1 deletion core/credit/src/ledger/velocity/disbursal_limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ impl DisbursalLimit {
#[instrument(name = "ledger.disbursal_limit.init", skip_all)]
pub async fn init(
ledger: &CalaLedger,
) -> Result<VelocityLimitId, crate::credit_facility::ledger::CreditLedgerError> {
) -> Result<VelocityLimitId, crate::ledger::CreditLedgerError> {
let limit = NewVelocityLimit::builder()
.id(DISBURSAL_LIMIT_ID)
.name("Disbursal Limit")
Expand Down
41 changes: 15 additions & 26 deletions core/credit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,17 @@ where

pub fn for_subject<'s>(
&'s self,
sub: &<<Perms as PermissionCheck>::Audit as AuditSvc>::Subject,
) -> Result<CreditFacilitiesForSubject<'s, Perms, E>, CreditFacilityError> {
let customer_id =
CustomerId::try_from(sub).map_err(|_| CreditFacilityError::SubjectIsNotCustomer)?;
sub: &'s <<Perms as PermissionCheck>::Audit as AuditSvc>::Subject,
) -> Result<CreditFacilitiesForSubject<'s, Perms, E>, CreditFacilityError>
where
CreditRecipientId:
for<'a> TryFrom<&'a <<Perms as PermissionCheck>::Audit as AuditSvc>::Subject>,
{
let credit_recipient_id = CreditRecipientId::try_from(sub)
.map_err(|_| CreditFacilityError::SubjectIsNotCreditRecipient)?;
Ok(CreditFacilitiesForSubject::new(
sub,
customer_id,
credit_recipient_id,
&self.authz,
&self.credit_facility_repo,
&self.disbursal_repo,
Expand All @@ -242,7 +246,10 @@ where
pub async fn initiate(
&self,
sub: &<<Perms as PermissionCheck>::Audit as AuditSvc>::Subject,
customer_id: impl Into<CustomerId> + Into<DepositAccountHolderId> + std::fmt::Debug + Copy,
credit_recipient_id: impl Into<CreditRecipientId>
+ Into<DepositAccountHolderId>
+ std::fmt::Debug
+ Copy,
facility: UsdCents,
terms: TermValues,
) -> Result<CreditFacility, CreditFacilityError> {
Expand All @@ -251,33 +258,15 @@ where
.await?
.expect("audit info missing");

let deposit_accounts: Vec<DepositAccount> = self
.deposits
.list_accounts_by_created_at_for_account_holder(
sub,
customer_id,
Default::default(),
ListDirection::Descending,
)
.await?
.entities
.into_iter()
.map(DepositAccount::from)
.collect();

let deposit_account = deposit_accounts.first().ok_or(
CreditFacilityError::DepositAccountForHolderNotFound(customer_id.into()),
)?;

let id = CreditFacilityId::new();
let new_credit_facility = NewCreditFacility::builder()
.id(id)
.approval_process_id(id)
.customer_id(customer_id)
.credit_recipient_id(credit_recipient_id)
.terms(terms)
.facility(facility)
.account_ids(CreditFacilityAccountIds::new())
.deposit_account_id(deposit_account.id)
.deposit_account_id(uuid::Uuid::new_v4())
.audit_info(audit_info.clone())
.build()
.expect("could not build new credit facility");
Expand Down
6 changes: 3 additions & 3 deletions core/credit/src/payment/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub enum PaymentEvent {
facility_id: CreditFacilityId,
amounts: CreditFacilityPaymentAmounts,
account_ids: CreditFacilityAccountIds,
deposit_account_id: DepositAccountId,
deposit_account_id: uuid::Uuid,
audit_info: AuditInfo,
},
}
Expand All @@ -32,7 +32,7 @@ pub struct Payment {
pub facility_id: CreditFacilityId,
pub amounts: CreditFacilityPaymentAmounts,
pub account_ids: CreditFacilityAccountIds,
pub deposit_account_id: DepositAccountId,
pub deposit_account_id: uuid::Uuid,

pub(super) events: EntityEvents<PaymentEvent>,
}
Expand Down Expand Up @@ -87,7 +87,7 @@ pub struct NewPayment {
pub(super) credit_facility_id: CreditFacilityId,
pub(super) amounts: CreditFacilityPaymentAmounts,
pub(super) account_ids: CreditFacilityAccountIds,
pub(super) deposit_account_id: DepositAccountId,
pub(super) deposit_account_id: uuid::Uuid,
#[builder(setter(into))]
pub(super) audit_info: AuditInfo,
}
Expand Down
Loading

0 comments on commit 19505cc

Please sign in to comment.