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> { protected async postAddDomain(req: Request, res: Response): Promise<void> {
const domain = MailDomain.create({ const domain = MailDomain.create(req.body);
name: req.body.name,
user_id: req.body.user_id,
});
await domain.save(); await domain.save();
req.flash('success', `Domain ${domain.name} successfully added with owner ${(await domain.owner.get())?.name}`); req.flash('success', `Domain ${domain.name} successfully added with owner ${(await domain.owner.get())?.name}`);
res.redirectBack(); res.redirectBack();

View File

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