fix(finance): gracefully handle initial state without rent rules

Allow building the finance dashboard even if no rent rule is configured for the cycle.
Defaults to 0 rent in the cycle's currency. Added regression tests in application
service and miniapp dashboard handler.
This commit is contained in:
2026-03-15 01:38:54 +04:00
parent f4e5a49621
commit d0475743f8
3 changed files with 102 additions and 4 deletions

View File

@@ -842,4 +842,34 @@ describe('createFinanceCommandService', () => {
// Alice paid 1000n and her share is 1000n -> offset 0n
expect(aliceLine?.purchaseOffset.amountMinor).toBe(0n)
})
test('generateDashboard succeeds even if rent rule is missing', async () => {
const repository = new FinanceRepositoryStub()
repository.members = [
{
id: 'alice',
telegramUserId: '1',
displayName: 'Alice',
rentShareWeight: 1,
isAdmin: true
}
]
repository.openCycleRecord = {
id: 'cycle-2026-03',
period: '2026-03',
currency: 'GEL'
}
// Simulate missing rent rule
repository.rentRule = null
const service = createService(repository)
const dashboard = await service.generateDashboard()
expect(dashboard).not.toBeNull()
expect(dashboard?.period).toBe('2026-03')
expect(dashboard?.rentSourceAmount.amountMinor).toBe(0n)
expect(dashboard?.rentDisplayAmount.amountMinor).toBe(0n)
expect(dashboard?.totalDue.amountMinor).toBe(0n)
})
})

View File

@@ -312,9 +312,8 @@ async function buildFinanceDashboard(
throw new Error('No household members configured')
}
if (!rentRule) {
throw new Error('No rent rule configured for this cycle period')
}
const rentAmountMinor = rentRule?.amountMinor ?? 0n
const rentCurrency = rentRule?.currency ?? cycle.currency
const period = BillingPeriod.fromString(cycle.period)
const { start, end } = monthRange(period)
@@ -344,7 +343,7 @@ async function buildFinanceDashboard(
period,
lockDay: settings.rentWarningDay,
timezone: settings.timezone,
amount: Money.fromMinor(rentRule.amountMinor, rentRule.currency)
amount: Money.fromMinor(rentAmountMinor, rentCurrency)
})
const convertedUtilityBills = await Promise.all(