import { Injectable, Logger } from "@nestjs/common"; import { OracleDBService } from "src/db/db.service"; import { COUNTRYTABLE_DTO, COUNTRYTABLE_ROW_DTO, GLTABLE_DTO, GLTABLE_ROW_DTO } from "src/dto/property.dto"; import { InternalServerException } from "src/exceptions/internalServerError.exception"; import { closeOracleDbConnection, handleError } from "src/utils/helper"; @Injectable() export class OracleService { private readonly logger = new Logger(OracleService.name); constructor(private readonly oracleDBService: OracleDBService) { } async get_GL_TABLE_INSTANCE(finalBody: any) { // P_GLTABLE: { val: GLTABLE_INSTANCE, type: 'CARNETSYS.GLTABLE' } // P_GLTABLE: { val: GLTABLE_INSTANCE, type: oracledb.DB_TYPE_OBJECT } let connection; try { connection = await this.oracleDBService.getConnection(); 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 ?? []); return GLTABLE_INSTANCE; } catch (error) { throw new Error("Error occured completing this process") } finally { await closeOracleDbConnection(connection, OracleService.name) } } async get_COUNTRY_TABLE_INSTANCE(finalBody: any) { // P_COUNTRYTABLE: { val: COUNTRYTABLE_INSTANCE, type: 'CARNETSYS.CARNETCOUNTRYTABLE' } // P_COUNTRYTABLE: { val: COUNTRYTABLE_INSTANCE, type: oracledb.DB_TYPE_OBJECT } let connection; try { connection = await this.oracleDBService.getConnection(); 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 COUNTRYTABLE_INSTANCE; } catch (error) { throw new Error("Error occured completing this process") } finally { await closeOracleDbConnection(connection, OracleService.name) } } }