This commit is contained in:
Kallesh B S 2025-03-11 10:52:58 +05:30
parent a82ef35a00
commit 7e82150b24
5 changed files with 134 additions and 1 deletions

View File

@ -1,9 +1,10 @@
import { Module } from '@nestjs/common';
import { DbModule } from './db/db.module';
import { OracleModule } from './oracle/oracle.module';
import { AuthModule } from './auth/auth.module';
@Module({
imports: [DbModule, OracleModule],
imports: [DbModule, OracleModule, AuthModule],
controllers: [],
providers: [],
})

View File

@ -0,0 +1,16 @@
import { Body, Controller, Post } from '@nestjs/common';
import { AuthService } from './auth.service';
import { ApiTags } from '@nestjs/swagger';
import { AuthLoginDTO } from './auth.dto';
@Controller('auth')
export class AuthController {
constructor(private readonly authService:AuthService){}
@ApiTags('Auth')
@Post('/login')
login(@Body() body:AuthLoginDTO){
return this.authService.login(body);
}
}

13
src/auth/auth.dto.ts Normal file
View File

@ -0,0 +1,13 @@
import { ApiProperty } from "@nestjs/swagger";
import { IsString } from "class-validator";
export class AuthLoginDTO{
@ApiProperty({required:true})
@IsString()
p_emailaddr:string;
@ApiProperty({required:true})
@IsString()
p_password:string;
}

12
src/auth/auth.module.ts Normal file
View File

@ -0,0 +1,12 @@
import { Module } from '@nestjs/common';
import { AuthService } from './auth.service';
import { AuthController } from './auth.controller';
import { OracleDBService } from 'src/db/db.service';
import { DbModule } from 'src/db/db.module';
@Module({
imports:[DbModule],
providers: [AuthService],
controllers: [AuthController]
})
export class AuthModule {}

91
src/auth/auth.service.ts Normal file
View File

@ -0,0 +1,91 @@
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 = [];
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 = rows.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');
}
return {rows,crows};
} catch (err) {
console.error('Error fetching users: ', err);
throw new Error('Error fetching users');
} finally { }
}
}