import bcrypt from "bcrypt" ; import { Exclude } from "class-transformer" ; import { Column , Entity , PrimaryGeneratedColumn , ManyToMany , OneToMany } from "typeorm" ; import { IUser } from "../../../../shared/entities" ; import { BCRYPT_SALT_ROUNDS } from "../../common/constants" ; import { Organization } from "../../organizations/entities/organization.entity" ; import { Project } from "../../projects/entities/project.entity" ; @ Entity ( ) export class User implements IUser { @ PrimaryGeneratedColumn ( "uuid" ) id : string ; @ Column ( { unique : true , type : "character varying" } ) email : string ; @ Column ( { type : "character varying" } ) name : string ; @ Column ( { default : false , type : "boolean" } ) isEmailVerified : boolean ; @ Column ( { default : false , type : "boolean" } ) hasSeenTour : boolean ; @ ManyToMany ( ( ) => Organization , organization => organization . users ) organizations : Organization [ ] ; @ OneToMany ( ( ) => Project , project => project . user ) projects : Project [ ] ; @ OneToMany ( ( ) => Organization , organization => organization . admin ) adminOrganizations : Organization [ ] ; @ Column ( { type : "character varying" } ) @ Exclude ( ) passwordHash : string ; constructor ( params ? ) { if ( ! params ) { return ; } this . email = params . email ; this . name = params . name ; if ( params . id ) { this . id = params . id ; } if ( params . passwordHash ) { this . passwordHash = params . passwordHash ; } } async comparePassword ( password ) { return bcrypt . compare ( password , this . passwordHash ) ; } async setPassword ( password ) { this . passwordHash = await bcrypt . hash ( password , BCRYPT_SALT_ROUNDS ) ; return ; } passwordOutdated ( ) { return bcrypt . getRounds ( this . passwordHash ) < BCRYPT_SALT_ROUNDS ; } }