import { Injectable } from '@nestjs/common'; import { CarnetProcessingCenterDTO, SaveCarnetApplicationDTO, TransmitApplicationtoProcessDTO, CreateApplicationDTO, UpdateExpGoodsAuthRepDTO, AddGenerallistItemsDTO, AddCountriesDTO, UpdateShippingDetailsDTO, CA_UpdateHolderDTO, GetCarnetControlCenterDTO, DeleteGenerallistItemsDTO, PrintCarnetDTO, PrintGLDTO, YON, CopyCarnetDTO, GetExtendedSectionDTO, SaveExtensionApplicationDTO, CarnetProcessingCenterDTO2, CapturePaymentDTO, InitiatePaymentDTO, GetOrderDetailsDTO, GetPaymentHistoryDTO, SaveHistoryDTO } from 'src/dto/property.dto'; import { BadRequestException } from 'src/exceptions/badRequest.exception'; import { PoolClient } from 'pg'; import { PgDBService } from 'src/db/db.service'; import { checkPgUserDefinedErrors, handlePgError, releasePgClient, ResponseStatus } from 'src/utils/helper'; @Injectable() export class CarnetApplicationService { constructor(private readonly pgDBService: PgDBService) { } // [ CARNETAPPLICATION_PKG ] // async SaveCarnetApplication(body: SaveCarnetApplicationDTO) { } async TransmitApplicationtoProcess(body: TransmitApplicationtoProcessDTO) { let client: PoolClient | null = null; const cursorName = 'p_cursor'; try { client = await this.pgDBService.getConnection(); await client.query('BEGIN'); const callProcQuery = ` CALL "CARNETSYS".carnetapplication_pkg_transmitapplicationtoprocess($1, $2, $3, $4) `; await client.query(callProcQuery, [body.P_SPID, body.P_USERID, body.P_HEADERID, cursorName]); // 🚫 DON'T quote the cursor name in FETCH const fetchResult = await client.query(`FETCH ALL FROM ${cursorName}`); // 🚫 DON'T quote the cursor name in CLOSE await client.query(`CLOSE ${cursorName}`); await client.query('COMMIT'); checkPgUserDefinedErrors(fetchResult); return { statusCode: 200, message: ResponseStatus.transmitted, ...fetchResult?.rows?.[0] || {} }; } catch (error) { if (client) await client.query('ROLLBACK'); handlePgError(error, CarnetApplicationService.name) } finally { releasePgClient(client) } } async CreateApplication(body: CreateApplicationDTO) { let client: PoolClient | null = null; const cursorName = 'p_cursor'; try { client = await this.pgDBService.getConnection(); await client.query('BEGIN'); const callProcQuery = ` CALL "CARNETSYS".carnetapplication_pkg_createapplication($1, $2, $3, $4, $5, $6, $7) `; await client.query(callProcQuery, [body.P_SPID, body.P_CLIENTID, body.P_LOCATIONID, body.P_USERID, body.P_APPLICATIONNAME, body.P_ORDERTYPE, cursorName]); // 🚫 DON'T quote the cursor name in FETCH const fetchResult = await client.query(`FETCH ALL FROM ${cursorName}`); // 🚫 DON'T quote the cursor name in CLOSE await client.query(`CLOSE ${cursorName}`); await client.query('COMMIT'); checkPgUserDefinedErrors(fetchResult); return { statusCode: 201, message: ResponseStatus.created, ...fetchResult?.rows?.[0] || {} }; } catch (error) { if (client) await client.query('ROLLBACK'); handlePgError(error, CarnetApplicationService.name) } finally { releasePgClient(client) } } async UpdateHolder(body: CA_UpdateHolderDTO) { let client: PoolClient | null = null; const cursorName = 'p_cursor'; try { client = await this.pgDBService.getConnection(); await client.query('BEGIN'); const callProcQuery = ` CALL "CARNETSYS".carnetapplication_pkg_updateholder($1, $2, $3) `; await client.query(callProcQuery, [body.P_HEADERID, body.P_HOLDERID, cursorName]); // 🚫 DON'T quote the cursor name in FETCH const fetchResult = await client.query(`FETCH ALL FROM ${cursorName}`); // 🚫 DON'T quote the cursor name in CLOSE await client.query(`CLOSE ${cursorName}`); await client.query('COMMIT'); checkPgUserDefinedErrors(fetchResult); return { statusCode: 200, message: ResponseStatus.updated, ...fetchResult?.rows?.[0] || {} }; } catch (error) { if (client) await client.query('ROLLBACK'); handlePgError(error, CarnetApplicationService.name) } finally { releasePgClient(client) } } async UpdateExpGoodsAuthRep(body: UpdateExpGoodsAuthRepDTO) { let client: PoolClient | null = null; const cursorName = 'p_cursor'; try { client = await this.pgDBService.getConnection(); await client.query('BEGIN'); const callProcQuery = ` CALL "CARNETSYS".carnetapplication_pkg_updateexpgoodsauthrep($1, $2, $3, $4, $5, $6, $7, $8) `; await client.query(callProcQuery, [body.P_HEADERID, body.P_COMMERCIALSAMPLEFLAG, body.P_PROFEQUIPMENTFLAG, body.P_EXHIBITIONSFAIRFLAG, body.P_AUTOFLAG, body.P_HORSEFLAG, body.P_AUTHREP, cursorName]); // 🚫 DON'T quote the cursor name in FETCH const fetchResult = await client.query(`FETCH ALL FROM ${cursorName}`); // 🚫 DON'T quote the cursor name in CLOSE await client.query(`CLOSE ${cursorName}`); await client.query('COMMIT'); checkPgUserDefinedErrors(fetchResult); return { statusCode: 200, message: ResponseStatus.updated, ...fetchResult?.rows?.[0] || {} }; } catch (error) { if (client) await client.query('ROLLBACK'); handlePgError(error, CarnetApplicationService.name) } finally { releasePgClient(client) } } async AddGenerallistItems(body: AddGenerallistItemsDTO) { let client: PoolClient | null = null; const cursorName = 'p_cursor'; try { client = await this.pgDBService.getConnection(); await client.query('BEGIN'); function escapePostgresString(str: string): string { if (str == null) return ''; return `"${str.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"`; } function escapePostgresValue(val: any): string { if (val == null || val === '') return ''; // NULL or empty becomes empty string if (typeof val === 'number') return String(val); // pass numbers as-is return escapePostgresString(String(val)); // quote and escape strings } const contactLiterals = body.P_GLTABLE.map(c => { const fields = [ escapePostgresValue(c.ITEMNO), // integer escapePostgresValue(c.ITEMDESCRIPTION), // varchar escapePostgresValue(c.ITEMVALUE), // numeric escapePostgresValue(c.NOOFPIECES), // integer escapePostgresValue(c.ITEMWEIGHT), // numeric escapePostgresValue(c.ITEMWEIGHTUOM), // varchar escapePostgresValue(c.GOODSORIGINCOUNTRY), // varchar ]; return `(${fields.join(',')})`; // composite literal without outer double quotes }); const pgArrayLiteral = `{${contactLiterals.join(',')}}`; const callProcQuery = ` CALL "CARNETSYS".carnetapplication_pkg_addgenerallistitems($1, $2::"CARNETSYS".gltable[], $3, $4) `; await client.query(callProcQuery, [body.P_HEADERID, pgArrayLiteral, body.P_USERID, cursorName]); // 🚫 DON'T quote the cursor name in FETCH const fetchResult = await client.query(`FETCH ALL FROM ${cursorName}`); // 🚫 DON'T quote the cursor name in CLOSE await client.query(`CLOSE ${cursorName}`); await client.query('COMMIT'); checkPgUserDefinedErrors(fetchResult); return { statusCode: 200, message: ResponseStatus.added, ...fetchResult?.rows?.[0] || {} }; } catch (error) { if (client) await client.query('ROLLBACK'); handlePgError(error, CarnetApplicationService.name) } finally { releasePgClient(client) } } async EditGenerallistItems(body: AddGenerallistItemsDTO) { let client: PoolClient | null = null; const cursorName = 'p_cursor'; try { client = await this.pgDBService.getConnection(); await client.query('BEGIN'); function escapePostgresString(str: string): string { if (str == null) return ''; return `"${str.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"`; } function escapePostgresValue(val: any): string { if (val == null || val === '') return ''; // NULL or empty becomes empty string if (typeof val === 'number') return String(val); // pass numbers as-is return escapePostgresString(String(val)); // quote and escape strings } const contactLiterals = body.P_GLTABLE.map(c => { const fields = [ escapePostgresValue(c.ITEMNO), // integer escapePostgresValue(c.ITEMDESCRIPTION), // varchar escapePostgresValue(c.ITEMVALUE), // numeric escapePostgresValue(c.NOOFPIECES), // integer escapePostgresValue(c.ITEMWEIGHT), // numeric escapePostgresValue(c.ITEMWEIGHTUOM), // varchar escapePostgresValue(c.GOODSORIGINCOUNTRY), // varchar ]; return `(${fields.join(',')})`; // composite literal without outer double quotes }); const pgArrayLiteral = `{${contactLiterals.join(',')}}`; const callProcQuery = ` CALL "CARNETSYS".carnetapplication_pkg_editgenerallistitems($1, $2::"CARNETSYS".gltable[], $3, $4) `; await client.query(callProcQuery, [body.P_HEADERID, pgArrayLiteral, body.P_USERID, cursorName]); // 🚫 DON'T quote the cursor name in FETCH const fetchResult = await client.query(`FETCH ALL FROM ${cursorName}`); // 🚫 DON'T quote the cursor name in CLOSE await client.query(`CLOSE ${cursorName}`); await client.query('COMMIT'); checkPgUserDefinedErrors(fetchResult); return { statusCode: 200, message: ResponseStatus.updated, ...fetchResult?.rows?.[0] || {} }; } catch (error) { if (client) await client.query('ROLLBACK'); handlePgError(error, CarnetApplicationService.name) } finally { releasePgClient(client) } } async DeleteGenerallistItems(body: DeleteGenerallistItemsDTO) { let client: PoolClient | null = null; const cursorName = 'p_cursor'; try { client = await this.pgDBService.getConnection(); await client.query('BEGIN'); const callProcQuery = ` CALL "CARNETSYS".carnetapplication_pkg_deletegenerallistitems($1, $2, $3, $4) `; await client.query(callProcQuery, [body.P_HEADERID, body.P_ITEMNO, body.P_USERID, cursorName]); // 🚫 DON'T quote the cursor name in FETCH const fetchResult = await client.query(`FETCH ALL FROM ${cursorName}`); // 🚫 DON'T quote the cursor name in CLOSE await client.query(`CLOSE ${cursorName}`); await client.query('COMMIT'); checkPgUserDefinedErrors(fetchResult); return { statusCode: 200, message: ResponseStatus.deleted, ...fetchResult?.rows?.[0] || {} }; } catch (error) { if (client) await client.query('ROLLBACK'); handlePgError(error, CarnetApplicationService.name) } finally { releasePgClient(client) } } async AddCountries(body: AddCountriesDTO) { let client: PoolClient | null = null; const cursorName = 'p_cursor'; try { client = await this.pgDBService.getConnection(); await client.query('BEGIN'); function escapePostgresString(str: string): string { if (str == null) return ''; return `"${str.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"`; } function escapePostgresValue(val: any): string { if (val == null || val === '') return ''; // NULL or empty becomes empty string if (typeof val === 'number') return String(val); // pass numbers as-is return escapePostgresString(String(val)); // quote and escape strings } const contactLiterals = body.P_COUNTRYTABLE.map(c => { const fields = [ escapePostgresValue(c.P_VISISTTRANSITIND), // integer escapePostgresValue(c.COUNTRYCODE), // varchar escapePostgresValue(c.NOOFTIMESENTLEAVE), // numeric ]; return `(${fields.join(',')})`; // composite literal without outer double quotes }); const pgArrayLiteral = `{${contactLiterals.join(',')}}`; const callProcQuery = ` CALL "CARNETSYS".carnetapplication_pkg_addcountries($1, $2, $3::"CARNETSYS".carnetcountrytable[], $4, $5) `; await client.query(callProcQuery, [body.P_HEADERID, body.P_USSETS, pgArrayLiteral, body.P_USERID, cursorName]); // 🚫 DON'T quote the cursor name in FETCH const fetchResult = await client.query(`FETCH ALL FROM ${cursorName}`); // 🚫 DON'T quote the cursor name in CLOSE await client.query(`CLOSE ${cursorName}`); await client.query('COMMIT'); checkPgUserDefinedErrors(fetchResult); return { statusCode: 200, message: ResponseStatus.added, ...fetchResult?.rows?.[0] || {} }; } catch (error) { if (client) await client.query('ROLLBACK'); handlePgError(error, CarnetApplicationService.name) } finally { releasePgClient(client) } } async UpdateShippingDetails(body: UpdateShippingDetailsDTO) { } async EstimatedFees(body: GetCarnetControlCenterDTO) { } // processing [ PROCESSINGCENTER_PKG ] async ProcessCarnet(body: CarnetProcessingCenterDTO) { } async UpdatePrintCarnet(body: CarnetProcessingCenterDTO) { } async VoidCarnet(body: CarnetProcessingCenterDTO) { } async CopyCarnet(body: CopyCarnetDTO) { } async DuplicateCarnet(body: CarnetProcessingCenterDTO2) { } async AddlSets(body: CarnetProcessingCenterDTO2) { } async ExtendCarnet(body: CarnetProcessingCenterDTO2) { } async GetExtendedSection(body: GetExtendedSectionDTO) { } async SaveExtensionApplication(body: SaveExtensionApplicationDTO) { } async CloseCarnet(body: CarnetProcessingCenterDTO) { } async ResetCarnet(body: CarnetProcessingCenterDTO) { } async DeleteCarnet(body: CarnetProcessingCenterDTO) { } // [ CARNETCONTROLCENTER_PKG ] async GetHolderstoEdit(body: GetCarnetControlCenterDTO) { let client: PoolClient | null = null; const cursorName = 'p_cursor'; try { client = await this.pgDBService.getConnection(); await client.query('BEGIN'); const callProcQuery = ` CALL "CARNETSYS".carnetcontrolcenter_pkg_getholderstoedit($1, $2, $3, $4) `; await client.query(callProcQuery, [body.P_SPID, body.P_USERID, body.P_HEADERID, cursorName]); // 🚫 DON'T quote the cursor name in FETCH const fetchResult = await client.query(`FETCH ALL FROM ${cursorName}`); // 🚫 DON'T quote the cursor name in CLOSE await client.query(`CLOSE ${cursorName}`); await client.query('COMMIT'); return fetchResult.rows; } catch (error) { if (client) await client.query('ROLLBACK'); handlePgError(error, CarnetApplicationService.name) } finally { releasePgClient(client) } } async GetGoodsDetailstoEdit(body: GetCarnetControlCenterDTO) { let client: PoolClient | null = null; const cursorName = 'p_cursor'; try { client = await this.pgDBService.getConnection(); await client.query('BEGIN'); const callProcQuery = ` CALL "CARNETSYS".carnetcontrolcenter_pkg_getgoodsdetailstoedit($1, $2, $3, $4) `; await client.query(callProcQuery, [body.P_SPID, body.P_USERID, body.P_HEADERID, cursorName]); // 🚫 DON'T quote the cursor name in FETCH const fetchResult = await client.query(`FETCH ALL FROM ${cursorName}`); // 🚫 DON'T quote the cursor name in CLOSE await client.query(`CLOSE ${cursorName}`); await client.query('COMMIT'); return fetchResult.rows; } catch (error) { if (client) await client.query('ROLLBACK'); handlePgError(error, CarnetApplicationService.name) } finally { releasePgClient(client) } } async GetCountryDetailstoEdit(body: GetCarnetControlCenterDTO) { let client: PoolClient | null = null; const cursorName = 'p_cursor'; try { client = await this.pgDBService.getConnection(); await client.query('BEGIN'); const callProcQuery = ` CALL "CARNETSYS".carnetcontrolcenter_pkg_getcountrydetailstoedit($1, $2, $3, $4) `; await client.query(callProcQuery, [body.P_SPID, body.P_USERID, body.P_HEADERID, cursorName]); // 🚫 DON'T quote the cursor name in FETCH const fetchResult = await client.query(`FETCH ALL FROM ${cursorName}`); // 🚫 DON'T quote the cursor name in CLOSE await client.query(`CLOSE ${cursorName}`); await client.query('COMMIT'); return fetchResult.rows; } catch (error) { if (client) await client.query('ROLLBACK'); handlePgError(error, CarnetApplicationService.name) } finally { releasePgClient(client) } } async GetShipPaymentDetailstoEdit(body: GetCarnetControlCenterDTO) { let client: PoolClient | null = null; const cursorName = 'p_cursor'; try { client = await this.pgDBService.getConnection(); await client.query('BEGIN'); const callProcQuery = ` CALL "CARNETSYS".carnetcontrolcenter_pkg_getshippaymentdetailstoedit($1, $2, $3, $4) `; await client.query(callProcQuery, [body.P_SPID, body.P_USERID, body.P_HEADERID, cursorName]); // 🚫 DON'T quote the cursor name in FETCH const fetchResult = await client.query(`FETCH ALL FROM ${cursorName}`); // 🚫 DON'T quote the cursor name in CLOSE await client.query(`CLOSE ${cursorName}`); await client.query('COMMIT'); return fetchResult.rows; } catch (error) { if (client) await client.query('ROLLBACK'); handlePgError(error, CarnetApplicationService.name) } finally { releasePgClient(client) } } async GetGoodsItemstoEdit(body: GetCarnetControlCenterDTO) { let client: PoolClient | null = null; const cursorName = 'p_cursor'; try { client = await this.pgDBService.getConnection(); await client.query('BEGIN'); const callProcQuery = ` CALL "CARNETSYS".carnetcontrolcenter_pkg_getgoodsitemstoedit($1, $2, $3, $4) `; await client.query(callProcQuery, [body.P_SPID, body.P_USERID, body.P_HEADERID, cursorName]); // 🚫 DON'T quote the cursor name in FETCH const fetchResult = await client.query(`FETCH ALL FROM ${cursorName}`); // 🚫 DON'T quote the cursor name in CLOSE await client.query(`CLOSE ${cursorName}`); await client.query('COMMIT'); return fetchResult.rows; } catch (error) { if (client) await client.query('ROLLBACK'); handlePgError(error, CarnetApplicationService.name) } finally { releasePgClient(client) } } // [PRINT_PKG] async getPrintingData(body: PrintCarnetDTO) { } async PrintCarnet(body: PrintCarnetDTO) { } async PrintGL(body: PrintGLDTO) { } // [payment] async formatAmount(amount) { if (typeof amount !== 'number' || isNaN(amount)) { throw new Error('Amount must be a valid number'); } if (amount < 0) { throw new BadRequestException('Amount must be non-negative'); } return amount.toFixed(2); // Always returns a string with 2 decimal places } async InitiatePayment(body: InitiatePaymentDTO) { } async CapturePayment(body: CapturePaymentDTO) { } async OrderDetails(body: GetOrderDetailsDTO) { } async GetPaymentHistory(body: GetPaymentHistoryDTO) { } async SaveHistory(body: SaveHistoryDTO) { } }