forked from MergeFi/backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusers.controller.ts
More file actions
68 lines (62 loc) · 1.79 KB
/
Copy pathusers.controller.ts
File metadata and controls
68 lines (62 loc) · 1.79 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
import {
Body,
Controller,
Get,
Param,
Patch,
Req,
UseGuards,
} from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { IsString } from 'class-validator';
import type { Request } from 'express';
import { UsersService } from './users.service';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { toPublicUser, PublicUser } from './user-response.mapper';
import { UserRole } from '../common/enums';
class SetStellarAddressDto {
@IsString()
stellarAddress: string;
}
interface AuthenticatedUserPayload {
userId: string;
username: string;
roles?: UserRole[];
}
@ApiTags('users')
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Controller('users')
export class UsersController {
constructor(private readonly usersService: UsersService) {}
@Get()
async list(@Req() req: Request): Promise<PublicUser[]> {
const userPayload = req.user as AuthenticatedUserPayload | undefined;
const users = await this.usersService.list();
return users.map((user) =>
toPublicUser(user, { currentUser: userPayload }),
);
}
@Get(':id')
async findOne(
@Param('id') id: string,
@Req() req: Request,
): Promise<PublicUser> {
const userPayload = req.user as AuthenticatedUserPayload | undefined;
const user = await this.usersService.findById(id);
return toPublicUser(user, { currentUser: userPayload });
}
@Patch(':id/stellar-address')
async setStellarAddress(
@Param('id') id: string,
@Body() dto: SetStellarAddressDto,
@Req() req: Request,
): Promise<PublicUser> {
const userPayload = req.user as AuthenticatedUserPayload | undefined;
const user = await this.usersService.setStellarAddress(
id,
dto.stellarAddress,
);
return toPublicUser(user, { currentUser: userPayload });
}
}