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

468 lines
17 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';
import { CreateHoldersDTO, GetHolderRecordDTO, p_contactstableDTO, UpdateHolderDTO } from './manage-holders.dto';
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 IN ('GLARRAY', 'GLTABLE')`)
// let res = await connection.execute(`SELECT owner, type_name FROM all_types WHERE type_name LIKE '%CONTACT%'`);
// return { res:res.rows };
// contactstable
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');
}
const CONTACTSTABLE_ARRAY = finalBody.p_contactstable ? finalBody.p_contactstable.map((x: p_contactstableDTO) => {
return new CONTACTSARRAY(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(CONTACTSTABLE_ARRAY);
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
}
);
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 { }
}
GetHolderRecord = async (body: GetHolderRecordDTO) => {
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 { }
}
}