import { Injectable, Logger } from '@nestjs/common'; import { PgDBService } from 'src/db/db.service'; import { InsertNewServiceProviderDTO, SPID_DTO, UpdateServiceProviderDTO } from 'src/dto/property.dto'; import { InternalServerException } from 'src/exceptions/internalServerError.exception'; import { PoolClient } from 'pg'; import { checkPgUserDefinedErrors, handlePgError, releasePgClient, ResponseStatus, toUpperCaseKeys } from 'src/utils/helper'; @Injectable() export class SpService { private readonly logger = new Logger(SpService.name); constructor(private readonly pgDBService: PgDBService) { } async insertNewServiceProvider(body: InsertNewServiceProviderDTO) { console.log(" i got executed here ...."); let client: PoolClient | null = null; const cursorName = 'p_cursor'; try { client = await this.pgDBService.getConnection(); console.log(" pg connection successfull ..... "); await client.query('BEGIN'); const callProcQuery = ` CALL "CARNETSYS".uscib_managed_pkg_insertnewsp($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17) `; await client.query(callProcQuery, [ body.P_NAME, body.P_LOOKUPCODE, body.P_ADDRESS1, body.P_ADDRESS2, body.P_CITY, body.P_STATE, body.P_ZIP, body.P_COUNTRY, body.P_ISSUINGREGION, body.P_REPLACEMENTREGION, body.P_BONDSURETY, body.P_CARGOPOLICYNO, body.P_CARGOSURETY, body.P_USER_ID, body.P_NOTES, body.P_FILEIDS, cursorName ]); console.log("Procedure called"); // 🚫 DON'T quote the cursor name in FETCH const fetchResult = await client.query(`FETCH ALL FROM ${cursorName}`); console.log("Fetch done", fetchResult?.rows); // 🚫 DON'T quote the cursor name in CLOSE await client.query(`CLOSE ${cursorName}`); console.log("Cursor closed"); await client.query('COMMIT'); console.log("Transaction committed"); checkPgUserDefinedErrors(fetchResult); return { statusCode: 201, message: ResponseStatus.created, ...toUpperCaseKeys(fetchResult?.rows)[0] || {} }; } catch (error) { if (client) await client.query('ROLLBACK'); console.log(" i went through here ... "); handlePgError(error, SpService.name) } finally { releasePgClient(client) } } async updateServiceProvider(body: UpdateServiceProviderDTO) { 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_updatesp($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18) `; await client.query(callProcQuery, [ body.P_SPID, body.P_NAME, body.P_LOOKUPCODE, body.P_ADDRESS1, body.P_ADDRESS2, body.P_CITY, body.P_STATE, body.P_ZIP, body.P_COUNTRY, body.P_BONDSURETY, body.P_CARGOPOLICYNO, body.P_CARGOSURETY, body.P_REPLACEMENTREGION, body.P_ISSUINGREGION, body.P_USER_ID, body.P_NOTES, body.P_FILEIDS, 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, ...toUpperCaseKeys(fetchResult?.rows)[0] || {} }; } catch (error) { if (client) await client.query('ROLLBACK'); handlePgError(error, SpService.name) } finally { releasePgClient(client) } } async getAllServiceproviders() { 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_getallsps($1) `; await client.query(callProcQuery, [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 toUpperCaseKeys(fetchResult.rows); } catch (error) { if (client) await client.query('ROLLBACK'); handlePgError(error, SpService.name) } finally { releasePgClient(client) } } async getServiceproviderByID(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_getspbyspid($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 toUpperCaseKeys(fetchResult.rows); } catch (error) { if (client) await client.query('ROLLBACK'); handlePgError(error, SpService.name) } finally { releasePgClient(client) } } }