84 lines
1.9 KiB
TypeScript
84 lines
1.9 KiB
TypeScript
|
|
/**
|
||
|
|
* Date utilities for effective date logic and rolling windows
|
||
|
|
*/
|
||
|
|
|
||
|
|
import { addDays, isAfter, isBefore, isWithinInterval, subDays } from 'date-fns';
|
||
|
|
|
||
|
|
export function isEffectiveDate(date: Date, effectiveDate: Date, expiryDate?: Date): boolean {
|
||
|
|
if (isBefore(date, effectiveDate)) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
if (expiryDate && isAfter(date, expiryDate)) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface RollingWindow {
|
||
|
|
startDate: Date;
|
||
|
|
endDate: Date;
|
||
|
|
days: number;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function calculateRollingWindow(
|
||
|
|
referenceDate: Date,
|
||
|
|
windowDays: number
|
||
|
|
): RollingWindow {
|
||
|
|
const startDate = subDays(referenceDate, windowDays);
|
||
|
|
return {
|
||
|
|
startDate,
|
||
|
|
endDate: referenceDate,
|
||
|
|
days: windowDays,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
export function isWithinRollingWindow(
|
||
|
|
date: Date,
|
||
|
|
window: RollingWindow
|
||
|
|
): boolean {
|
||
|
|
return isWithinInterval(date, {
|
||
|
|
start: window.startDate,
|
||
|
|
end: window.endDate,
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
export function filterDatesInWindow(
|
||
|
|
dates: Date[],
|
||
|
|
window: RollingWindow
|
||
|
|
): Date[] {
|
||
|
|
return dates.filter((date) => isWithinRollingWindow(date, window));
|
||
|
|
}
|
||
|
|
|
||
|
|
export function calculateRetentionExpiry(
|
||
|
|
creationDate: Date,
|
||
|
|
retentionDays: number
|
||
|
|
): Date {
|
||
|
|
return addDays(creationDate, retentionDays);
|
||
|
|
}
|
||
|
|
|
||
|
|
export function shouldArchive(
|
||
|
|
creationDate: Date,
|
||
|
|
archivalAfterDays: number,
|
||
|
|
currentDate: Date = new Date()
|
||
|
|
): boolean {
|
||
|
|
const archivalDate = addDays(creationDate, archivalAfterDays);
|
||
|
|
return isAfter(currentDate, archivalDate);
|
||
|
|
}
|
||
|
|
|
||
|
|
export function shouldDelete(
|
||
|
|
creationDate: Date,
|
||
|
|
retentionDays: number,
|
||
|
|
currentDate: Date = new Date()
|
||
|
|
): boolean {
|
||
|
|
const expiryDate = calculateRetentionExpiry(creationDate, retentionDays);
|
||
|
|
return isAfter(currentDate, expiryDate);
|
||
|
|
}
|
||
|
|
|
||
|
|
export function formatISO20022Date(date: Date): string {
|
||
|
|
return date.toISOString().split('T')[0];
|
||
|
|
}
|
||
|
|
|
||
|
|
export function formatISO20022DateTime(date: Date): string {
|
||
|
|
return date.toISOString().split('.')[0];
|
||
|
|
}
|