BE/src/oracle/param-table/param-table.service.ts
2025-06-07 14:42:21 +05:30

285 lines
11 KiB
TypeScript

import { Injectable, Logger } from '@nestjs/common';
import { OracleDBService } from 'src/db/db.service';
import * as oracledb from 'oracledb';
import { BadRequestException } from 'src/exceptions/badRequest.exception';
import { InternalServerException } from 'src/exceptions/internalServerError.exception';
import { closeOracleDbConnection, fetchCursor, handleError } from 'src/utils/helper';
import {
ActivateOrInactivateParamRecordDTO, CreateParamRecordDTO, CreateTableRecordDTO,
getParamValuesDTO, UpdateParamRecordDTO
} from 'src/dto/property.dto';
@Injectable()
export class ParamTableService {
private readonly logger = new Logger(ParamTableService.name);
constructor(private readonly oracleDBService: OracleDBService) { }
async GETPARAMVALUES(body: getParamValuesDTO) {
const finalBody = {
P_SPID: body.P_SPID === 0 ? body.P_SPID : body.P_SPID ? body.P_SPID : null,
P_PARAMTYPE: body.P_PARAMTYPE ? body.P_PARAMTYPE : null,
};
let connection;
try {
connection = await this.oracleDBService.getConnection();
const result = await connection.execute(
`BEGIN
MANAGEPARAMTABLE_PKG.GETPARAMVALUES(:P_SPID,:P_PARAMTYPE,:P_CURSOR);
END;`,
{
P_SPID: { val: finalBody.P_SPID, type: oracledb.DB_TYPE_NUMBER },
P_PARAMTYPE: { val: finalBody.P_PARAMTYPE, type: oracledb.DB_TYPE_NVARCHAR },
P_CURSOR: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT }
},
{ outFormat: oracledb.OUT_FORMAT_OBJECT }
);
await connection.commit();
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.");
}
// const fres: any = await fetchCursor(outBinds.P_CURSOR, ParamTableService.name);
// if (fres.length > 0 && fres[0].ERRORMESG) {
// this.logger.warn(fres[0].ERRORMESG);
// throw new BadRequestException(fres[0].ERRORMESG)
// }
return await fetchCursor(outBinds.P_CURSOR, ParamTableService.name);
} catch (error) {
handleError(error, ParamTableService.name)
} finally {
await closeOracleDbConnection(connection, ParamTableService.name)
}
}
async CREATETABLERECORD(body: CreateTableRecordDTO) {
let connection;
try {
connection = await this.oracleDBService.getConnection();
const result = await connection.execute(
`BEGIN
MANAGEPARAMTABLE_PKG.CREATETABLERECORD(:P_USERID,:P_TABLEFULLDESC,:P_CURSOR);
END;`,
{
P_TABLEFULLDESC: { val: body.P_TABLEFULLDESC, type: oracledb.DB_TYPE_VARCHAR },
P_USERID: { val: body.P_USERID, type: oracledb.DB_TYPE_VARCHAR },
P_CURSOR: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT }
},
{ outFormat: oracledb.OUT_FORMAT_OBJECT }
);
await connection.commit();
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.");
}
const fres: any = await fetchCursor(outBinds.P_CURSOR, ParamTableService.name);
if (fres.length > 0 && fres[0].ERRORMESG) {
this.logger.warn(fres[0].ERRORMESG);
throw new BadRequestException(fres[0].ERRORMESG)
}
return { statusCode: 201, message: "Createdted Successfully", ...fres[0] };
} catch (error) {
handleError(error, ParamTableService.name)
} finally {
await closeOracleDbConnection(connection, ParamTableService.name)
}
}
async CREATEPARAMRECORD(body: CreateParamRecordDTO) {
let connection;
try {
connection = await this.oracleDBService.getConnection();
const result = await connection.execute(
`BEGIN
MANAGEPARAMTABLE_PKG.CREATEPARAMRECORD(
:P_SPID, :P_PARAMTYPE, :P_PARAMDESC, :P_PARAMVALUE,
:P_ADDLPARAMVALUE1, :P_ADDLPARAMVALUE2, :P_ADDLPARAMVALUE3, :P_ADDLPARAMVALUE4,
:P_ADDLPARAMVALUE5, :P_SORTSEQ, :P_USERID, :P_CURSOR
);
END;`,
{
P_SPID: { val: body.P_SPID, type: oracledb.DB_TYPE_NUMBER },
P_PARAMTYPE: { val: body.P_PARAMTYPE, type: oracledb.DB_TYPE_VARCHAR },
P_PARAMDESC: { val: body.P_PARAMDESC, type: oracledb.DB_TYPE_VARCHAR },
P_PARAMVALUE: { val: body.P_PARAMVALUE, type: oracledb.DB_TYPE_VARCHAR },
P_ADDLPARAMVALUE1: { val: body.P_ADDLPARAMVALUE1, type: oracledb.DB_TYPE_VARCHAR },
P_ADDLPARAMVALUE2: { val: body.P_ADDLPARAMVALUE2, type: oracledb.DB_TYPE_VARCHAR },
P_ADDLPARAMVALUE3: { val: body.P_ADDLPARAMVALUE3, type: oracledb.DB_TYPE_VARCHAR },
P_ADDLPARAMVALUE4: { val: body.P_ADDLPARAMVALUE4, type: oracledb.DB_TYPE_VARCHAR },
P_ADDLPARAMVALUE5: { val: body.P_ADDLPARAMVALUE5, type: oracledb.DB_TYPE_VARCHAR },
P_SORTSEQ: { val: body.P_SORTSEQ, type: oracledb.DB_TYPE_NUMBER },
P_USERID: { val: body.P_USERID, type: oracledb.DB_TYPE_VARCHAR },
P_CURSOR: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT }
},
{ outFormat: oracledb.OUT_FORMAT_OBJECT }
);
await connection.commit();
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.");
}
const fres: any = await fetchCursor(outBinds.P_CURSOR, ParamTableService.name);
if (fres.length > 0 && fres[0].ERRORMESG) {
this.logger.warn(fres[0].ERRORMESG);
throw new BadRequestException(fres[0].ERRORMESG)
}
return { statusCode: 201, message: "Createdted Successfully", ...fres[0] };
} catch (error) {
handleError(error, ParamTableService.name)
} finally {
await closeOracleDbConnection(connection, ParamTableService.name)
}
}
async UPDATEPARAMRECORD(body: UpdateParamRecordDTO) {
let connection;
try {
connection = await this.oracleDBService.getConnection();
const result = await connection.execute(
`BEGIN
MANAGEPARAMTABLE_PKG.UPDATEPARAMRECORD(
:P_SPID, :P_PARAMID, :P_PARAMDESC, :P_ADDLPARAMVALUE1,
:P_ADDLPARAMVALUE2,:P_ADDLPARAMVALUE3, :P_ADDLPARAMVALUE4, :P_ADDLPARAMVALUE5,
:P_SORTSEQ, :P_USERID, :P_CURSOR
);
END;`,
{
P_SPID: { val: body.P_SPID, type: oracledb.DB_TYPE_NUMBER },
P_PARAMID: { val: body.P_PARAMID, type: oracledb.DB_TYPE_NUMBER },
P_PARAMDESC: { val: body.P_PARAMDESC, type: oracledb.DB_TYPE_VARCHAR },
P_ADDLPARAMVALUE1: { val: body.P_ADDLPARAMVALUE1, type: oracledb.DB_TYPE_VARCHAR },
P_ADDLPARAMVALUE2: { val: body.P_ADDLPARAMVALUE2, type: oracledb.DB_TYPE_VARCHAR },
P_ADDLPARAMVALUE3: { val: body.P_ADDLPARAMVALUE3, type: oracledb.DB_TYPE_VARCHAR },
P_ADDLPARAMVALUE4: { val: body.P_ADDLPARAMVALUE4, type: oracledb.DB_TYPE_VARCHAR },
P_ADDLPARAMVALUE5: { val: body.P_ADDLPARAMVALUE5, type: oracledb.DB_TYPE_VARCHAR },
P_SORTSEQ: { val: body.P_SORTSEQ, type: oracledb.DB_TYPE_NUMBER },
P_USERID: { val: body.P_USERID, type: oracledb.DB_TYPE_VARCHAR },
P_CURSOR: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT }
},
{ outFormat: oracledb.OUT_FORMAT_OBJECT }
);
await connection.commit();
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.");
}
const fres: any = await fetchCursor(outBinds.P_CURSOR, ParamTableService.name);
if (fres.length > 0 && fres[0].ERRORMESG) {
this.logger.warn(fres[0].ERRORMESG);
throw new BadRequestException(fres[0].ERRORMESG)
}
return { statusCode: 200, message: "Updated Successfully", ...fres[0] };
} catch (error) {
handleError(error, ParamTableService.name)
} finally {
await closeOracleDbConnection(connection, ParamTableService.name)
}
}
async INACTIVATEPARAMRECORD(body: ActivateOrInactivateParamRecordDTO) {
let connection;
try {
connection = await this.oracleDBService.getConnection();
const result = await connection.execute(
`BEGIN
MANAGEPARAMTABLE_PKG.REACTIVATEPARAMRECORD(:P_PARAMID,:P_USERID);
END;`,
{
P_PARAMID: { val: body.P_PARAMID, type: oracledb.DB_TYPE_NUMBER },
P_USERID: { val: body.P_USERID, type: oracledb.DB_TYPE_VARCHAR }
},
{ outFormat: oracledb.OUT_FORMAT_OBJECT }
);
await connection.commit();
// 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 { statusCode: 200, message: 'Inactivated Successfully' };
} catch (error) {
handleError(error, ParamTableService.name)
} finally {
await closeOracleDbConnection(connection, ParamTableService.name)
}
}
async REACTIVATEPARAMRECORD(body: ActivateOrInactivateParamRecordDTO) {
let connection;
try {
connection = await this.oracleDBService.getConnection();
const result = await connection.execute(
`BEGIN
MANAGEPARAMTABLE_PKG.REACTIVATEPARAMRECORD(:P_PARAMID,:P_USERID);
END;`,
{
P_PARAMID: { val: body.P_PARAMID, type: oracledb.DB_TYPE_NUMBER },
P_USERID: { val: body.P_USERID, type: oracledb.DB_TYPE_VARCHAR }
},
{ outFormat: oracledb.OUT_FORMAT_OBJECT }
);
await connection.commit();
// 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 { statusCode: 200, message: 'Reactivated Successfully' };
} catch (error) {
handleError(error, ParamTableService.name)
} finally {
await closeOracleDbConnection(connection, ParamTableService.name)
}
}
}