import { BadRequestException, Body, Controller, Delete, Get, HttpCode, Param, Patch, Post, Put, Res, StreamableFile, UseGuards } from '@nestjs/common'; import { CarnetApplicationService } from './carnet-application.service'; import { ApiTags } from '@nestjs/swagger'; import { AddCountriesDTO, AddGenerallistItemsDTO, CA_UpdateHolderDTO, CarnetProcessingCenterDTO, CopyCarnetDTO, CreateApplicationDTO, DeleteGenerallistItemsDTO, EditGenerallistItemsDTO, GetCarnetControlCenterDTO, GetExtendedSectionDTO, PrintCarnetDTO, PrintGLDTO, SaveCarnetApplicationDTO, SaveExtensionApplicationDTO, TransmitApplicationtoProcessDTO, UpdateExpGoodsAuthRepDTO, UpdateShippingDetailsDTO } from 'src/dto/property.dto'; import { Roles } from 'src/decorators/roles.decorator'; import { JwtAuthGuard } from 'src/guards/jwt-auth.guard'; import { RolesGuard } from 'src/guards/roles.guard'; import { Response } from 'express'; import { join } from 'path'; import { createReadStream } from 'fs'; import { deleteFilesAsync, generateFinalPDF, replaceSlashWithDash } from 'src/utils/helper'; import { BadRequestException as BR } from 'src/exceptions/badRequest.exception'; import { InternalServerException } from 'src/exceptions/internalServerError.exception'; @ApiTags('Carnet Application - Oracle') @UseGuards(JwtAuthGuard, RolesGuard) @Roles('ca', 'sa') @Controller('oracle') export class CarnetApplicationController { constructor(private readonly carnetApplicationService: CarnetApplicationService) { } // [ CARNETAPPLICATION_PKG ] @Post('SaveCarnetApplication') async CreateClientData(@Body() body: SaveCarnetApplicationDTO) { return this.carnetApplicationService.SaveCarnetApplication(body); } @Post('TransmitApplicationtoProcess') @Roles('ca') TransmitApplicationtoProcess(@Body() body: TransmitApplicationtoProcessDTO) { return this.carnetApplicationService.TransmitApplicationtoProcess(body); } @Post('CreateApplication') @Roles('ca') CreateApplication(@Body() body: CreateApplicationDTO) { return this.carnetApplicationService.CreateApplication(body); } @Patch('update-holder') UpdateHolder(@Body() body: CA_UpdateHolderDTO) { return this.carnetApplicationService.UpdateHolder(body); } @Patch('UpdateExpGoodsAuthRep') UpdateExpGoodsAuthRep(@Body() body: UpdateExpGoodsAuthRepDTO) { return this.carnetApplicationService.UpdateExpGoodsAuthRep(body); } @Post('AddGenerallistItems') AddGenerallistItems(@Body() body: AddGenerallistItemsDTO) { return this.carnetApplicationService.AddGenerallistItems(body); } @Post('AddCountries') AddCountries(@Body() body: AddCountriesDTO) { return this.carnetApplicationService.AddCountries(body); } @Put('EditGenerallistItems') EditGenerallistItems(@Body() body: EditGenerallistItemsDTO) { return this.carnetApplicationService.EditGenerallistItems(body); } @Delete('DeleteGenerallistItems') DeleteGenerallistItems(@Body() body: DeleteGenerallistItemsDTO) { return this.carnetApplicationService.DeleteGenerallistItems(body); } @Patch('UpdateShippingDetails') UpdateShippingDetails(@Body() body: UpdateShippingDetailsDTO) { return this.carnetApplicationService.UpdateShippingDetails(body); } @Get('EstimatedFees/:P_SPID/:P_USERID/:P_HEADERID') EstimatedFees(@Param() body: GetCarnetControlCenterDTO) { return this.carnetApplicationService.EstimatedFees(body); } // processing [ PROCESSINGCENTER_PKG ] @Patch('ProcessCarnet') ProcessOriginalCarnet(@Body() body: CarnetProcessingCenterDTO) { return this.carnetApplicationService.ProcessCarnet(body); } @Patch('UpdatePrintCarnet') UpdatePrintCarnet(@Body() body: CarnetProcessingCenterDTO) { return this.carnetApplicationService.UpdatePrintCarnet(body); } @Patch('VoidCarnet') @Roles('sa') VoidCarnet(@Body() body: CarnetProcessingCenterDTO) { return this.carnetApplicationService.VoidCarnet(body); } // CopyCarnetDTO @Patch('CopyCarnet') @Roles('ca') CopyCarnet(@Body() body: CopyCarnetDTO) { return this.carnetApplicationService.CopyCarnet(body); } @Patch('DuplicateCarnet') @Roles('ca') DuplicateCarnet(@Body() body: CarnetProcessingCenterDTO) { return this.carnetApplicationService.DuplicateCarnet(body); } @Patch('AddlSets') @Roles('ca') AddlSets(@Body() body: CarnetProcessingCenterDTO) { return this.carnetApplicationService.AddlSets(body); } @Patch('ExtendCarnet') @Roles('ca') ExtendCarnet(@Body() body: CarnetProcessingCenterDTO) { return this.carnetApplicationService.ExtendCarnet(body); } @Get('GetExtendedSection/:P_SPID/:P_HEADERID') @Roles('ca') GetExtendedSection(@Param() body: GetExtendedSectionDTO) { return this.carnetApplicationService.GetExtendedSection(body); } @Post('SaveExtensionApplication') @Roles('ca') @HttpCode(200) SaveExtensionApplication(@Body() body: SaveExtensionApplicationDTO) { return this.carnetApplicationService.SaveExtensionApplication(body); } @Patch('CloseCarnet') @Roles('sa') CloseCarnet(@Body() body: CarnetProcessingCenterDTO) { return this.carnetApplicationService.CloseCarnet(body); } @Patch('ResetCarnet') @Roles('sa') ResetCarnet(@Body() body: CarnetProcessingCenterDTO) { return this.carnetApplicationService.ResetCarnet(body); } @Delete('DeleteCarnet') DeleteCarnet(@Body() body: CarnetProcessingCenterDTO) { return this.carnetApplicationService.DeleteCarnet(body); } // [ CARNETCONTROLCENTER_PKG ] @Get('GetHolderstoEdit/:P_SPID/:P_USERID/:P_HEADERID') GetHolderstoEdit(@Param() body: GetCarnetControlCenterDTO) { return this.carnetApplicationService.GetHolderstoEdit(body); } @Get('GetGoodsDetailstoEdit/:P_SPID/:P_USERID/:P_HEADERID') GetGoodsDetailstoEdit(@Param() body: GetCarnetControlCenterDTO) { return this.carnetApplicationService.GetGoodsDetailstoEdit(body); } @Get('GetGoodsItemstoEdit/:P_SPID/:P_USERID/:P_HEADERID') GetGoodsItemstoEdit(@Param() body: GetCarnetControlCenterDTO) { return this.carnetApplicationService.GetGoodsItemstoEdit(body); } @Get('GetCountryDetailstoEdit/:P_SPID/:P_USERID/:P_HEADERID') GetCountryDetailstoEdit(@Param() body: GetCarnetControlCenterDTO) { return this.carnetApplicationService.GetCountryDetailstoEdit(body); } @Get('GetShipPaymentDetailstoEdit/:P_SPID/:P_USERID/:P_HEADERID') GetShipPaymentDetailstoEdit(@Param() body: GetCarnetControlCenterDTO) { return this.carnetApplicationService.GetShipPaymentDetailstoEdit(body); } // [PRINT_PKG] @Post('PrintCarnet') @Roles('ca', 'sa', 'ua') @HttpCode(200) async PrintCarnet(@Body() body: PrintCarnetDTO, @Res({ passthrough: true }) res: Response) { try { const { finalArray, carnetData }: any = await this.carnetApplicationService.PrintCarnet(body); // const filterCarnetData = finalArray.filter(item => item !== '2GeneralList.pdf') let filesArray = [`${replaceSlashWithDash(carnetData.carnetNo)}p1.pdf`, `${replaceSlashWithDash(carnetData.carnetNo)}p2.pdf`]; // await generateFinalPDF([...filesArray, '2GeneralList.pdf', ...carnetData], 'carnet.pdf'); await generateFinalPDF([...filesArray, ...finalArray, '13LastSheetP3.pdf', '13LastSheetP4.pdf'], `${replaceSlashWithDash(carnetData.carnetNo)}carnet.pdf`); const fileName = `${replaceSlashWithDash(carnetData.carnetNo)}carnet.pdf`; res.set({ 'Content-Type': 'application/pdf', 'Content-Disposition': `attachment; filename="${fileName}"`, }); const filePath = join(process.cwd(), 'dist/public/carnet-pdf', fileName); //server // const filePath = join(process.cwd(), 'public/carnet-pdf', fileName); //local console.log(filePath); // console.log(filePath); const stream = createReadStream(filePath); // Clean up files after response finishes streaming res.on('finish', async () => { await deleteFilesAsync([...filesArray, ...finalArray.filter(x => x !== `${replaceSlashWithDash(carnetData.carnetNo)}p2.pdf`), fileName]); }); return new StreamableFile(stream); } catch (error) { if (error instanceof BadRequestException || error instanceof BR) { throw new BadRequestException("Download failed please check the details") } else { throw new InternalServerException("Error while downloading"); } } } @Post('PrintGL') @Roles('ca', 'sa', 'ua') @HttpCode(200) async PrintGL(@Body() body: PrintGLDTO, @Res({ passthrough: true }) res: Response) { try { const { finalArray, carnetData }: any = await this.carnetApplicationService.PrintGL(body); let filesArray = [`${replaceSlashWithDash(carnetData.carnetNo)}p2.pdf`]; // await generateFinalPDF([...filesArray, '2GeneralList.pdf', ...carnetData], 'carnet.pdf'); await generateFinalPDF([...filesArray, ...finalArray], `${replaceSlashWithDash(carnetData.carnetNo)}-GL.pdf`); const fileName = `${replaceSlashWithDash(carnetData.carnetNo)}-GL.pdf`; res.set({ 'Content-Type': 'application/pdf', 'Content-Disposition': `attachment; filename="${fileName}"`, }); const filePath = join(process.cwd(), 'dist/public/carnet-pdf', fileName); //server // const filePath = join(process.cwd(), 'public/carnet-pdf', fileName); //local // console.log(filePath); // console.log(filePath); const stream = createReadStream(filePath); // Clean up files after response finishes streaming res.on('finish', async () => { await deleteFilesAsync([...filesArray, ...finalArray, fileName]); }); return new StreamableFile(stream); } catch (error) { return error.message; } } }