2024-03-22 10:23:28 +01:00
|
|
|
import { z } from "zod"
|
|
|
|
|
2024-04-04 12:31:56 +02:00
|
|
|
import { GoalCreateSchema, type Goal, type GoalBaseJSON } from "./Goal"
|
2024-03-22 10:23:28 +01:00
|
|
|
import { Entity, EntitySchema } from "./_Entity"
|
|
|
|
|
|
|
|
export const HabitSchema = EntitySchema.extend({
|
|
|
|
userId: z.string(),
|
|
|
|
name: z.string().min(1).max(50),
|
|
|
|
color: z.string().min(4).max(9).regex(/^#/),
|
|
|
|
icon: z.string().min(1),
|
|
|
|
})
|
|
|
|
|
2024-04-04 12:31:56 +02:00
|
|
|
export const HabitCreateSchema = HabitSchema.extend({
|
|
|
|
goal: GoalCreateSchema,
|
|
|
|
}).omit({ id: true })
|
2024-03-22 10:23:28 +01:00
|
|
|
export type HabitCreateData = z.infer<typeof HabitCreateSchema>
|
|
|
|
|
2024-04-11 13:07:17 +02:00
|
|
|
export const HabitEditSchema = HabitSchema.extend({})
|
2024-04-11 12:31:45 +02:00
|
|
|
export type HabitEditData = z.infer<typeof HabitEditSchema>
|
|
|
|
|
2024-04-05 00:08:40 +02:00
|
|
|
type HabitBase = z.infer<typeof HabitSchema>
|
2024-03-22 10:23:28 +01:00
|
|
|
|
2024-04-05 00:08:40 +02:00
|
|
|
export interface HabitData extends HabitBase {
|
2024-03-15 22:48:28 +01:00
|
|
|
goal: Goal
|
|
|
|
startDate: Date
|
|
|
|
endDate?: Date
|
|
|
|
}
|
|
|
|
|
2024-04-05 00:08:40 +02:00
|
|
|
export interface HabitJSON extends HabitBase {
|
2024-03-22 10:23:28 +01:00
|
|
|
goal: GoalBaseJSON
|
|
|
|
startDate: string
|
|
|
|
endDate?: string
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Habit extends Entity implements HabitData {
|
|
|
|
public userId: HabitData["userId"]
|
|
|
|
public name: HabitData["name"]
|
|
|
|
public color: HabitData["color"]
|
|
|
|
public icon: HabitData["icon"]
|
|
|
|
public goal: HabitData["goal"]
|
|
|
|
public startDate: HabitData["startDate"]
|
|
|
|
public endDate?: HabitData["endDate"]
|
2024-03-15 22:48:28 +01:00
|
|
|
|
2024-03-22 10:23:28 +01:00
|
|
|
public constructor(options: HabitData) {
|
2024-03-15 22:48:28 +01:00
|
|
|
const { id, userId, name, color, icon, goal, startDate, endDate } = options
|
|
|
|
super({ id })
|
|
|
|
this.userId = userId
|
|
|
|
this.name = name
|
|
|
|
this.color = color
|
|
|
|
this.icon = icon
|
|
|
|
this.goal = goal
|
|
|
|
this.startDate = startDate
|
|
|
|
this.endDate = endDate
|
|
|
|
}
|
2024-03-22 10:23:28 +01:00
|
|
|
|
|
|
|
public override toJSON(): HabitJSON {
|
|
|
|
return {
|
|
|
|
id: this.id,
|
|
|
|
userId: this.userId,
|
|
|
|
name: this.name,
|
|
|
|
color: this.color,
|
|
|
|
icon: this.icon,
|
|
|
|
goal: this.goal,
|
|
|
|
startDate: this.startDate.toISOString(),
|
|
|
|
endDate: this.endDate?.toISOString(),
|
|
|
|
}
|
|
|
|
}
|
2024-03-15 22:48:28 +01:00
|
|
|
}
|