All files / framework/src/api/db/model/user UserModel.js

100% Statements 10/10
100% Branches 0/0
100% Functions 4/4
100% Lines 10/10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 721x 1x 1x 1x 1x               441x               35x                       1x                                 2x                                         1x  
const PublicModel = require('../PublicModel');
const TABLES = require('../../../../constants/tables');
const AccountModel = require('./AccountModel');
const RoleModel = require('../access/RoleModel');
const _ = require('lodash');
 
/**
 * UserModel implementation of default user database entity
 * @augments PublicModel
 */
class UserModel extends PublicModel {
    static get tableName() {
        return TABLES.USER_TABLE_NAME;
    }
 
    /**
     * Relations getter
     * @return {String} list of relations names
     */
    static get relations() {
        return '[accounts, role]';
    }
 
    /**
     * JsonSchema getter merge user schema with super class schema
     * @type {Object}
     * @property {string} name full name of user
     * @property {string} email user email
     * @property {string} password hashed password
     * @property {string} salt salt for save password hash
     */
    static get jsonSchema() {
        return _.merge(super.jsonSchema, {
            properties: {
                name: { type: 'string' },
                email: { type: 'string' },
                password: { type: 'string' },
                salt: { type: 'string' },
                roleId: { type: 'number' },
            },
        });
    }
 
    /**
     * User relations mapping
     * @type {Object}
     * @property {AccountModel[]} accounts user accounts
     */
    static get relationMappings() {
        return {
            accounts: {
                relation: this.HasManyRelation,
                modelClass: AccountModel,
                join: {
                    from: `${TABLES.USER_TABLE_NAME}.id`,
                    to: `${TABLES.ACCOUNT_TABLE_NAME}.userId`,
                },
            },
            role: {
                relation: this.BelongsToOneRelation,
                modelClass: RoleModel,
                join: {
                    from: `${TABLES.USER_TABLE_NAME}.roleId`,
                    to: `${TABLES.ROLE_TABLE_NAME}.id`,
                },
            },
        };
    }
}
 
module.exports = UserModel;