import { Injectable, Logger } from '@nestjs/common'; import { OracleDBService } from 'src/db/db.service'; import { CarnetProcessingCenterDTO, SaveCarnetApplicationDTO, TransmitApplicationtoProcessDTO } from 'src/dto/carnet-application/carnet-application.dto'; import * as oracledb from 'oracledb' import { InternalServerException } from 'src/exceptions/internalServerError.exception'; import { closeOracleDbConnection, fetchCursor, handleError } from 'src/utils/helper'; import { COUNTRYTABLE_DTO, COUNTRYTABLE_ROW_DTO, GLTABLE_DTO, GLTABLE_ROW_DTO } from 'src/dto/property.dto'; @Injectable() export class CarnetApplicationService { private readonly logger = new Logger(CarnetApplicationService.name); constructor(private readonly oracleDBService: OracleDBService) { } async SaveCarnetApplication(body: SaveCarnetApplicationDTO) { const newBody = { p_spid: null, // P_CLIENTID: null, P_LOCATIONID: null, p_userid: null, // P_HEADERID: null, P_APPLICATIONNAME: null, P_HOLDERID: null, P_COMMERCIALSAMPLEFLAG: null, P_PROFEQUIPMENTFLAG: null, P_EXHIBITIONSFAIRFLAG: null, P_AUTOFLAG: null, P_HORSEFLAG: null, P_AUTHREP: null, P_GLTABLE: null, P_USSETS: null, P_COUNTRYTABLE: null, P_SHIPTOTYPE: null, P_SHIPADDRID: null, P_FORMOFSECURITY: null, P_INSPROTECTION: null, P_LDIPROTECTION: null, P_DELIVERYTYPE: null, P_DELIVERYMETHOD: null, P_PAYMENTMETHOD: null, P_CUSTCOURIERNO: null, P_REFNO: null, P_NOTES: null, }; const reqBody = JSON.parse(JSON.stringify(body)); function setEmptyStringsToNull(obj) { Object.keys(obj).forEach((key) => { if (typeof obj[key] === 'object' && obj[key] !== null) { setEmptyStringsToNull(obj[key]); } else if (obj[key] === '') { obj[key] = null; } }); } setEmptyStringsToNull(reqBody); const finalBody = { ...newBody, ...reqBody }; let connection; try { connection = await this.oracleDBService.getConnection(); // const res = await connection.execute(`SELECT CARNETSYS.GLARRAY(1, 'Description for itemno 1', 2500, 20, 12, 'LBS', 'US') FROM dual`); // return res; const GLTABLE = await connection.getDbObjectClass('CARNETSYS.GLTABLE'); if (typeof GLTABLE !== 'function') { throw new InternalServerException('GLTABLE is not a constructor'); } async function createGLArrayInstance( ITEMNO, ITEMDESCRIPTION, ITEMVALUE, NOOFPIECES, ITEMWEIGHT, ITEMWEIGHTUOM, GOODSORIGINCOUNTRY, ) { const result = await connection.execute( `SELECT CARNETSYS.GLARRAY(:ITEMNO, :ITEMDESCRIPTION, :ITEMVALUE, :NOOFPIECES, :ITEMWEIGHT, :ITEMWEIGHTUOM, :GOODSORIGINCOUNTRY) FROM dual`, { ITEMNO, ITEMDESCRIPTION, ITEMVALUE, NOOFPIECES, ITEMWEIGHT, ITEMWEIGHTUOM, GOODSORIGINCOUNTRY, }, ); console.log(result.rows); return result.rows[0][0]; } const GLTABLE_ARRAY: GLTABLE_DTO = { P_GLTABLE: await Promise.all( (finalBody.P_GLTABLE ?? []).map(async (x: GLTABLE_ROW_DTO) => { return await createGLArrayInstance( x.ITEMNO, x.ITEMDESCRIPTION, x.ITEMVALUE, x.NOOFPIECES, x.ITEMWEIGHT, x.ITEMWEIGHTUOM, x.GOODSORIGINCOUNTRY, ); }) ) }; const GLTABLE_INSTANCE = new GLTABLE(GLTABLE_ARRAY.P_GLTABLE ?? []); const COUNTRYTABLE = await connection.getDbObjectClass('CARNETSYS.CARNETCOUNTRYTABLE'); if (typeof COUNTRYTABLE !== 'function') { throw new Error('COUNTRYTABLE is not a constructor'); } async function createCarnetCountryArrayInstance( P_VISISTTRANSITIND, COUNTRYCODE, NOOFTIMESENTLEAVE, ) { const result = await connection.execute( `SELECT CARNETSYS.CARNETCOUNTRYARRAY(:P_VISISTTRANSITIND, :COUNTRYCODE, :NOOFTIMESENTLEAVE) FROM dual`, { P_VISISTTRANSITIND, COUNTRYCODE, NOOFTIMESENTLEAVE, }, ); console.log(result.rows); return result.rows[0][0]; } const COUNTRYTABLE_ARRAY: COUNTRYTABLE_DTO = { P_COUNTRYTABLE: await Promise.all( (finalBody.P_COUNTRYTABLE ?? []).map(async (x: COUNTRYTABLE_ROW_DTO) => { return await createCarnetCountryArrayInstance( x.P_VISISTTRANSITIND, x.COUNTRYCODE, x.NOOFTIMESENTLEAVE, ); }) ) }; const COUNTRYTABLE_INSTANCE = new COUNTRYTABLE(COUNTRYTABLE_ARRAY.P_COUNTRYTABLE ?? []); // return { aa:GLTABLE_ARRAY, ab:COUNTRYTABLE_ARRAY, a: GLTABLE_INSTANCE, b: COUNTRYTABLE_INSTANCE } const result = await connection.execute( `BEGIN CARNETAPPLICATION_PKG.SaveCarnetApplication( :P_SPID, :P_CLIENTID, :P_LOCATIONID, :P_USERID, :P_HEADERID, :P_APPLICATIONNAME, :P_HOLDERID, :P_COMMERCIALSAMPLEFLAG, :P_PROFEQUIPMENTFLAG, :P_EXHIBITIONSFAIRFLAG, :P_AUTOFLAG, :P_HORSEFLAG, :P_AUTHREP, :P_GLTABLE, :P_USSETS, :P_COUNTRYTABLE, :P_SHIPTOTYPE, :P_SHIPADDRID, :P_FORMOFSECURITY, :P_INSPROTECTION, :P_LDIPROTECTION, :P_DELIVERYTYPE, :P_DELIVERYMETHOD, :P_PAYMENTMETHOD, :P_CUSTCOURIERNO, :P_REFNO, :P_NOTES, :P_CURSOR ); END;`, { P_SPID: { val: body.P_SPID, type: oracledb.DB_TYPE_NUMBER }, P_CLIENTID: { val: body.P_CLIENTID, type: oracledb.DB_TYPE_NUMBER }, P_LOCATIONID: { val: body.P_LOCATIONID, type: oracledb.DB_TYPE_NUMBER }, P_USERID: { val: body.P_USERID, type: oracledb.DB_TYPE_VARCHAR }, P_HEADERID: { val: body.P_HEADERID, type: oracledb.DB_TYPE_NUMBER }, P_APPLICATIONNAME: { val: body.P_APPLICATIONNAME, type: oracledb.DB_TYPE_VARCHAR }, P_HOLDERID: { val: body.P_HOLDERID, type: oracledb.DB_TYPE_NUMBER }, P_COMMERCIALSAMPLEFLAG: { val: body.P_COMMERCIALSAMPLEFLAG, type: oracledb.DB_TYPE_VARCHAR }, P_PROFEQUIPMENTFLAG: { val: body.P_PROFEQUIPMENTFLAG, type: oracledb.DB_TYPE_VARCHAR }, P_EXHIBITIONSFAIRFLAG: { val: body.P_EXHIBITIONSFAIRFLAG, type: oracledb.DB_TYPE_VARCHAR }, P_AUTOFLAG: { val: body.P_AUTOFLAG, type: oracledb.DB_TYPE_VARCHAR }, P_HORSEFLAG: { val: body.P_HORSEFLAG, type: oracledb.DB_TYPE_VARCHAR }, P_AUTHREP: { val: body.P_AUTHREP, type: oracledb.DB_TYPE_VARCHAR }, P_GLTABLE: { val: GLTABLE_INSTANCE, type: 'CARNETSYS.GLTABLE' }, // P_GLTABLE: { val: GLTABLE_INSTANCE, type: oracledb.DB_TYPE_OBJECT }, P_USSETS: { val: body.P_USSETS, type: oracledb.DB_TYPE_NUMBER }, // P_COUNTRYTABLE: { val: COUNTRYTABLE_INSTANCE, type: 'CARNETSYS.CARNETCOUNTRYTABLE' }, P_COUNTRYTABLE: { val: COUNTRYTABLE_INSTANCE, type: oracledb.DB_TYPE_OBJECT }, P_SHIPTOTYPE: { val: body.P_SHIPTOTYPE, type: oracledb.DB_TYPE_VARCHAR }, P_SHIPADDRID: { val: body.P_SHIPADDRID, type: oracledb.DB_TYPE_NUMBER }, P_FORMOFSECURITY: { val: body.P_FORMOFSECURITY, type: oracledb.DB_TYPE_VARCHAR }, P_INSPROTECTION: { val: body.P_INSPROTECTION, type: oracledb.DB_TYPE_VARCHAR }, P_LDIPROTECTION: { val: body.P_LDIPROTECTION, type: oracledb.DB_TYPE_VARCHAR }, P_DELIVERYTYPE: { val: body.P_DELIVERYTYPE, type: oracledb.DB_TYPE_VARCHAR }, P_DELIVERYMETHOD: { val: body.P_DELIVERYMETHOD, type: oracledb.DB_TYPE_VARCHAR }, P_PAYMENTMETHOD: { val: body.P_PAYMENTMETHOD, type: oracledb.DB_TYPE_VARCHAR }, P_CUSTCOURIERNO: { val: body.P_CUSTCOURIERNO, type: oracledb.DB_TYPE_VARCHAR }, P_REFNO: { val: body.P_REFNO, type: oracledb.DB_TYPE_VARCHAR }, P_NOTES: { val: body.P_NOTES, type: oracledb.DB_TYPE_VARCHAR }, P_CURSOR: { dir: oracledb.BIND_OUT, type: oracledb.CURSOR }, // P_CURSOR: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT } }, { outFormat: oracledb.OUT_FORMAT_OBJECT } ); await connection.commit(); const outBinds = result.outBinds; if (!outBinds?.P_CURSOR) { this.logger.error('One or more expected cursors are missing from stored procedure output.'); throw new InternalServerException("Incomplete data received from the database."); } return await fetchCursor(outBinds.P_CURSOR, CarnetApplicationService.name); } catch (error) { handleError(error, CarnetApplicationService.name) } finally { await closeOracleDbConnection(connection, CarnetApplicationService.name) } } async TransmitApplicationtoProcess(body: TransmitApplicationtoProcessDTO) { let connection; try { connection = await this.oracleDBService.getConnection(); const result = await connection.execute( `BEGIN CARNETAPPLICATION_PKG.TransmitApplicationtoProcess( :P_SPID, :P_USERID, :P_HEADERID :P_CURSOR ); END;`, { P_SPID: { val: body.P_SPID, type: oracledb.DB_TYPE_NUMBER }, P_USERID: { val: body.P_USERID, type: oracledb.DB_TYPE_NUMBER }, P_HEADERID: { val: body.P_HEADERID, type: oracledb.DB_TYPE_NUMBER }, P_CURSOR: { dir: oracledb.BIND_OUT, type: oracledb.CURSOR }, // P_CURSOR: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT } }, { outFormat: oracledb.OUT_FORMAT_OBJECT } ); await connection.commit(); const outBinds = result.outBinds; if (!outBinds?.P_CURSOR) { this.logger.error('One or more expected cursors are missing from stored procedure output.'); throw new InternalServerException("Incomplete data received from the database."); } return await fetchCursor(outBinds.P_CURSOR, CarnetApplicationService.name); } catch (error) { handleError(error, CarnetApplicationService.name) } finally { await closeOracleDbConnection(connection, CarnetApplicationService.name) } } // processing [ PROCESSINGCENTER_PKG ] async ProcessOriginalCarnet(body: CarnetProcessingCenterDTO) { let connection; try { connection = await this.oracleDBService.getConnection(); const result = await connection.execute( `BEGIN PROCESSINGCENTER_PKG.ProcessOriginalCarnet( :P_USERID, :P_HEADERID :P_CURSOR ); END;`, { P_USERID: { val: body.P_USERID, type: oracledb.DB_TYPE_NUMBER }, P_HEADERID: { val: body.P_HEADERID, type: oracledb.DB_TYPE_NUMBER }, P_CURSOR: { dir: oracledb.BIND_OUT, type: oracledb.CURSOR }, // P_CURSOR: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT } }, { outFormat: oracledb.OUT_FORMAT_OBJECT } ); await connection.commit(); const outBinds = result.outBinds; if (!outBinds?.P_CURSOR) { this.logger.error('One or more expected cursors are missing from stored procedure output.'); throw new InternalServerException("Incomplete data received from the database."); } return await fetchCursor(outBinds.P_CURSOR, CarnetApplicationService.name); } catch (error) { handleError(error, CarnetApplicationService.name) } finally { await closeOracleDbConnection(connection, CarnetApplicationService.name) } } async UpdatePrintCarnet(body: CarnetProcessingCenterDTO) { let connection; try { connection = await this.oracleDBService.getConnection(); const result = await connection.execute( `BEGIN PROCESSINGCENTER_PKG.UpdatePrintCarnet( :P_USERID, :P_HEADERID :P_CURSOR ); END;`, { P_USERID: { val: body.P_USERID, type: oracledb.DB_TYPE_NUMBER }, P_HEADERID: { val: body.P_HEADERID, type: oracledb.DB_TYPE_NUMBER }, P_CURSOR: { dir: oracledb.BIND_OUT, type: oracledb.CURSOR }, // P_CURSOR: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT } }, { outFormat: oracledb.OUT_FORMAT_OBJECT } ); await connection.commit(); const outBinds = result.outBinds; if (!outBinds?.P_CURSOR) { this.logger.error('One or more expected cursors are missing from stored procedure output.'); throw new InternalServerException("Incomplete data received from the database."); } return await fetchCursor(outBinds.P_CURSOR, CarnetApplicationService.name); } catch (error) { handleError(error, CarnetApplicationService.name) } finally { await closeOracleDbConnection(connection, CarnetApplicationService.name) } } async VoidCarnet(body: CarnetProcessingCenterDTO) { let connection; try { connection = await this.oracleDBService.getConnection(); const result = await connection.execute( `BEGIN PROCESSINGCENTER_PKG.VoidCarnet( :P_USERID, :P_HEADERID :P_CURSOR ); END;`, { P_USERID: { val: body.P_USERID, type: oracledb.DB_TYPE_NUMBER }, P_HEADERID: { val: body.P_HEADERID, type: oracledb.DB_TYPE_NUMBER }, P_CURSOR: { dir: oracledb.BIND_OUT, type: oracledb.CURSOR }, // P_CURSOR: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT } }, { outFormat: oracledb.OUT_FORMAT_OBJECT } ); await connection.commit(); const outBinds = result.outBinds; if (!outBinds?.P_CURSOR) { this.logger.error('One or more expected cursors are missing from stored procedure output.'); throw new InternalServerException("Incomplete data received from the database."); } return await fetchCursor(outBinds.P_CURSOR, CarnetApplicationService.name); } catch (error) { handleError(error, CarnetApplicationService.name) } finally { await closeOracleDbConnection(connection, CarnetApplicationService.name) } } async DuplicateCarnet(body: CarnetProcessingCenterDTO) { let connection; try { connection = await this.oracleDBService.getConnection(); const result = await connection.execute( `BEGIN PROCESSINGCENTER_PKG.DuplicateCarnet( :P_USERID, :P_HEADERID :P_CURSOR ); END;`, { P_USERID: { val: body.P_USERID, type: oracledb.DB_TYPE_NUMBER }, P_HEADERID: { val: body.P_HEADERID, type: oracledb.DB_TYPE_NUMBER }, P_CURSOR: { dir: oracledb.BIND_OUT, type: oracledb.CURSOR }, // P_CURSOR: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT } }, { outFormat: oracledb.OUT_FORMAT_OBJECT } ); await connection.commit(); const outBinds = result.outBinds; if (!outBinds?.P_CURSOR) { this.logger.error('One or more expected cursors are missing from stored procedure output.'); throw new InternalServerException("Incomplete data received from the database."); } return await fetchCursor(outBinds.P_CURSOR, CarnetApplicationService.name); } catch (error) { handleError(error, CarnetApplicationService.name) } finally { await closeOracleDbConnection(connection, CarnetApplicationService.name) } } async CloseCarnet(body: CarnetProcessingCenterDTO) { let connection; try { connection = await this.oracleDBService.getConnection(); const result = await connection.execute( `BEGIN PROCESSINGCENTER_PKG.CloseCarnet( :P_USERID, :P_HEADERID :P_CURSOR ); END;`, { P_USERID: { val: body.P_USERID, type: oracledb.DB_TYPE_NUMBER }, P_HEADERID: { val: body.P_HEADERID, type: oracledb.DB_TYPE_NUMBER }, P_CURSOR: { dir: oracledb.BIND_OUT, type: oracledb.CURSOR }, // P_CURSOR: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT } }, { outFormat: oracledb.OUT_FORMAT_OBJECT } ); await connection.commit(); const outBinds = result.outBinds; if (!outBinds?.P_CURSOR) { this.logger.error('One or more expected cursors are missing from stored procedure output.'); throw new InternalServerException("Incomplete data received from the database."); } return await fetchCursor(outBinds.P_CURSOR, CarnetApplicationService.name); } catch (error) { handleError(error, CarnetApplicationService.name) } finally { await closeOracleDbConnection(connection, CarnetApplicationService.name) } } }