From c598290174e6c1dc9606edadb727739b5978701d Mon Sep 17 00:00:00 2001 From: khris-xp Date: Fri, 29 Nov 2024 15:17:12 +0700 Subject: [PATCH] feat: create progress and enroll by user endpoint --- src/enrollment/enrollment.controller.ts | 15 +++++++++++++ src/enrollment/enrollment.service.ts | 28 +++++++++++++++++++++++++ src/pretest/pretest.service.ts | 2 -- src/progress/progress.controller.ts | 15 +++++++++++++ src/progress/progress.service.ts | 24 +++++++++++++++++++++ src/roadmap/roadmap.service.ts | 5 +++-- 6 files changed, 85 insertions(+), 4 deletions(-) diff --git a/src/enrollment/enrollment.controller.ts b/src/enrollment/enrollment.controller.ts index 276d2ad..6ba16a2 100644 --- a/src/enrollment/enrollment.controller.ts +++ b/src/enrollment/enrollment.controller.ts @@ -47,6 +47,21 @@ export class EnrollmentController { return this.enrollmentService.findAll(query); } + @Get('/user') + @ApiResponse({ + status: HttpStatus.OK, + type: EnrollmentResponseDto, + description: 'Get all enrollments by user', + isArray: true, + }) + @Roles(Role.STUDENT) + async findAllByUser( + @Req() request: AuthenticatedRequest, + @Query() query: PaginateQueryDto, + ): Promise { + return await this.enrollmentService.findAllByUser(request.user.id, query); + } + @Get(':id') @ApiParam({ name: 'id', diff --git a/src/enrollment/enrollment.service.ts b/src/enrollment/enrollment.service.ts index 815c94d..afc4613 100644 --- a/src/enrollment/enrollment.service.ts +++ b/src/enrollment/enrollment.service.ts @@ -58,6 +58,34 @@ export class EnrollmentService { return enrollment; } + async findAllByUser( + userId: string, + { + page = 1, + limit = 20, + }: { + page?: number; + limit?: number; + }, + ): Promise { + const { find } = await createPagination(this.enrollmentRepository, { + page, + limit, + }); + + const enrollments = await find({ + where: { + user: { id: userId }, + }, + relations: { + user: true, + course: true, + }, + }).run(); + + return enrollments; + } + async create(createEnrollmentDto: CreateEnrollmentDto): Promise { const enrollment = await this.enrollmentRepository.findOne({ where: { diff --git a/src/pretest/pretest.service.ts b/src/pretest/pretest.service.ts index 2f587ee..24c8682 100644 --- a/src/pretest/pretest.service.ts +++ b/src/pretest/pretest.service.ts @@ -259,7 +259,6 @@ export class PretestService { ); return { data: response.data }; } catch (error) { - console.log(error); throw new Error('Failed to fetch data or process request'); } } @@ -279,7 +278,6 @@ export class PretestService { search: '', }, ); - const requestBody: CreateEvaluate = { question: preTestExam.data.map((data) => data.question.question), correct_answer: preTestExam.data.map( diff --git a/src/progress/progress.controller.ts b/src/progress/progress.controller.ts index 7a5e3a1..4e5f35c 100644 --- a/src/progress/progress.controller.ts +++ b/src/progress/progress.controller.ts @@ -69,6 +69,21 @@ export class ProgressController { return this.progressService.findAll(query); } + @Get('/user') + @ApiResponse({ + status: HttpStatus.OK, + type: ProgressResponseDto, + description: 'Get all progress by user', + isArray: true, + }) + @Roles(Role.STUDENT) + async findAllByUser( + @Req() request: AuthenticatedRequest, + @Query() query: PaginateQueryDto, + ): Promise { + return this.progressService.findAllByUser(request.user.id, query); + } + @Get(':id') @ApiParam({ name: 'id', diff --git a/src/progress/progress.service.ts b/src/progress/progress.service.ts index 3a586f7..a1fba3a 100644 --- a/src/progress/progress.service.ts +++ b/src/progress/progress.service.ts @@ -1,6 +1,7 @@ import { Injectable, NotFoundException } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { createPagination } from 'src/shared/pagination'; +import { PaginateQueryDto } from 'src/shared/pagination/dtos/paginate-query.dto'; import { FindOneOptions, FindOptionsWhere, Repository } from 'typeorm'; import { CreateProgressDto } from './dtos/create-progress.dto'; import { PaginatedProgressResponseDto } from './dtos/progress-response.dto'; @@ -36,6 +37,29 @@ export class ProgressService { return progress; } + async findAllByUser( + userId: string, + query: PaginateQueryDto, + ): Promise { + const { find } = await createPagination(this.progressRepository, query); + + const progress = await find({ + where: { + enrollment: { + user: { + id: userId, + }, + }, + }, + relations: { + enrollment: true, + chapter: true, + }, + }).run(); + + return progress; + } + async findOne( id: string, options: FindOneOptions, diff --git a/src/roadmap/roadmap.service.ts b/src/roadmap/roadmap.service.ts index 88e17b8..49fa5c7 100644 --- a/src/roadmap/roadmap.service.ts +++ b/src/roadmap/roadmap.service.ts @@ -55,7 +55,6 @@ export class RoadmapService { userID: userId, }, }; - console.log('Request body', requestBody); const response = await this.httpService.axiosRef.post( `https://ai.edusaig.com/ai/generate-roadmap`, requestBody, @@ -129,7 +128,9 @@ export class RoadmapService { const queryBuilder = this.roadmapRepository .createQueryBuilder('roadmap') .leftJoinAndSelect('roadmap.user', 'user') - .leftJoinAndSelect('roadmap.courses', 'courses'); + .leftJoinAndSelect('roadmap.courses', 'courses') + .leftJoinAndSelect('courses.teacher', 'teacher') + .leftJoinAndSelect('courses.category', 'category'); if (userId) { queryBuilder.andWhere('user.id = :userId', { userId });