import { getCoursesByFinished } from '../../DashboardModule/interfaces/getCoursesByFinished' ; import { OrderEnum } from '../../CommonsModule/enum/order.enum' ; import { EntityRepository , IsNull , MoreThanOrEqual , Not , Repository } from 'typeorm' ; import { CourseTakenStatusEnum } from '../enum/course-taken-status.enum' ; import { User } from '../../UserModule/entity/user.entity' ; import { CourseTaken } from '../entity/course-taken.entity' ; import { Pageable , PageableDTO } from '../../CommonsModule/dto/pageable.dto' ; @ EntityRepository ( CourseTaken ) export class CourseTakenRepository extends Repository < CourseTaken > { public async findByUserId ( userId ) { return this . find ( { where : { user : { id : userId } } , } ) ; } public async getCompletedByUserIdAndCourseId ( userId , courseId ) { return this . findOne ( { user : { id : userId } , courseId , completion : 100 , status : CourseTakenStatusEnum . COMPLETED , } , { relations : [ 'user' ] , } ) ; } public async findByUser ( user ) { return this . find ( { relations : [ 'user' ] , where : { user } } ) ; } public async getActiveUsersQuantity ( ) { const activeUsers = await this . createQueryBuilder ( 'coursetaken' ) . where ( 'coursetaken.completion' , MoreThanOrEqual < number > ( 30 ) ) . select ( 'DISTINCT coursetaken.user' , 'user' ) . orderBy ( 'user' ) . getRawMany ( ) ; return activeUsers . length ; } public async getCertificateQuantity ( ) { return this . count ( { where : { status : CourseTakenStatusEnum . COMPLETED , completion : 100 , challenge : Not ( IsNull ( ) ) , } , } ) ; } public async findCertificateByUserIdAndCourseId ( userId , courseId ) { return this . findOne ( { user : { id : userId } , courseId , status : CourseTakenStatusEnum . COMPLETED , } , { relations : [ 'user' ] } ) ; } public async findCertificatesByUserId ( user ) { return this . find ( { relations : [ 'user' ] , where : { user : user , status : CourseTakenStatusEnum . COMPLETED } , } ) ; } public async findByUserIdAndCourseId ( userId , courseId ) { const response = await this . find ( { where : { user : { id : userId } , courseId } , take : 1 , relations : [ 'user' ] , } ) ; return response [ 0 ] ; } public async findChallengesByCourseIdPaginated ( courseId , options ) { const challenges = await this . findChallengesByCourseId ( courseId , options ) ; const count = await this . count ( { where : { challenge : Not ( IsNull ( ) ) , courseId } , relations : [ 'user' ] , } ) ; return new PageableDTO < CourseTaken > ( { content : challenges , totalElements : count , ... options , } ) ; } public findChallengesByCourseId ( courseId , { limit , page } ) { return this . find ( { where : { challenge : Not ( IsNull ( ) ) , courseId } , take : limit , skip : limit * ( page - 1 ) , order : { rating : "DESC" } , relations : [ 'user' ] , } ) ; } public async getUsersWithTakenCourses ( ) { const entities = await this . createQueryBuilder ( 'coursetaken' ) . where ( 'coursetaken.status = :courseTakenStatus' , { courseTakenStatus : CourseTakenStatusEnum . TAKEN , } ) . select ( 'DISTINCT coursetaken.user' , 'user' ) . orderBy ( 'user' ) . getRawMany ( ) ; return entities . length ; } public async getUsersWithCompletedCourses ( ) { const entities = await this . createQueryBuilder ( 'coursetaken' ) . where ( 'coursetaken.status = :courseTakenStatus' , { courseTakenStatus : CourseTakenStatusEnum . COMPLETED , } ) . select ( 'DISTINCT coursetaken.user' , 'user' ) . orderBy ( 'user' ) . getRawMany ( ) ; return entities . length ; } public async getUsersWithCompletedAndTakenCourses ( ) { const entities = await this . createQueryBuilder ( 'coursetaken' ) . select ( 'DISTINCT coursetaken.user' , 'user' ) . orderBy ( 'user' ) . getRawMany ( ) ; return entities . length ; } public async getDistinctCourses ( order , limit ) { return this . query ( `
      SELECT
        COUNT(*)
      AS
        'frequency'
      FROM
        course_taken
      GROUP BY
        course_id
      ORDER BY course_id
        ${ order } LIMIT ${ limit }` ) ; } async findCompletedByUserIdAndCourseId ( userId , courseId ) { const response = await this . find ( { where : { userId , courseId , status : CourseTakenStatusEnum . COMPLETED , completion : 100 , } , take : 1 , } ) ; return response [ 0 ] ; } async findCompletedWithRatingByUserIdAndCourseId ( userId , courseId ) { const response = await this . find ( { where : { user : { id : userId } , courseId , status : CourseTakenStatusEnum . COMPLETED , completion : 100 , rating : Not ( IsNull ( ) ) , } , take : 1 , relations : [ 'user' ] , } ) ; return response [ 0 ] ; } public async findFinishedCoursesByUserId ( userId ) { return this . find ( { where : { userId , status : CourseTakenStatusEnum . COMPLETED , completion : 100 , challenge : Not ( IsNull ( ) ) , } , } ) ; } }