Repository navigation
Expand file tree
/
Copy pathUser.ts
More file actions
76 lines (68 loc) · 1.96 KB
/
Copy pathUser.ts
File metadata and controls
76 lines (68 loc) · 1.96 KB
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
import {
Authorized,
Body,
CurrentUser,
ForbiddenError,
Get,
HttpCode,
JsonController,
OnNull,
Param,
Post,
Put,
QueryParams
} from 'routing-controllers';
import { ResponseSchema } from 'routing-controllers-openapi';
import { PhoneSignInData, User, UserFilter, UserListChunk, UserRole } from '../model';
import { activityLogService, sessionService } from '../service';
import { searchConditionOf } from '../utility';
@JsonController('/user')
export class UserController {
store = sessionService.userStore;
@Post()
@HttpCode(201)
@ResponseSchema(User)
signUp(@Body() data: PhoneSignInData) {
return sessionService.signUp(data);
}
@Get()
// @Authorized(UserRole.Admin)
@ResponseSchema(UserListChunk)
async getList(@QueryParams() { gender, keywords, pageSize, pageIndex }: UserFilter) {
const where = searchConditionOf<User>(
['email', 'mobilePhone', 'name'],
keywords,
gender && { gender }
);
const [list, count] = await this.store.findAndCount({
where,
skip: pageSize * (pageIndex - 1),
take: pageSize
});
return { list, count };
}
@Get('/:id')
@ResponseSchema(User)
@OnNull(404)
getOne(@Param('id') id: number) {
return this.store.findOneBy({ id });
}
@Put('/:id')
@Authorized()
@ResponseSchema(User)
async updateOne(
@Param('id') id: number,
@CurrentUser() updatedBy: User,
@Body() { password, ...data }: User
) {
if (!updatedBy.roles.includes(UserRole.Admin) && id !== updatedBy.id)
throw new ForbiddenError();
const saved = await this.store.save({
...data,
password: password && sessionService.encrypt(password),
id
});
await activityLogService.logUpdate(updatedBy, 'User', id);
return sessionService.sign(saved);
}
}