Files
auth-service/src/users/users.contoller.ts

166 lines
5.0 KiB
TypeScript

import {Controller, Get, Req, Post, Options, Header, Delete, HttpCode, Put} from '@nestjs/common';
import {ApiResponse, ApiTags, ApiParam, ApiBody} from '@nestjs/swagger';
import {
ALLOW_ORIGIN_ALL,
ALLOW_METHOD,
ALLOW_CREDENTIALS,
CONTENT_LENGTH,
ALLOW_HEADERS,
USERS_CONTROLLER,
} from '../consts';
import {UserService} from './users.service';
import {
UserResponse,
CreateUserRequest,
UpdateUserRequest,
UpdateUserSelf,
ChangePasswordRequest,
} from './users.schema';
import {Request} from 'express';
import {
FIND_ALL_SUCCESS,
FIND_ONE_SUCCESS,
FIND_ONE_NOT_FOUND,
CREATE_SUCCESS,
CREATE_CONFLICT,
CREATE_NOT_VALID,
UPDATE_SUCCESS,
UPDATE_NOT_FOUND,
UPDATE_NOT_VALID,
REMOVE_SUCCESS,
REMOVE_NOT_FOUND,
EDIT_ME_SUCCESS,
EDIT_ME_NOT_VALID,
CHANGE_PASSWORD_SUCCESS,
CHANGE_PASSWORD_NOT_VALID,
} from './users.responses';
import {AuthService} from 'src/auth/auth.service';
@Controller(USERS_CONTROLLER)
@ApiTags(USERS_CONTROLLER)
export class UsersController {
constructor(
private readonly userService: UserService,
private readonly authService: AuthService,
) {}
@Get()
@Header(...ALLOW_ORIGIN_ALL)
@ApiResponse(FIND_ALL_SUCCESS)
async findAll(@Req() request: Request): Promise<UserResponse[]> {
await this.authService.checkRequest(request);
return this.userService.findAll();
}
@Get('search/:login')
@Header(...ALLOW_ORIGIN_ALL)
@ApiResponse(FIND_ONE_SUCCESS)
@ApiResponse(FIND_ONE_NOT_FOUND)
@ApiParam({
name: 'login',
description: 'Логин пользователя',
})
async findOne(@Req() request: Request<{login: string}>): Promise<UserResponse> {
await this.authService.checkRequest(request);
return await this.userService.findOne(request.params.login);
}
@Get('me')
@Header(...ALLOW_ORIGIN_ALL)
@ApiResponse(FIND_ONE_SUCCESS)
async findMe(@Req() request: Request): Promise<UserResponse> {
await this.authService.checkRequest(request);
return this.userService.findMe(request.headers.authorization);
}
@Post('edit-me')
@Header(...ALLOW_ORIGIN_ALL)
@ApiResponse(EDIT_ME_SUCCESS)
@ApiResponse(EDIT_ME_NOT_VALID)
@ApiBody({
type: UpdateUserSelf,
description: 'Объект обновления пользователя',
})
async findEdit(@Req() request: Request<null, UpdateUserSelf>): Promise<UserResponse> {
await this.authService.checkRequest(request);
return this.userService.updateSelf(request.headers.authorization, request.body);
}
@Post()
@Header(...ALLOW_ORIGIN_ALL)
@ApiResponse(CREATE_SUCCESS)
@ApiResponse(CREATE_CONFLICT)
@ApiResponse(CREATE_NOT_VALID)
@ApiBody({
type: CreateUserRequest,
description: 'Объект для создания пользователя'
})
async createUser(@Req() request: Request<null, CreateUserRequest>): Promise<UserResponse> {
await this.authService.checkRequest(request);
return await this.userService.create(request.body);
}
@Put()
@Header(...ALLOW_ORIGIN_ALL)
@ApiResponse(UPDATE_SUCCESS)
@ApiResponse(UPDATE_NOT_FOUND)
@ApiResponse(UPDATE_NOT_VALID)
@ApiBody({
type: UpdateUserRequest,
description: 'Объект обновления данных пользователя'
})
async updateUser(@Req() request: Request<null, UpdateUserRequest>): Promise<UserResponse> {
await this.authService.checkRequest(request);
return await this.userService.update(request.body);
}
@Delete(':login')
@Header(...ALLOW_ORIGIN_ALL)
@ApiResponse(REMOVE_SUCCESS)
@ApiResponse(REMOVE_NOT_FOUND)
@ApiParam({
name: 'login',
description: 'Логин пользователя',
})
async removeUser(@Req() request: Request<{login: string}>): Promise<UpdateUserRequest> {
await this.authService.checkRequest(request);
return await this.userService.removeOne(request.params.login);
}
@Post('change-password')
@Header(...ALLOW_ORIGIN_ALL)
@ApiResponse(CHANGE_PASSWORD_SUCCESS)
@ApiResponse(CHANGE_PASSWORD_NOT_VALID)
@ApiBody({
type: ChangePasswordRequest,
description: 'Объект изменения пароля',
})
async changePassword(@Req() request: Request<null, {old_password: string, new_password: string}>): Promise<string> {
await this.authService.checkRequest(request);
const {headers, body} = request;
return await this.userService.changePassword(headers.authorization, body.old_password, body.new_password);
}
@Options([
'', 'search/:login', ':login', 'me', 'edit-me'
])
@Header(...ALLOW_ORIGIN_ALL)
@Header(...ALLOW_METHOD)
@Header(...ALLOW_CREDENTIALS)
@Header(...CONTENT_LENGTH)
@Header(...ALLOW_HEADERS)
@HttpCode(204)
async options(): Promise<string> {
return '';
}
}