BE/src/oracle/uscib-managed-sp/sp/sp.service.ts
2025-04-02 13:29:06 +05:30

355 lines
9.5 KiB
TypeScript

import { Injectable } from '@nestjs/common';
import { OracleDBService } from 'src/db/db.service';
import * as oracledb from 'oracledb';
import {
getSelectedServiceproviderDTO,
InsertNewServiceProviderDTO,
UpdateServiceProviderDTO,
} from './sp.dto';
@Injectable()
export class SpService {
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');
}
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();
return fres;
} catch (err) {
if (err instanceof Error) {
return { error: err.message };
} else {
return { error: 'An unknown error occurred' };
}
}
}
async updateServiceProvider(body: UpdateServiceProviderDTO) {
let connection;
try {
connection = await this.oracleDBService.getConnection();
if (!connection) {
throw new Error('No DB Connected');
}
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: body.p_spid,
type: oracledb.DB_TYPE_NUMBER,
},
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();
return fres;
} catch (err) {
if (err instanceof Error) {
return { error: err.message };
} else {
return { error: 'An unknown error occurred' };
}
}
}
async getAllServiceproviders() {
let connection;
let rows = [];
try {
connection = await this.oracleDBService.getConnection();
if (!connection) {
throw new Error('No DB Connected');
}
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();
return rows;
} else {
throw new Error('No cursor returned from the stored procedure');
}
} catch (err) {
if (err instanceof Error) {
return { error: err.message };
} else {
return { error: 'An unknown error occurred' };
}
}
}
async getServiceproviderByID(body: getSelectedServiceproviderDTO) {
let connection;
let rows = [];
try {
connection = await this.oracleDBService.getConnection();
if (!connection) {
throw new Error('No DB Connected');
}
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 Error('No cursor returned from the stored procedure');
}
return rows;
} catch (err) {
if (err instanceof Error) {
return { error: err.message };
} else {
return { error: 'An unknown error occurred' };
}
}
}
}