2022-06-02 06:49:49 +00:00
|
|
|
import { Body, Controller, Post, Patch, UseGuards, UseInterceptors, Req, UploadedFile } from '@nestjs/common';
|
|
|
|
|
import { Express } from 'express';
|
|
|
|
|
import { FileInterceptor } from '@nestjs/platform-express';
|
2021-08-12 15:33:52 +00:00
|
|
|
import { JwtAuthGuard } from 'src/modules/auth/jwt-auth.guard';
|
|
|
|
|
import { PasswordRevalidateGuard } from 'src/modules/auth/password-revalidate.guard';
|
2021-07-19 06:51:21 +00:00
|
|
|
import { UsersService } from 'src/services/users.service';
|
2022-05-05 07:08:42 +00:00
|
|
|
import { User } from 'src/decorators/user.decorator';
|
|
|
|
|
import { SignupDisableGuard } from 'src/modules/auth/signup-disable.guard';
|
2022-04-20 09:16:57 +00:00
|
|
|
import { CreateUserDto, UpdateUserDto } from '@dto/user.dto';
|
2022-05-05 07:08:42 +00:00
|
|
|
import { AcceptInviteDto } from '@dto/accept-organization-invite.dto';
|
2022-05-24 16:46:01 +00:00
|
|
|
import { MultiOrganizationGuard } from 'src/modules/auth/multi-organization.guard';
|
2021-07-19 06:51:21 +00:00
|
|
|
|
|
|
|
|
@Controller('users')
|
|
|
|
|
export class UsersController {
|
2021-09-21 13:48:28 +00:00
|
|
|
constructor(private usersService: UsersService) {}
|
2021-07-19 06:51:21 +00:00
|
|
|
|
2022-05-05 07:08:42 +00:00
|
|
|
@UseGuards(MultiOrganizationGuard, SignupDisableGuard)
|
2021-07-19 06:51:21 +00:00
|
|
|
@Post('set_password_from_token')
|
2022-04-20 09:16:57 +00:00
|
|
|
async create(@Body() userCreateDto: CreateUserDto) {
|
2022-05-05 07:08:42 +00:00
|
|
|
await this.usersService.setupAccountFromInvitationToken(userCreateDto);
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Post('accept-invite')
|
|
|
|
|
async acceptInvite(@Body() acceptInviteDto: AcceptInviteDto) {
|
|
|
|
|
await this.usersService.acceptOrganizationInvite(acceptInviteDto);
|
|
|
|
|
return {};
|
2021-07-19 06:51:21 +00:00
|
|
|
}
|
|
|
|
|
|
2021-08-12 15:33:52 +00:00
|
|
|
@UseGuards(JwtAuthGuard)
|
|
|
|
|
@Patch('update')
|
2022-05-05 07:08:42 +00:00
|
|
|
async update(@User() user, @Body() updateUserDto: UpdateUserDto) {
|
|
|
|
|
const { first_name: firstName, last_name: lastName } = updateUserDto;
|
|
|
|
|
await this.usersService.update(user.id, { firstName, lastName });
|
|
|
|
|
await user.reload();
|
2021-08-12 15:33:52 +00:00
|
|
|
return {
|
2022-05-05 07:08:42 +00:00
|
|
|
first_name: user.firstName,
|
|
|
|
|
last_name: user.lastName,
|
2021-08-12 15:33:52 +00:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-02 06:49:49 +00:00
|
|
|
@Post('avatar')
|
|
|
|
|
@UseGuards(JwtAuthGuard)
|
|
|
|
|
@UseInterceptors(FileInterceptor('file'))
|
|
|
|
|
async addAvatar(@Req() req, @UploadedFile() file: Express.Multer.File) {
|
|
|
|
|
return this.usersService.addAvatar(req.user.id, file.buffer, file.originalname);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-12 15:33:52 +00:00
|
|
|
@UseGuards(JwtAuthGuard, PasswordRevalidateGuard)
|
|
|
|
|
@Patch('change_password')
|
2022-05-05 07:08:42 +00:00
|
|
|
async changePassword(@User() user, @Body('newPassword') newPassword) {
|
|
|
|
|
return await this.usersService.update(user.id, {
|
2022-04-20 09:16:57 +00:00
|
|
|
password: newPassword,
|
|
|
|
|
});
|
2021-08-12 15:33:52 +00:00
|
|
|
}
|
2021-07-19 06:51:21 +00:00
|
|
|
}
|