MailDomain: fix 0 to null user_id conversion

This commit is contained in:
Alice Gaudon 2020-11-03 11:21:32 +01:00
parent 08a34561e5
commit 0306180321
2 changed files with 3 additions and 8 deletions

View File

@ -100,10 +100,7 @@ export default class MailboxBackendController extends Controller {
}
protected async postAddDomain(req: Request, res: Response): Promise<void> {
const domain = MailDomain.create({
name: req.body.name,
user_id: req.body.user_id,
});
const domain = MailDomain.create(req.body);
await domain.save();
req.flash('success', `Domain ${domain.name} successfully added with owner ${(await domain.owner.get())?.name}`);
res.redirectBack();

View File

@ -6,7 +6,7 @@ import MailIdentity from "./MailIdentity";
export default class MailDomain extends Model {
public id?: number = undefined;
public name?: string = undefined;
public user_id?: number = undefined;
public user_id?: number | null = undefined;
public readonly owner: OneModelRelation<MailDomain, User> = new OneModelRelation(this, User, {
localKey: 'user_id',
@ -22,9 +22,7 @@ export default class MailDomain extends Model {
public updateWithData(data: Pick<this, keyof this> | Record<string, unknown>): void {
super.updateWithData(data);
if (typeof this.user_id !== 'undefined' && this.user_id <= 0) {
this.user_id = undefined;
}
if (this.user_id !== null && this.user_id !== undefined && this.user_id <= 0) this.user_id = null;
}
protected init(): void {