File

src/sales-force/organization-chart/organization-chart.controller.ts

Prefix

organization-chart

Index

Methods

Methods

Async createOrganizationChart
createOrganizationChart(res, req, clientId: number, body: OrganizationChartBody)
Decorators :
@Post(':clientId')
Parameters :
Name Type Optional
res No
req No
clientId number No
body OrganizationChartBody No
Returns : any
Async deleteOrganizationChart
deleteOrganizationChart(res, id: number, clientId: number)
Decorators :
@Delete(':clientId/:id')
Parameters :
Name Type Optional
res No
id number No
clientId number No
Returns : any
Async getOrganizationChart
getOrganizationChart(res, req, clientId: number, salesForceId: number, query: OrganizationChartTableQuery)
Decorators :
@Get('table/:clientId/:salesForceId')
Parameters :
Name Type Optional
res No
req No
clientId number No
salesForceId number No
query OrganizationChartTableQuery No
Returns : any
Async getOrganizationChartDistributor
getOrganizationChartDistributor(res, req, clientId: number, salesForceId: number, query: OrganizationChartTableQuery)
Decorators :
@Get('table/distributor/:clientId/:salesForceId')
Parameters :
Name Type Optional
res No
req No
clientId number No
salesForceId number No
query OrganizationChartTableQuery No
Returns : any
Async getOrganizationChartHeaders
getOrganizationChartHeaders(res, salesForceId: number, clientId: number, type: string)
Decorators :
@Get('excelHeader/:clientId/:salesForceId/:type')
Parameters :
Name Type Optional
res No
salesForceId number No
clientId number No
type string No
Returns : any
Async getOrganizationChartId
getOrganizationChartId(res, id: number, clientId: number)
Decorators :
@Get(':clientId/:id')
Parameters :
Name Type Optional
res No
id number No
clientId number No
Returns : any
Async getOrganizationChartReport
getOrganizationChartReport(res, clientId: number, salesForceId: number, query: OrganizationChartExcelQuery)
Decorators :
@Get('report/excel/:clientId/:salesForceId')
Parameters :
Name Type Optional
res No
clientId number No
salesForceId number No
query OrganizationChartExcelQuery No
Returns : any
Async getOrganizationChartTree
getOrganizationChartTree(res, clientId: number, salesForceId: number, query)
Decorators :
@Get('tree/:clientId/:salesForceId')
Parameters :
Name Type Optional
res No
clientId number No
salesForceId number No
query No
Returns : any
Async updateOrganizationChart
updateOrganizationChart(res, id: number, clientId: number, body: OrganizationChartUpdate)
Decorators :
@Put(':clientId/:id')
Parameters :
Name Type Optional
res No
id number No
clientId number No
body OrganizationChartUpdate No
Returns : any
Async uploadFile
uploadFile(file: Express.Multer.File, clientId: number, salesForceId: number, req, res)
Decorators :
@Post('excel/:clientId/:salesForceId')
@ApiConsumes('multipart/form-data')
@ApiBody({type: FileUploadDto})
@UseInterceptors(undefined)
Parameters :
Name Type Optional
file Express.Multer.File No
clientId number No
salesForceId number No
req No
res No
Returns : unknown
Async uploadFileType
uploadFileType(file: Express.Multer.File, clientId: number, salesForceId: number, type: string, req, res)
Decorators :
@Post('excel/:clientId/:salesForceId/:type')
@ApiConsumes('multipart/form-data')
@ApiBody({type: FileUploadDto})
@UseInterceptors(undefined)
Parameters :
Name Type Optional
file Express.Multer.File No
clientId number No
salesForceId number No
type string No
req No
res No
Returns : unknown
import { Controller, Get, Put, Post, Param, Delete, Res, Body, Query, HttpStatus, UseInterceptors, UploadedFile, Req, HttpException } from '@nestjs/common';
import { ApiBearerAuth, ApiBody, ApiConsumes, ApiTags } from '@nestjs/swagger';
import { OrganizationChartService } from './organization-chart.services';
import { OrganizationChartBody, OrganizationChartExcelQuery, OrganizationChartTableQuery, OrganizationChartUpdate } from '../dto/organization-chart.dto';
import { FileInterceptor } from '@nestjs/platform-express';
import { FileUploadDto } from '../dto/sales-force.dto';

