import { Injectable } from "@nestjs/common"; import { OracleDBService } from "src/db/db.service"; import * as oracledb from 'oracledb' @Injectable() export class HomePageDataService { constructor(private readonly oracleDBService: OracleDBService) { } async GetHomePageData(P_spid: number) { let connection; let p_basic_details = []; let p_contacts = [] let p_sequence = []; let p_fees_comm = []; let p_bf_fee = []; let p_cf_Fee = []; let p_cont_sheet_fee = []; let p_ef_fee = []; let p_security_deposit = []; let p_param = []; let p_region = []; try { connection = await this.oracleDBService.getConnection() if (!connection) { throw new Error('No DB Connected') } const result = await connection.execute( `BEGIN USERLOGIN_PKG.GetHomePageData( :P_spid, :p_basic_details_cur, :p_contacts_cur, :p_sequence_cur, :p_fees_comm_cur, :p_bf_fee_cur, :p_cf_Fee_cur, :p_cont_sheet_fee_cur, :p_ef_fee_cur, :p_security_deposit_cur, :p_param_cur, :p_region_cur ); END;`, { P_spid: { val: P_spid, type: oracledb.DB_TYPE_NUMBER, }, p_basic_details_cur: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT }, p_contacts_cur: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT }, p_sequence_cur: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT }, p_fees_comm_cur: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT }, p_bf_fee_cur: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT }, p_cf_Fee_cur: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT }, p_cont_sheet_fee_cur: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT }, p_ef_fee_cur: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT }, p_security_deposit_cur: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT }, p_param_cur: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT }, p_region_cur: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT } }, { outFormat: oracledb.OUT_FORMAT_OBJECT } ); if (result.outBinds && result.outBinds.p_basic_details_cur) { const cursor = result.outBinds.p_basic_details_cur; let rowsBatch; do { rowsBatch = await cursor.getRows(100); p_basic_details = p_basic_details.concat(rowsBatch); } while (rowsBatch.length > 0); await cursor.close(); } if (result.outBinds && result.outBinds.p_contacts_cur) { const cursor = result.outBinds.p_contacts_cur; let rowsBatch; do { rowsBatch = await cursor.getRows(100); p_contacts = p_contacts.concat(rowsBatch); } while (rowsBatch.length > 0); await cursor.close(); } if (result.outBinds && result.outBinds.p_sequence_cur) { const cursor = result.outBinds.p_sequence_cur; let rowsBatch; do { rowsBatch = await cursor.getRows(100); p_sequence = p_sequence.concat(rowsBatch); } while (rowsBatch.length > 0); await cursor.close(); } if (result.outBinds && result.outBinds.p_fees_comm_cur) { const cursor = result.outBinds.p_fees_comm_cur; let rowsBatch; do { rowsBatch = await cursor.getRows(100); p_fees_comm = p_fees_comm.concat(rowsBatch); } while (rowsBatch.length > 0); await cursor.close(); } if (result.outBinds && result.outBinds.p_bf_fee_cur) { const cursor = result.outBinds.p_bf_fee_cur; let rowsBatch; do { rowsBatch = await cursor.getRows(100); p_bf_fee = p_bf_fee.concat(rowsBatch); } while (rowsBatch.length > 0); await cursor.close(); } if (result.outBinds && result.outBinds.p_cf_Fee_cur) { const cursor = result.outBinds.p_cf_Fee_cur; let rowsBatch; do { rowsBatch = await cursor.getRows(100); p_cf_Fee = p_cf_Fee.concat(rowsBatch); } while (rowsBatch.length > 0); await cursor.close(); } if (result.outBinds && result.outBinds.p_cont_sheet_fee_cur) { const cursor = result.outBinds.p_cont_sheet_fee_cur; let rowsBatch; do { rowsBatch = await cursor.getRows(100); p_cont_sheet_fee = p_cont_sheet_fee.concat(rowsBatch); } while (rowsBatch.length > 0); await cursor.close(); } if (result.outBinds && result.outBinds.p_ef_fee_cur) { const cursor = result.outBinds.p_ef_fee_cur; let rowsBatch; do { rowsBatch = await cursor.getRows(100); p_ef_fee = p_ef_fee.concat(rowsBatch); } while (rowsBatch.length > 0); await cursor.close(); } if (result.outBinds && result.outBinds.p_security_deposit_cur) { const cursor = result.outBinds.p_security_deposit_cur; let rowsBatch; do { rowsBatch = await cursor.getRows(100); p_security_deposit = p_security_deposit.concat(rowsBatch); } while (rowsBatch.length > 0); await cursor.close(); } if (result.outBinds && result.outBinds.p_param_cur) { const cursor = result.outBinds.p_param_cur; let rowsBatch; do { rowsBatch = await cursor.getRows(100); p_param = p_param.concat(rowsBatch); } while (rowsBatch.length > 0); await cursor.close(); } if (result.outBinds && result.outBinds.p_region_cur) { const cursor = result.outBinds.p_region_cur; let rowsBatch; do { rowsBatch = await cursor.getRows(100); p_region = p_region.concat(rowsBatch); } while (rowsBatch.length > 0); await cursor.close(); } else { throw new Error('No cursor returned from the stored procedure'); } let tableData = { p_basic_details, p_contacts, p_sequence, p_fees_comm, p_bf_fee, p_cf_Fee, p_cont_sheet_fee, p_ef_fee, p_security_deposit, p_param, p_region, }; let output = {} for (const key in tableData) { // console.log(key); output[key] = tableData[key].map(obj => { const newObj = { ...obj }; for (const innerKey in obj) { if (key === 'p_fees_comm') { if (innerKey.includes(' ')) { // Check if the key has a space const newKey = innerKey.replace(/ /g, '_').toUpperCase(); // Replace spaces and convert to uppercase newObj[newKey] = obj[innerKey]; // Add the new key delete newObj[innerKey]; // Remove the old key } } } return newObj; }); } return output; } catch (err) { console.error('Error fetching users: ', err); throw new Error('Error fetching users'); } finally { } } async GetCarnetSummaryData(p_emailaddr: string) { let connection; let rows:any = []; try { connection = await this.oracleDBService.getConnection() if (!connection) { throw new Error('No DB Connected') } const result = await connection.execute( `BEGIN USERLOGIN_PKG.GetCarnetSummaryData(:p_emailaddr,:p_cursor); END;`, { p_emailaddr: { val: p_emailaddr, type: oracledb.DB_TYPE_NVARCHAR, }, 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'); } rows = rows.map(obj => { // Create a new object to hold the modified keys let newObj = {}; // Iterate over the keys of the current object for (let key in obj) { // Replace spaces with underscores in the key let newKey = key.replace(/ /g, "_"); newObj[newKey] = obj[key]; } return newObj; }); rows = rows.reduce((acc, curr) => { // Find if the current item already exists in the accumulator let existing = acc.find(item => item["Service_Provider_Name"] === curr["Service_Provider_Name"] && item.SPID === curr.SPID); if (existing) { // If it exists, push the current "cs" and "c c" values into the arrays existing.CARNETSTATUS.push(curr.CARNETSTATUS); existing["Carnet_Count"].push(curr["Carnet_Count"]); } else { // If it doesn't exist, create a new entry acc.push({ "Service_Provider_Name": curr["Service_Provider_Name"], SPID: curr.SPID, CARNETSTATUS: [curr.CARNETSTATUS], "Carnet_Count": [curr["Carnet_Count"]] }); } return acc; }, []); return rows; } catch (err) { console.error('Error fetching users: ', err); throw new Error('Error fetching users'); } finally { } } }