import { Entity , Column , PrimaryGeneratedColumn , Index , OneToOne , JoinColumn } from 'typeorm' ; import { UserAdministrative } from './UserAdministrative.entity' ; export enum UserRole { COOPERATOR = 'cooperator' , EMPLOYEE = 'employee' , ACCOUNTANT = 'accountant' } @ Entity ( ) export class User { @ PrimaryGeneratedColumn ( 'uuid' ) private id : string ; @ Column ( { type : 'varchar' , nullable : false } ) private firstName : string ; @ Column ( { type : 'varchar' , nullable : false } ) private lastName : string ; @ Column ( { type : 'varchar' , unique : true , nullable : false } ) private email : string ; @ Index ( 'api-token' ) @ Column ( { type : 'varchar' , nullable : true } ) private apiToken : string ; @ Column ( { type : 'varchar' , nullable : false } ) private password : string ; @ Column ( 'enum' , { enum : UserRole , nullable : false } ) private role : UserRole ; @ OneToOne ( type => UserAdministrative , userAdministrative => userAdministrative . user , { nullable : true } ) @ JoinColumn ( ) public userAdministrative : UserAdministrative ; @ Column ( { type : 'timestamp' , default : ( ) => 'CURRENT_TIMESTAMP' } ) private createdAt : Date ; constructor ( firstName , lastName , email , apiToken , password , role , userAdministrative ? ) { this . firstName = firstName ; this . lastName = lastName ; this . email = email ; this . apiToken = apiToken ; this . password = password ; this . role = role ; this . userAdministrative = userAdministrative ; } public getId ( ) { return this . id ; } public getFirstName ( ) { return this . firstName ; } public getLastName ( ) { return this . lastName ; } public getEmail ( ) { return this . email ; } public getApiToken ( ) { return this . apiToken ; } public getPassword ( ) { return this . password ; } public getRole ( ) { return this . role ; } public isAdministrativeEditable ( ) { return this . role !== UserRole . ACCOUNTANT ; } public getFullName ( ) { return `${ this . firstName } ${ this . lastName }` ; } public getUserAdministrative ( ) { return this . userAdministrative ; } public update ( firstName , lastName , email ) { this . firstName = firstName ; this . lastName = lastName ; this . email = email ; } public updatePassword ( password ) { this . password = password ; } public updateRole ( role ) { this . role = role ; } }