import { Injectable } from '@nestjs/common'; import { OracleDBService } from 'src/db/db.service'; import { AuthLoginDTO } from './auth.dto'; import * as oracledb from 'oracledb' @Injectable() export class AuthService { constructor(private readonly oracleDBService: OracleDBService) { } async login(body:AuthLoginDTO) { let connection; let rows = []; let crows:any = []; try { connection = await this.oracleDBService.getConnection() if (!connection) { throw new Error('No DB Connected') } const result = await connection.execute( `BEGIN USERLOGIN_PKG.ValidateUser(:p_emailaddr,:p_password,:p_login_cursor,:p_carnet_count_cur); END;`, { p_emailaddr: { val: body.p_emailaddr, type: oracledb.DB_TYPE_NVARCHAR }, p_password: { val: body.p_password, type: oracledb.DB_TYPE_NVARCHAR }, p_login_cursor: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT }, p_carnet_count_cur: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT } }, { outFormat: oracledb.OUT_FORMAT_OBJECT } ); if (result.outBinds && result.outBinds.p_login_cursor) { const cursor = result.outBinds.p_login_cursor; // The OUT cursor let rowsBatch; do { rowsBatch = await cursor.getRows(100); // Fetch 100 rows at a time rows = rows.concat(rowsBatch); // Append fetched rows to the main array } while (rowsBatch.length > 0); console.log('Rows fetched:', rows); // Close the cursor after you're done await cursor.close(); } else { throw new Error('No cursor returned from the stored procedure'); } if (result.outBinds && result.outBinds.p_carnet_count_cur) { const cursor = result.outBinds.p_carnet_count_cur; // The OUT cursor let rowsBatch; do { rowsBatch = await cursor.getRows(100); // Fetch 100 rows at a time crows = crows.concat(rowsBatch); // Append fetched rows to the main array } while (rowsBatch.length > 0); console.log('Rows fetched:', crows); // Close the cursor after you're done await cursor.close(); } else { throw new Error('No cursor returned from the stored procedure'); } crows = crows.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; }); crows = crows.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,chartResult:crows}; } catch (err) { console.error('Error fetching users: ', err.message); // throw new Error('Error fetching users'); return {error:"Invalid username or password"} } finally { } } }