BE/src/pg/param-table/param-table.service.ts
2025-09-15 14:39:44 +05:30

309 lines
9.7 KiB
TypeScript

import { Injectable, Logger } from '@nestjs/common';
import { PoolClient } from 'pg';
import { PgDBService } from 'src/db/db.service';
import { ActivateOrInactivateParamRecordDTO, CreateParamRecordDTO, CreateTableRecordDTO, getParamValuesDTO, SPID_DTO, UpdateParamRecordDTO } from 'src/dto/property.dto';
import { checkPgUserDefinedErrors, handlePgError, releasePgClient, ResponseStatus } from 'src/utils/helper';
@Injectable()
export class ParamTableService {
private readonly logger = new Logger(ParamTableService.name);
constructor(private readonly pgDBService: PgDBService) { }
async GETPARAMVALUES(body: getParamValuesDTO) {
let client: PoolClient | null = null;
const cursorName = 'p_cursor';
try {
client = await this.pgDBService.getConnection();
await client.query('BEGIN');
const callProcQuery = `
CALL "CARNETSYS".manageparamtable_pkg_getparamvalues($1, $2, $3)
`;
await client.query(callProcQuery, [body.P_SPID, body.P_PARAMTYPE, cursorName]);
// 🚫 DON'T quote the cursor name in FETCH
const fetchResult = await client.query(`FETCH ALL FROM ${cursorName}`);
// 🚫 DON'T quote the cursor name in CLOSE
await client.query(`CLOSE ${cursorName}`);
await client.query('COMMIT');
return fetchResult.rows;
} catch (error) {
if (client) await client.query('ROLLBACK');
handlePgError(error, ParamTableService.name)
} finally {
releasePgClient(client)
}
}
async GETALLPARAMVALUES(body: getParamValuesDTO) {
let client: PoolClient | null = null;
const cursorName = 'p_cursor';
try {
client = await this.pgDBService.getConnection();
await client.query('BEGIN');
const callProcQuery = `
CALL "CARNETSYS".manageparamtable_pkg_getallparamvalues($1, $2, $3)
`;
await client.query(callProcQuery, [body.P_SPID, body.P_PARAMTYPE, cursorName]);
// 🚫 DON'T quote the cursor name in FETCH
const fetchResult = await client.query(`FETCH ALL FROM ${cursorName}`);
// 🚫 DON'T quote the cursor name in CLOSE
await client.query(`CLOSE ${cursorName}`);
await client.query('COMMIT');
return fetchResult.rows;
} catch (error) {
if (client) await client.query('ROLLBACK');
handlePgError(error, ParamTableService.name)
} finally {
releasePgClient(client)
}
}
async GET_COUNTRIES_AND_MESSAGES(body: SPID_DTO) {
let client: PoolClient | null = null;
const cursorName = 'p_cursor';
try {
client = await this.pgDBService.getConnection();
await client.query('BEGIN');
const callProcQuery = `
CALL "CARNETSYS".manageparamtable_pkg_getcountriesandmessages($1, $2)
`;
await client.query(callProcQuery, [body.P_SPID, cursorName]);
// 🚫 DON'T quote the cursor name in FETCH
const fetchResult = await client.query(`FETCH ALL FROM ${cursorName}`);
// 🚫 DON'T quote the cursor name in CLOSE
await client.query(`CLOSE ${cursorName}`);
await client.query('COMMIT');
return fetchResult.rows;
} catch (error) {
if (client) await client.query('ROLLBACK');
handlePgError(error, ParamTableService.name)
} finally {
releasePgClient(client)
}
}
async CREATETABLERECORD(body: CreateTableRecordDTO) {
let client: PoolClient | null = null;
const cursorName = 'p_cursor';
try {
client = await this.pgDBService.getConnection();
await client.query('BEGIN');
const callProcQuery = `
CALL "CARNETSYS".manageparamtable_pkg_createtablerecord($1, $2, $3)
`;
await client.query(callProcQuery, [body.P_USERID, body.P_TABLEFULLDESC, cursorName]);
// 🚫 DON'T quote the cursor name in FETCH
const fetchResult = await client.query(`FETCH ALL FROM ${cursorName}`);
// 🚫 DON'T quote the cursor name in CLOSE
await client.query(`CLOSE ${cursorName}`);
await client.query('COMMIT');
checkPgUserDefinedErrors(fetchResult);
// return fetchResult.rows;
return { statusCode: 201, message: ResponseStatus.created, ...fetchResult?.rows[0] };
} catch (error) {
if (client) await client.query('ROLLBACK');
handlePgError(error, ParamTableService.name)
} finally {
releasePgClient(client)
}
}
async CREATEPARAMRECORD(body: CreateParamRecordDTO) {
let client: PoolClient | null = null;
const cursorName = 'p_cursor';
try {
client = await this.pgDBService.getConnection();
await client.query('BEGIN');
// Call the stored procedure
const callProcQuery = `
CALL "CARNETSYS".manageparamtable_pkg_createparamrecord(
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12
)
`;
await client.query(callProcQuery, [
body.P_SPID,
body.P_PARAMTYPE,
body.P_PARAMDESC,
body.P_PARAMVALUE,
body.P_ADDLPARAMVALUE1,
body.P_ADDLPARAMVALUE2,
body.P_ADDLPARAMVALUE3,
body.P_ADDLPARAMVALUE4,
body.P_ADDLPARAMVALUE5,
body.P_SORTSEQ,
body.P_USERID,
cursorName
]);
// ✅ Correct: quote the cursor name when using it in SQL
const fetchResult = await client.query(`FETCH ALL FROM "${cursorName}"`);
// ✅ Always close the cursor
await client.query(`CLOSE "${cursorName}"`);
await client.query('COMMIT');
checkPgUserDefinedErrors(fetchResult);
// Return response
return {
statusCode: 201,
message: ResponseStatus.created,
...fetchResult?.rows?.[0] || {}
};
} catch (error) {
if (client) await client.query('ROLLBACK');
handlePgError(error, ParamTableService.name)
} finally {
releasePgClient(client)
}
}
async UPDATEPARAMRECORD(body: UpdateParamRecordDTO) {
let client: PoolClient | null = null;
const cursorName = 'p_cursor';
try {
client = await this.pgDBService.getConnection();
await client.query('BEGIN');
const callProcQuery = `
CALL "CARNETSYS".manageparamtable_pkg_updateparamrecord(
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11
)
`;
await client.query(callProcQuery, [
body.P_SPID,
body.P_PARAMID,
body.P_PARAMDESC,
body.P_ADDLPARAMVALUE1,
body.P_ADDLPARAMVALUE2,
body.P_ADDLPARAMVALUE3,
body.P_ADDLPARAMVALUE4,
body.P_ADDLPARAMVALUE5,
body.P_SORTSEQ,
body.P_USERID,
cursorName
]);
// ✅ Correct: quote the cursor name when using it in SQL
const fetchResult = await client.query(`FETCH ALL FROM "${cursorName}"`);
// ✅ Always close the cursor
await client.query(`CLOSE "${cursorName}"`);
await client.query('COMMIT');
checkPgUserDefinedErrors(fetchResult);
// Return response
return {
statusCode: 200,
message: ResponseStatus.updated,
...fetchResult?.rows?.[0] || {}
};
} catch (error) {
if (client) await client.query('ROLLBACK');
handlePgError(error, ParamTableService.name)
} finally {
releasePgClient(client)
}
}
async INACTIVATEPARAMRECORD(body: ActivateOrInactivateParamRecordDTO) {
let client: PoolClient | null = null;
try {
client = await this.pgDBService.getConnection();
await client.query('BEGIN');
const callProcQuery = `
CALL "CARNETSYS".manageparamtable_pkg_inactivateparamrecord($1, $2)
`;
await client.query(callProcQuery, [body.P_PARAMID, body.P_USERID]);
await client.query('COMMIT');
return {
statusCode: 200,
message: ResponseStatus.inActivate
};
} catch (error) {
if (client) await client.query('ROLLBACK');
handlePgError(error, ParamTableService.name)
} finally {
releasePgClient(client)
}
}
async REACTIVATEPARAMRECORD(body: ActivateOrInactivateParamRecordDTO) {
let client: PoolClient | null = null;
try {
client = await this.pgDBService.getConnection();
await client.query('BEGIN');
const callProcQuery = `
CALL "CARNETSYS".manageparamtable_pkg_reactivateparamrecord($1, $2)
`;
await client.query(callProcQuery, [body.P_PARAMID, body.P_USERID]);
await client.query('COMMIT');
return {
statusCode: 200,
message: ResponseStatus.reActivate
};
} catch (error) {
if (client) await client.query('ROLLBACK');
handlePgError(error, ParamTableService.name)
} finally {
releasePgClient(client)
}
}
}