From e0efed793deed67b68d5650de6e01375e537ca43 Mon Sep 17 00:00:00 2001 From: csehatt741 <77381875+csehatt741@users.noreply.github.com> Date: Wed, 15 Jan 2025 12:47:16 +0100 Subject: [PATCH] Create a new GET /courses_user endpoint #720 (#737) * CourseUserService.getCourseUserByUserId return type annotated * CoursesUserController.getCourseUserByUserId endpoint created * CourseUserModule CoursesUserController dependencies added --------- Co-authored-by: Attila Cseh Co-authored-by: Kylee Fields <43586156+kyleecodes@users.noreply.github.com> --- src/course-user/course-user.module.ts | 49 +++++++++++++++++++++- src/course-user/course-user.service.ts | 2 +- src/course-user/courses-user.controller.ts | 28 +++++++++++++ 3 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 src/course-user/courses-user.controller.ts diff --git a/src/course-user/course-user.module.ts b/src/course-user/course-user.module.ts index 4cc775c5..12bedef4 100644 --- a/src/course-user/course-user.module.ts +++ b/src/course-user/course-user.module.ts @@ -1,10 +1,55 @@ import { Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; +import { SlackMessageClient } from 'src/api/slack/slack-api'; +import { ZapierWebhookClient } from 'src/api/zapier/zapier-webhook-client'; +import { CrispService } from 'src/crisp/crisp.service'; import { CourseUserEntity } from 'src/entities/course-user.entity'; +import { EventLogEntity } from 'src/entities/event-log.entity'; +import { PartnerAccessEntity } from 'src/entities/partner-access.entity'; +import { PartnerEntity } from 'src/entities/partner.entity'; +import { SubscriptionUserEntity } from 'src/entities/subscription-user.entity'; +import { SubscriptionEntity } from 'src/entities/subscription.entity'; +import { TherapySessionEntity } from 'src/entities/therapy-session.entity'; +import { UserEntity } from 'src/entities/user.entity'; +import { EventLoggerService } from 'src/event-logger/event-logger.service'; +import { ServiceUserProfilesService } from 'src/service-user-profiles/service-user-profiles.service'; +import { SubscriptionUserService } from 'src/subscription-user/subscription-user.service'; +import { SubscriptionService } from 'src/subscription/subscription.service'; +import { TherapySessionService } from 'src/therapy-session/therapy-session.service'; +import { AuthService } from '../auth/auth.service'; +import { FirebaseModule } from '../firebase/firebase.module'; +import { PartnerAccessService } from '../partner-access/partner-access.service'; +import { UserService } from '../user/user.service'; import { CourseUserService } from './course-user.service'; +import { CoursesUserController } from './courses-user.controller'; @Module({ - imports: [TypeOrmModule.forFeature([CourseUserEntity])], - providers: [CourseUserService], + imports: [ + TypeOrmModule.forFeature([ + CourseUserEntity, + UserEntity, + PartnerAccessEntity, + PartnerEntity, + SubscriptionUserEntity, + TherapySessionEntity, + SubscriptionEntity, + EventLogEntity, + ]), + FirebaseModule + ], + controllers: [CoursesUserController], + providers: [ + AuthService, + UserService, + CourseUserService, + SubscriptionUserService, + TherapySessionService, + PartnerAccessService, + ServiceUserProfilesService, + CrispService, + SubscriptionService, + ZapierWebhookClient, + SlackMessageClient, + EventLoggerService], }) export class CourseUserModule {} diff --git a/src/course-user/course-user.service.ts b/src/course-user/course-user.service.ts index 8835e4f2..2892c7c0 100644 --- a/src/course-user/course-user.service.ts +++ b/src/course-user/course-user.service.ts @@ -21,7 +21,7 @@ export class CourseUserService { .getOne(); } - async getCourseUserByUserId(userId: string) { + async getCourseUserByUserId(userId: string): Promise { return await this.courseUserRepository .createQueryBuilder('course_user') .leftJoinAndSelect('course_user.course', 'course') diff --git a/src/course-user/courses-user.controller.ts b/src/course-user/courses-user.controller.ts new file mode 100644 index 00000000..b0d7a370 --- /dev/null +++ b/src/course-user/courses-user.controller.ts @@ -0,0 +1,28 @@ +import { Controller, Get, Req, UseGuards } from '@nestjs/common'; +import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger'; +import { ICoursesWithSessions } from 'src/course/course.interface'; +import { UserEntity } from 'src/entities/user.entity'; +import { ControllerDecorator } from 'src/utils/controller.decorator'; +import { FirebaseAuthGuard } from '../firebase/firebase-auth.guard'; +import { formatCourseUserObjects } from '../utils/serialize'; +import { CourseUserService } from './course-user.service'; + +@ApiTags('Courses User') +@ControllerDecorator() +@Controller('/v1/courses-user') +export class CoursesUserController { + constructor(private readonly courseUserService: CourseUserService) {} + + @Get() + @ApiBearerAuth('access-token') + @ApiOperation({ + description: + 'Returns user courses and session data.', + }) + @UseGuards(FirebaseAuthGuard) + async getCourseUserByUserId(@Req() req: Request) : Promise { + const user = req['userEntity'] as UserEntity; + const coursesUser = await this.courseUserService.getCourseUserByUserId(user.id); + return formatCourseUserObjects(coursesUser); + } +} \ No newline at end of file