BE/src/auth/auth.service.ts

132 lines
4.8 KiB
TypeScript
Raw Normal View History

2025-03-11 10:52:58 +05:30
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 = [];
2025-03-13 11:29:54 +05:30
let crows:any = [];
2025-03-11 10:52:58 +05:30
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
2025-03-11 15:53:25 +05:30
crows = crows.concat(rowsBatch); // Append fetched rows to the main array
2025-03-11 10:52:58 +05:30
} 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');
}
2025-03-13 11:29:54 +05:30
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;
}, []);
2025-03-13 17:30:51 +05:30
if(!crows[0].SPID){
return {error:"Invalid username or password"}
}
2025-03-13 11:29:54 +05:30
return {rows,chartResult:crows};
2025-03-11 10:52:58 +05:30
} catch (err) {
console.error('Error fetching users: ', err.message);
// throw new Error('Error fetching users');
return {error:"Invalid username or password"}
2025-03-11 10:52:58 +05:30
} finally { }
}
}