import { Injectable, Logger } from '@nestjs/common'; import { PgDBService } from 'src/db/db.service'; import { InsertRegionsDto, SPID_DTO, UpdateRegionDto } from 'src/dto/property.dto'; import { InternalServerException } from 'src/exceptions/internalServerError.exception'; import { PoolClient } from 'pg'; import { checkPgUserDefinedErrors, handlePgError, releasePgClient, ResponseStatus } from 'src/utils/helper'; @Injectable() export class RegionService { private readonly logger = new Logger(RegionService.name); constructor(private readonly pgDBService: PgDBService) { } async insertRegions(body: InsertRegionsDto) { let client: PoolClient | null = null; const cursorName = 'p_cursor'; try { client = await this.pgDBService.getConnection(); await client.query('BEGIN'); const callProcQuery = ` CALL "CARNETSYS".uscib_managed_pkg_insertnewregion($1, $2, $3, $4) `; await client.query(callProcQuery, [body.P_REGION, body.P_NAME, 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'); checkPgUserDefinedErrors(fetchResult); return { statusCode: 201, message: ResponseStatus.created, ...fetchResult?.rows?.[0] || {} }; } catch (error) { if (client) await client.query('ROLLBACK'); handlePgError(error, RegionService.name) } finally { releasePgClient(client) } } async updateRegions(body: UpdateRegionDto) { let client: PoolClient | null = null; const cursorName = 'p_cursor'; try { client = await this.pgDBService.getConnection(); await client.query('BEGIN'); const callProcQuery = ` CALL "CARNETSYS".uscib_managed_pkg_updateregion($1, $2, $3) `; await client.query(callProcQuery, [body.P_REGIONID, body.P_NAME, 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 { statusCode: 200, message: ResponseStatus.updated, ...fetchResult?.rows[0] || {} }; } catch (error) { if (client) await client.query('ROLLBACK'); handlePgError(error, RegionService.name) } finally { releasePgClient(client) } } async getRegions(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".uscib_managed_pkg_getregions($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, RegionService.name) } finally { releasePgClient(client) } } }