import Model from "wms-core/db/Model"; import User from "wms-core/auth/models/User"; import {ManyModelRelation, OneModelRelation} from "wms-core/db/ModelRelation"; import ModelFactory from "wms-core/db/ModelFactory"; import MailIdentity from "./MailIdentity"; export default class MailDomain extends Model { public id?: number = undefined; public name?: string = undefined; public user_id?: number = undefined; public readonly owner: OneModelRelation = new OneModelRelation(this, ModelFactory.get(User), { localKey: 'user_id', foreignKey: 'id', }); public readonly identities: ManyModelRelation = new ManyModelRelation(this, ModelFactory.get(MailIdentity), { localKey: 'id', foreignKey: 'mail_domain_id', }); public updateWithData(data: any) { super.updateWithData(data); if (typeof this.user_id !== 'undefined' && this.user_id <= 0) { this.user_id = undefined; } } protected init(): void { this.setValidation('name').acceptUndefined() .maxLength(252) .regexp(/^[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+$/) .unique(this); this.setValidation('user_id').acceptUndefined().exists(User, 'id'); } public isPublic(): boolean { return !this.user_id; } public canCreateAddresses(user: User): boolean { return this.user_id === user.id || this.isPublic(); } }