import { Injectable } from '@nestjs/common'; import { InsertSPContactsDTO, SP_CONTACTID_DTO, SPID_DTO, UpdateSPContactsDTO } from 'src/dto/property.dto'; import { InternalServerException } from 'src/exceptions/internalServerError.exception'; import { PoolClient } from 'pg'; import { PgDBService } from 'src/db/db.service'; import { checkPgUserDefinedErrors, handlePgError, releasePgClient, ResponseStatus, toUpperCaseKeys } from 'src/utils/helper'; @Injectable() export class SpContactsService { constructor(private readonly pgDBService: PgDBService) { } async insertSPContacts(body: InsertSPContactsDTO) { 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_insertspcontacts($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) `; await client.query(callProcQuery, [ body.P_SPID, body.P_DEFCONTACTFLAG, body.P_FIRSTNAME, body.P_LASTNAME, body.P_MIDDLEINITIAL, body.P_TITLE, body.P_PHONENO, body.P_MOBILENO, body.P_FAXNO, body.P_EMAILADDRESS, body.P_USER_ID, 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, SpContactsService.name) } finally { releasePgClient(client) } } async setSPDefaultcontact(body: SP_CONTACTID_DTO) { let client: PoolClient | null = null; try { client = await this.pgDBService.getConnection(); await client.query('BEGIN'); const callProcQuery = ` CALL "CARNETSYS".uscib_managed_pkg_setdefaultcontact($1) `; await client.query(callProcQuery, [body.P_SPCONTACTID]); await client.query('COMMIT'); return { statusCode: 200, message: ResponseStatus.defaultSpContactAdded }; } catch (error) { if (client) await client.query('ROLLBACK'); handlePgError(error, SpContactsService.name) } finally { releasePgClient(client) } } async updateSPContacts(body: UpdateSPContactsDTO) { 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_updatespcontacts($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) `; await client.query(callProcQuery, [ body.P_SPCONTACTID, body.P_FIRSTNAME, body.P_LASTNAME, body.P_MIDDLEINITIAL, body.P_TITLE, body.P_PHONENO, body.P_MOBILENO, body.P_FAXNO, body.P_EMAILADDRESS, body.P_USER_ID, 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, ...toUpperCaseKeys(fetchResult?.rows)[0] }; } catch (error) { if (client) await client.query('ROLLBACK'); handlePgError(error, SpContactsService.name) } finally { releasePgClient(client) } } async inactivateSPContact(body: SP_CONTACTID_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_setdefaultcontact($1) `; await client.query(callProcQuery, [body.P_SPCONTACTID]); // 🚫 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 { statusCode: 200, message: ResponseStatus.inActivate }; } catch (error) { if (client) await client.query('ROLLBACK'); handlePgError(error, SpContactsService.name) } finally { releasePgClient(client) } } async getSPDefaultcontacts(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_getspdefaultcontacts($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, SpContactsService.name) } finally { releasePgClient(client) } } async getSPAllContacts(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_getspallcontacts($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, SpContactsService.name) } finally { releasePgClient(client) } } }