From 0306180321ef09685a7d4727785fb9a75b5ad431 Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Tue, 3 Nov 2020 11:21:32 +0100 Subject: [PATCH] MailDomain: fix 0 to null user_id conversion --- src/controllers/MailboxBackendController.ts | 5 +---- src/models/MailDomain.ts | 6 ++---- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/src/controllers/MailboxBackendController.ts b/src/controllers/MailboxBackendController.ts index 8dc5826..4d3b035 100644 --- a/src/controllers/MailboxBackendController.ts +++ b/src/controllers/MailboxBackendController.ts @@ -100,10 +100,7 @@ export default class MailboxBackendController extends Controller { } protected async postAddDomain(req: Request, res: Response): Promise { - 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(); diff --git a/src/models/MailDomain.ts b/src/models/MailDomain.ts index 62bfc2e..9dc3c69 100644 --- a/src/models/MailDomain.ts +++ b/src/models/MailDomain.ts @@ -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 = new OneModelRelation(this, User, { localKey: 'user_id', @@ -22,9 +22,7 @@ export default class MailDomain extends Model { public updateWithData(data: Pick | Record): 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 {