File

src/user/user.controller.ts

Prefix

user

Index

Methods

Methods

Async addUserClient
addUserClient(response: Response, body: AddUserClientDTO)
Decorators :
@Post('/client')
@ApiResponse({status: undefined, type: UserClient})
Parameters :
Name Type Optional
response Response No
body AddUserClientDTO No
Returns : any
Async create
create(response, body: UserCreateDTO)
Decorators :
@Post()
Parameters :
Name Type Optional
response No
body UserCreateDTO No
Returns : any
Async createUsersInBulk
createUsersInBulk(req: Request, res: Response, file: Express.Multer.File)
Decorators :
@Post('/upload')
@ApiResponse({status: undefined})
@ApiConsumes('multipart/form-data')
@ApiBody({type: FileUploadDto})
@UseInterceptors(undefined)
Parameters :
Name Type Optional
req Request No
res Response No
file Express.Multer.File No
Returns : any
Async delete
delete(response, id: string)
Decorators :
@Delete(':id')
Parameters :
Name Type Optional
response No
id string No
Returns : any
Async deleteUserClient
deleteUserClient(response: Response, userId: number, clientId: number)
Decorators :
@Delete(':userId/client')
@ApiResponse({status: undefined, type: UserClient})
Parameters :
Name Type Optional
response Response No
userId number No
clientId number No
Returns : unknown
Async disable
disable(response, id: string)
Decorators :
@Patch(':id/disable')
Parameters :
Name Type Optional
response No
id string No
Returns : any
Async enable
enable(response, id: string)
Decorators :
@Patch(':id/enable')
Parameters :
Name Type Optional
response No
id string No
Returns : any
findAll
findAll()
Decorators :
@Get('all')
Returns : Promise<User[]>
findOne
findOne(id: string)
Decorators :
@Get(':id')
Parameters :
Name Type Optional
id string No
findPaginated
findPaginated(query: PaginateUserQueryDto)
Decorators :
@Get('paginate')
Parameters :
Name Type Optional
query PaginateUserQueryDto No
findPositions
findPositions(query: PaginateQuery)
Decorators :
@Get('positions')
@ApiResponse({type: JobPosition})
Parameters :
Name Type Optional
query PaginateQuery No
Returns : any
getClientsByUser
getClientsByUser(clientId: number, query: PaginateQuery)
Decorators :
@Get(':userId/client')
@ApiResponse({type: undefined})
Parameters :
Name Type Optional
clientId number No
query PaginateQuery No
Returns : any
update
update(id: string, body: UserUpdateDTO)
Decorators :
@Put(':id')
Parameters :
Name Type Optional
id string No
body UserUpdateDTO No
import {
  Body,
  Controller,
  HttpStatus,
  Post,
  Put,
  Req,
  Res,
  Param,
  Delete,
  Get,
  Query,
  Patch,
  UploadedFile,
  UseInterceptors,
  ParseFilePipe,
  FileTypeValidator,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import {
  ApiBearerAuth,
  ApiBody,
  ApiConsumes,
  ApiExtraModels,
  ApiOkResponse,
  ApiResponse,
  ApiTags,
} from '@nestjs/swagger';
import { UserService } from './user.service';
import {
  AddUserClientDTO,
  PaginateUserQueryDto,
  UserCreateDTO,
  UserUpdateDTO,
} from './dto/user.dto';
import { User } from 'src/entities/user';
import { PaginatedResult } from 'src/types/paginated-result';
import { UserClient } from 'src/entities/user-client';
import { UserClientService } from './user-client/user-client.service';
import { Request, Response } from 'express';
import { PaginateQuery } from 'src/types/paginate-query';
import { heydrateErrorCatch } from 'src/util/exception-error';
import { JobPosition } from 'src/entities/job-position';
import { FileUploadDto } from 'src/sales-force/dto/sales-force.dto';

@ApiTags('user')
@ApiBearerAuth('token')
@Controller('user')
export class UserController {
  constructor(
    private readonly userService: UserService,
    private readonly userClientService: UserClientService
  ) {}

  @Get('positions')
  @ApiResponse({ type: JobPosition })
  findPositions(@Query() query: PaginateQuery) {
    return this.userService.findPositions(query);
  }

  @Post('/upload')
  @ApiResponse({ status: HttpStatus.OK })
  @ApiConsumes('multipart/form-data')
  @ApiBody({ type: FileUploadDto })
  @UseInterceptors(FileInterceptor('file'))
  async createUsersInBulk(
    @Req() req: Request,
    @Res() res: Response,
    @UploadedFile(
      new ParseFilePipe({
        validators: [
          new FileTypeValidator({ fileType: /(application\/vnd\.openxmlformats-officedocument\.spreadsheetml\.sheet|text\/csv)/i }),
        ],
      }),
    )
    file: Express.Multer.File,
  ) {
    try {
      const { session } = req;

      await this.userService.createUsersInBulkUpload(file, session)
      res.status(HttpStatus.OK).send();
    } catch (error) {
      heydrateErrorCatch(error, res);
    }
  }

  @Get('paginate')
  findPaginated(
    @Query() query: PaginateUserQueryDto
  ): Promise<PaginatedResult<User>> {
    return this.userService.findPaginate(query);
  }

  @Get('all')
  findAll(): Promise<User[]> {
    return this.userService.findAll();
  }

  @Get(':id')
  findOne(@Param('id') id: string): Promise<User | null> {
    return this.userService.findOne(+id);
  }

  @Post()
  async create(@Res() response, @Body() body: UserCreateDTO) {
    const id = await this.userService.create(body);
    response.status(HttpStatus.CREATED).json({ id });
  }

  @Put(':id')
  update(
    @Param('id') id: string,
    @Body() body: UserUpdateDTO
  ): Promise<User | null> {
    return this.userService.update(+id, body);
  }

  @Patch(':id/enable')
  async enable(@Res() response, @Param('id') id: string) {
    await this.userService.enable(+id);
    response.status(HttpStatus.OK).json();
  }

  @Patch(':id/disable')
  async disable(@Res() response, @Param('id') id: string) {
    await this.userService.disable(+id);
    response.status(HttpStatus.OK).json();
  }

  @Delete(':id')
  async delete(@Res() response, @Param('id') id: string) {
    await this.userService.delete(+id);
    response.status(HttpStatus.OK).json();
  }

  // UserClient
  @Get(':userId/client')
  @ApiResponse({ type: [UserClient] })
  getClientsByUser(
    @Param('userId') clientId: number,
    @Query() query: PaginateQuery
  ) {
    return this.userClientService.getClientsByUser(+clientId, query);
  }

  @Post('/client')
  @ApiResponse({ status: HttpStatus.OK, type: UserClient })
  async addUserClient(
    @Res() response: Response,
    @Body() body: AddUserClientDTO
  ) {
    const id = await this.userClientService.addUserToClient({
      clientId: body.clientId,
      userId: body.userId,
    });

    response.status(HttpStatus.CREATED).json({ id });
  }

  @Delete(':userId/client')
  @ApiResponse({ status: HttpStatus.NO_CONTENT, type: UserClient })
  async deleteUserClient(
    @Res() response: Response,
    @Param('userId') userId: number,
    @Query('clientId') clientId: number
  ) {
    await this.userClientService.delete(+userId, +clientId);
    return response.status(HttpStatus.NO_CONTENT).json();
  }
}

results matching ""

    No results matching ""