Mist, jetzt vielleicht
This commit is contained in:
26
lib/checkAblauf.ts
Normal file
26
lib/checkAblauf.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
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,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user