@ApiTags('Organization Chart')
@ApiBearerAuth('token')
@Controller('organization-chart')
export class OrganizationChartController {
  constructor(
    private readonly salesForceService: OrganizationChartService
  ) { }

  @Post(':clientId')
  async createOrganizationChart(
    @Res() res,
    @Req() req,
    @Param('clientId') clientId: number,
    @Body() body: OrganizationChartBody) {

    try {
      const { authorization } = req.headers;
      const recordset = await this.salesForceService.createOrganizationChart(body, clientId, authorization);
      res.status(201).json({ recordset });
    } catch (error) {
      res.status(error?.status || 501).json({ message: error.detail || error.message })
    }
  }

  @Put(':clientId/:id')
  async updateOrganizationChart(
    @Res() res,
    @Param('id') id: number,
    @Param('clientId') clientId: number,
    @Body() body: OrganizationChartUpdate) {
    try {
      const recordset = await this.salesForceService.putOrganizationChart(body, clientId, id)
      res.status(201).json({ recordset });
    } catch (error) {
      res.status(error?.status || 501).json({ message: error.detail || error.message })
    }
  }


  @Get('tree/:clientId/:salesForceId')
  async getOrganizationChartTree(
    @Res() res,
    @Param('clientId') clientId: number,
    @Param('salesForceId') salesForceId: number,
    @Query() query) {

    try {
      const response = await this.salesForceService.getOrganizationChartTree(clientId, +salesForceId)
      res.status(201).json(response);
    } catch (error) {
      res.status(error?.status || 501).json({ message: error.detail || error.message })
    }
  }
  @Get('table/:clientId/:salesForceId')
  async getOrganizationChart(
    @Res() res,
    @Req() req,
    @Param('clientId') clientId: number,
    @Param('salesForceId') salesForceId: number,
    @Query() query: OrganizationChartTableQuery) {
    try {

      const response = await this.salesForceService.getOrganizationChart(clientId, +salesForceId, query)
      res.status(201).json(response);
    } catch (error) {
      res.status(error?.status || 501).json({ message: error.detail || error.message })
    }
  }
  @Get('table/distributor/:clientId/:salesForceId')
  async getOrganizationChartDistributor(
    @Res() res,
    @Req() req,
    @Param('clientId') clientId: number,
    @Param('salesForceId') salesForceId: number,
    @Query() query: OrganizationChartTableQuery) {
    try {

      const response = await this.salesForceService.getOrganizationChartDistributor(clientId, +salesForceId, query)
      res.status(201).json(response);
    } catch (error) {
      res.status(error?.status || 501).json({ message: error.detail || error.message })
    }
  }
  @Get('report/excel/:clientId/:salesForceId')
  async getOrganizationChartReport(
    @Res() res,
    @Param('clientId') clientId: number,
    @Param('salesForceId') salesForceId: number,
    @Query() query: OrganizationChartExcelQuery) {

    try {
      const result = await this.salesForceService.excelReportOrganizationCharts(clientId, +salesForceId, query)
      const nameDate = (new Date()).toISOString();
      res.setHeader("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
      res.setHeader("Content-Disposition", `attachment; filename=${result[0]}-OC-${nameDate}.xlsx`);
      res.status(HttpStatus.OK).send(result[1]);
    } catch (error) {
      res.status(error?.status || 501).json({ message: error.detail || error.message })
    }

  }

  @Post('excel/:clientId/:salesForceId/:type')
  @ApiConsumes('multipart/form-data')
  @ApiBody({ type: FileUploadDto })
  @UseInterceptors(FileInterceptor('file'))
  async uploadFileType(
    @UploadedFile() file: Express.Multer.File,
    @Param('clientId') clientId: number,
    @Param('salesForceId') salesForceId: number,
    @Param('type') type: string,
    @Req() req,
    @Res() res,
  ) {
    try {
      if (!['cnpjMtrix', 'cdMtrix','nId'].includes(type)){
        throw new HttpException('invalid_param', HttpStatus.INTERNAL_SERVER_ERROR)
      }
      const {
        headers: { authorization },
      } = req;
  
      const token = authorization.split(' ')[1];
      const result =
        await this.salesForceService.uploadFileType(
          file,
          +clientId,
          +salesForceId,
          type,
          token
        );
      res.status(HttpStatus.OK).json(result);
    } catch (error) {
      if (error.status === 415) {
        return res
          .status(200)
          .json({ error: true, recordset: error.response });
      }
      res
        .status(error.status || 500)
        .json({ message: error.message || 'Bad Request' });
    }
  }

  @Post('excel/:clientId/:salesForceId')
  @ApiConsumes('multipart/form-data')
  @ApiBody({ type: FileUploadDto })
  @UseInterceptors(FileInterceptor('file', {
    limits: {
      fileSize: 35 * 1024, // Limite de 10 MB (altere conforme necessário)
    },
  }))
  async uploadFile(
    @UploadedFile() file: Express.Multer.File,
    @Param('clientId') clientId: number,
    @Param('salesForceId') salesForceId: number,
    @Req() req,
    @Res() res,
  ) {
    try {
      const {
        headers: { authorization },
      } = req;
  
      const token = authorization.split(' ')[1];
      const result =
        await this.salesForceService.uploadFile(
          file,
          +clientId,
          +salesForceId,
          token
        );
      res.status(HttpStatus.OK).json(result);
    } catch (error) {
      if (error.status === 415) {
        return res
          .status(200)
          .json({ error: true, recordset: error.response });
      }
      res
        .status(error.status || 500)
        .json({ message: error.message || 'Bad Request' });
    }
  }



  @Delete(':clientId/:id')
  async deleteOrganizationChart(
    @Res() res,
    @Param('id') id: number,
    @Param('clientId') clientId: number
  ) {
    try {
      const recordset = await this.salesForceService.deleteOrganizationChart(+id, +clientId)
      res.status(201).json({ recordset });
    } catch (error) {
      res.status(error?.status || 501).json({ message: error.detail || error.message })
    }
  }

  @Get(':clientId/:id')
  async getOrganizationChartId(
    @Res() res,
    @Param('id') id: number,
    @Param('clientId') clientId: number
  ) {
    try {
      const recordset = await this.salesForceService.getOrganizationChartId(+id, +clientId)
      res.status(201).json({ recordset });
    } catch (error) {
      res.status(error?.status || 501).json({ message: error.detail || error.message })
    }
  }

  @Get('excelHeader/:clientId/:salesForceId/:type')
  async getOrganizationChartHeaders(
    @Res() res,
    @Param('salesForceId') salesForceId: number,
    @Param('clientId') clientId: number,
    @Param('type') type: string
  ) {
    try {
      if (!['cnpjMtrix', 'cdMtrix','nId'].includes(type)){
        throw new HttpException('invalid_param', HttpStatus.INTERNAL_SERVER_ERROR)
      }
      const result = await this.salesForceService.createOrganizationChartExcel(+salesForceId, +clientId, type)
      res.setHeader("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
      const nameDate = (new Date()).toISOString();
      res.setHeader("Content-Disposition", `attachment; filename=${result[0]}-OC-${nameDate}.xlsx`);
      res.status(HttpStatus.OK).send(result[1]);
      // res.status(HttpStatus.OK).send(result[1]);
    } catch (error) {
      res.status(error?.status || 501).json({ message: error.detail || error.message })
    }
  }
}

results matching ""

    No results matching ""