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 ) { } @ Get ( "profile" ) async profile ( @ Req ( ) request ) { const user = request . user ; const instance = this . usersService . getProfile ( user ) ; return instance ; } @ Post ( ) async create ( @ Body ( ) user ) { return this . usersService . create ( plainToClass ( CreateUserReq , user ) ) ; } @ Post ( "role" ) async addUsersToRole ( @ Body ( ) users ) { return this . usersService . addAccountsToRole ( users . role , users . userIds ) ; } @ Put ( ) async update ( @ Body ( ) user ) { return this . usersService . update ( plainToClass ( EditUserReq , user ) ) ; } @ Put ( "profile" ) async updateProfile ( @ Body ( ) user , @ Req ( ) req ) { return this . usersService . updateProfile ( req . user . id , user ) ; } @ Put ( "password" ) async changePassword ( @ Body ( ) entry , @ Req ( ) req ) { return this . usersService . changePassword ( req . user . id , entry ) ; } @ Get ( "search" ) async search ( @ Query ( "keyword" ) keyword ? , @ Query ( "value" ) value ? ) { return this . usersService . search ( keyword , value ) ; } @ Get ( "query" ) async query ( @ Query ( "keyword" ) keyword ? , @ Query ( "group" ) group ? , @ Query ( "role" ) role ? , @ Query ( "page" , new NullableParseIntPipe ( ) ) page = 1 , @ Query ( "size" , new NullableParseIntPipe ( ) ) size = 10 , @ Query ( "sort" ) sort ? ) { return this . usersService . querySearch ( keyword , group , role , page , size , sort ) ; } @ Delete ( "role" ) async removeAccountFromRole ( @ Query ( "role" ) role , @ Query ( "id" ) accountId ) { return this . usersService . removeUserFromRole ( role , accountId ) ; } @ Get ( ":id" ) async findOne ( @ Param ( "id" ) id ) { return this . usersService . getProfile ( { id } ) ; } }