feat(medication): improve snooze timer handling
This commit is contained in:
35
utils/doseStatus.ts
Normal file
35
utils/doseStatus.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { DoseStatus } from '../types';
|
||||
|
||||
export interface DoseStatusParams {
|
||||
takenAt?: string;
|
||||
snoozedUntil?: string;
|
||||
scheduledTime: Date;
|
||||
now: Date;
|
||||
}
|
||||
|
||||
export const determineDoseStatus = ({
|
||||
takenAt,
|
||||
snoozedUntil,
|
||||
scheduledTime,
|
||||
now,
|
||||
}: DoseStatusParams): DoseStatus => {
|
||||
if (takenAt) {
|
||||
return DoseStatus.TAKEN;
|
||||
}
|
||||
|
||||
if (snoozedUntil) {
|
||||
const snoozeTime = new Date(snoozedUntil);
|
||||
if (!Number.isNaN(snoozeTime.getTime())) {
|
||||
if (snoozeTime.getTime() > now.getTime()) {
|
||||
return DoseStatus.SNOOZED;
|
||||
}
|
||||
return DoseStatus.UPCOMING;
|
||||
}
|
||||
}
|
||||
|
||||
if (scheduledTime.getTime() < now.getTime()) {
|
||||
return DoseStatus.MISSED;
|
||||
}
|
||||
|
||||
return DoseStatus.UPCOMING;
|
||||
};
|
||||
Reference in New Issue
Block a user