BE/src/oracle/uscib-managed-sp/sp/sp.service.ts

452 lines
12 KiB
TypeScript
Raw Normal View History

2025-05-06 13:04:00 +05:30
import { Injectable, Logger } from '@nestjs/common';
2025-03-25 17:10:33 +05:30
import { OracleDBService } from 'src/db/db.service';
2025-04-02 13:29:06 +05:30
import * as oracledb from 'oracledb';
import {
getSelectedServiceproviderDTO,
InsertNewServiceProviderDTO,
UpdateServiceProviderDTO,
} from './sp.dto';
2025-05-06 13:04:00 +05:30
import { BadRequestException } from 'src/exceptions/badRequest.exception';
import { InternalServerException } from 'src/exceptions/internalServerError.exception';
2025-03-25 17:10:33 +05:30
@Injectable()
export class SpService {
2025-05-06 13:04:00 +05:30
private readonly logger = new Logger(SpService.name);
constructor(private readonly oracleDBService: OracleDBService) { }
2025-04-02 13:29:06 +05:30
async insertNewServiceProvider(body: InsertNewServiceProviderDTO) {
2025-05-09 10:38:16 +05:30
2025-04-02 13:29:06 +05:30
let connection;
try {
connection = await this.oracleDBService.getConnection();
if (!connection) {
2025-05-06 13:04:00 +05:30
throw new InternalServerException();
2025-04-02 13:29:06 +05:30
}
const result = await connection.execute(
`BEGIN
2025-05-06 13:04:00 +05:30
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;`,
2025-04-02 13:29:06 +05:30
{
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();
2025-05-06 13:04:00 +05:30
if (fres[0].ERRORMESG) {
throw new BadRequestException(fres[0].ERRORMESG)
}
2025-05-09 16:47:29 +05:30
// return fres[0];
return { statusCode: 201, message: "Createdted Successfully", ...fres[0] };
2025-05-06 13:04:00 +05:30
} catch (error) {
if (error instanceof BadRequestException) {
this.logger.warn(error.message);
2025-05-06 13:04:00 +05:30
throw error;
2025-04-02 13:29:06 +05:30
}
2025-05-06 13:04:00 +05:30
this.logger.error('insertNewServiceProvider failed', error.stack || error);
throw new InternalServerException();
} finally {
2025-04-30 17:28:09 +05:30
if (connection) {
try {
await connection.close();
} catch (closeErr) {
2025-05-06 13:04:00 +05:30
this.logger.error('Failed to close DB connection', closeErr);
2025-04-30 17:28:09 +05:30
}
}
}
2025-04-02 13:29:06 +05:30
}
async updateServiceProvider(body: UpdateServiceProviderDTO) {
2025-05-06 13:04:00 +05:30
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 };
2025-04-02 13:29:06 +05:30
let connection;
try {
connection = await this.oracleDBService.getConnection();
if (!connection) {
2025-05-06 13:04:00 +05:30
throw new InternalServerException();
2025-04-02 13:29:06 +05:30
}
const result = await connection.execute(
`BEGIN
2025-05-06 13:04:00 +05:30
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,
2025-05-09 16:47:29 +05:30
:p_replacementregion,
:p_issuingregion,
2025-05-06 13:04:00 +05:30
:p_user_id,
:P_NOTES,
:P_FILEIDS,
:p_cursor);
END;`,
2025-04-02 13:29:06 +05:30
{
p_spid: {
2025-05-06 13:04:00 +05:30
val: finalBody.p_spid,
2025-04-02 13:29:06 +05:30
type: oracledb.DB_TYPE_NUMBER,
},
p_name: {
2025-05-06 13:04:00 +05:30
val: finalBody.p_name,
2025-04-02 13:29:06 +05:30
type: oracledb.DB_TYPE_VARCHAR,
},
p_lookupcode: {
2025-05-06 13:04:00 +05:30
val: finalBody.p_lookupcode,
2025-04-02 13:29:06 +05:30
type: oracledb.DB_TYPE_VARCHAR,
},
p_address1: {
2025-05-06 13:04:00 +05:30
val: finalBody.p_address1,
2025-04-02 13:29:06 +05:30
type: oracledb.DB_TYPE_VARCHAR,
},
p_address2: {
2025-05-06 13:04:00 +05:30
val: finalBody.p_address2,
2025-04-02 13:29:06 +05:30
type: oracledb.DB_TYPE_VARCHAR,
},
p_city: {
2025-05-06 13:04:00 +05:30
val: finalBody.p_city,
2025-04-02 13:29:06 +05:30
type: oracledb.DB_TYPE_VARCHAR,
},
p_state: {
2025-05-06 13:04:00 +05:30
val: finalBody.p_state,
2025-04-02 13:29:06 +05:30
type: oracledb.DB_TYPE_VARCHAR,
},
p_zip: {
2025-05-06 13:04:00 +05:30
val: finalBody.p_zip,
2025-04-02 13:29:06 +05:30
type: oracledb.DB_TYPE_VARCHAR,
},
p_country: {
2025-05-06 13:04:00 +05:30
val: finalBody.p_country,
2025-04-02 13:29:06 +05:30
type: oracledb.DB_TYPE_VARCHAR,
},
p_bondsurety: {
2025-05-06 13:04:00 +05:30
val: finalBody.p_bondsurety,
2025-04-02 13:29:06 +05:30
type: oracledb.DB_TYPE_VARCHAR,
},
p_cargopolicyno: {
2025-05-06 13:04:00 +05:30
val: finalBody.p_cargopolicyno,
2025-04-02 13:29:06 +05:30
type: oracledb.DB_TYPE_VARCHAR,
},
p_cargosurety: {
2025-05-06 13:04:00 +05:30
val: finalBody.p_cargosurety,
2025-04-02 13:29:06 +05:30
type: oracledb.DB_TYPE_VARCHAR,
},
2025-05-09 16:47:29 +05:30
p_replacementregion: {
val: finalBody.p_replacementregion,
type: oracledb.DB_TYPE_VARCHAR,
},
p_issuingregion: {
val: finalBody.p_issuingregion,
type: oracledb.DB_TYPE_VARCHAR,
},
2025-04-02 13:29:06 +05:30
p_user_id: {
2025-05-06 13:04:00 +05:30
val: finalBody.p_user_id,
2025-04-02 13:29:06 +05:30
type: oracledb.DB_TYPE_VARCHAR,
},
P_NOTES: {
2025-05-06 13:04:00 +05:30
val: finalBody.P_NOTES,
2025-04-02 13:29:06 +05:30
type: oracledb.DB_TYPE_VARCHAR,
},
P_FILEIDS: {
2025-05-06 13:04:00 +05:30
val: finalBody.P_FILEIDS,
2025-04-02 13:29:06 +05:30
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();
2025-05-06 13:04:00 +05:30
if (fres[0].ERRORMESG) {
throw new BadRequestException(fres[0].ERRORMESG)
}
2025-05-09 16:47:29 +05:30
return { statusCode: 200, message: "Updated Successfully" };
2025-05-06 13:04:00 +05:30
} catch (error) {
if (error instanceof BadRequestException) {
this.logger.warn(error.message);
2025-05-06 13:04:00 +05:30
throw error;
2025-04-02 13:29:06 +05:30
}
2025-05-06 13:04:00 +05:30
this.logger.error('updateServiceProvider failed', error.stack || error);
throw new InternalServerException();
} finally {
2025-04-30 17:28:09 +05:30
if (connection) {
try {
await connection.close();
} catch (closeErr) {
2025-05-06 13:04:00 +05:30
this.logger.error('Failed to close DB connection', closeErr);
2025-04-30 17:28:09 +05:30
}
}
}
2025-04-02 13:29:06 +05:30
}
async getAllServiceproviders() {
let connection;
2025-05-06 13:04:00 +05:30
let rows: any = [];
2025-04-02 13:29:06 +05:30
try {
connection = await this.oracleDBService.getConnection();
if (!connection) {
2025-05-06 13:04:00 +05:30
throw new InternalServerException();
2025-04-02 13:29:06 +05:30
}
const result = await connection.execute(
`BEGIN
2025-05-06 13:04:00 +05:30
USCIB_Managed_Pkg.GetAllSPs(:p_cursor);
END;`,
2025-04-02 13:29:06 +05:30
{
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();
2025-05-06 13:04:00 +05:30
if (rows.length > 0 && rows[0].ERRORMESG) {
throw new BadRequestException(rows[0].ERRORMESG);
}
2025-04-02 13:29:06 +05:30
return rows;
} else {
2025-05-06 13:04:00 +05:30
throw new BadRequestException();
2025-04-02 13:29:06 +05:30
}
2025-05-06 13:04:00 +05:30
} catch (error) {
if (error instanceof BadRequestException) {
this.logger.warn(error.message);
2025-05-06 13:04:00 +05:30
throw error;
2025-04-02 13:29:06 +05:30
}
2025-05-06 13:04:00 +05:30
this.logger.error('getAllServiceproviders failed', error.stack || error);
throw new InternalServerException();
} finally {
2025-04-30 17:28:09 +05:30
if (connection) {
try {
await connection.close();
} catch (closeErr) {
2025-05-06 13:04:00 +05:30
this.logger.error('Failed to close DB connection', closeErr);
2025-04-30 17:28:09 +05:30
}
}
}
2025-04-02 13:29:06 +05:30
}
async getServiceproviderByID(body: getSelectedServiceproviderDTO) {
let connection;
2025-05-06 13:04:00 +05:30
let rows: any = [];
2025-04-02 13:29:06 +05:30
try {
connection = await this.oracleDBService.getConnection();
if (!connection) {
2025-05-06 13:04:00 +05:30
throw new InternalServerException();
2025-04-02 13:29:06 +05:30
}
const result = await connection.execute(
`BEGIN
2025-05-06 13:04:00 +05:30
USCIB_Managed_Pkg.GetSPbySPID(:p_spid,:p_cursor);
END;`,
2025-04-02 13:29:06 +05:30
{
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 {
2025-05-06 13:04:00 +05:30
throw new BadRequestException();
}
if (rows.length > 0 && rows[0].ERRORMESG) {
throw new BadRequestException(rows[0].ERRORMESG);
2025-04-02 13:29:06 +05:30
}
return rows;
2025-05-06 13:04:00 +05:30
} catch (error) {
if (error instanceof BadRequestException) {
this.logger.warn(error.message);
2025-05-06 13:04:00 +05:30
throw error;
2025-04-02 13:29:06 +05:30
}
2025-05-06 13:04:00 +05:30
else if (error.message === "NJS-107: invalid cursor") {
this.logger.warn(error.message);
2025-05-06 13:04:00 +05:30
throw new BadRequestException();
}
this.logger.error('getServiceproviderByID failed', error.stack || error);
throw new InternalServerException();
} finally {
2025-04-30 17:28:09 +05:30
if (connection) {
try {
await connection.close();
} catch (closeErr) {
2025-05-06 13:04:00 +05:30
this.logger.error('Failed to close DB connection', closeErr);
2025-04-30 17:28:09 +05:30
}
}
}
2025-04-02 13:29:06 +05:30
}
2025-03-25 17:10:33 +05:30
}