import Model from "../../db/Model"; import MysqlConnectionManager from "../../db/MysqlConnectionManager"; import AddApprovedFieldToUsersTable from "../migrations/AddApprovedFieldToUsersTable"; import config from "config"; import {ManyModelRelation} from "../../db/ModelRelation"; import UserEmail from "./UserEmail"; import ModelFactory from "../../db/ModelFactory"; import UserApprovedComponent from "./UserApprovedComponent"; export default class User extends Model { public static isApprovalMode(): boolean { return config.get('approval_mode') && MysqlConnectionManager.hasMigration(AddApprovedFieldToUsersTable); } public name?: string; public is_admin!: boolean; public created_at?: Date; public updated_at?: Date; public readonly emails = new ManyModelRelation(this, ModelFactory.get(UserEmail), { localKey: 'id', foreignKey: 'user_id' }); public readonly mainEmail = this.emails.cloneReduceToOne().constraint(q => q.where('main', true)); public constructor(data: any) { super(data); if (this.approved === undefined) this.approved = false; if (this.is_admin === undefined) this.is_admin = false; } protected init(): void { this.setValidation('name').acceptUndefined().between(3, 64); if (User.isApprovalMode()) { this.setValidation('approved').defined(); } this.setValidation('is_admin').defined(); } public isApproved(): boolean { return !User.isApprovalMode() || this.as(UserApprovedComponent).approved; } }