diff --git a/src/exceptions/badRequest.exception.ts b/src/exceptions/badRequest.exception.ts index 68a1ca3..8574072 100644 --- a/src/exceptions/badRequest.exception.ts +++ b/src/exceptions/badRequest.exception.ts @@ -8,12 +8,12 @@ export class BadRequestException extends HttpException { ) { super( { - statusCode: HttpStatus.INTERNAL_SERVER_ERROR, + statusCode: HttpStatus.BAD_REQUEST , message, // errorCode, // data, }, - HttpStatus.INTERNAL_SERVER_ERROR, + HttpStatus.BAD_REQUEST, ); } } diff --git a/src/main.ts b/src/main.ts index 976d4dc..a646b63 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,12 +1,12 @@ import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; import { - BadRequestException, ValidationError, ValidationPipe, } from '@nestjs/common'; import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger'; import { SwaggerDocumentOptions } from '@nestjs/swagger'; +import { BadRequestException } from './exceptions/badRequest.exception'; async function bootstrap() { const app = await NestFactory.create(AppModule, { @@ -56,10 +56,7 @@ async function bootstrap() { }) .filter(Boolean); - throw new BadRequestException({ - message: 'Validation failed', - errors: newResult, - }); + throw new BadRequestException(newResult[0].message); }; app.useGlobalPipes( @@ -77,11 +74,11 @@ async function bootstrap() { .setTitle('API') .setDescription('API description') .setVersion('1.0') - .addServer('http://localhost:3000', 'Development Server 1') .addServer( 'https://dev.alphaomegainfosys.com/test-api', 'Development Server 2', ) + .addServer('http://localhost:3000', 'Development Server 1') .build(); const options: SwaggerDocumentOptions = { diff --git a/src/oracle/manage-fee/manage-fee.controller.ts b/src/oracle/manage-fee/manage-fee.controller.ts index fdd5ba1..db51d3b 100644 --- a/src/oracle/manage-fee/manage-fee.controller.ts +++ b/src/oracle/manage-fee/manage-fee.controller.ts @@ -17,6 +17,7 @@ import { UpdateCsFeeDTO, UpdateEfFeeDTO, UpdateFeeCommBodyDTO, + UpdateFeeCommDTO, } from './manage-fee.dto'; @Controller('oracle') @@ -145,7 +146,7 @@ export class ManageFeeController { @ApiTags('Manage Fee - Oracle') @Patch('/UpdateFeeComm') - UpdateFeeComm(@Body() body: UpdateFeeCommBodyDTO) { + UpdateFeeComm(@Body() body: UpdateFeeCommDTO) { return this.manageFeeService.UPDATEFEECOMM(body); } } diff --git a/src/oracle/manage-fee/manage-fee.service.ts b/src/oracle/manage-fee/manage-fee.service.ts index 304f13d..06c139f 100644 --- a/src/oracle/manage-fee/manage-fee.service.ts +++ b/src/oracle/manage-fee/manage-fee.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@nestjs/common'; +import { Injectable, Logger } from '@nestjs/common'; import * as oracledb from 'oracledb'; import { OracleDBService } from 'src/db/db.service'; import { @@ -17,34 +17,38 @@ import { UpdateCsFeeDTO, UpdateEfFeeDTO, UpdateFeeCommBodyDTO, + UpdateFeeCommDTO, } from './manage-fee.dto'; import { BadRequestException } from 'src/exceptions/badRequest.exception'; +import { InternalServerException } from 'src/exceptions/internalServerError.exception'; @Injectable() export class ManageFeeService { + private readonly logger = new Logger(ManageFeeService.name); + constructor(private readonly oracleDBService: OracleDBService) { } // get - async GETBASICFEERATES(body: GetFeeGeneralDTO) { + async GETBASICFEERATES(body: GetFeeGeneralDTO): Promise { let connection; - let rows = []; + let rows: any[] = []; + try { connection = await this.oracleDBService.getConnection(); if (!connection) { - throw new Error('No DB Connected'); + this.logger.error(`Database connection failed`); + throw new InternalServerException(); } - const result = await connection.execute( `BEGIN - MANAGEFEE_SETUP_PKG.GETBASICFEERATES(:P_SPID,:P_ACTIVE_INACTIVE,:p_cursor); - END;`, + MANAGEFEE_SETUP_PKG.GETBASICFEERATES(:P_SPID, :P_ACTIVE_INACTIVE, :p_cursor); + END;`, { P_SPID: { val: body.P_SPID, type: oracledb.DB_TYPE_NUMBER, }, - P_ACTIVE_INACTIVE: { val: body.P_ACTIVE_INACTIVE, type: oracledb.DB_TYPE_NVARCHAR, @@ -54,12 +58,10 @@ export class ManageFeeService { dir: oracledb.BIND_OUT, }, }, - { - outFormat: oracledb.OUT_FORMAT_OBJECT, - }, + { outFormat: oracledb.OUT_FORMAT_OBJECT }, ); - if (result.outBinds && result.outBinds.p_cursor) { + if (result.outBinds?.p_cursor) { const cursor = result.outBinds.p_cursor; let rowsBatch; @@ -70,43 +72,51 @@ export class ManageFeeService { await cursor.close(); } else { - throw new Error('No cursor returned from the stored procedure'); + this.logger.error(`Invalid response from database procedure`); + throw new BadRequestException(); + } + + if (rows.length > 0 && rows[0].ERRORMESG) { + this.logger.warn(`error from DB: ${rows[0].ERRORMESG}`); + throw new BadRequestException(rows[0].ERRORMESG); } return rows; - } catch (err) { - if (err instanceof Error) { - if(err.message === "NJS-107: invalid cursor"){ - throw new BadRequestException("Bad Request") - } - return { error: err.message }; - } else { - return { error: 'An unknown error occurred' }; + + } catch (error) { + if (error instanceof BadRequestException) { + throw error; } - } - finally { + else if (error.message === "NJS-107: invalid cursor") { + throw new BadRequestException(); + } + this.logger.error('GETBASICFEERATES failed', error.stack || error); + throw new InternalServerException(); + } finally { if (connection) { try { await connection.close(); } catch (closeErr) { - console.error('Failed to close connection:', closeErr); + this.logger.error('Failed to close DB connection', closeErr); } } } } - async GETBONDRATES(body: GetFeeGeneralDTO) { + + async GETBONDRATES(body: GetFeeGeneralDTO): Promise { let connection; - let rows = []; + let rows: any = []; try { connection = await this.oracleDBService.getConnection(); if (!connection) { - throw new Error('No DB Connected'); + this.logger.error(`Database connection failed`); + throw new InternalServerException(); } const result = await connection.execute( `BEGIN - MANAGEFEE_SETUP_PKG.GETBONDRATES(:P_SPID,:P_ACTIVE_INACTIVE,:p_cursor); - END;`, + MANAGEFEE_SETUP_PKG.GETBONDRATES(:P_SPID,:P_ACTIVE_INACTIVE,:p_cursor); + END;`, { P_SPID: { val: body.P_SPID, @@ -137,40 +147,49 @@ export class ManageFeeService { await cursor.close(); } else { - throw new Error('No cursor returned from the stored procedure'); + throw new BadRequestException() + } + + if (rows[0].ERRORMESG) { + console.log(rows[0].ERRORMESG); + throw new BadRequestException(rows[0].ERRORMESG) } return rows; - } catch (err) { - if (err instanceof Error) { - return { error: err.message }; - } else { - return { error: 'An unknown error occurred' }; + } catch (error) { + if (error instanceof BadRequestException) { + throw error; } - } - finally { + else if (error.message === "NJS-107: invalid cursor") { + throw new BadRequestException(); + } + this.logger.error('GETBONDRATES failed', error.stack || error); + throw new InternalServerException(); + } finally { if (connection) { try { await connection.close(); } catch (closeErr) { - console.error('Failed to close connection:', closeErr); + this.logger.error('Failed to close DB connection', closeErr); } } } } - async GETCARGORATES(body: GetFeeGeneralDTO) { + + async GETCARGORATES(body: GetFeeGeneralDTO): Promise { let connection; - let rows = []; + let rows: any = []; try { connection = await this.oracleDBService.getConnection(); if (!connection) { - throw new Error('No DB Connected'); + this.logger.error(`Database connection failed`); + throw new InternalServerException(); } const result = await connection.execute( `BEGIN - MANAGEFEE_SETUP_PKG.GETCARGORATES(:P_SPID,:P_ACTIVE_INACTIVE,:p_cursor); - END;`, + MANAGEFEE_SETUP_PKG.GETCARGORATES(:P_SPID,:P_ACTIVE_INACTIVE,:p_cursor); + END;`, { P_SPID: { val: body.P_SPID, @@ -201,41 +220,51 @@ export class ManageFeeService { await cursor.close(); } else { - throw new Error('No cursor returned from the stored procedure'); + throw new BadRequestException(); + } + + if (rows.length > 0 && rows[0].ERRORMESG) { + this.logger.warn(`error from DB: ${rows[0].ERRORMESG}`); + throw new BadRequestException(rows[0].ERRORMESG); } return rows; - } catch (err) { - if (err instanceof Error) { - return { error: err.message }; - } else { - return { error: 'An unknown error occurred' }; + } catch (error) { + if (error instanceof BadRequestException) { + throw error; } - } - finally { + else if (error.message === "NJS-107: invalid cursor") { + throw new BadRequestException(); + } + this.logger.error('GETCARGORATES failed', error.stack || error); + throw new InternalServerException(); + } finally { + // Ensure connection is closed if (connection) { try { await connection.close(); } catch (closeErr) { - console.error('Failed to close connection:', closeErr); + this.logger.error('Failed to close DB connection', closeErr); } } } } - async GETCFFEERATES(body: GetFeeGeneralDTO) { + + async GETCFFEERATES(body: GetFeeGeneralDTO): Promise { let connection; - let rows = []; + let rows: any = []; try { connection = await this.oracleDBService.getConnection(); if (!connection) { - throw new Error('No DB Connected'); + this.logger.error(`Database connection failed`); + throw new InternalServerException(); } const result = await connection.execute( `BEGIN - MANAGEFEE_SETUP_PKG.GETCFFEERATES(:P_SPID,:P_ACTIVE_INACTIVE,:p_cursor); - END;`, + MANAGEFEE_SETUP_PKG.GETCFFEERATES(:P_SPID,:P_ACTIVE_INACTIVE,:p_cursor); + END;`, { P_SPID: { val: body.P_SPID, @@ -266,40 +295,49 @@ export class ManageFeeService { await cursor.close(); } else { - throw new Error('No cursor returned from the stored procedure'); + throw new BadRequestException(); + } + + if (rows.length > 0 && rows[0].ERRORMESG) { + this.logger.warn(`error from DB: ${rows[0].ERRORMESG}`); + throw new BadRequestException(rows[0].ERRORMESG); } return rows; - } catch (err) { - if (err instanceof Error) { - return { error: err.message }; - } else { - return { error: 'An unknown error occurred' }; + } catch (error) { + if (error instanceof BadRequestException) { + throw error; } - } - finally { + else if (error.message === "NJS-107: invalid cursor") { + throw new BadRequestException(); + } + this.logger.error('GETCFFEERATES failed', error.stack || error); + throw new InternalServerException(); + } finally { if (connection) { try { await connection.close(); } catch (closeErr) { - console.error('Failed to close connection:', closeErr); + this.logger.error('Failed to close DB connection', closeErr); } } } } - async GETCSFEERATES(body: GetFeeGeneralDTO) { + + async GETCSFEERATES(body: GetFeeGeneralDTO): Promise { let connection; - let rows = []; + let rows: any = []; try { connection = await this.oracleDBService.getConnection(); if (!connection) { - throw new Error('No DB Connected'); + this.logger.error(`Database connection failed`); + throw new InternalServerException(); } const result = await connection.execute( `BEGIN - MANAGEFEE_SETUP_PKG.GETCSFEERATES(:P_SPID,:P_ACTIVE_INACTIVE,:p_cursor); - END;`, + MANAGEFEE_SETUP_PKG.GETCSFEERATES(:P_SPID,:P_ACTIVE_INACTIVE,:p_cursor); + END;`, { P_SPID: { val: body.P_SPID, @@ -330,40 +368,44 @@ export class ManageFeeService { await cursor.close(); } else { - throw new Error('No cursor returned from the stored procedure'); + throw new BadRequestException(); } return rows; - } catch (err) { - if (err instanceof Error) { - return { error: err.message }; - } else { - return { error: 'An unknown error occurred' }; + } catch (error) { + if (error instanceof BadRequestException) { + throw error; } - } - finally { + else if (error.message === "NJS-107: invalid cursor") { + throw new BadRequestException(); + } + this.logger.error('GETCSFEERATES failed', error.stack || error); + throw new InternalServerException(); + } finally { if (connection) { try { await connection.close(); } catch (closeErr) { - console.error('Failed to close connection:', closeErr); + this.logger.error('Failed to close DB connection', closeErr); } } } } - async GETEFFEERATES(body: GetFeeGeneralDTO) { + + async GETEFFEERATES(body: GetFeeGeneralDTO): Promise { let connection; - let rows = []; + let rows: any = []; try { connection = await this.oracleDBService.getConnection(); if (!connection) { - throw new Error('No DB Connected'); + this.logger.error(`Database connection failed`); + throw new InternalServerException(); } const result = await connection.execute( `BEGIN - MANAGEFEE_SETUP_PKG.GETEFFEERATES(:P_SPID,:P_ACTIVE_INACTIVE,:p_cursor); - END;`, + MANAGEFEE_SETUP_PKG.GETEFFEERATES(:P_SPID,:P_ACTIVE_INACTIVE,:p_cursor); + END;`, { P_SPID: { val: body.P_SPID, @@ -394,40 +436,44 @@ export class ManageFeeService { await cursor.close(); } else { - throw new Error('No cursor returned from the stored procedure'); + throw new BadRequestException(); } return rows; - } catch (err) { - if (err instanceof Error) { - return { error: err.message }; - } else { - return { error: 'An unknown error occurred' }; + } catch (error) { + if (error instanceof BadRequestException) { + throw error; } - } - finally { + else if (error.message === "NJS-107: invalid cursor") { + throw new BadRequestException(); + } + this.logger.error('GETEFFEERATES failed', error.stack || error); + throw new InternalServerException(); + } finally { if (connection) { try { await connection.close(); } catch (closeErr) { - console.error('Failed to close connection:', closeErr); + this.logger.error('Failed to close DB connection', closeErr); } } } } - async GETFEECOMM(body: GetFeeGeneralDTO) { + + async GETFEECOMM(body: GetFeeGeneralDTO): Promise { let connection; - let rows = []; + let rows: any = []; try { connection = await this.oracleDBService.getConnection(); if (!connection) { - throw new Error('No DB Connected'); + this.logger.error(`Database connection failed`); + throw new InternalServerException(); } const result = await connection.execute( `BEGIN - MANAGEFEE_SETUP_PKG.GETFEECOMM(:P_SPID,:P_ACTIVE_INACTIVE,:p_cursor); - END;`, + MANAGEFEE_SETUP_PKG.GETFEECOMM(:P_SPID,:P_ACTIVE_INACTIVE,:p_cursor); + END;`, { P_SPID: { val: body.P_SPID, @@ -458,23 +504,25 @@ export class ManageFeeService { await cursor.close(); } else { - throw new Error('No cursor returned from the stored procedure'); + throw new BadRequestException(); } return rows; - } catch (err) { - if (err instanceof Error) { - return { error: err.message }; - } else { - return { error: 'An unknown error occurred' }; + } catch (error) { + if (error instanceof BadRequestException) { + throw error; } - } - finally { + else if (error.message === "NJS-107: invalid cursor") { + throw new BadRequestException(); + } + this.logger.error('GETFEECOMM failed', error.stack || error); + throw new InternalServerException(); + } finally { if (connection) { try { await connection.close(); } catch (closeErr) { - console.error('Failed to close connection:', closeErr); + this.logger.error('Failed to close DB connection', closeErr); } } } @@ -487,20 +535,21 @@ export class ManageFeeService { try { connection = await this.oracleDBService.getConnection(); if (!connection) { - throw new Error('No DB Connected'); + this.logger.error(`Database connection failed`); + throw new InternalServerException(); } const result = await connection.execute( `BEGIN - MANAGEFEE_SETUP_PKG.CREATEBASICFEE( - :P_SPID, - :P_STARTCARNETVALUE, - :P_ENDCARNETVALUE, - :P_EFFDATE, - :P_FEES, - :P_USERID, - :P_CURSOR); - END;`, + MANAGEFEE_SETUP_PKG.CREATEBASICFEE( + :P_SPID, + :P_STARTCARNETVALUE, + :P_ENDCARNETVALUE, + :P_EFFDATE, + :P_FEES, + :P_USERID, + :P_CURSOR); + END;`, { P_SPID: { val: body.P_SPID, @@ -540,45 +589,54 @@ export class ManageFeeService { const fres = await result.outBinds.P_CURSOR.getRows(); - return fres; - } catch (err) { - if (err instanceof Error) { - return { error: err.message }; - } else { - return { error: 'An unknown error occurred' }; + if (fres[0].ERRORMESG) { + console.log(fres[0].ERRORMESG); + throw new BadRequestException(fres[0].ERRORMESG) } - } - finally { + + return fres; + } catch (error) { + if (error instanceof BadRequestException) { + throw error; + } + else if (error.message === "NJS-107: invalid cursor") { + throw new BadRequestException(); + } + this.logger.error('CREATEBASICFEE failed', error.stack || error); + throw new InternalServerException(); + } finally { if (connection) { try { await connection.close(); } catch (closeErr) { - console.error('Failed to close connection:', closeErr); + this.logger.error('Failed to close DB connection', closeErr); } } } } + async CREATEBONDRATE(body: CreateBondRateDTO) { let connection; try { connection = await this.oracleDBService.getConnection(); if (!connection) { - throw new Error('No DB Connected'); + this.logger.error(`Database connection failed`); + throw new InternalServerException(); } const result = await connection.execute( `BEGIN - MANAGEFEE_SETUP_PKG.CREATEBONDRATE( - :P_SPID, - :P_HOLDERTYPE, - :P_USCIBMEMBERFLAG, - :P_SPCLCOMMODITY, - :P_SPCLCOUNTRY, - :P_EFFDATE, - :P_RATE, - :P_USERID, - :P_CURSOR); - END;`, + MANAGEFEE_SETUP_PKG.CREATEBONDRATE( + :P_SPID, + :P_HOLDERTYPE, + :P_USCIBMEMBERFLAG, + :P_SPCLCOMMODITY, + :P_SPCLCOUNTRY, + :P_EFFDATE, + :P_RATE, + :P_USERID, + :P_CURSOR); + END;`, { P_SPID: { val: body.P_SPID, @@ -625,44 +683,53 @@ export class ManageFeeService { const fres = await result.outBinds.P_CURSOR.getRows(); - return fres; - } catch (err) { - if (err instanceof Error) { - return { error: err.message }; - } else { - return { error: 'An unknown error occurred' }; + if (fres[0].ERRORMESG) { + console.log(fres[0].ERRORMESG); + throw new BadRequestException(fres[0].ERRORMESG) } - } - finally { + + return fres; + } catch (error) { + if (error instanceof BadRequestException) { + throw error; + } + else if (error.message === "NJS-107: invalid cursor") { + throw new BadRequestException(); + } + this.logger.error('CREATEBONDRATE failed', error.stack || error); + throw new InternalServerException(); + } finally { if (connection) { try { await connection.close(); } catch (closeErr) { - console.error('Failed to close connection:', closeErr); + this.logger.error('Failed to close DB connection', closeErr); } } } } + async CREATECARGORATE(body: CreateCargoRateDTO) { let connection; try { connection = await this.oracleDBService.getConnection(); if (!connection) { - throw new Error('No DB Connected'); + this.logger.error(`Database connection failed`); + throw new InternalServerException(); } const result = await connection.execute( `BEGIN - MANAGEFEE_SETUP_PKG.CREATECARGORATE( - :P_SPID, - :P_CARNETTYPE, - :P_STARTSETS, - :P_ENDSETS, - :P_EFFDATE, - :P_RATE, - :P_USERID, - :P_CURSOR); - END;`, + MANAGEFEE_SETUP_PKG.CREATECARGORATE( + :P_SPID, + :P_CARNETTYPE, + :P_STARTSETS, + :P_ENDSETS, + :P_EFFDATE, + :P_RATE, + :P_USERID, + :P_CURSOR); + END;`, { P_SPID: { val: body.P_SPID, @@ -705,45 +772,54 @@ export class ManageFeeService { const fres = await result.outBinds.P_CURSOR.getRows(); - return fres; - } catch (err) { - if (err instanceof Error) { - return { error: err.message }; - } else { - return { error: 'An unknown error occurred' }; + if (fres[0].ERRORMESG) { + console.log(fres[0].ERRORMESG); + throw new BadRequestException(fres[0].ERRORMESG) } - } - finally { + + return fres; + } catch (error) { + if (error instanceof BadRequestException) { + throw error; + } + else if (error.message === "NJS-107: invalid cursor") { + throw new BadRequestException(); + } + this.logger.error('CREATECARGORATE failed', error.stack || error); + throw new InternalServerException(); + } finally { if (connection) { try { await connection.close(); } catch (closeErr) { - console.error('Failed to close connection:', closeErr); + this.logger.error('Failed to close DB connection', closeErr); } } } } + async CREATECFFEE(body: CreateCfFeeDTO) { let connection; try { connection = await this.oracleDBService.getConnection(); if (!connection) { - throw new Error('No DB Connected'); + this.logger.error(`Database connection failed`); + throw new InternalServerException(); } const result = await connection.execute( `BEGIN - MANAGEFEE_SETUP_PKG.CREATECFFEE( - :P_SPID, - :P_STARTSETS, - :P_ENDSETS, - :P_EFFDATE, - :P_CUSTOMERTYPE, - :P_CARNETTYPE, - :P_RATE, - :P_USERID, - :P_CURSOR); - END;`, + MANAGEFEE_SETUP_PKG.CREATECFFEE( + :P_SPID, + :P_STARTSETS, + :P_ENDSETS, + :P_EFFDATE, + :P_CUSTOMERTYPE, + :P_CARNETTYPE, + :P_RATE, + :P_USERID, + :P_CURSOR); + END;`, { P_SPID: { val: body.P_SPID, @@ -790,43 +866,52 @@ export class ManageFeeService { const fres = await result.outBinds.P_CURSOR.getRows(); - return fres; - } catch (err) { - if (err instanceof Error) { - return { error: err.message }; - } else { - return { error: 'An unknown error occurred' }; + if (fres[0].ERRORMESG) { + console.log(fres[0].ERRORMESG); + throw new BadRequestException(fres[0].ERRORMESG) } - } - finally { + + return fres; + } catch (error) { + if (error instanceof BadRequestException) { + throw error; + } + else if (error.message === "NJS-107: invalid cursor") { + throw new BadRequestException(); + } + this.logger.error('CREATECFFEE failed', error.stack || error); + throw new InternalServerException(); + } finally { if (connection) { try { await connection.close(); } catch (closeErr) { - console.error('Failed to close connection:', closeErr); + this.logger.error('Failed to close DB connection', closeErr); } } } } + async CREATECSFEE(body: CreateCsFeeDTO) { let connection; try { connection = await this.oracleDBService.getConnection(); if (!connection) { - throw new Error('No DB Connected'); + this.logger.error(`Database connection failed`); + throw new InternalServerException(); } const result = await connection.execute( `BEGIN - MANAGEFEE_SETUP_PKG.CREATECSFEE( - :P_SPID, - :P_CUSTOMERTYPE, - :P_CARNETTYPE, - :P_EFFDATE, - :P_RATE, - :P_USERID, - :P_CURSOR); - END;`, + MANAGEFEE_SETUP_PKG.CREATECSFEE( + :P_SPID, + :P_CUSTOMERTYPE, + :P_CARNETTYPE, + :P_EFFDATE, + :P_RATE, + :P_USERID, + :P_CURSOR); + END;`, { P_SPID: { val: body.P_SPID, @@ -865,47 +950,56 @@ export class ManageFeeService { const fres = await result.outBinds.P_CURSOR.getRows(); - return fres; - } catch (err) { - if (err instanceof Error) { - return { error: err.message }; - } else { - return { error: 'An unknown error occurred' }; + if (fres[0].ERRORMESG) { + console.log(fres[0].ERRORMESG); + throw new BadRequestException(fres[0].ERRORMESG) } - } - finally { + + return fres; + } catch (error) { + if (error instanceof BadRequestException) { + throw error; + } + else if (error.message === "NJS-107: invalid cursor") { + throw new BadRequestException(); + } + this.logger.error('CREATECSFEE failed', error.stack || error); + throw new InternalServerException(); + } finally { if (connection) { try { await connection.close(); } catch (closeErr) { - console.error('Failed to close connection:', closeErr); + this.logger.error('Failed to close DB connection', closeErr); } } } } + async CREATEEFFEE(body: CreateEfFeeDTO) { let connection; try { connection = await this.oracleDBService.getConnection(); if (!connection) { - throw new Error('No DB Connected'); + this.logger.error(`Database connection failed`); + throw new InternalServerException(); } const result = await connection.execute( `BEGIN - MANAGEFEE_SETUP_PKG.CREATEEFFEE( - :P_SPID, - :P_CUSTOMERTYPE, - :P_DELIVERYTYPE, - :P_STARTTIME, - :P_ENDTIME, - :P_TIMEZONE, - :P_EFFDATE, - :P_FEES, - :P_USERID, - :P_CURSOR - ); - END;`, + MANAGEFEE_SETUP_PKG.CREATEEFFEE( + :P_SPID, + :P_CUSTOMERTYPE, + :P_DELIVERYTYPE, + :P_STARTTIME, + :P_ENDTIME, + :P_TIMEZONE, + :P_EFFDATE, + :P_FEES, + :P_USERID, + :P_CURSOR + ); + END;`, { P_SPID: { val: body.P_SPID, @@ -956,42 +1050,51 @@ export class ManageFeeService { const fres = await result.outBinds.P_CURSOR.getRows(); - return fres; - } catch (err) { - if (err instanceof Error) { - return { error: err.message }; - } else { - return { error: 'An unknown error occurred' }; + if (fres[0].ERRORMESG) { + console.log(fres[0].ERRORMESG); + throw new BadRequestException(fres[0].ERRORMESG) } - } - finally { + + return fres; + } catch (error) { + if (error instanceof BadRequestException) { + throw error; + } + else if (error.message === "NJS-107: invalid cursor") { + throw new BadRequestException(); + } + this.logger.error('CREATEEFFEE failed', error.stack || error); + throw new InternalServerException(); + } finally { if (connection) { try { await connection.close(); } catch (closeErr) { - console.error('Failed to close connection:', closeErr); + this.logger.error('Failed to close DB connection', closeErr); } } } } + async CREATEFEECOMM(body: CreateFeeCommDTO) { let connection; try { connection = await this.oracleDBService.getConnection(); if (!connection) { - throw new Error('No DB Connected'); + this.logger.error(`Database connection failed`); + throw new InternalServerException(); } const result = await connection.execute( `BEGIN - MANAGEFEE_SETUP_PKG.CREATEFEECOMM( - :P_SPID, - :P_PARAMID, - :P_COMMRATE, - :P_EFFDATE, - :P_USERID, - :P_CURSOR); - END;`, + MANAGEFEE_SETUP_PKG.CREATEFEECOMM( + :P_SPID, + :P_PARAMID, + :P_COMMRATE, + :P_EFFDATE, + :P_USERID, + :P_CURSOR); + END;`, { P_SPID: { val: body.P_SPID, @@ -1026,20 +1129,27 @@ export class ManageFeeService { const fres = await result.outBinds.P_CURSOR.getRows(); - return fres; - } catch (err) { - if (err instanceof Error) { - return { error: err.message }; - } else { - return { error: 'An unknown error occurred' }; + if (fres[0].ERRORMESG) { + console.log(fres[0].ERRORMESG); + throw new BadRequestException(fres[0].ERRORMESG) } - } - finally { + + return fres; + } catch (error) { + if (error instanceof BadRequestException) { + throw error; + } + else if (error.message === "NJS-107: invalid cursor") { + throw new BadRequestException(); + } + this.logger.error('CREATEFEECOMM failed', error.stack || error); + throw new InternalServerException(); + } finally { if (connection) { try { await connection.close(); } catch (closeErr) { - console.error('Failed to close connection:', closeErr); + this.logger.error('Failed to close DB connection', closeErr); } } } @@ -1052,18 +1162,19 @@ export class ManageFeeService { try { connection = await this.oracleDBService.getConnection(); if (!connection) { - throw new Error('No DB Connected'); + this.logger.error(`Database connection failed`); + throw new InternalServerException(); } const result = await connection.execute( `BEGIN - MANAGEFEE_SETUP_PKG.UPDATEBASICFEE( - :P_BASICFEESETUPID, - :P_FEES, - :P_EFFDATE, - :P_USERID, - :P_CURSOR); - END;`, + MANAGEFEE_SETUP_PKG.UPDATEBASICFEE( + :P_BASICFEESETUPID, + :P_FEES, + :P_EFFDATE, + :P_USERID, + :P_CURSOR); + END;`, { P_BASICFEESETUPID: { val: body.P_BASICFEESETUPID, @@ -1094,41 +1205,50 @@ export class ManageFeeService { const fres = await result.outBinds.P_CURSOR.getRows(); - return fres; - } catch (err) { - if (err instanceof Error) { - return { error: err.message }; - } else { - return { error: 'An unknown error occurred' }; + if (fres[0].ERRORMESG) { + console.log(fres[0].ERRORMESG); + throw new BadRequestException(fres[0].ERRORMESG) } - } - finally { + + return fres; + } catch (error) { + if (error instanceof BadRequestException) { + throw error; + } + else if (error.message === "NJS-107: invalid cursor") { + throw new BadRequestException(); + } + this.logger.error('UPDATEBASICFEE failed', error.stack || error); + throw new InternalServerException(); + } finally { if (connection) { try { await connection.close(); } catch (closeErr) { - console.error('Failed to close connection:', closeErr); + this.logger.error('Failed to close DB connection', closeErr); } } } } + async UPDATEBONDRATE(body: UpdateBondRateDTO) { let connection; try { connection = await this.oracleDBService.getConnection(); if (!connection) { - throw new Error('No DB Connected'); + this.logger.error(`Database connection failed`); + throw new InternalServerException(); } const result = await connection.execute( `BEGIN - MANAGEFEE_SETUP_PKG.UPDATEBONDRATE( - :P_BONDRATESETUPID, - :P_RATE, - :P_EFFDATE, - :P_USERID, - :P_CURSOR); - END;`, + MANAGEFEE_SETUP_PKG.UPDATEBONDRATE( + :P_BONDRATESETUPID, + :P_RATE, + :P_EFFDATE, + :P_USERID, + :P_CURSOR); + END;`, { P_BONDRATESETUPID: { val: body.P_BONDRATESETUPID, @@ -1159,41 +1279,50 @@ export class ManageFeeService { const fres = await result.outBinds.P_CURSOR.getRows(); - return fres; - } catch (err) { - if (err instanceof Error) { - return { error: err.message }; - } else { - return { error: 'An unknown error occurred' }; + if (fres[0].ERRORMESG) { + console.log(fres[0].ERRORMESG); + throw new BadRequestException(fres[0].ERRORMESG) } - } - finally { + + return fres; + } catch (error) { + if (error instanceof BadRequestException) { + throw error; + } + else if (error.message === "NJS-107: invalid cursor") { + throw new BadRequestException(); + } + this.logger.error('UPDATEBONDRATE failed', error.stack || error); + throw new InternalServerException(); + } finally { if (connection) { try { await connection.close(); } catch (closeErr) { - console.error('Failed to close connection:', closeErr); + this.logger.error('Failed to close DB connection', closeErr); } } } } + async UPDATECARGORATE(body: UpdateCargoRateDTO) { let connection; try { connection = await this.oracleDBService.getConnection(); if (!connection) { - throw new Error('No DB Connected'); + this.logger.error(`Database connection failed`); + throw new InternalServerException(); } const result = await connection.execute( `BEGIN - MANAGEFEE_SETUP_PKG.UPDATECARGORATE( - :P_CARGORATESETUPID, - :P_RATE, - :P_EFFDATE, - :P_USERID, - :P_CURSOR); - END;`, + MANAGEFEE_SETUP_PKG.UPDATECARGORATE( + :P_CARGORATESETUPID, + :P_RATE, + :P_EFFDATE, + :P_USERID, + :P_CURSOR); + END;`, { P_CARGORATESETUPID: { val: body.P_CARGORATESETUPID, @@ -1224,41 +1353,50 @@ export class ManageFeeService { const fres = await result.outBinds.P_CURSOR.getRows(); - return fres; - } catch (err) { - if (err instanceof Error) { - return { error: err.message }; - } else { - return { error: 'An unknown error occurred' }; + if (fres[0].ERRORMESG) { + console.log(fres[0].ERRORMESG); + throw new BadRequestException(fres[0].ERRORMESG) } - } - finally { + + return fres; + } catch (error) { + if (error instanceof BadRequestException) { + throw error; + } + else if (error.message === "NJS-107: invalid cursor") { + throw new BadRequestException(); + } + this.logger.error('UPDATECARGORATE failed', error.stack || error); + throw new InternalServerException(); + } finally { if (connection) { try { await connection.close(); } catch (closeErr) { - console.error('Failed to close connection:', closeErr); + this.logger.error('Failed to close DB connection', closeErr); } } } } + async UPDATECFFEE(body: UpdateCfFeeDTO) { let connection; try { connection = await this.oracleDBService.getConnection(); if (!connection) { - throw new Error('No DB Connected'); + this.logger.error(`Database connection failed`); + throw new InternalServerException(); } const result = await connection.execute( `BEGIN - MANAGEFEE_SETUP_PKG.UPDATECFFEE( - :P_CFFEESETUPID, - :P_RATE, - :P_EFFDATE, - :P_USERID, - :P_CURSOR); - END;`, + MANAGEFEE_SETUP_PKG.UPDATECFFEE( + :P_CFFEESETUPID, + :P_RATE, + :P_EFFDATE, + :P_USERID, + :P_CURSOR); + END;`, { P_CFFEESETUPID: { val: body.P_CFFEESETUPID, @@ -1289,41 +1427,50 @@ export class ManageFeeService { const fres = await result.outBinds.P_CURSOR.getRows(); - return fres; - } catch (err) { - if (err instanceof Error) { - return { error: err.message }; - } else { - return { error: 'An unknown error occurred' }; + if (fres[0].ERRORMESG) { + console.log(fres[0].ERRORMESG); + throw new BadRequestException(fres[0].ERRORMESG) } - } - finally { + + return fres; + } catch (error) { + if (error instanceof BadRequestException) { + throw error; + } + else if (error.message === "NJS-107: invalid cursor") { + throw new BadRequestException(); + } + this.logger.error('UPDATECFFEE failed', error.stack || error); + throw new InternalServerException(); + } finally { if (connection) { try { await connection.close(); } catch (closeErr) { - console.error('Failed to close connection:', closeErr); + this.logger.error('Failed to close DB connection', closeErr); } } } } + async UPDATECSFEE(body: UpdateCsFeeDTO) { let connection; try { connection = await this.oracleDBService.getConnection(); if (!connection) { - throw new Error('No DB Connected'); + this.logger.error(`Database connection failed`); + throw new InternalServerException(); } const result = await connection.execute( `BEGIN - MANAGEFEE_SETUP_PKG.UPDATECSFEE( - :P_CSFEESETUPID, - :P_RATE, - :P_EFFDATE, - :P_USERID, - :P_CURSOR); - END;`, + MANAGEFEE_SETUP_PKG.UPDATECSFEE( + :P_CSFEESETUPID, + :P_RATE, + :P_EFFDATE, + :P_USERID, + :P_CURSOR); + END;`, { P_CSFEESETUPID: { val: body.P_CSFEESETUPID, @@ -1354,41 +1501,50 @@ export class ManageFeeService { const fres = await result.outBinds.P_CURSOR.getRows(); - return fres; - } catch (err) { - if (err instanceof Error) { - return { error: err.message }; - } else { - return { error: 'An unknown error occurred' }; + if (fres[0].ERRORMESG) { + console.log(fres[0].ERRORMESG); + throw new BadRequestException(fres[0].ERRORMESG) } - } - finally { + + return fres; + } catch (error) { + if (error instanceof BadRequestException) { + throw error; + } + else if (error.message === "NJS-107: invalid cursor") { + throw new BadRequestException(); + } + this.logger.error('UPDATECSFEE failed', error.stack || error); + throw new InternalServerException(); + } finally { if (connection) { try { await connection.close(); } catch (closeErr) { - console.error('Failed to close connection:', closeErr); + this.logger.error('Failed to close DB connection', closeErr); } } } } + async UPDATEEFFEE(body: UpdateEfFeeDTO) { let connection; try { connection = await this.oracleDBService.getConnection(); if (!connection) { - throw new Error('No DB Connected'); + this.logger.error(`Database connection failed`); + throw new InternalServerException(); } const result = await connection.execute( `BEGIN - MANAGEFEE_SETUP_PKG.UPDATEEFFEE( - :P_EFFEESETUPID, - :P_FEES, - :P_EFFDATE, - :P_USERID, - :P_CURSOR); - END;`, + MANAGEFEE_SETUP_PKG.UPDATEEFFEE( + :P_EFFEESETUPID, + :P_FEES, + :P_EFFDATE, + :P_USERID, + :P_CURSOR); + END;`, { P_EFFEESETUPID: { val: body.P_EFFEESETUPID, @@ -1419,56 +1575,65 @@ export class ManageFeeService { const fres = await result.outBinds.P_CURSOR.getRows(); - return fres; - } catch (err) { - if (err instanceof Error) { - return { error: err.message }; - } else { - return { error: 'An unknown error occurred' }; + if (fres[0].ERRORMESG) { + console.log(fres[0].ERRORMESG); + throw new BadRequestException(fres[0].ERRORMESG) } - } - finally { + + return fres; + } catch (error) { + if (error instanceof BadRequestException) { + throw error; + } + else if (error.message === "NJS-107: invalid cursor") { + throw new BadRequestException(); + } + this.logger.error('UPDATEEFFEE failed', error.stack || error); + throw new InternalServerException(); + } finally { if (connection) { try { await connection.close(); } catch (closeErr) { - console.error('Failed to close connection:', closeErr); + this.logger.error('Failed to close DB connection', closeErr); } } } } - async UPDATEFEECOMM(body: UpdateFeeCommBodyDTO) { + + async UPDATEFEECOMM(body: UpdateFeeCommDTO) { let connection; try { connection = await this.oracleDBService.getConnection(); if (!connection) { - throw new Error('No DB Connected'); + this.logger.error(`Database connection failed`); + throw new InternalServerException(); } const result = await connection.execute( `BEGIN - MANAGEFEE_SETUP_PKG.UPDATEFEECOMM( - :P_FEECOMMID, - :P_RATE, - :P_EFFDATE, - :P_USERID, - :P_CURSOR); - END;`, + MANAGEFEE_SETUP_PKG.UPDATEFEECOMM( + :P_FEECOMMID, + :P_RATE, + :P_EFFDATE, + :P_USERID, + :P_CURSOR); + END;`, { P_FEECOMMID: { - val: body.p_fees_comm.P_FEECOMMID, + val: body.P_FEECOMMID, type: oracledb.DB_TYPE_NUMBER, }, P_RATE: { - val: Number(body.p_fees_comm.P_RATE) | 0, + val: Number(body.P_RATE) | 0, type: oracledb.DB_TYPE_NUMBER, }, P_EFFDATE: { - val: body.p_fees_comm.P_EFFDATE, + val: body.P_EFFDATE, type: oracledb.DB_TYPE_NVARCHAR, }, P_USERID: { - val: body.p_fees_comm.P_USERID, + val: body.P_USERID, type: oracledb.DB_TYPE_NVARCHAR, }, P_CURSOR: { @@ -1487,21 +1652,30 @@ export class ManageFeeService { console.log('pcursor: ', fres); + if (fres[0].ERRORMESG) { + console.log(fres[0].ERRORMESG); + throw new BadRequestException(fres[0].ERRORMESG) + } + return fres; } else { - console.log('No cursor returned....'); - return { error: 'No cursor found' }; + throw new BadRequestException(); } - } catch (err) { - return { error: err.message }; - // return {error: err.message} - } - finally { + } catch (error) { + if (error instanceof BadRequestException) { + throw error; + } + else if (error.message === "NJS-107: invalid cursor") { + throw new BadRequestException(); + } + this.logger.error('UPDATEFEECOMM failed', error.stack || error); + throw new InternalServerException(); + } finally { if (connection) { try { await connection.close(); } catch (closeErr) { - console.error('Failed to close connection:', closeErr); + this.logger.error('Failed to close DB connection', closeErr); } } } diff --git a/src/oracle/uscib-managed-sp/sp/sp.dto.ts b/src/oracle/uscib-managed-sp/sp/sp.dto.ts index 5aacd3d..c11bf81 100644 --- a/src/oracle/uscib-managed-sp/sp/sp.dto.ts +++ b/src/oracle/uscib-managed-sp/sp/sp.dto.ts @@ -40,16 +40,16 @@ export class InsertNewServiceProviderDTO { @IsDefined({ message: 'Property p_state is required' }) p_state: string; - @ApiProperty({ required: true }) - @IsString({ message: 'Property p_zip must be a string' }) - @IsDefined({ message: 'Property p_zip is required' }) - p_zip: string; - @ApiProperty({ required: true }) @IsString({ message: 'Property p_country must be a string' }) @IsDefined({ message: 'Property p_country is required' }) p_country: string; + @ApiProperty({ required: true }) + @IsString({ message: 'Property p_zip must be a string' }) + @IsDefined({ message: 'Property p_zip is required' }) + p_zip: string; + @ApiProperty({ required: true }) @IsString({ message: 'Property p_issuingregion must be a string' }) @IsDefined({ message: 'Property p_issuingregion is required' }) @@ -127,11 +127,6 @@ export class UpdateServiceProviderDTO { @IsDefined({ message: 'Property p_state is required' }) p_state: string; - @ApiProperty({ required: true }) - @IsString({ message: 'Property p_zip must be a string' }) - @IsDefined({ message: 'Property p_zip is required' }) - p_zip: string; - @ApiProperty({ required: true }) @IsString({ message: 'Property p_country must be a string' }) @IsDefined({ message: 'Property p_country is required' }) @@ -162,6 +157,11 @@ export class UpdateServiceProviderDTO { @IsDefined({ message: 'Property p_cargosurety is required' }) p_cargosurety: string; + @ApiProperty({ required: true }) + @IsString({ message: 'Property p_zip must be a string' }) + @IsDefined({ message: 'Property p_zip is required' }) + p_zip: string; + @ApiProperty({ required: true }) @IsString({ message: 'Property p_user_id must be a string' }) @IsDefined({ message: 'Property p_user_id is required' }) @@ -176,6 +176,7 @@ export class UpdateServiceProviderDTO { @IsString({ message: 'Property P_FILEIDS must be a string' }) @IsOptional() P_FILEIDS?: string; + } export class getSelectedServiceproviderDTO { diff --git a/src/oracle/uscib-managed-sp/sp/sp.service.ts b/src/oracle/uscib-managed-sp/sp/sp.service.ts index ebf06dc..4aa4db2 100644 --- a/src/oracle/uscib-managed-sp/sp/sp.service.ts +++ b/src/oracle/uscib-managed-sp/sp/sp.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@nestjs/common'; +import { Injectable, Logger } from '@nestjs/common'; import { OracleDBService } from 'src/db/db.service'; import * as oracledb from 'oracledb'; import { @@ -6,40 +6,45 @@ import { InsertNewServiceProviderDTO, UpdateServiceProviderDTO, } from './sp.dto'; +import { BadRequestException } from 'src/exceptions/badRequest.exception'; +import { InternalServerException } from 'src/exceptions/internalServerError.exception'; @Injectable() export class SpService { - constructor(private readonly oracleDBService: OracleDBService) {} + 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 Error('No DB Connected'); + this.logger.error(`Database connection failed`); + 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;`, + 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, @@ -119,122 +124,165 @@ export class SpService { const fres = await result.outBinds.p_cursor.getRows(); - return fres; - } catch (err) { - if (err instanceof Error) { - return { error: err.message }; - } else { - return { error: 'An unknown error occurred' }; + if (fres[0].ERRORMESG) { + console.log(fres[0].ERRORMESG); + throw new BadRequestException(fres[0].ERRORMESG) } - } - finally { + + return fres; + } catch (error) { + if (error instanceof BadRequestException) { + throw error; + } + else if (error.message === "NJS-107: invalid cursor") { + throw new BadRequestException(); + } + this.logger.error('insertNewServiceProvider failed', error.stack || error); + throw new InternalServerException(); + } finally { if (connection) { try { await connection.close(); } catch (closeErr) { - console.error('Failed to close connection:', 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 Error('No DB Connected'); + this.logger.error(`Database connection failed`); + 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_issuingregion, - :p_replacementregion, - :p_bondsurety, - :p_cargopolicyno, - :p_cargosurety, - :p_user_id, - :P_NOTES, - :P_FILEIDS, - :p_cursor); - END;`, + USCIB_Managed_Pkg.UpdateSP( + :p_spid, + :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_spid: { - val: body.p_spid, + val: finalBody.p_spid, type: oracledb.DB_TYPE_NUMBER, }, p_name: { - val: body.p_name, + val: finalBody.p_name, type: oracledb.DB_TYPE_VARCHAR, }, p_lookupcode: { - val: body.p_lookupcode, + val: finalBody.p_lookupcode, type: oracledb.DB_TYPE_VARCHAR, }, p_address1: { - val: body.p_address1, + val: finalBody.p_address1, type: oracledb.DB_TYPE_VARCHAR, }, p_address2: { - val: body.p_address2, + val: finalBody.p_address2, type: oracledb.DB_TYPE_VARCHAR, }, p_city: { - val: body.p_city, + val: finalBody.p_city, type: oracledb.DB_TYPE_VARCHAR, }, p_state: { - val: body.p_state, + val: finalBody.p_state, type: oracledb.DB_TYPE_VARCHAR, }, p_zip: { - val: body.p_zip, + val: finalBody.p_zip, type: oracledb.DB_TYPE_VARCHAR, }, p_country: { - val: body.p_country, + val: finalBody.p_country, type: oracledb.DB_TYPE_VARCHAR, }, p_issuingregion: { - val: body.p_issuingregion, + val: finalBody.p_issuingregion, type: oracledb.DB_TYPE_VARCHAR, }, p_replacementregion: { - val: body.p_replacementregion, + val: finalBody.p_replacementregion, type: oracledb.DB_TYPE_VARCHAR, }, p_bondsurety: { - val: body.p_bondsurety, + val: finalBody.p_bondsurety, type: oracledb.DB_TYPE_VARCHAR, }, p_cargopolicyno: { - val: body.p_cargopolicyno, + val: finalBody.p_cargopolicyno, type: oracledb.DB_TYPE_VARCHAR, }, p_cargosurety: { - val: body.p_cargosurety, + val: finalBody.p_cargosurety, type: oracledb.DB_TYPE_VARCHAR, }, p_user_id: { - val: body.p_user_id, + val: finalBody.p_user_id, type: oracledb.DB_TYPE_VARCHAR, }, P_NOTES: { - val: body.P_NOTES, + val: finalBody.P_NOTES, type: oracledb.DB_TYPE_VARCHAR, }, P_FILEIDS: { - val: body.P_FILEIDS, + val: finalBody.P_FILEIDS, type: oracledb.DB_TYPE_VARCHAR, }, p_cursor: { @@ -251,20 +299,27 @@ export class SpService { const fres = await result.outBinds.p_cursor.getRows(); - return fres; - } catch (err) { - if (err instanceof Error) { - return { error: err.message }; - } else { - return { error: 'An unknown error occurred' }; + if (fres[0].ERRORMESG) { + console.log(fres[0].ERRORMESG); + throw new BadRequestException(fres[0].ERRORMESG) } - } - finally { + + return fres; + } catch (error) { + if (error instanceof BadRequestException) { + throw error; + } + else if (error.message === "NJS-107: invalid cursor") { + throw new BadRequestException(); + } + this.logger.error('updateServiceProvider failed', error.stack || error); + throw new InternalServerException(); + } finally { if (connection) { try { await connection.close(); } catch (closeErr) { - console.error('Failed to close connection:', closeErr); + this.logger.error('Failed to close DB connection', closeErr); } } } @@ -272,17 +327,18 @@ export class SpService { async getAllServiceproviders() { let connection; - let rows = []; + let rows: any = []; try { connection = await this.oracleDBService.getConnection(); if (!connection) { - throw new Error('No DB Connected'); + this.logger.error(`Database connection failed`); + throw new InternalServerException(); } const result = await connection.execute( `BEGIN - USCIB_Managed_Pkg.GetAllSPs(:p_cursor); - END;`, + USCIB_Managed_Pkg.GetAllSPs(:p_cursor); + END;`, { p_cursor: { type: oracledb.CURSOR, @@ -305,23 +361,30 @@ export class SpService { await cursor.close(); + if (rows.length > 0 && rows[0].ERRORMESG) { + this.logger.warn(`error from DB: ${rows[0].ERRORMESG}`); + throw new BadRequestException(rows[0].ERRORMESG); + } + return rows; } else { - throw new Error('No cursor returned from the stored procedure'); + throw new BadRequestException(); } - } catch (err) { - if (err instanceof Error) { - return { error: err.message }; - } else { - return { error: 'An unknown error occurred' }; + } catch (error) { + if (error instanceof BadRequestException) { + throw error; } - } - finally { + else if (error.message === "NJS-107: invalid cursor") { + throw new BadRequestException(); + } + this.logger.error('getAllServiceproviders failed', error.stack || error); + throw new InternalServerException(); + } finally { if (connection) { try { await connection.close(); } catch (closeErr) { - console.error('Failed to close connection:', closeErr); + this.logger.error('Failed to close DB connection', closeErr); } } } @@ -329,17 +392,18 @@ export class SpService { async getServiceproviderByID(body: getSelectedServiceproviderDTO) { let connection; - let rows = []; + let rows: any = []; try { connection = await this.oracleDBService.getConnection(); if (!connection) { - throw new Error('No DB Connected'); + this.logger.error(`Database connection failed`); + throw new InternalServerException(); } const result = await connection.execute( `BEGIN - USCIB_Managed_Pkg.GetSPbySPID(:p_spid,:p_cursor); - END;`, + USCIB_Managed_Pkg.GetSPbySPID(:p_spid,:p_cursor); + END;`, { p_spid: { val: body.p_spid, @@ -366,23 +430,30 @@ export class SpService { await cursor.close(); } else { - throw new Error('No cursor returned from the stored procedure'); + throw new BadRequestException(); + } + + if (rows.length > 0 && rows[0].ERRORMESG) { + this.logger.warn(`error from DB: ${rows[0].ERRORMESG}`); + throw new BadRequestException(rows[0].ERRORMESG); } return rows; - } catch (err) { - if (err instanceof Error) { - return { error: err.message }; - } else { - return { error: 'An unknown error occurred' }; + } catch (error) { + if (error instanceof BadRequestException) { + throw error; } - } - finally { + else if (error.message === "NJS-107: invalid cursor") { + throw new BadRequestException(); + } + this.logger.error('getServiceproviderByID failed', error.stack || error); + throw new InternalServerException(); + } finally { if (connection) { try { await connection.close(); } catch (closeErr) { - console.error('Failed to close connection:', closeErr); + this.logger.error('Failed to close DB connection', closeErr); } } }