BE/src/oracle/manage-holders/manage-holders.service.ts

904 lines
32 KiB
TypeScript
Raw Normal View History

2025-03-25 17:10:33 +05:30
import { Injectable } from '@nestjs/common';
2025-03-26 16:24:06 +05:30
import { OracleDBService } from 'src/db/db.service';
2025-03-27 11:21:57 +05:30
import { CreateHoldersDTO, GetHolderContactActivateOrInactivateDTO, GetHolderOrContactDTO, p_contactstableDTO, UpdateHolderContactDTO, UpdateHolderDTO } from './manage-holders.dto';
2025-03-26 16:24:06 +05:30
import * as oracledb from 'oracledb'
2025-03-25 17:10:33 +05:30
@Injectable()
2025-03-26 16:24:06 +05:30
export class ManageHoldersService {
constructor(private readonly oracleDBService: OracleDBService) { }
CreateHolders = async (body: CreateHoldersDTO) => {
let newBody = {
"p_spid": null,
"p_clientlocationid": null,
"p_holderno": null,
"p_holdertype": null,
"p_uscibmemberflag": null,
"p_govagencyflag": null,
"p_holdername": null,
"p_namequalifier": null,
"p_addlname": null,
"p_address1": null,
"p_address2": null,
"p_city": null,
"p_state": null,
"p_zip": null,
"p_country": null,
"p_userid": null,
"p_contactstable": null
}
let 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: CreateHoldersDTO = { ...newBody, ...reqBody };
let connection;
let p_holdercursor_rows = [];
let p_holdercontactcursor_rows = [];
try {
connection = await this.oracleDBService.getConnection()
if (!connection) {
throw new Error('No DB Connected')
}
// let res = await connection.execute(`SELECT owner, type_name FROM all_types WHERE type_name LIKE '%CONTACT%'`);
// return { res:res.rows };
2025-03-29 10:28:51 +05:30
// const CONTACTSARRAY = await connection.getDbObjectClass('CARNETSYS.CONTACTSARRAY');
2025-03-26 16:24:06 +05:30
const CONTACTSTABLE = await connection.getDbObjectClass('CARNETSYS.CONTACTSTABLE');
// Check if GLTABLE is a constructor
if (typeof CONTACTSTABLE !== 'function') {
throw new Error('CONTACTSTABLE is not a constructor');
}
2025-03-29 10:28:51 +05:30
async function CREATECONTACTSTABLE_INSTANCE(connection, FirstName, LastName, MiddleInitial, Title, EmailAddress, PhoneNo, MobileNo, FaxNo) {
const result = await connection.execute(
`SELECT CARNETSYS.CONTACTSARRAY(:FirstName, :LastName, :MiddleInitial, :Title, :EmailAddress, :PhoneNo, :MobileNo, :FaxNo) FROM dual`,
{
FirstName,
LastName,
MiddleInitial,
Title,
EmailAddress,
PhoneNo,
MobileNo,
FaxNo
}
);
return result.rows[0][0];
}
const CONTACTSARRAY = finalBody.p_contactstable ? await Promise.all(finalBody.p_contactstable.map(async (x: p_contactstableDTO) => {
return await CREATECONTACTSTABLE_INSTANCE(connection, x.FirstName, x.LastName, x.MiddleInitial, x.Title, x.EmailAddress, x.PhoneNo, x.MobileNo, x.FaxNo);
})) : [];
2025-03-26 16:24:06 +05:30
// Create an instance of GLTABLE
2025-03-29 10:28:51 +05:30
const CONTACTSTABLE_INSTANCE = new CONTACTSTABLE(CONTACTSARRAY);
2025-03-26 16:24:06 +05:30
const result = await connection.execute(
`BEGIN
MANAGEHOLDER_PKG.CreateHolderData(
:p_spid,
:p_clientlocationid,
:p_holderno,
:p_holdertype,
:p_uscibmemberflag,
:p_govagencyflag,
:p_holdername,
:p_namequalifier,
:p_addlname,
:p_address1,
:p_address2,
:p_city,
:p_state,
:p_zip,
:p_country,
:p_userid,
:p_contactstable,
:p_holdercursor,
:p_holdercontactcursor
);
END;`,
{
p_spid: {
val: finalBody.p_spid ? finalBody.p_spid : null,
type: oracledb.DB_TYPE_NUMBER
},
p_clientlocationid: {
val: finalBody.p_clientlocationid ? finalBody.p_clientlocationid : null,
type: oracledb.DB_TYPE_NUMBER
},
p_holderno: {
val: finalBody.p_holderno ? finalBody.p_holderno : null,
type: oracledb.DB_TYPE_NVARCHAR
},
p_holdertype: {
val: finalBody.p_holdertype ? finalBody.p_holdertype : null,
type: oracledb.DB_TYPE_NVARCHAR
},
p_uscibmemberflag: {
val: finalBody.p_uscibmemberflag ? finalBody.p_uscibmemberflag : null,
type: oracledb.DB_TYPE_NVARCHAR
},
p_govagencyflag: {
val: finalBody.p_govagencyflag ? finalBody.p_govagencyflag : null,
type: oracledb.DB_TYPE_NVARCHAR
},
p_holdername: {
val: finalBody.p_holdername ? finalBody.p_holdername : null,
type: oracledb.DB_TYPE_NVARCHAR
},
p_namequalifier: {
val: finalBody.p_namequalifier ? finalBody.p_namequalifier : null,
type: oracledb.DB_TYPE_NVARCHAR
},
p_addlname: {
val: finalBody.p_addlname ? finalBody.p_addlname : null,
type: oracledb.DB_TYPE_NVARCHAR
},
p_address1: {
val: finalBody.p_address1 ? finalBody.p_address1 : null,
type: oracledb.DB_TYPE_NVARCHAR
},
p_address2: {
val: finalBody.p_address2 ? finalBody.p_address2 : null,
type: oracledb.DB_TYPE_NVARCHAR
},
p_city: {
val: finalBody.p_city ? finalBody.p_city : null,
type: oracledb.DB_TYPE_NVARCHAR
},
p_state: {
val: finalBody.p_state ? finalBody.p_state : null,
type: oracledb.DB_TYPE_NVARCHAR
},
p_zip: {
val: finalBody.p_zip ? finalBody.p_zip : null,
type: oracledb.DB_TYPE_NVARCHAR
},
p_country: {
val: finalBody.p_country ? finalBody.p_country : null,
type: oracledb.DB_TYPE_NVARCHAR
},
p_userid: {
val: finalBody.p_userid ? finalBody.p_userid : null,
type: oracledb.DB_TYPE_NVARCHAR
},
p_contactstable: {
val: CONTACTSTABLE_INSTANCE,
type: oracledb.DB_TYPE_NVARCHAR
},
p_holdercursor: {
type: oracledb.CURSOR,
dir: oracledb.BIND_OUT
},
p_holdercontactcursor: {
type: oracledb.CURSOR,
dir: oracledb.BIND_OUT
}
}, {
outFormat: oracledb.OUT_FORMAT_OBJECT
}
);
2025-03-29 10:28:51 +05:30
2025-03-26 16:24:06 +05:30
await connection.commit();
// let fres = await result.outBinds.P_cursor.getRows();
if (result.outBinds && result.outBinds.p_holdercursor) {
const cursor = result.outBinds.p_holdercursor;
let rowsBatch;
do {
rowsBatch = await cursor.getRows(100);
p_holdercursor_rows = p_holdercursor_rows.concat(rowsBatch);
} while (rowsBatch.length > 0);
await cursor.close();
} else {
throw new Error('No cursor returned from the stored procedure');
}
if (result.outBinds && result.outBinds.p_holdercontactcursor) {
const cursor = result.outBinds.p_holdercontactcursor;
let rowsBatch;
do {
rowsBatch = await cursor.getRows(100);
p_holdercontactcursor_rows = p_holdercontactcursor_rows.concat(rowsBatch);
} while (rowsBatch.length > 0);
await cursor.close();
} else {
throw new Error('No cursor returned from the stored procedure');
}
return { p_holdercursor: p_holdercursor_rows, p_holdercontactcursor: p_holdercontactcursor_rows };
// return fres
} catch (err) {
console.error('Error fetching users: ', err);
return { error: err.message }
} finally { }
}
UpdateHolder = async (body: UpdateHolderDTO) => {
let newBody = {
"p_holderid": null,
"p_spid": null,
"p_locationid": null,
"p_holderno": null,
"p_holdertype": null,
"p_uscibmemberflag": null,
"p_govagencyflag": null,
"p_holdername": null,
"p_namequalifier": null,
"p_addlname": null,
"p_address1": null,
"p_address2": null,
"p_city": null,
"p_state": null,
"p_zip": null,
"p_country": null,
"p_userid": null
}
let 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 = { ...newBody, ...reqBody };
let connection;
let P_cursor_rows = [];
try {
connection = await this.oracleDBService.getConnection()
if (!connection) {
throw new Error('No DB Connected')
}
const result = await connection.execute(
`BEGIN
MANAGEHOLDER_PKG.UpdateHolders(
:p_holderid,
:p_spid,
:p_locationid,
:p_holderno,
:p_holdertype,
:p_uscibmemberflag,
:p_govagencyflag,
:p_holdername,
:p_namequalifier,
:p_addlname,
:p_address1,
:p_address2,
:p_city,
:p_state,
:p_zip,
:p_country,
:p_userid,
:P_cursor
);
END;`,
{
p_holderid: {
val: finalBody.p_holderid ? finalBody.p_holderid : null,
type: oracledb.DB_TYPE_NUMBER
},
p_spid: {
val: finalBody.p_spid ? finalBody.p_spid : null,
type: oracledb.DB_TYPE_NUMBER
},
p_locationid: {
val: finalBody.p_locationid ? finalBody.p_locationid : null,
type: oracledb.DB_TYPE_NUMBER
},
p_holderno: {
val: finalBody.p_holderno ? finalBody.p_holderno : null,
type: oracledb.DB_TYPE_NVARCHAR
},
p_holdertype: {
val: finalBody.p_holdertype ? finalBody.p_holdertype : null,
type: oracledb.DB_TYPE_NVARCHAR
},
p_uscibmemberflag: {
val: finalBody.p_uscibmemberflag ? finalBody.p_uscibmemberflag : null,
type: oracledb.DB_TYPE_NVARCHAR
},
p_govagencyflag: {
val: finalBody.p_govagencyflag ? finalBody.p_govagencyflag : null,
type: oracledb.DB_TYPE_NVARCHAR
},
p_holdername: {
val: finalBody.p_holdername ? finalBody.p_holdername : null,
type: oracledb.DB_TYPE_NVARCHAR
},
p_namequalifier: {
val: finalBody.p_namequalifier ? finalBody.p_namequalifier : null,
type: oracledb.DB_TYPE_NVARCHAR
},
p_addlname: {
val: finalBody.p_addlname ? finalBody.p_addlname : null,
type: oracledb.DB_TYPE_NVARCHAR
},
p_address1: {
val: finalBody.p_address1 ? finalBody.p_address1 : null,
type: oracledb.DB_TYPE_NVARCHAR
},
p_address2: {
val: finalBody.p_address2 ? finalBody.p_address2 : null,
type: oracledb.DB_TYPE_NVARCHAR
},
p_city: {
val: finalBody.p_city ? finalBody.p_city : null,
type: oracledb.DB_TYPE_NVARCHAR
},
p_state: {
val: finalBody.p_state ? finalBody.p_state : null,
type: oracledb.DB_TYPE_NVARCHAR
},
p_zip: {
val: finalBody.p_zip ? finalBody.p_zip : null,
type: oracledb.DB_TYPE_NVARCHAR
},
p_country: {
val: finalBody.p_country ? finalBody.p_country : null,
type: oracledb.DB_TYPE_NVARCHAR
},
p_userid: {
val: finalBody.p_userid ? finalBody.p_userid : null,
type: oracledb.DB_TYPE_NVARCHAR
},
P_cursor: {
type: oracledb.CURSOR,
dir: oracledb.BIND_OUT
}
}, {
outFormat: oracledb.OUT_FORMAT_OBJECT
}
);
await connection.commit();
// let fres = await result.outBinds.P_cursor.getRows();
if (result.outBinds && result.outBinds.P_cursor) {
const cursor = result.outBinds.P_cursor;
let rowsBatch;
do {
rowsBatch = await cursor.getRows(100);
P_cursor_rows = P_cursor_rows.concat(rowsBatch);
} while (rowsBatch.length > 0);
await cursor.close();
} else {
throw new Error('No cursor returned from the stored procedure');
}
return { P_cursor: P_cursor_rows };
// return fres
} catch (err) {
console.error('Error fetching users: ', err);
return { error: err.message }
} finally { }
}
2025-03-27 11:21:57 +05:30
GetHolderRecord = async (body: GetHolderOrContactDTO) => {
2025-03-26 16:24:06 +05:30
let connection;
let p_cursor_rows = [];
try {
connection = await this.oracleDBService.getConnection()
if (!connection) {
throw new Error('No DB Connected')
}
const result = await connection.execute(
`BEGIN
MANAGEHOLDER_PKG.GetHolderMaster(
:p_spid,
:p_holderid,
:p_cursor
);
END;`,
{
P_spid: {
val: body.p_spid,
type: oracledb.DB_TYPE_NUMBER,
},
p_holderid: {
val: body.p_holderid,
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);
p_cursor_rows = p_cursor_rows.concat(rowsBatch);
} while (rowsBatch.length > 0);
await cursor.close();
}
return { p_cursor: p_cursor_rows };
} catch (err) {
console.error('Error fetching users: ', err);
return { error: err.message }
} finally { }
}
2025-03-27 11:21:57 +05:30
UpdateHolderContact = async (body: UpdateHolderContactDTO) => {
let newBody = {
"p_holdercontactid": null,
"p_spid": null,
"p_firstname": null,
"p_lastname": null,
"p_middleinitial": null,
"p_title": null,
"p_phone": null,
"p_mobile": null,
"p_fax": null,
"p_emailaddress": null,
"p_userid": null
}
let 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: UpdateHolderContactDTO = { ...newBody, ...reqBody };
let connection;
let P_cursor_rows = [];
try {
connection = await this.oracleDBService.getConnection()
if (!connection) {
throw new Error('No DB Connected')
}
const result = await connection.execute(
`BEGIN
MANAGEHOLDER_PKG.UpdateHolders(
:p_holdercontactid,
:p_spid,
:p_firstname,
:p_lastname,
:p_middleinitial,
:p_title,
:p_phone,
:p_mobile,
:p_fax,
:p_emailaddress,
:p_userid,
:P_cursor
);
END;`,
{
p_holdercontactid: {
val: finalBody.p_holdercontactid ? finalBody.p_holdercontactid : null,
type: oracledb.DB_TYPE_NUMBER
},
p_spid: {
val: finalBody.p_spid ? finalBody.p_spid : null,
type: oracledb.DB_TYPE_NUMBER
},
p_firstname: {
val: finalBody.p_firstname ? finalBody.p_firstname : null,
type: oracledb.DB_TYPE_NVARCHAR
},
p_lastname: {
val: finalBody.p_lastname ? finalBody.p_lastname : null,
type: oracledb.DB_TYPE_NVARCHAR
},
p_middleinitial: {
val: finalBody.p_middleinitial ? finalBody.p_middleinitial : null,
type: oracledb.DB_TYPE_NVARCHAR
},
p_title: {
val: finalBody.p_title ? finalBody.p_title : null,
type: oracledb.DB_TYPE_NVARCHAR
},
p_phone: {
val: finalBody.p_phone ? finalBody.p_phone : null,
type: oracledb.DB_TYPE_NVARCHAR
},
p_mobile: {
val: finalBody.p_mobile ? finalBody.p_mobile : null,
type: oracledb.DB_TYPE_NVARCHAR
},
p_fax: {
val: finalBody.p_fax ? finalBody.p_fax : null,
type: oracledb.DB_TYPE_NVARCHAR
},
p_emailaddress: {
val: finalBody.p_emailaddress ? finalBody.p_emailaddress : null,
type: oracledb.DB_TYPE_NVARCHAR
},
p_userid: {
val: finalBody.p_userid ? finalBody.p_userid : null,
type: oracledb.DB_TYPE_NVARCHAR
},
P_cursor: {
type: oracledb.CURSOR,
dir: oracledb.BIND_OUT
}
}, {
outFormat: oracledb.OUT_FORMAT_OBJECT
}
);
await connection.commit();
// let fres = await result.outBinds.P_cursor.getRows();
if (result.outBinds && result.outBinds.P_cursor) {
const cursor = result.outBinds.P_cursor;
let rowsBatch;
do {
rowsBatch = await cursor.getRows(100);
P_cursor_rows = P_cursor_rows.concat(rowsBatch);
} while (rowsBatch.length > 0);
await cursor.close();
} else {
throw new Error('No cursor returned from the stored procedure');
}
return { P_cursor: P_cursor_rows };
// return fres
} catch (err) {
console.error('Error fetching users: ', err);
return { error: err.message }
} finally { }
}
GetHolderContacts = async (body: GetHolderOrContactDTO) => {
let connection;
let p_cursor_rows = [];
try {
connection = await this.oracleDBService.getConnection()
if (!connection) {
throw new Error('No DB Connected')
}
const result = await connection.execute(
`BEGIN
MANAGEHOLDER_PKG.GetHolderContacts(
:p_spid,
:p_holderid,
:p_cursor
);
END;`,
{
P_spid: {
val: body.p_spid,
type: oracledb.DB_TYPE_NUMBER,
},
p_holderid: {
val: body.p_holderid,
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);
p_cursor_rows = p_cursor_rows.concat(rowsBatch);
} while (rowsBatch.length > 0);
await cursor.close();
}
return { p_cursor: p_cursor_rows };
} catch (err) {
console.error('Error fetching users: ', err);
return { error: err.message }
} finally { }
}
InactivateHolder = async (body: GetHolderOrContactDTO) => {
let connection;
let p_cursor_rows = [];
2025-03-26 16:24:06 +05:30
2025-03-27 11:21:57 +05:30
try {
connection = await this.oracleDBService.getConnection()
if (!connection) {
throw new Error('No DB Connected')
}
const result = await connection.execute(
`BEGIN
ManageHolder_Pkg.InactivateHolder(
:p_spid,
:p_holderid,
:p_cursor
);
END;`,
{
P_spid: {
val: body.p_spid,
type: oracledb.DB_TYPE_NUMBER,
},
p_holderid: {
val: body.p_holderid,
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);
p_cursor_rows = p_cursor_rows.concat(rowsBatch);
} while (rowsBatch.length > 0);
await cursor.close();
}
return { p_cursor: p_cursor_rows };
} catch (err) {
console.error('Error fetching users: ', err);
return { error: err.message }
} finally { }
}
ReactivateHolder = async (body: GetHolderOrContactDTO) => {
let connection;
let p_cursor_rows = [];
try {
connection = await this.oracleDBService.getConnection()
if (!connection) {
throw new Error('No DB Connected')
}
const result = await connection.execute(
`BEGIN
ManageHolder_Pkg.ReactivateHolder(
:p_spid,
:p_holderid,
:p_cursor
);
END;`,
{
P_spid: {
val: body.p_spid,
type: oracledb.DB_TYPE_NUMBER,
},
p_holderid: {
val: body.p_holderid,
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);
p_cursor_rows = p_cursor_rows.concat(rowsBatch);
} while (rowsBatch.length > 0);
await cursor.close();
}
return { p_cursor: p_cursor_rows };
} catch (err) {
console.error('Error fetching users: ', err);
return { error: err.message }
} finally { }
}
InactivateHolderContact = async (body: GetHolderContactActivateOrInactivateDTO) => {
let connection;
let p_cursor_rows = [];
try {
connection = await this.oracleDBService.getConnection()
if (!connection) {
throw new Error('No DB Connected')
}
const result = await connection.execute(
`BEGIN
ManageHolder_Pkg.InactivateHolderContact(
:p_spid,
:p_holderid,
:p_cursor
);
END;`,
{
P_spid: {
val: body.p_spid,
type: oracledb.DB_TYPE_NUMBER,
},
p_holderid: {
val: body.p_holderContactid,
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);
p_cursor_rows = p_cursor_rows.concat(rowsBatch);
} while (rowsBatch.length > 0);
await cursor.close();
}
return { p_cursor: p_cursor_rows };
} catch (err) {
console.error('Error fetching users: ', err);
return { error: err.message }
} finally { }
}
ReactivateHolderContact = async (body: GetHolderContactActivateOrInactivateDTO) => {
let connection;
let p_cursor_rows = [];
try {
connection = await this.oracleDBService.getConnection()
if (!connection) {
throw new Error('No DB Connected')
}
const result = await connection.execute(
`BEGIN
ManageHolder_Pkg.ReactivateHolderContact(
:p_spid,
:p_holderid,
:p_cursor
);
END;`,
{
P_spid: {
val: body.p_spid,
type: oracledb.DB_TYPE_NUMBER,
},
p_holderid: {
val: body.p_holderContactid,
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);
p_cursor_rows = p_cursor_rows.concat(rowsBatch);
} while (rowsBatch.length > 0);
await cursor.close();
}
return { p_cursor: p_cursor_rows };
} catch (err) {
console.error('Error fetching users: ', err);
return { error: err.message }
} finally { }
}
2025-03-26 16:24:06 +05:30
}