2020-04-24 12:12:27 +02:00
|
|
|
import Model from "../../db/Model";
|
2020-06-16 11:12:58 +02:00
|
|
|
import MysqlConnectionManager from "../../db/MysqlConnectionManager";
|
2020-11-11 19:08:33 +01:00
|
|
|
import AddApprovedFieldToUsersTableMigration from "../migrations/AddApprovedFieldToUsersTableMigration";
|
2020-06-16 11:12:58 +02:00
|
|
|
import config from "config";
|
2020-06-27 14:36:50 +02:00
|
|
|
import {ManyModelRelation} from "../../db/ModelRelation";
|
|
|
|
import UserEmail from "./UserEmail";
|
2020-07-24 12:13:28 +02:00
|
|
|
import UserApprovedComponent from "./UserApprovedComponent";
|
2021-01-21 15:58:03 +01:00
|
|
|
import UserNameComponent from "./UserNameComponent";
|
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 {
|
2020-09-25 23:42:15 +02:00
|
|
|
return config.get<boolean>('approval_mode') &&
|
2020-11-11 19:08:33 +01:00
|
|
|
MysqlConnectionManager.hasMigration(AddApprovedFieldToUsersTableMigration);
|
2020-06-16 11:12:58 +02:00
|
|
|
}
|
|
|
|
|
2020-07-24 15:40:10 +02:00
|
|
|
public readonly id?: number = undefined;
|
2020-07-25 10:28:50 +02:00
|
|
|
public main_email_id?: number = undefined;
|
2020-07-24 15:40:10 +02:00
|
|
|
public is_admin: boolean = false;
|
|
|
|
public created_at?: Date = undefined;
|
|
|
|
public updated_at?: Date = undefined;
|
2020-04-24 12:12:27 +02:00
|
|
|
|
2020-09-07 14:28:18 +02:00
|
|
|
public readonly emails = new ManyModelRelation(this, UserEmail, {
|
2020-06-27 14:36:50 +02:00
|
|
|
localKey: 'id',
|
2020-09-25 23:42:15 +02:00
|
|
|
foreignKey: 'user_id',
|
2020-06-27 14:36:50 +02:00
|
|
|
});
|
|
|
|
|
2020-07-25 10:28:50 +02:00
|
|
|
public readonly mainEmail = this.emails.cloneReduceToOne().constraint(q => q.where('id', this.main_email_id));
|
2020-06-27 14:36:50 +02:00
|
|
|
|
|
|
|
protected init(): void {
|
2020-07-24 12:13:28 +02:00
|
|
|
this.setValidation('name').acceptUndefined().between(3, 64);
|
2020-07-25 10:28:50 +02:00
|
|
|
this.setValidation('main_email_id').acceptUndefined().exists(UserEmail, 'id');
|
2020-07-24 12:13:28 +02:00
|
|
|
if (User.isApprovalMode()) {
|
|
|
|
this.setValidation('approved').defined();
|
|
|
|
}
|
|
|
|
this.setValidation('is_admin').defined();
|
2020-04-24 12:12:27 +02:00
|
|
|
}
|
|
|
|
|
2020-06-16 11:12:58 +02:00
|
|
|
public isApproved(): boolean {
|
2020-07-24 12:13:28 +02:00
|
|
|
return !User.isApprovalMode() || this.as(UserApprovedComponent).approved;
|
2020-04-24 12:12:27 +02:00
|
|
|
}
|
2021-01-21 15:58:03 +01:00
|
|
|
|
|
|
|
protected getPersonalInfoFields(): { name: string, value: string }[] {
|
|
|
|
const fields: { name: string, value: string }[] = [];
|
|
|
|
const nameComponent = this.asOptional(UserNameComponent);
|
|
|
|
if (nameComponent && nameComponent.name) {
|
|
|
|
fields.push({
|
|
|
|
name: 'Name',
|
|
|
|
value: nameComponent.name,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return fields;
|
|
|
|
}
|
2020-07-24 12:13:28 +02:00
|
|
|
}
|