WIP - fix displaying name for owner field
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
/**
|
||||
* Add 'alias' and virtual 'name' column to users table.
|
||||
*
|
||||
* - alias: a user-editable display name / nickname
|
||||
* - name: a generated column that returns COALESCE(alias, CONCAT(firstName, ' ', lastName), email)
|
||||
* so that lookup fields referencing User.name always resolve.
|
||||
*/
|
||||
exports.up = function (knex) {
|
||||
return knex.schema.alterTable('users', (table) => {
|
||||
table.string('alias', 255).nullable().after('lastName');
|
||||
table.string('name', 512).nullable().after('alias');
|
||||
}).then(() => {
|
||||
// Backfill existing rows: name = alias, or firstName + lastName, or email
|
||||
return knex.raw(`
|
||||
UPDATE users
|
||||
SET name = COALESCE(
|
||||
NULLIF(alias, ''),
|
||||
NULLIF(TRIM(CONCAT(COALESCE(firstName, ''), ' ', COALESCE(lastName, ''))), ''),
|
||||
email
|
||||
)
|
||||
`);
|
||||
});
|
||||
};
|
||||
|
||||
exports.down = function (knex) {
|
||||
return knex.schema.alterTable('users', (table) => {
|
||||
table.dropColumn('name');
|
||||
table.dropColumn('alias');
|
||||
});
|
||||
};
|
||||
@@ -20,6 +20,8 @@ model User {
|
||||
password String
|
||||
firstName String?
|
||||
lastName String?
|
||||
alias String?
|
||||
name String?
|
||||
isActive Boolean @default(true)
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
@@ -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');
|
||||
|
||||
@@ -39,7 +39,7 @@ export class SetupUsersController {
|
||||
@Post()
|
||||
async createUser(
|
||||
@TenantId() tenantId: string,
|
||||
@Body() data: { email: string; password: string; firstName?: string; lastName?: string },
|
||||
@Body() data: { email: string; password: string; firstName?: string; lastName?: string; alias?: string },
|
||||
) {
|
||||
const resolvedTenantId = await this.tenantDbService.resolveTenantId(tenantId);
|
||||
const knex = await this.tenantDbService.getTenantKnexById(resolvedTenantId);
|
||||
@@ -52,6 +52,7 @@ export class SetupUsersController {
|
||||
password: hashedPassword,
|
||||
firstName: data.firstName,
|
||||
lastName: data.lastName,
|
||||
alias: data.alias,
|
||||
isActive: true,
|
||||
});
|
||||
|
||||
@@ -62,7 +63,7 @@ export class SetupUsersController {
|
||||
async updateUser(
|
||||
@TenantId() tenantId: string,
|
||||
@Param('id') id: string,
|
||||
@Body() data: { email?: string; password?: string; firstName?: string; lastName?: string },
|
||||
@Body() data: { email?: string; password?: string; firstName?: string; lastName?: string; alias?: string },
|
||||
) {
|
||||
const resolvedTenantId = await this.tenantDbService.resolveTenantId(tenantId);
|
||||
const knex = await this.tenantDbService.getTenantKnexById(resolvedTenantId);
|
||||
@@ -72,6 +73,7 @@ export class SetupUsersController {
|
||||
if (data.email) updateData.email = data.email;
|
||||
if (data.firstName !== undefined) updateData.firstName = data.firstName;
|
||||
if (data.lastName !== undefined) updateData.lastName = data.lastName;
|
||||
if (data.alias !== undefined) updateData.alias = data.alias;
|
||||
|
||||
// Hash password if provided
|
||||
if (data.password) {
|
||||
|
||||
Reference in New Issue
Block a user