BE/src/oracle/uscib-managed-sp/sp/sp.service.ts
2025-06-04 17:20:45 +05:30

676 lines
22 KiB
TypeScript

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 {
SPID_DTO, InsertNewServiceProviderDTO,
UpdateServiceProviderDTO,
} from 'src/dto/property.dto';
@Injectable()
export class SpService {
private readonly logger = new Logger(SpService.name);
constructor(private readonly oracleDBService: OracleDBService) { }
// async insertNewServiceProviderX(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 insertNewServiceProvider(body: InsertNewServiceProviderDTO) {
const newBody = {
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: InsertNewServiceProviderDTO = { ...newBody, ...reqBody };
let connection;
try {
connection = await this.oracleDBService.getConnection();
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: 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_ISSUINGREGION: { val: finalBody.P_ISSUINGREGION, type: oracledb.DB_TYPE_VARCHAR },
P_REPLACEMENTREGION: { val: finalBody.P_REPLACEMENTREGION, 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_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 }
);
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.");
}
let fres: any = await fetchCursor(outBinds.P_CURSOR, SpService.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, SpService.name)
} finally {
await closeOracleDbConnection(connection, SpService.name)
}
}
// async updateServiceProviderX(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 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();
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;`,
{
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_ISSUINGREGION: { val: finalBody.P_ISSUINGREGION, type: oracledb.DB_TYPE_VARCHAR },
P_REPLACEMENTREGION: { val: finalBody.P_REPLACEMENTREGION, 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_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 }
);
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.");
}
let fres: any = await fetchCursor(outBinds.P_CURSOR, SpService.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, SpService.name)
} finally {
await closeOracleDbConnection(connection, SpService.name)
}
}
// async getAllServiceprovidersX() {
// 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 getAllServiceproviders() {
let connection;
try {
connection = await this.oracleDBService.getConnection();
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 }
);
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, SpService.name);
} catch (error) {
handleError(error, SpService.name)
} finally {
await closeOracleDbConnection(connection, SpService.name)
}
}
// async getServiceproviderByIDX(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);
// }
// }
// }
// }
async getServiceproviderByID(body: SPID_DTO) {
let connection;
try {
connection = await this.oracleDBService.getConnection();
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 }
);
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, SpService.name);
} catch (error) {
handleError(error, SpService.name)
} finally {
await closeOracleDbConnection(connection, SpService.name)
}
}
}