BE/src/oracle/manage-holders/manage-holders.service.ts
2025-04-21 11:00:34 +05:30

952 lines
27 KiB
TypeScript

import { Injectable } from '@nestjs/common';
import { OracleDBService } from 'src/db/db.service';
import {
CreateHoldersDTO,
GetHolderContactActivateOrInactivateDTO,
GetHolderOrContactDTO,
p_contactstableDTO,
UpdateHolderContactDTO,
UpdateHolderDTO,
} from './manage-holders.dto';
import * as oracledb from 'oracledb';
import { Connection, Result } from 'oracledb';
@Injectable()
export class ManageHoldersService {
constructor(private readonly oracleDBService: OracleDBService) {}
CreateHolders = async (body: CreateHoldersDTO) => {
const 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,
};
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: CreateHoldersDTO = { ...newBody, ...reqBody };
let connection: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 };
// const CONTACTSARRAY = await connection.getDbObjectClass('CARNETSYS.CONTACTSARRAY');
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');
}
async function CREATECONTACTSTABLE_INSTANCE(
connection,
FirstName,
LastName,
MiddleInitial,
Title,
EmailAddress,
PhoneNo,
MobileNo,
FaxNo,
) {
const result:Result<any> = 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,
);
}),
)
: [];
// Create an instance of GLTABLE
const CONTACTSTABLE_INSTANCE = new CONTACTSTABLE(CONTACTSARRAY);
const result:Result<any> = 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,
},
);
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) {
if (err instanceof Error) {
return { error: err.message };
} else {
return { error: 'An unknown error occurred' };
}
}
};
UpdateHolder = async (body: UpdateHolderDTO) => {
const 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,
};
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 = { ...newBody, ...reqBody };
let connection:Connection;
let P_cursor_rows = [];
try {
connection = await this.oracleDBService.getConnection();
if (!connection) {
throw new Error('No DB Connected');
}
const result:Result<any> = 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) {
if (err instanceof Error) {
return { error: err.message };
} else {
return { error: 'An unknown error occurred' };
}
}
};
GetHolderRecord = async (body: GetHolderOrContactDTO) => {
let connection:Connection;
let p_cursor_rows = [];
try {
connection = await this.oracleDBService.getConnection();
if (!connection) {
throw new Error('No DB Connected');
}
const result:Result<any> = 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) {
if (err instanceof Error) {
return { error: err.message };
} else {
return { error: 'An unknown error occurred' };
}
}
};
UpdateHolderContact = async (body: UpdateHolderContactDTO) => {
const 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,
};
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: UpdateHolderContactDTO = { ...newBody, ...reqBody };
let connection:Connection;
let P_cursor_rows = [];
try {
connection = await this.oracleDBService.getConnection();
if (!connection) {
throw new Error('No DB Connected');
}
const result:Result<any> = 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) {
if (err instanceof Error) {
return { error: err.message };
} else {
return { error: 'An unknown error occurred' };
}
}
};
GetHolderContacts = async (body: GetHolderOrContactDTO) => {
let connection:Connection;
let p_cursor_rows = [];
try {
connection = await this.oracleDBService.getConnection();
if (!connection) {
throw new Error('No DB Connected');
}
const result:Result<any> = 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) {
if (err instanceof Error) {
return { error: err.message };
} else {
return { error: 'An unknown error occurred' };
}
}
};
InactivateHolder = async (body: GetHolderOrContactDTO) => {
let connection:Connection;
let p_cursor_rows = [];
try {
connection = await this.oracleDBService.getConnection();
if (!connection) {
throw new Error('No DB Connected');
}
const result:Result<any> = 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) {
if (err instanceof Error) {
return { error: err.message };
} else {
return { error: 'An unknown error occurred' };
}
}
};
ReactivateHolder = async (body: GetHolderOrContactDTO) => {
let connection:Connection;
let p_cursor_rows = [];
try {
connection = await this.oracleDBService.getConnection();
if (!connection) {
throw new Error('No DB Connected');
}
const result:Result<any> = 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) {
if (err instanceof Error) {
return { error: err.message };
} else {
return { error: 'An unknown error occurred' };
}
}
};
InactivateHolderContact = async (body: GetHolderContactActivateOrInactivateDTO) => {
let connection:Connection;
let p_cursor_rows = [];
try {
connection = await this.oracleDBService.getConnection();
if (!connection) {
throw new Error('No DB Connected');
}
const result:Result<any> = 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) {
if (err instanceof Error) {
return { error: err.message };
} else {
return { error: 'An unknown error occurred' };
}
}
};
ReactivateHolderContact = async (body: GetHolderContactActivateOrInactivateDTO) => {
let connection:Connection;
let p_cursor_rows = [];
try {
connection = await this.oracleDBService.getConnection();
if (!connection) {
throw new Error('No DB Connected');
}
const result:Result<any> = 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) {
if (err instanceof Error) {
return { error: err.message };
} else {
return { error: 'An unknown error occurred' };
}
}
};
}