BE/src/auth/auth.service.ts

133 lines
4.9 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
2025-03-17 11:05:03 +05:30
USERLOGIN_PKG.ValidateUser(:p_emailaddr,:p_password,:p_login_cursor);
2025-03-11 10:52:58 +05:30
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
}
},
{
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');
}
2025-03-17 11:05:03 +05:30
// if (result.outBinds && result.outBinds.p_carnet_count_cur) {
// const cursor = result.outBinds.p_carnet_count_cur; // The OUT cursor
// let rowsBatch;
2025-03-11 10:52:58 +05:30
2025-03-17 11:05:03 +05:30
// 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);
2025-03-11 10:52:58 +05:30
2025-03-17 11:05:03 +05:30
// console.log('Rows fetched:', crows);
2025-03-11 10:52:58 +05:30
2025-03-17 11:05:03 +05:30
// // Close the cursor after you're done
// await cursor.close();
// } else {
// throw new Error('No cursor returned from the stored procedure');
// }
2025-03-11 10:52:58 +05:30
2025-03-17 11:05:03 +05:30
// crows = crows.map(obj => {
// // Create a new object to hold the modified keys
// let newObj = {};
2025-03-13 11:29:54 +05:30
2025-03-17 11:05:03 +05:30
// // 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];
// }
2025-03-13 11:29:54 +05:30
2025-03-17 11:05:03 +05:30
// return newObj;
// });
2025-03-13 11:29:54 +05:30
2025-03-17 11:05:03 +05:30
// 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);
2025-03-13 11:29:54 +05:30
2025-03-17 11:05:03 +05:30
// 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"]]
// });
// }
2025-03-13 11:29:54 +05:30
2025-03-17 11:05:03 +05:30
// return acc;
// }, []);
2025-03-13 11:29:54 +05:30
2025-03-17 11:05:03 +05:30
// if(!crows[0].SPID){
// return {error:"Invalid username or password"}
// }
2025-03-13 17:30:51 +05:30
2025-03-17 11:05:03 +05:30
// return {rows,chartResult:crows};
if(rows[0]["ERRORMESG"]){
return {error:"Invalid username or password!"}
}
return {msg:"Logged in successfully"}
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 { }
}
}