import { Injectable, Logger } from '@nestjs/common'; import { OracleDBService } from 'src/db/db.service'; import * as oracledb from 'oracledb'; import { BadRequestException } from 'src/exceptions/badRequest.exception'; import { InternalServerException } from 'src/exceptions/internalServerError.exception'; import { closeOracleDbConnection, fetchCursor, handleError } from 'src/utils/helper'; import { InsertRegionsDto, UpdateRegionDto } from 'src/dto/property.dto'; @Injectable() export class RegionService { private readonly logger = new Logger(RegionService.name); constructor(private readonly oracleDBService: OracleDBService) { } async insertRegions(body: InsertRegionsDto) { let connection; try { connection = await this.oracleDBService.getConnection(); const result = await connection.execute( `BEGIN USCIB_Managed_Pkg.InsertNewRegion(:P_REGION,:P_NAME,:P_CURSOR); END;`, { P_REGION: { val: body.P_REGION, type: oracledb.DB_TYPE_VARCHAR }, P_NAME: { val: body.P_NAME, type: oracledb.DB_TYPE_VARCHAR }, 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."); } const fres: any = await fetchCursor(outBinds.P_CURSOR, RegionService.name); if (fres.length > 0 && fres[0].ERRORMESG) { this.logger.warn(fres[0].ERRORMESG); throw new BadRequestException(fres[0].ERRORMESG) } return { statusCode: 201, message: "Createdted Successfully", ...fres[0] }; } catch (error) { handleError(error, RegionService.name) } finally { await closeOracleDbConnection(connection, RegionService.name) } } async updateRegions(body: UpdateRegionDto) { let connection; try { connection = await this.oracleDBService.getConnection(); const result = await connection.execute( `BEGIN USCIB_Managed_Pkg.UpdateRegion(:P_REGIONID,:P_NAME,:P_CURSOR); END;`, { P_REGIONID: { val: body.P_REGIONID, type: oracledb.DB_TYPE_NUMBER }, P_NAME: { val: body.P_NAME, type: oracledb.DB_TYPE_VARCHAR }, 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."); } const fres: any = await fetchCursor(outBinds.P_CURSOR, RegionService.name); 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", ...fres[0] }; } catch (error) { handleError(error, RegionService.name) } finally { await closeOracleDbConnection(connection, RegionService.name) } } async getRegions() { let connection; try { connection = await this.oracleDBService.getConnection(); const result = await connection.execute( `BEGIN USCIB_Managed_Pkg.GetRegions(:P_CURSOR); END;`, { P_CURSOR: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT } }, { outFormat: oracledb.OUT_FORMAT_OBJECT } ); 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, RegionService.name); } catch (error) { handleError(error, RegionService.name) } finally { await closeOracleDbConnection(connection, RegionService.name) } } }