2020-04-24 12:12:27 +02:00
|
|
|
import Model from "../../db/Model";
|
|
|
|
import Validator from "../../db/Validator";
|
2020-06-16 11:12:58 +02:00
|
|
|
import MysqlConnectionManager from "../../db/MysqlConnectionManager";
|
|
|
|
import AddApprovedFieldToUsersTable from "../migrations/AddApprovedFieldToUsersTable";
|
|
|
|
import config from "config";
|
2020-06-27 14:36:50 +02:00
|
|
|
import {ManyModelRelation} from "../../db/ModelRelation";
|
|
|
|
import UserEmail from "./UserEmail";
|
2020-04-24 12:12:27 +02:00
|
|
|
|
|
|
|
export default class User extends Model {
|
2020-06-16 11:12:58 +02:00
|
|
|
public static isApprovalMode(): boolean {
|
|
|
|
return config.get<boolean>('approval_mode') && MysqlConnectionManager.hasMigration(AddApprovedFieldToUsersTable);
|
|
|
|
}
|
|
|
|
|
2020-04-24 12:12:27 +02:00
|
|
|
public name?: string;
|
2020-06-27 17:11:31 +02:00
|
|
|
public approved!: boolean;
|
|
|
|
public is_admin!: boolean;
|
2020-04-24 12:12:27 +02:00
|
|
|
public created_at?: Date;
|
|
|
|
public updated_at?: Date;
|
|
|
|
|
2020-06-27 14:36:50 +02:00
|
|
|
public readonly emails = new ManyModelRelation<this, UserEmail>(this, UserEmail, {
|
|
|
|
localKey: 'id',
|
|
|
|
foreignKey: 'user_id'
|
|
|
|
});
|
|
|
|
|
2020-06-27 14:58:39 +02:00
|
|
|
public readonly mainEmail = this.emails.cloneReduceToOne().constraint(q => q.where('main', true));
|
2020-06-27 14:36:50 +02:00
|
|
|
|
2020-06-27 17:11:31 +02:00
|
|
|
constructor(data: any) {
|
|
|
|
super(data);
|
|
|
|
if (this.approved === undefined) this.approved = false;
|
|
|
|
if (this.is_admin === undefined) this.is_admin = false;
|
|
|
|
}
|
|
|
|
|
2020-06-27 14:36:50 +02:00
|
|
|
protected init(): void {
|
|
|
|
this.addProperty<string>('name', new Validator().acceptUndefined().between(3, 64));
|
|
|
|
if (User.isApprovalMode()) this.addProperty<boolean>('approved', new Validator().defined());
|
|
|
|
this.addProperty<boolean>('is_admin', new Validator().defined());
|
2020-06-16 11:12:58 +02:00
|
|
|
|
2020-06-27 14:36:50 +02:00
|
|
|
this.addProperty<Date>('created_at');
|
|
|
|
this.addProperty<Date>('updated_at');
|
2020-04-24 12:12:27 +02:00
|
|
|
}
|
|
|
|
|
2020-06-16 11:12:58 +02:00
|
|
|
public isApproved(): boolean {
|
|
|
|
return !User.isApprovalMode() || this.approved!;
|
2020-04-24 12:12:27 +02:00
|
|
|
}
|
|
|
|
}
|