File size: 984 Bytes
abc493d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { Controller, Get, Param, Patch, Body, UseGuards, ForbiddenException, Request } from '@nestjs/common';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { UsersService } from './users.service';

@Controller('users')
@UseGuards(JwtAuthGuard)
export class UsersController {
  constructor(private svc: UsersService) {}

  @Get()
  findAll(@Request() req: { user: { role: string } }) {
    if (req.user.role !== 'admin') throw new ForbiddenException();
    return this.svc.findAll();
  }

  @Patch(':id/approve')
  approve(@Param('id') id: string, @Request() req: { user: { role: string } }) {
    if (req.user.role !== 'admin') throw new ForbiddenException();
    return this.svc.approve(+id);
  }

  @Patch(':id/role')
  setRole(
    @Param('id') id: string,
    @Body() body: { role: string },
    @Request() req: { user: { role: string } },
  ) {
    if (req.user.role !== 'admin') throw new ForbiddenException();
    return this.svc.setRole(+id, body.role);
  }
}