import { createQueryBuilder , EntityRepository , Repository } from 'typeorm' ; import { User } from '../entity/user.entity' ; @ EntityRepository ( User ) export class UserRepository extends Repository < User > { async findByEmail ( email ) { return this . findOne ( { email } , { relations : [ 'role' , 'role.policies' ] } ) ; } async findByIdWithCertificates ( id ) { return this . findOneOrFail ( id , { relations : [ 'certificates' ] } ) ; } public async getCertificateByUser ( userId ) { return createQueryBuilder ( 'user' , 'user' ) . innerJoinAndSelect ( 'certificate_users_user' , 'certificate_user' , 'certificate_user.userId = user.id' ) . innerJoinAndSelect ( 'certificate' , 'certificate' , 'certificate.id = certificate_user.certificateId' ) . where ( 'user.id = :userId' , { userId } ) . getRawMany ( ) ; } async findByIdWithCourses ( id ) { return this . findOneOrFail ( id , { relations : [ 'createdCourses' ] } ) ; } async findByEmailAndFacebookId ( email , facebookId ) { return this . findOne ( { email , facebookId } ) ; } getUsersQuantity ( ) { return this . count ( ) ; } getActiveUsersQuantity ( ) { return this . count ( ) ; } getInactiveUsersQuantity ( ) { return this . count ( ) ; } async findByInviteKey ( inviteKey ) { const response = await this . find ( { where : { inviteKey } } ) ; return response [ 0 ] ; } public async countUsersInvitedByUserId ( id ) { return this . count ( { where : { invitedByUserId : { id } } } ) ; } }