homeman-deno/models.ts
2024-01-21 14:23:45 -06:00

68 lines
1.4 KiB
TypeScript

import { z } from 'https://deno.land/x/zod@v3.21.4/mod.ts'
const User = z.object({
id: z.string().ulid().describe('primary'),
createdAt: z.date(),
name: z.string(),
avatarUrl: z.string(),
color: z.string(),
})
export const UserModel = User
export type User = z.infer<typeof User>
export type UserWithTodos = User & {
assignedTodos: Todo[]
}
const Todo = z.object({
id: z.string().ulid().describe('primary'),
createdAt: z.date(),
description: z.string(),
emoji: z.string().nullable(),
doneAt: z.date().nullable(),
assigneeUserId: z.string().ulid().nullable(),
})
export const TodoModel = Todo
export type Todo = z.infer<typeof Todo>
const DailyPhase = z.enum([
'Morning',
'Midday',
'Evening',
'Bedtime',
'Night',
])
export const DailyPhaseModel = DailyPhase
export type DailyPhase = z.infer<typeof DailyPhase>
export function toPhase(dt?: Date | null): z.infer<typeof DailyPhase> {
const d = dt || new Date()
const h = d.getHours()
if (h >= 6 && h < 12) {
return 'Morning'
} else if (h >= 12 && h < 17) {
return 'Midday'
} else if (h >= 17 && h < 19) {
return 'Evening'
} else if (h >= 19 && h < 23) {
return 'Bedtime'
}
// if (h >= 21 || h < 6) {
return 'Night'
// } else
}
const Task = z.object({
id: z.string(),
description: z.string(),
emoji: z.string().nullable(),
doneAt: z.date().nullable(),
phase: DailyPhase,
})
export const TaskModel = Task
export type Task = z.infer<typeof Task>