swaf/src/auth/models/User.ts

27 lines
982 B
TypeScript
Raw Normal View History

2020-04-24 12:12:27 +02:00
import UserEmail from "./UserEmail";
import Model from "../../db/Model";
import Validator from "../../db/Validator";
export default class User extends Model {
public static async fromEmail(email: string): Promise<User | null> {
const users = await this.models<User>(this.select().where('id', UserEmail.select('user_id').where('email', email).first()).first());
return users.length > 0 ? users[0] : null;
}
public name?: string;
private is_admin?: boolean;
public created_at?: Date;
public updated_at?: Date;
protected defineProperties(): void {
this.defineProperty<string>('name', new Validator().acceptUndefined().between(3, 64));
this.defineProperty<boolean>('is_admin', new Validator());
this.defineProperty<Date>('created_at');
this.defineProperty<Date>('updated_at');
}
public isAdmin(): boolean {
// @ts-ignore
return this.is_admin === true || this.is_admin === 1;
}
}