27 lines
739 B
TypeScript
27 lines
739 B
TypeScript
import moment from 'moment';
|
|
import { TabletteRaw } from '@/types/tablette';
|
|
|
|
const VORLAUF = parseInt(process.env.VORLAUF || '14', 10);
|
|
|
|
export interface AblaufResult {
|
|
akt: number;
|
|
until: Date;
|
|
rtage: number;
|
|
warn: boolean;
|
|
}
|
|
|
|
export function checkAblauf(item: Pick<TabletteRaw, 'cnt' | 'pday' | 'at'>): AblaufResult {
|
|
const now = moment();
|
|
const atday = moment(item.at);
|
|
const days = moment.duration(now.diff(atday)).asDays();
|
|
const aktAnzahl = item.cnt - Math.floor(days) * item.pday;
|
|
const reichtTage = Math.floor(aktAnzahl / item.pday);
|
|
const rbis = now.add(reichtTage, 'day').startOf('day');
|
|
return {
|
|
akt: aktAnzahl,
|
|
until: rbis.toDate(),
|
|
rtage: reichtTage,
|
|
warn: reichtTage <= VORLAUF,
|
|
};
|
|
}
|