- Extract computeNextPeriodicInform to app/utils/periodic_inform.ts - 11 unit tests for the periodic computation logic - DeviceDetail: 3 date columns on same row (created, last seen, next estimated)
28 lines
943 B
TypeScript
28 lines
943 B
TypeScript
/**
|
|
* Computes the estimated next periodic inform date.
|
|
*
|
|
* @param lastPeriodicInformAt - Date of the last periodic inform
|
|
* @param periodicInformInterval - Interval in seconds (from device parameter)
|
|
* @returns The estimated next connection date, or null if cannot be computed
|
|
*/
|
|
export function computeNextPeriodicInform(
|
|
lastPeriodicInformAt: Date | string | null | undefined,
|
|
periodicInformInterval: number | string | null | undefined
|
|
): Date | null {
|
|
if (!lastPeriodicInformAt) return null
|
|
|
|
const intervalSeconds = typeof periodicInformInterval === 'string'
|
|
? parseInt(periodicInformInterval, 10)
|
|
: periodicInformInterval
|
|
|
|
if (!intervalSeconds || intervalSeconds <= 0) return null
|
|
|
|
const lastDate = lastPeriodicInformAt instanceof Date
|
|
? lastPeriodicInformAt
|
|
: new Date(lastPeriodicInformAt)
|
|
|
|
if (isNaN(lastDate.getTime())) return null
|
|
|
|
return new Date(lastDate.getTime() + intervalSeconds * 1000)
|
|
}
|