Skip to content

Commit dd05e5c

Browse files
committed
chore: implement 'defaults_at'
1 parent b6743bd commit dd05e5c

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

core/credit/src/credit_facility/entity.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,8 @@ pub struct CreditFacility {
323323
pub activated_at: Option<DateTime<Utc>>,
324324
#[builder(setter(strip_option), default)]
325325
pub matures_at: Option<DateTime<Utc>>,
326+
#[builder(default)]
327+
pub defaults_at: Option<DateTime<Utc>>,
326328

327329
#[es_entity(nested)]
328330
#[builder(default)]
@@ -500,7 +502,9 @@ impl CreditFacility {
500502
}
501503

502504
pub fn is_defaulted(&self) -> bool {
503-
false
505+
let now = crate::time::now();
506+
self.defaults_at
507+
.is_some_and(|defaults_at| now > defaults_at)
504508
}
505509

506510
pub fn status(&self) -> CreditFacilityStatus {
@@ -563,6 +567,10 @@ impl CreditFacility {
563567

564568
self.activated_at = Some(activated_at);
565569
self.matures_at = Some(self.terms.duration.maturity_date(activated_at));
570+
self.defaults_at = self
571+
.terms
572+
.interest_overdue_duration
573+
.map(|d| d.end_date(self.matures_at.expect("No 'matures_at' date set")));
566574
let tx_id = LedgerTxId::new();
567575
self.events.push(CreditFacilityEvent::Activated {
568576
ledger_tx_id: tx_id,
@@ -1158,12 +1166,18 @@ impl TryFromEvents<CreditFacilityEvent> for CreditFacility {
11581166
.approval_process_id(*approval_process_id)
11591167
}
11601168
CreditFacilityEvent::Activated { activated_at, .. } => {
1161-
builder = builder.activated_at(*activated_at).matures_at(
1162-
terms
1163-
.expect("terms should be set")
1164-
.duration
1165-
.maturity_date(*activated_at),
1166-
)
1169+
let matures_at = terms
1170+
.expect("terms should be set")
1171+
.duration
1172+
.maturity_date(*activated_at);
1173+
let defaults_at = terms
1174+
.expect("terms should be set")
1175+
.interest_overdue_duration
1176+
.map(|d| d.end_date(matures_at));
1177+
builder = builder
1178+
.activated_at(*activated_at)
1179+
.matures_at(matures_at)
1180+
.defaults_at(defaults_at)
11671181
}
11681182
CreditFacilityEvent::ApprovalProcessConcluded { .. } => (),
11691183
CreditFacilityEvent::DisbursalInitiated { .. } => (),

core/credit/src/terms/value.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ pub enum InterestDuration {
290290
}
291291

292292
impl InterestDuration {
293-
fn end_date(&self, start_date: DateTime<Utc>) -> DateTime<Utc> {
293+
pub fn end_date(&self, start_date: DateTime<Utc>) -> DateTime<Utc> {
294294
match self {
295295
Self::Days(days) => start_date
296296
.checked_add_days(chrono::Days::new(*days))

0 commit comments

Comments
 (0)