2024-03-22 10:23:28 +01:00
|
|
|
import { z } from "zod"
|
2024-03-15 22:48:28 +01:00
|
|
|
|
2024-03-22 10:23:28 +01:00
|
|
|
import { Entity, EntitySchema } from "./_Entity"
|
|
|
|
|
|
|
|
export const UserSchema = EntitySchema.extend({
|
|
|
|
email: z.string().min(1).email(),
|
|
|
|
displayName: z.string().min(1),
|
|
|
|
})
|
|
|
|
|
|
|
|
export const UserRegisterSchema = UserSchema.extend({
|
2024-03-22 23:41:51 +01:00
|
|
|
password: z.string().min(6),
|
2024-03-22 10:23:28 +01:00
|
|
|
}).omit({ id: true })
|
|
|
|
export type UserRegisterData = z.infer<typeof UserRegisterSchema>
|
2024-03-15 22:48:28 +01:00
|
|
|
|
2024-03-22 23:41:51 +01:00
|
|
|
export const UserLoginSchema = UserRegisterSchema.omit({ displayName: true })
|
|
|
|
export type UserLoginData = z.infer<typeof UserLoginSchema>
|
|
|
|
|
2024-03-22 10:23:28 +01:00
|
|
|
export type UserData = z.infer<typeof UserSchema>
|
2024-03-15 22:48:28 +01:00
|
|
|
|
2024-03-22 10:23:28 +01:00
|
|
|
export class User extends Entity implements UserData {
|
|
|
|
public email: UserData["email"]
|
|
|
|
public displayName: UserData["displayName"]
|
|
|
|
|
|
|
|
public constructor(options: UserData) {
|
2024-03-15 22:48:28 +01:00
|
|
|
const { id, email, displayName } = options
|
|
|
|
super({ id })
|
|
|
|
this.email = email
|
|
|
|
this.displayName = displayName
|
|
|
|
}
|
2024-03-22 10:23:28 +01:00
|
|
|
|
|
|
|
public override toJSON(): UserData {
|
|
|
|
return {
|
|
|
|
id: this.id,
|
|
|
|
email: this.email,
|
|
|
|
displayName: this.displayName,
|
|
|
|
}
|
|
|
|
}
|
2024-03-15 22:48:28 +01:00
|
|
|
}
|