fix(migrations): adjust retention expiry trigger and validation for fiscal periods (#1104)

This commit is contained in:
Mattsson
2026-07-22 00:43:46 +02:00
committed by GitHub
parent e11f70b347
commit 3920c893f4
4 changed files with 117 additions and 2 deletions
+72
View File
@@ -44,4 +44,76 @@ describe('BFL retention expiry', () => {
expect(result.rows[0].retention_expires_at).toBe('2035-01-01')
})
it('does not restore the old expiry calculation on unrelated updates', async () => {
const userId = await insertAuthUser()
const companyId = await insertCompany({ createdBy: userId })
const fiscalPeriodId = await insertFiscalPeriod({
userId,
companyId,
periodStart: '2025-07-01',
periodEnd: '2026-06-30',
name: '2025/2026',
})
await getPool().query(
`UPDATE public.fiscal_periods
SET name = 'Updated 2025/2026'
WHERE id = $1`,
[fiscalPeriodId],
)
const result = await getPool().query<{ retention_expires_at: string }>(
`SELECT retention_expires_at::text
FROM public.fiscal_periods
WHERE id = $1`,
[fiscalPeriodId],
)
expect(result.rows[0].retention_expires_at).toBe('2034-01-01')
})
it('allows retention backfills when a historical mid-month start is unchanged', async () => {
const userId = await insertAuthUser()
const companyId = await insertCompany({ createdBy: userId })
const historicalPeriodId = await insertFiscalPeriod({
userId,
companyId,
periodStart: '2025-10-20',
periodEnd: '2025-12-31',
name: 'Historical period',
})
// Adding an earlier period makes the existing mid-month row match the
// historical state that used to abort unrelated fiscal-period updates.
await insertFiscalPeriod({
userId,
companyId,
periodStart: '2024-01-01',
periodEnd: '2024-12-31',
name: 'Earlier period',
})
await expect(
getPool().query(
`UPDATE public.fiscal_periods
SET retention_expires_at = make_date(
extract(year FROM period_end)::integer + 8,
1,
1
)
WHERE id = $1`,
[historicalPeriodId],
),
).resolves.toBeDefined()
const result = await getPool().query<{ retention_expires_at: string }>(
`SELECT retention_expires_at::text
FROM public.fiscal_periods
WHERE id = $1`,
[historicalPeriodId],
)
expect(result.rows[0].retention_expires_at).toBe('2033-01-01')
})
})