BE/src/oracle/user-maintenance/user-maintenance.service.ts
2025-05-23 17:32:44 +05:30

383 lines
15 KiB
TypeScript

import { Injectable, Logger } from '@nestjs/common';
import { OracleDBService } from 'src/db/db.service';
import { CreateClientLoginsDTO, CreateSPLoginsDTO, CreateUSCIBLoginsDTO, SPID_CLIENTID_DTO, SPID_EMAIL_DTO } from '../../dto/user-maintenance/user-maintenance.dto';
import { InternalServerException } from 'src/exceptions/internalServerError.exception';
import * as oracledb from 'oracledb';
import { BadRequestException } from 'src/exceptions/badRequest.exception';
import { SPID_DTO, USERID_DTO } from 'src/dto/property.dto';
import { closeOracleDbConnection, fetchCursor, handleError } from 'src/utils/helper';
interface RoleDetail { [key: string]: any; }
interface MenuDetail { [key: string]: any; }
interface MenuPageDetail { [key: string]: any; }
interface UserDetail { [key: string]: any; }
export interface GetUserDetailsResult {
roleDetails: RoleDetail[];
menuDetails: MenuDetail[];
menuPageDetails: MenuPageDetail[];
userDetails: UserDetail[];
}
@Injectable()
export class UserMaintenanceService {
private readonly logger = new Logger(UserMaintenanceService.name);
constructor(private readonly oracleDBService: OracleDBService) { }
// private async fetchCursor<T>(cursor: oracledb.ResultSet<T>): Promise<T[]> {
// try {
// const rows = await cursor.getRows(); // or getRows(1000) if needed
// await cursor.close();
// return rows;
// } catch (err) {
// this.logger.error('Failed to fetch from cursor', err.stack || err);
// throw new InternalServerException("Error reading data from database.");
// }
// }
// SP_USER_DETAILS
async GetSPUserDetails(body: USERID_DTO): Promise<any> {
let connection;
try {
connection = await this.oracleDBService.getConnection();
const result = await connection.execute(
`BEGIN
SPCLIENTUSER_PKG.GetSPUserDetails(
:P_USERID, :P_ROLECUR, :P_MENUCUR, :P_MENUPAGECUR, :P_USERDETAILS
);
END;`,
{
P_USERID: { val: body.p_userid, type: oracledb.DB_TYPE_NVARCHAR },
P_ROLECUR: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT },
P_MENUCUR: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT },
P_MENUPAGECUR: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT },
P_USERDETAILS: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT },
},
{ outFormat: oracledb.OUT_FORMAT_OBJECT }
);
const outBinds = result.outBinds;
if (!outBinds?.P_ROLECUR || !outBinds?.P_MENUCUR || !outBinds?.P_MENUPAGECUR || !outBinds?.P_USERDETAILS) {
this.logger.error('One or more expected cursors are missing from stored procedure output.');
throw new InternalServerException("Incomplete data received from the database.");
}
let roleDetailsTemp: { "ROLENAME": "" }[] = await fetchCursor(outBinds.P_ROLECUR, UserMaintenanceService.name);
let menuDetailsTemp: { "MENUNAME": "" }[] = await fetchCursor(outBinds.P_MENUCUR, UserMaintenanceService.name);
let userdetailsTemp: any = await fetchCursor(outBinds.P_USERDETAILS, UserMaintenanceService.name);
return {
roleDetails: roleDetailsTemp.map(x => x.ROLENAME),
menuDetails: menuDetailsTemp.map(x => x.MENUNAME),
menuPageDetails: await fetchCursor(outBinds.P_MENUPAGECUR, UserMaintenanceService.name),
userDetails: userdetailsTemp[0]
};
} catch (error) {
handleError(error, UserMaintenanceService.name)
} finally {
await closeOracleDbConnection(connection, UserMaintenanceService.name)
}
}
async LockUserAccount(body: SPID_EMAIL_DTO) {
let connection;
try {
connection = await this.oracleDBService.getConnection();
const result = await connection.execute(
`BEGIN
Userlogin_pkg.LockUserAccount(
:p_spid, :p_emailaddr, :p_cursor
);
END;`,
{
p_spid: { val: body.p_spid, type: oracledb.DB_TYPE_NUMBER },
p_emailaddr: { val: body.p_emailaddr, type: oracledb.DB_TYPE_NVARCHAR },
p_cursor: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT }
},
{ outFormat: oracledb.OUT_FORMAT_OBJECT }
);
const outBinds = result.outBinds;
if (!outBinds?.p_cursor) {
this.logger.error('One or more expected cursors are missing from stored procedure output.');
throw new InternalServerException("Incomplete data received from the database.");
}
return await fetchCursor(outBinds.p_cursor, UserMaintenanceService.name);
} catch (error) {
handleError(error, UserMaintenanceService.name)
} finally {
await closeOracleDbConnection(connection, UserMaintenanceService.name)
}
}
// USCIB_LOGINS
async GetUSCIBLogins() {
let connection;
try {
connection = await this.oracleDBService.getConnection();
const result = await connection.execute(
`BEGIN
Userlogin_pkg.GetUSCIBLogins(
:p_cursor
);
END;`,
{
p_cursor: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT }
},
{ outFormat: oracledb.OUT_FORMAT_OBJECT }
);
const outBinds = result.outBinds;
if (!outBinds?.p_cursor) {
this.logger.error('One or more expected cursors are missing from stored procedure output.');
throw new InternalServerException("Incomplete data received from the database.");
}
return await fetchCursor(outBinds.p_cursor, UserMaintenanceService.name);
} catch (error) {
handleError(error, UserMaintenanceService.name)
} finally {
await closeOracleDbConnection(connection, UserMaintenanceService.name)
}
}
async CreateUSCIBLogins(body: CreateUSCIBLoginsDTO) {
let connection;
try {
connection = await this.oracleDBService.getConnection();
const result = await connection.execute(
`BEGIN
Userlogin_pkg.CreateUSCIBLogins(
:p_userid , :p_emailaddr , :p_lookupCode , :p_password , :P_EnablePasswordPolicy, :p_cursor
);
END;`,
{
p_userid: { val: body.p_userid, type: oracledb.DB_TYPE_NVARCHAR },
p_emailaddr: { val: body.p_emailaddr, type: oracledb.DB_TYPE_NVARCHAR },
p_lookupCode: { val: body.p_lookupCode, type: oracledb.DB_TYPE_NVARCHAR },
p_password: { val: body.p_password, type: oracledb.DB_TYPE_NVARCHAR },
P_EnablePasswordPolicy: { val: body.P_EnablePasswordPolicy, type: oracledb.DB_TYPE_NVARCHAR },
p_cursor: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT }
},
{ outFormat: oracledb.OUT_FORMAT_OBJECT }
);
const outBinds = result.outBinds;
if (!outBinds?.p_cursor) {
this.logger.error('One or more expected cursors are missing from stored procedure output.');
throw new InternalServerException("Incomplete data received from the database.");
}
return {
data: await fetchCursor(outBinds.p_cursor),
};
} catch (error) {
handleError(error, UserMaintenanceService.name)
} finally {
await closeOracleDbConnection(connection, UserMaintenanceService.name)
}
}
// SP LOGINS
async GetSPLogins(body: SPID_DTO) {
let connection;
try {
connection = await this.oracleDBService.getConnection();
const result = await connection.execute(
`BEGIN
Userlogin_pkg.GetSPLogins(
:p_spid , :p_cursor
);
END;`,
{
p_spid: { val: body.p_spid, type: oracledb.DB_TYPE_NUMBER },
p_cursor: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT }
},
{ outFormat: oracledb.OUT_FORMAT_OBJECT }
);
const outBinds = result.outBinds;
if (!outBinds?.p_cursor) {
this.logger.error('One or more expected cursors are missing from stored procedure output.');
throw new InternalServerException("Incomplete data received from the database.");
}
return await fetchCursor(outBinds.p_cursor, UserMaintenanceService.name);
} catch (error) {
handleError(error, UserMaintenanceService.name)
} finally {
await closeOracleDbConnection(connection, UserMaintenanceService.name)
}
}
async CreateSPLogins(body: CreateSPLoginsDTO) {
let connection;
try {
connection = await this.oracleDBService.getConnection();
const result = await connection.execute(
`BEGIN
Userlogin_pkg.CreateSPLogins(
:p_spid, :p_userid, :p_domain, :p_emailaddr, :p_lookupCode, :p_password, :P_EnablePasswordPolicy, :p_cursor
);
END;`,
{
p_spid: { val: body.p_spid, type: oracledb.DB_TYPE_NUMBER },
p_userid: { val: body.p_userid, type: oracledb.DB_TYPE_NVARCHAR },
p_domain: { val: body.p_domain, type: oracledb.DB_TYPE_NVARCHAR },
p_emailaddr: { val: body.p_emailaddr, type: oracledb.DB_TYPE_NVARCHAR },
p_lookupCode: { val: body.p_lookupCode, type: oracledb.DB_TYPE_NVARCHAR },
p_password: { val: body.p_password, type: oracledb.DB_TYPE_NVARCHAR },
P_EnablePasswordPolicy: { val: body.P_EnablePasswordPolicy, type: oracledb.DB_TYPE_NVARCHAR },
p_cursor: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT }
},
{ outFormat: oracledb.OUT_FORMAT_OBJECT }
);
const outBinds = result.outBinds;
if (!outBinds?.p_cursor) {
this.logger.error('One or more expected cursors are missing from stored procedure output.');
throw new InternalServerException("Incomplete data received from the database.");
}
return await fetchCursor(outBinds.p_cursor, UserMaintenanceService.name);
} catch (error) {
handleError(error, UserMaintenanceService.name)
} finally {
await closeOracleDbConnection(connection, UserMaintenanceService.name)
}
}
// CLIENT LOGINS
async GetClientloginsBySPID(body: SPID_DTO) {
let connection;
try {
connection = await this.oracleDBService.getConnection();
const result = await connection.execute(
`BEGIN
Userlogin_pkg.GetClientloginsBySPID(
:p_spid , :p_cursor
);
END;`,
{
p_spid: { val: body.p_spid, type: oracledb.DB_TYPE_NUMBER },
p_cursor: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT }
},
{ outFormat: oracledb.OUT_FORMAT_OBJECT }
);
const outBinds = result.outBinds;
if (!outBinds?.p_cursor) {
this.logger.error('One or more expected cursors are missing from stored procedure output.');
throw new InternalServerException("Incomplete data received from the database.");
}
return await fetchCursor(outBinds.p_cursor, UserMaintenanceService.name);
} catch (error) {
handleError(error, UserMaintenanceService.name)
} finally {
await closeOracleDbConnection(connection, UserMaintenanceService.name)
}
}
async GetClientloginsByClientID(body: SPID_CLIENTID_DTO) {
let connection;
try {
connection = await this.oracleDBService.getConnection();
const result = await connection.execute(
`BEGIN
Userlogin_pkg.GetClientloginsByClientID(
:p_spid, :p_clientid , :p_cursor
);
END;`,
{
p_spid: { val: body.p_spid, type: oracledb.DB_TYPE_NUMBER },
p_clientid: { val: body.P_CLIENTID, type: oracledb.DB_TYPE_NUMBER },
p_cursor: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT }
},
{ outFormat: oracledb.OUT_FORMAT_OBJECT }
);
const outBinds = result.outBinds;
if (!outBinds?.p_cursor) {
this.logger.error('One or more expected cursors are missing from stored procedure output.');
throw new InternalServerException("Incomplete data received from the database.");
}
return await fetchCursor(outBinds.p_cursor, UserMaintenanceService.name);
} catch (error) {
handleError(error, UserMaintenanceService.name)
} finally {
await closeOracleDbConnection(connection, UserMaintenanceService.name)
}
}
async CreateClientLogins(body: CreateClientLoginsDTO) {
let connection;
try {
connection = await this.oracleDBService.getConnection();
const result = await connection.execute(
`BEGIN
Userlogin_pkg.CreateClientLogins(
:p_spid, :p_userid, :p_clientcontactid, :p_password, :P_EnablePasswordPolicy, :p_cursor
);
END;`,
{
p_spid: { val: body.p_spid, type: oracledb.DB_TYPE_NUMBER },
p_userid: { val: body.p_userid, type: oracledb.DB_TYPE_NVARCHAR },
p_clientcontactid: { val: body.p_clientcontactid, type: oracledb.DB_TYPE_NUMBER },
p_password: { val: body.p_password, type: oracledb.DB_TYPE_NVARCHAR },
P_EnablePasswordPolicy: { val: body.P_EnablePasswordPolicy, type: oracledb.DB_TYPE_NVARCHAR },
p_cursor: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT }
},
{ outFormat: oracledb.OUT_FORMAT_OBJECT }
);
const outBinds = result.outBinds;
if (!outBinds?.p_cursor) {
this.logger.error('One or more expected cursors are missing from stored procedure output.');
throw new InternalServerException("Incomplete data received from the database.");
}
return await fetchCursor(outBinds.p_cursor, UserMaintenanceService.name);
} catch (error) {
handleError(error, UserMaintenanceService.name)
} finally {
await closeOracleDbConnection(connection, UserMaintenanceService.name)
}
}
}