Skip to content

Commit

Permalink
fix: handle zero disbursed amount for check_disbursal function (#1229)
Browse files Browse the repository at this point in the history
  • Loading branch information
vindard authored Jan 9, 2025
1 parent 07372aa commit 6b5d972
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
22 changes: 21 additions & 1 deletion lana/app/src/credit_facility/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,13 @@ impl FacilityCVL {
}

fn check_disbursal_allowed(&self, terms: TermValues) -> Result<(), CreditFacilityError> {
if self.disbursed < terms.margin_call_cvl {
let cvl = if self.disbursed.is_zero() {
self.total
} else {
self.disbursed
};

if cvl < terms.margin_call_cvl {
return Err(CreditFacilityError::BelowMarginLimit);
}
Ok(())
Expand Down Expand Up @@ -1745,6 +1751,20 @@ mod test {
));
}

#[test]
fn cvl_check_disbursal_allowed_for_zero_amount() {
let terms = default_terms();

let facility_cvl = FacilityCVL {
total: terms.margin_call_cvl,
disbursed: CVLPct::ZERO,
};
assert!(matches!(
facility_cvl.check_disbursal_allowed(terms),
Ok(())
));
}

#[test]
fn check_activated_at() {
let mut credit_facility = facility_from(initial_events());
Expand Down
4 changes: 4 additions & 0 deletions lana/app/src/terms/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ impl CVLPct {
Self(Decimal::from(value))
}

pub fn is_zero(&self) -> bool {
*self == Self::ZERO
}

pub fn scale(&self, value: UsdCents) -> UsdCents {
let cents = value.to_usd() * dec!(100) * (self.0 / dec!(100));
UsdCents::from(
Expand Down

0 comments on commit 6b5d972

Please sign in to comment.