Skip to content

Commit

Permalink
545-Replace req user with req userEntity in the user.controller.ts (#579
Browse files Browse the repository at this point in the history
)

* Replace req["user"] with req["userEntity"] in the user.controller.ts

* clean up

* use the service method user.getUserProfile rather than returning req["user"]

* cc points

* use getUserProfile
  • Loading branch information
SangilYun committed Sep 10, 2024
1 parent 382c840 commit ca7a904
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ export class UserController {
@Get('/me')
@UseGuards(FirebaseAuthGuard)
async getUserByFirebaseId(@Req() req: Request): Promise<GetUserDto> {
const user = req['user'];
this.userService.updateUser({ lastActiveAt: new Date() }, user.user.id);
return user as GetUserDto;
const user = req['userEntity'];
this.userService.updateUser({ lastActiveAt: new Date() }, user.id);
return (await this.userService.getUserProfile(user.id)).userDto;
}

@ApiBearerAuth()
@Delete()
@UseGuards(FirebaseAuthGuard)
async deleteUser(@Req() req: Request): Promise<UserEntity> {
return await this.userService.deleteUser(req['user'].user as UserEntity);
return await this.userService.deleteUser(req['userEntity']);
}

// This route must go before the Delete user route below as we want nestjs to check against this one first
Expand Down Expand Up @@ -84,8 +84,8 @@ export class UserController {
@ApiBearerAuth()
@Patch()
@UseGuards(FirebaseAuthGuard)
async updateUser(@Body() updateUserDto: UpdateUserDto, @Req() req: Request) {
return await this.userService.updateUser(updateUserDto, req['user'].user.id);
async updateUser(@Body() updateUserDto: UpdateUserDto, @Req() req: Request): Promise<UserEntity> {
return await this.userService.updateUser(updateUserDto, req['userEntity'].id);
}

@ApiBearerAuth()
Expand Down
28 changes: 28 additions & 0 deletions src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,34 @@ export class UserService {
return { userEntity: queryResult, userDto: formatUserObject(queryResult) };
}

public async getUserProfile(id: string): Promise<{
userEntity: UserEntity | undefined;
userDto: GetUserDto | undefined;
}> {
const queryResult = await this.userRepository
.createQueryBuilder('user')
.leftJoinAndSelect('user.partnerAccess', 'partnerAccess')
.leftJoinAndSelect('user.partnerAdmin', 'partnerAdmin')
.leftJoinAndSelect('partnerAccess.therapySession', 'therapySession')
.leftJoinAndSelect('partnerAccess.partner', 'partner')
.leftJoinAndSelect('partnerAccess.partner', 'partnerAccessPartner')
.leftJoinAndSelect('partnerAdmin.partner', 'partnerAdminPartner')
.leftJoinAndSelect('user.courseUser', 'courseUser')
.leftJoinAndSelect('courseUser.course', 'course')
.leftJoinAndSelect('courseUser.sessionUser', 'sessionUser')
.leftJoinAndSelect('sessionUser.session', 'session')
.leftJoinAndSelect('user.subscriptionUser', 'subscriptionUser')
.leftJoinAndSelect('subscriptionUser.subscription', 'subscription')
.where('user.id = :id', { id })
.getOne();

if (!queryResult) {
throw new HttpException('USER NOT FOUND', HttpStatus.NOT_FOUND);
}

return { userEntity: queryResult, userDto: formatUserObject(queryResult) };
}

public async getUserById(id: string): Promise<UserEntity | undefined> {
const queryResult = await this.userRepository
.createQueryBuilder('user')
Expand Down

0 comments on commit ca7a904

Please sign in to comment.