import { Injectable, Logger } from '@nestjs/common'; import { OracleDBService } from 'src/db/db.service'; import * as oracledb from 'oracledb'; import { InsertNewServiceProviderDTO, UpdateServiceProviderDTO, } from '../../../dto/sp/sp.dto'; import { BadRequestException } from 'src/exceptions/badRequest.exception'; import { InternalServerException } from 'src/exceptions/internalServerError.exception'; import { SPID_DTO } from 'src/dto/property.dto'; @Injectable() export class SpService { private readonly logger = new Logger(SpService.name); constructor(private readonly oracleDBService: OracleDBService) { } async insertNewServiceProvider(body: InsertNewServiceProviderDTO) { let connection; try { connection = await this.oracleDBService.getConnection(); if (!connection) { throw new InternalServerException(); } const result = await connection.execute( `BEGIN USCIB_Managed_Pkg.InsertNewSP( :P_NAME, :p_lookupcode, :p_address1, :p_address2, :p_city, :p_state, :p_zip, :p_country, :p_issuingregion, :p_replacementregion, :p_bondsurety, :p_cargopolicyno, :p_cargosurety, :p_user_id, :P_NOTES, :P_FILEIDS, :p_cursor); END;`, { P_NAME: { val: body.P_NAME, type: oracledb.DB_TYPE_VARCHAR, }, p_lookupcode: { val: body.p_lookupCode, type: oracledb.DB_TYPE_VARCHAR, }, p_address1: { val: body.p_address1, type: oracledb.DB_TYPE_VARCHAR, }, p_address2: { val: body.p_address2, type: oracledb.DB_TYPE_VARCHAR, }, p_city: { val: body.p_city, type: oracledb.DB_TYPE_VARCHAR, }, p_state: { val: body.p_state, type: oracledb.DB_TYPE_VARCHAR, }, p_zip: { val: body.p_zip, type: oracledb.DB_TYPE_VARCHAR, }, p_country: { val: body.p_country, type: oracledb.DB_TYPE_VARCHAR, }, p_issuingregion: { val: body.p_issuingregion, type: oracledb.DB_TYPE_VARCHAR, }, p_replacementregion: { val: body.p_replacementregion, type: oracledb.DB_TYPE_VARCHAR, }, p_bondsurety: { val: body.p_bondsurety, type: oracledb.DB_TYPE_VARCHAR, }, p_cargopolicyno: { val: body.p_cargopolicyno, type: oracledb.DB_TYPE_VARCHAR, }, p_cargosurety: { val: body.p_cargosurety, type: oracledb.DB_TYPE_VARCHAR, }, p_user_id: { val: body.p_user_id, type: oracledb.DB_TYPE_VARCHAR, }, P_NOTES: { val: body.P_NOTES, type: oracledb.DB_TYPE_VARCHAR, }, P_FILEIDS: { val: body.P_FILEIDS, type: oracledb.DB_TYPE_VARCHAR, }, p_cursor: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT, }, }, { outFormat: oracledb.OUT_FORMAT_OBJECT, }, ); await connection.commit(); const fres = await result.outBinds.p_cursor.getRows(); if (fres.length>0 && fres[0].ERRORMESG) { this.logger.warn(fres[0].ERRORMESG); throw new BadRequestException(fres[0].ERRORMESG) } // return fres[0]; return { statusCode: 201, message: "Createdted Successfully", ...fres[0] }; } catch (error) { if (error instanceof BadRequestException) { this.logger.warn(error.message); throw error; } this.logger.error('insertNewServiceProvider failed', error.stack || error); throw new InternalServerException(); } finally { if (connection) { try { await connection.close(); } catch (closeErr) { this.logger.error('Failed to close DB connection', closeErr); } } } } async updateServiceProvider(body: UpdateServiceProviderDTO) { const newBody = { p_spid: null, p_name: null, p_lookupcode: null, p_address1: null, p_address2: null, p_city: null, p_state: null, p_zip: null, p_country: null, p_issuingregion: null, p_replacementregion: null, p_bondsurety: null, p_cargopolicyno: null, p_cargosurety: null, p_user_id: 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: UpdateServiceProviderDTO = { ...newBody, ...reqBody }; let connection; try { connection = await this.oracleDBService.getConnection(); if (!connection) { throw new InternalServerException(); } const result = await connection.execute( `BEGIN USCIB_Managed_Pkg.UpdateSP( :p_spid, :P_NAME, :p_lookupcode, :p_address1, :p_address2, :p_city, :p_state, :p_zip, :p_country, :p_bondsurety, :p_cargopolicyno, :p_cargosurety, :p_replacementregion, :p_issuingregion, :p_user_id, :P_NOTES, :P_FILEIDS, :p_cursor); END;`, { p_spid: { val: finalBody.p_spid, type: oracledb.DB_TYPE_NUMBER, }, P_NAME: { val: finalBody.P_NAME, type: oracledb.DB_TYPE_VARCHAR, }, p_lookupcode: { val: finalBody.p_lookupCode, type: oracledb.DB_TYPE_VARCHAR, }, p_address1: { val: finalBody.p_address1, type: oracledb.DB_TYPE_VARCHAR, }, p_address2: { val: finalBody.p_address2, type: oracledb.DB_TYPE_VARCHAR, }, p_city: { val: finalBody.p_city, type: oracledb.DB_TYPE_VARCHAR, }, p_state: { val: finalBody.p_state, type: oracledb.DB_TYPE_VARCHAR, }, p_zip: { val: finalBody.p_zip, type: oracledb.DB_TYPE_VARCHAR, }, p_country: { val: finalBody.p_country, type: oracledb.DB_TYPE_VARCHAR, }, p_bondsurety: { val: finalBody.p_bondsurety, type: oracledb.DB_TYPE_VARCHAR, }, p_cargopolicyno: { val: finalBody.p_cargopolicyno, type: oracledb.DB_TYPE_VARCHAR, }, p_cargosurety: { val: finalBody.p_cargosurety, type: oracledb.DB_TYPE_VARCHAR, }, p_replacementregion: { val: finalBody.p_replacementregion, type: oracledb.DB_TYPE_VARCHAR, }, p_issuingregion: { val: finalBody.p_issuingregion, type: oracledb.DB_TYPE_VARCHAR, }, p_user_id: { val: finalBody.p_user_id, type: oracledb.DB_TYPE_VARCHAR, }, P_NOTES: { val: finalBody.P_NOTES, type: oracledb.DB_TYPE_VARCHAR, }, P_FILEIDS: { val: finalBody.P_FILEIDS, type: oracledb.DB_TYPE_VARCHAR, }, p_cursor: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT, }, }, { outFormat: oracledb.OUT_FORMAT_OBJECT, }, ); await connection.commit(); const fres = await result.outBinds.p_cursor.getRows(); if (fres.length>0 && fres[0].ERRORMESG) { this.logger.warn(fres[0].ERRORMESG); throw new BadRequestException(fres[0].ERRORMESG) } return { statusCode: 200, message: "Updated Successfully" }; } catch (error) { if (error instanceof BadRequestException) { this.logger.warn(error.message); throw error; } this.logger.error('updateServiceProvider failed', error.stack || error); throw new InternalServerException(); } finally { if (connection) { try { await connection.close(); } catch (closeErr) { this.logger.error('Failed to close DB connection', closeErr); } } } } async getAllServiceproviders() { let connection; let rows: any = []; try { connection = await this.oracleDBService.getConnection(); if (!connection) { throw new InternalServerException(); } const result = await connection.execute( `BEGIN USCIB_Managed_Pkg.GetAllSPs(:p_cursor); END;`, { p_cursor: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT, }, }, { outFormat: oracledb.OUT_FORMAT_OBJECT, }, ); if (result.outBinds && result.outBinds.p_cursor) { const cursor = result.outBinds.p_cursor; let rowsBatch; do { rowsBatch = await cursor.getRows(100); rows = rows.concat(rowsBatch); } while (rowsBatch.length > 0); await cursor.close(); if (rows.length > 0 && rows[0].ERRORMESG) { throw new BadRequestException(rows[0].ERRORMESG); } return rows; } else { throw new BadRequestException(); } } catch (error) { if (error instanceof BadRequestException) { this.logger.warn(error.message); throw error; } this.logger.error('getAllServiceproviders failed', error.stack || error); throw new InternalServerException(); } finally { if (connection) { try { await connection.close(); } catch (closeErr) { this.logger.error('Failed to close DB connection', closeErr); } } } } async getServiceproviderByID(body: SPID_DTO) { let connection; let rows: any = []; try { connection = await this.oracleDBService.getConnection(); if (!connection) { throw new InternalServerException(); } const result = await connection.execute( `BEGIN USCIB_Managed_Pkg.GetSPbySPID(:p_spid,:p_cursor); END;`, { p_spid: { val: body.p_spid, type: oracledb.DB_TYPE_NUMBER, }, p_cursor: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT, }, }, { outFormat: oracledb.OUT_FORMAT_OBJECT, }, ); if (result.outBinds && result.outBinds.p_cursor) { const cursor = result.outBinds.p_cursor; let rowsBatch; do { rowsBatch = await cursor.getRows(100); rows = rows.concat(rowsBatch); } while (rowsBatch.length > 0); await cursor.close(); } else { throw new BadRequestException(); } if (rows.length > 0 && rows[0].ERRORMESG) { throw new BadRequestException(rows[0].ERRORMESG); } return rows; } catch (error) { if (error instanceof BadRequestException) { this.logger.warn(error.message); throw error; } else if (error.message === "NJS-107: invalid cursor") { this.logger.warn(error.message); throw new BadRequestException(); } this.logger.error('getServiceproviderByID failed', error.stack || error); throw new InternalServerException(); } finally { if (connection) { try { await connection.close(); } catch (closeErr) { this.logger.error('Failed to close DB connection', closeErr); } } } } }