users.controller.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import {
  2. Controller,
  3. Get,
  4. Body,
  5. Param,
  6. HttpStatus,
  7. NotFoundException,
  8. BadRequestException,
  9. Req,
  10. Post
  11. } from '@nestjs/common'
  12. import { UsersService } from './users.service'
  13. import { UserProfileDto } from './dto/user-profile.dto'
  14. import { IUsers } from './interfaces/users.interface'
  15. import { ApiBearerAuth, ApiTags } from '@nestjs/swagger'
  16. import { Users } from './entities/users.entity'
  17. @ApiTags('users')
  18. @Controller('users')
  19. @ApiBearerAuth()
  20. export class UsersController {
  21. constructor(private readonly usersService: UsersService) {}
  22. @Get('/my')
  23. public async my(@Req() req) {
  24. return await this.get(req)
  25. }
  26. @Get('/get')
  27. public async get(@Req() req): Promise<IUsers> {
  28. const user = await this.usersService.findById(req.user.userId)
  29. return user
  30. }
  31. @Post('/update')
  32. public async update(@Body() userProfileDto: UserProfileDto, @Req() req): Promise<any> {
  33. try {
  34. await this.usersService.updateProfileUser(req.user.userId, userProfileDto)
  35. return {
  36. message: 'User Updated successfully!',
  37. status: HttpStatus.OK
  38. }
  39. } catch (err) {
  40. throw new BadRequestException(err, 'Error: User not updated!')
  41. }
  42. }
  43. @Post('/updatePassword')
  44. public async updatePassword(@Body() { password }, @Req() req) {
  45. await this.usersService.updatePassword(req.user.id, password)
  46. }
  47. @Get('/invites')
  48. public async invites(@Req() req) {
  49. return this.usersService.getInvites(req.user.id)
  50. }
  51. @Get('/:userId')
  52. public async findOneUser(@Param('userId') userId: string): Promise<IUsers> {
  53. return this.usersService.findById(Number(userId))
  54. }
  55. @Get('/:userId/profile')
  56. public async getUser(@Param('userId') userId: string): Promise<any> {
  57. const user = await this.findOneUser(userId)
  58. if (!user) {
  59. throw new NotFoundException('User does not exist!')
  60. }
  61. return {
  62. user: user,
  63. status: HttpStatus.OK
  64. }
  65. }
  66. }