WIP - fix displaying name for owner field
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { BaseModel } from './base.model';
|
||||
import { ModelOptions, QueryContext } from 'objection';
|
||||
|
||||
export class User extends BaseModel {
|
||||
static tableName = 'users';
|
||||
@@ -8,6 +9,8 @@ export class User extends BaseModel {
|
||||
password: string;
|
||||
firstName?: string;
|
||||
lastName?: string;
|
||||
alias?: string;
|
||||
name?: string;
|
||||
isActive: boolean;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
@@ -22,11 +25,37 @@ export class User extends BaseModel {
|
||||
password: { type: 'string' },
|
||||
firstName: { type: 'string' },
|
||||
lastName: { type: 'string' },
|
||||
alias: { type: 'string' },
|
||||
name: { type: 'string' },
|
||||
isActive: { type: 'boolean' },
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute the `name` column before insert/update so lookup fields
|
||||
* referencing User.name always have a value.
|
||||
*/
|
||||
private computeName() {
|
||||
if (this.alias) {
|
||||
this.name = this.alias;
|
||||
} else if (this.firstName || this.lastName) {
|
||||
this.name = [this.firstName, this.lastName].filter(Boolean).join(' ');
|
||||
} else if (this.email) {
|
||||
this.name = this.email;
|
||||
}
|
||||
}
|
||||
|
||||
$beforeInsert(queryContext: QueryContext) {
|
||||
super.$beforeInsert(queryContext);
|
||||
this.computeName();
|
||||
}
|
||||
|
||||
$beforeUpdate(opt: ModelOptions, queryContext: QueryContext) {
|
||||
super.$beforeUpdate(opt, queryContext);
|
||||
this.computeName();
|
||||
}
|
||||
|
||||
static get relationMappings() {
|
||||
const { UserRole } = require('./user-role.model');
|
||||
const { Role } = require('./role.model');
|
||||
|
||||
Reference in New Issue
Block a user