users.controller.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. import { Public } from 'src/auth/public.decorator'
  18. @ApiTags('users')
  19. @Controller('users')
  20. @ApiBearerAuth()
  21. export class UsersController {
  22. constructor(private readonly usersService: UsersService) {}
  23. @Get('/my')
  24. public async my(@Req() req) {
  25. return await this.get(req)
  26. }
  27. @Get('/get')
  28. public async get(@Req() req) {
  29. const user = await this.usersService.findById(req.user.userId)
  30. return user
  31. }
  32. @Post('/update')
  33. public async update(@Body() userProfileDto: UserProfileDto, @Req() req): Promise<any> {
  34. if (!req.user.roles.includes('admin')) {
  35. if (req.user.userId !== userProfileDto.id) {
  36. throw new BadRequestException('Permission denied!')
  37. }
  38. }
  39. try {
  40. await this.usersService.updateProfileUser(req.user.userId, userProfileDto)
  41. return {
  42. message: 'User Updated successfully!',
  43. status: HttpStatus.OK
  44. }
  45. } catch (err) {
  46. throw new BadRequestException(err, 'Error: User not updated!')
  47. }
  48. }
  49. @Post('/updatePassword')
  50. public async updatePassword(@Body() { password }, @Req() req) {
  51. await this.usersService.updatePassword(req.user.id, password)
  52. }
  53. @Get('/invites')
  54. public async invites(@Req() req) {
  55. return this.usersService.getInvites(req.user.id)
  56. }
  57. @Get('/:userId')
  58. public async findOneUser(@Param('userId') userId: string): Promise<IUsers> {
  59. return this.usersService.findById(Number(userId))
  60. }
  61. @Get('/:userId/profile')
  62. public async getUser(@Param('userId') userId: string): Promise<any> {
  63. const user = await this.findOneUser(userId)
  64. if (!user) {
  65. throw new NotFoundException('User does not exist!')
  66. }
  67. return {
  68. user: user,
  69. status: HttpStatus.OK
  70. }
  71. }
  72. @Get('/:userId/hasInvite')
  73. public async hasInvite(@Param('userId') userId: string) {
  74. return await this.usersService.hasInvite(parseInt(userId))
  75. }
  76. }