-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.controller.ts
executable file
·120 lines (109 loc) · 2.81 KB
/
users.controller.ts
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import {
Controller,
Get,
Post,
Body,
UseGuards,
Param,
Put,
Query,
Req,
Delete,
UseInterceptors
} from "@nestjs/common";
import { UsersService } from "./users.service";
import { User } from "./../interfaces/user.interface";
import { AuthGuard } from "@nestjs/passport";
import { plainToClass } from "class-transformer";
import {
ResultList,
NullableParseIntPipe,
Result,
RolesGuard,
LoggingInterceptor
} from "nestx-common";
import {
KeyValueDto,
CreateUserReq,
EditUserReq,
EditProfileReq,
UsersOfRole,
UserRes,
ChangePasswordReq
} from "./../dto";
import { Tags } from "nest-swagger";
@Tags("core")
@Controller("user")
@UseGuards(AuthGuard("jwt"), RolesGuard)
@UseInterceptors(LoggingInterceptor)
export class UsersController {
constructor(private readonly usersService: UsersService) {}
@Get("profile")
async profile(@Req() request: any): Promise<User> {
const user = request.user;
const instance = this.usersService.getProfile(user);
return instance;
}
@Post()
async create(@Body() user: CreateUserReq): Promise<UserRes> {
return this.usersService.create(plainToClass(CreateUserReq, user));
}
@Post("role")
async addUsersToRole(@Body() users: UsersOfRole): Promise<Result> {
return this.usersService.addAccountsToRole(users.role, users.userIds);
}
@Put()
async update(@Body() user: EditUserReq): Promise<UserRes> {
return this.usersService.update(plainToClass(EditUserReq, user));
}
@Put("profile")
async updateProfile(
@Body() user: EditProfileReq,
@Req() req: any
): Promise<UserRes> {
return this.usersService.updateProfile(req.user.id, user);
}
@Put("password")
async changePassword(
@Body() entry: ChangePasswordReq,
@Req() req: any
): Promise<Result> {
return this.usersService.changePassword(req.user.id, entry);
}
@Get("search")
async search(
@Query("keyword") keyword?: string,
@Query("value") value?: string
): Promise<KeyValueDto[]> {
return this.usersService.search(keyword, value);
}
@Get("query")
async query(
@Query("keyword") keyword?: string,
@Query("group") group?: string,
@Query("role") role?: string,
@Query("page", new NullableParseIntPipe()) page: number = 1,
@Query("size", new NullableParseIntPipe()) size: number = 10,
@Query("sort") sort?: string
): Promise<ResultList<User>> {
return this.usersService.querySearch(
keyword,
group,
role,
page,
size,
sort
);
}
@Delete("role")
async removeAccountFromRole(
@Query("role") role: string,
@Query("id") accountId: string
): Promise<Result> {
return this.usersService.removeUserFromRole(role, accountId);
}
@Get(":id")
async findOne(@Param("id") id: string): Promise<UserRes> {
return this.usersService.getProfile({ id });
}
}