All files / profile profile.controller.ts

100% Statements 15/15
100% Branches 0/0
100% Functions 4/4
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 1041x                           1x                     1x 1x 1x 1x               1x 1x                                         1x       3x                                       1x       8x                         1x 7x      
import {
  Body,
  ClassSerializerInterceptor,
  Controller,
  Delete,
  HttpCode,
  HttpStatus,
  Patch,
  Post,
  Req,
  SerializeOptions,
  UseGuards,
  UseInterceptors,
} from '@nestjs/common'
import {
  ApiBadRequestResponse,
  ApiBearerAuth,
  ApiBody,
  ApiInternalServerErrorResponse,
  ApiOkResponse,
  ApiOperation,
  ApiTags,
  ApiUnauthorizedResponse,
} from '@nestjs/swagger'
import { RequestWithJWTPayload } from 'src/shared/interfaces/request-with-user.interface'
import { JwtGuard } from '../shared/guards/jwt.guard'
import { ChangeEmailDTO } from './dtos/change-email.dto'
import { ChangePasswordDto } from './dtos/change-password.dto'
import { ProfileService } from './profile.service'
 
@Controller('profile')
@ApiTags('profile')
@ApiBearerAuth('access_token')
@UseInterceptors(ClassSerializerInterceptor)
@UseGuards(JwtGuard)
@SerializeOptions({ strategy: 'excludeAll' })
export class ProfileController {
  constructor(private readonly profileService: ProfileService) {}
 
  @Post('change-password')
  // OK because 201 would be kind of out of place
  @HttpCode(HttpStatus.OK)
  @ApiOperation({
    summary: 'Change password',
  })
  @ApiBody({
    type: ChangePasswordDto,
  })
  @ApiOkResponse({
    description: 'Password has been updated',
  })
  @ApiUnauthorizedResponse({
    description:
      'Either jwt was invalid, old password was wrong or user does not exist anymore',
  })
  @ApiBadRequestResponse({
    description: 'Provided body did not comply to specs of DTO',
  })
  async updatePassword(
    @Req() { user }: RequestWithJWTPayload,
    @Body() { oldPassword, password }: ChangePasswordDto,
  ) {
    await this.profileService.updatePassword(user.id, oldPassword, password)
  }
 
  @Patch('change-email')
  @HttpCode(HttpStatus.OK)
  @ApiOperation({
    summary:
      'Change a users email address and therefore also send a new verify mail',
  })
  @ApiBody({ type: ChangeEmailDTO })
  @ApiOkResponse({ description: 'Email has been updated.' })
  @ApiUnauthorizedResponse({
    description: 'Jwt invalid or user does not exist',
  })
  @ApiInternalServerErrorResponse({
    description: 'Db write could not performed or other internal error',
  })
  @ApiBadRequestResponse({
    description: 'Provided body did not comply to specs of DTO',
  })
  async updateUserEmail(
    @Req() { user }: RequestWithJWTPayload,
    @Body() { email }: ChangeEmailDTO,
  ) {
    await this.profileService.updateUserEmail(user.id, email)
  }
 
  @Delete()
  @ApiOperation({
    summary: 'Delete the users account and related last wills',
  })
  @ApiOkResponse({
    description: 'Account was deleted',
  })
  @ApiUnauthorizedResponse({
    description: 'Invalid or expired JWT',
  })
  async deleteUser(@Req() { user }: RequestWithJWTPayload) {
    await this.profileService.deleteProfile(user.id)
  }
}