import { Injectable } from '@nestjs/common'; import { OracleDBService } from 'src/db/db.service'; import * as oracledb from 'oracledb'; import { getSPAllContactsDTO, getSPDefaultcontactDTO, inactivateSPContactDTO, InsertSPContactsDTO, setSPDefaultcontactDTO, UpdateSPContactsDTO, } from './sp-contacts.dto'; @Injectable() export class SpContactsService { constructor(private readonly oracleDBService: OracleDBService) { } async insertSPContacts(body: InsertSPContactsDTO) { let connection; try { connection = await this.oracleDBService.getConnection(); if (!connection) { throw new Error('No DB Connected'); } const result = await connection.execute( `BEGIN USCIB_Managed_Pkg.InsertSPContacts( :p_spid, :p_defcontactflag, :p_firstname, :p_lastname, :P_MIDDLEINITIAL, :p_title, :p_phoneno, :p_mobileno, :p_faxno, :p_emailaddress, :p_user_id, :p_cursor); END;`, { p_spid: { val: body.p_spid, type: oracledb.DB_TYPE_NUMBER, }, p_defcontactflag: { val: body.p_defcontactflag, type: oracledb.DB_TYPE_VARCHAR, }, p_firstname: { val: body.p_firstname, type: oracledb.DB_TYPE_VARCHAR, }, p_lastname: { val: body.p_lastname, type: oracledb.DB_TYPE_VARCHAR, }, P_MIDDLEINITIAL: { val: body.P_MIDDLEINITIAL, type: oracledb.DB_TYPE_VARCHAR, }, p_title: { val: body.p_title, type: oracledb.DB_TYPE_VARCHAR, }, p_phoneno: { val: body.p_phoneno, type: oracledb.DB_TYPE_VARCHAR, }, p_mobileno: { val: body.p_mobileno, type: oracledb.DB_TYPE_VARCHAR, }, p_faxno: { val: body.p_faxno, type: oracledb.DB_TYPE_VARCHAR, }, p_emailaddress: { val: body.p_emailaddress, type: oracledb.DB_TYPE_VARCHAR, }, p_user_id: { val: body.p_user_id, type: oracledb.DB_TYPE_VARCHAR, }, p_cursor: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT, }, }, { outFormat: oracledb.OUT_FORMAT_OBJECT, }, ); await connection.commit(); const fres = await result.outBinds.p_cursor.getRows(); return fres; } catch (err) { if (err instanceof Error) { return { error: err.message }; } else { return { error: 'An unknown error occurred' }; } } } async setSPDefaultcontact(body: setSPDefaultcontactDTO) { let connection; try { connection = await this.oracleDBService.getConnection(); if (!connection) { throw new Error('No DB Connected'); } const result = await connection.execute( `BEGIN USCIB_Managed_Pkg.SetDefaultContact(:p_spcontactid); END;`, { p_spcontactid: { val: body.p_spcontactid, type: oracledb.DB_TYPE_NUMBER, }, }, ); await connection.commit(); return 'SP executed successfully'; } catch (err) { if (err instanceof Error) { return { error: err.message }; } else { return { error: 'An unknown error occurred' }; } } } async updateSPContacts(body: UpdateSPContactsDTO) { let connection; try { connection = await this.oracleDBService.getConnection(); if (!connection) { throw new Error('No DB Connected'); } const result = await connection.execute( `BEGIN USCIB_Managed_Pkg.UpdateSPContacts( :p_spcontactid, :p_firstname, :p_lastname, :P_MIDDLEINITIAL, :p_title, :p_phoneno, :p_mobileno, :p_faxno, :p_emailaddress, :p_user_id, :p_cursor); END;`, { p_spcontactid: { val: body.p_spcontactid, type: oracledb.DB_TYPE_NUMBER, }, p_firstname: { val: body.p_firstname, type: oracledb.DB_TYPE_VARCHAR, }, p_lastname: { val: body.p_lastname, type: oracledb.DB_TYPE_VARCHAR, }, P_MIDDLEINITIAL: { val: body.P_MIDDLEINITIAL, type: oracledb.DB_TYPE_VARCHAR, }, p_title: { val: body.p_title, type: oracledb.DB_TYPE_VARCHAR, }, p_phoneno: { val: body.p_phoneno, type: oracledb.DB_TYPE_VARCHAR, }, p_mobileno: { val: body.p_mobileno, type: oracledb.DB_TYPE_VARCHAR, }, p_faxno: { val: body.p_faxno, type: oracledb.DB_TYPE_VARCHAR, }, p_emailaddress: { val: body.p_emailaddress, type: oracledb.DB_TYPE_VARCHAR, }, p_user_id: { val: body.p_user_id, type: oracledb.DB_TYPE_VARCHAR, }, p_cursor: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT, }, }, { outFormat: oracledb.OUT_FORMAT_OBJECT, }, ); await connection.commit(); const fres = await result.outBinds.p_cursor.getRows(); return fres; } catch (err) { if (err instanceof Error) { return { error: err.message }; } else { return { error: 'An unknown error occurred' }; } } } async inactivateSPContact(body: inactivateSPContactDTO) { let connection; try { connection = await this.oracleDBService.getConnection(); if (!connection) { throw new Error('No DB Connected'); } const result = await connection.execute( `BEGIN USCIB_Managed_Pkg.InActivateSPContacts(:p_spcontactid); END;`, { p_spcontactid: { val: body.p_spcontactid, type: oracledb.DB_TYPE_NUMBER, }, }, ); await connection.commit(); return 'SP executed successfully'; } catch (err) { if (err instanceof Error) { return { error: err.message }; } else { return { error: 'An unknown error occurred' }; } } } async getSPDefaultcontacts(body: getSPDefaultcontactDTO) { let connection; let rows = []; try { connection = await this.oracleDBService.getConnection(); if (!connection) { throw new Error('No DB Connected'); } const result = await connection.execute( `BEGIN USCIB_Managed_Pkg.GetSPDefaultContacts(:p_SPid,:p_cursor); END;`, { p_SPid: { val: body.p_SPid, type: oracledb.DB_TYPE_NUMBER, }, p_cursor: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT, }, }, { outFormat: oracledb.OUT_FORMAT_OBJECT, }, ); if (result.outBinds && result.outBinds.p_cursor) { const cursor = result.outBinds.p_cursor; let rowsBatch; do { rowsBatch = await cursor.getRows(100); rows = rows.concat(rowsBatch); } while (rowsBatch.length > 0); await cursor.close(); } else { throw new Error('No cursor returned from the stored procedure'); } return rows; } catch (err) { if (err instanceof Error) { return { error: err.message }; } else { return { error: 'An unknown error occurred' }; } } } async getSPAllContacts(body: getSPAllContactsDTO) { let connection; let rows = []; try { connection = await this.oracleDBService.getConnection(); if (!connection) { throw new Error('No DB Connected'); } const result = await connection.execute( `BEGIN USCIB_Managed_Pkg.GetSPAllContacts(:p_SPid,:p_cursor); END;`, { p_SPid: { val: body.p_SPid, type: oracledb.DB_TYPE_NUMBER, }, p_cursor: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT, }, }, { outFormat: oracledb.OUT_FORMAT_OBJECT, }, ); if (result.outBinds && result.outBinds.p_cursor) { const cursor = result.outBinds.p_cursor; let rowsBatch; do { rowsBatch = await cursor.getRows(100); rows = rows.concat(rowsBatch); } while (rowsBatch.length > 0); await cursor.close(); } else { throw new Error('No cursor returned from the stored procedure'); } return rows; } catch (err) { if (err instanceof Error) { return { error: err.message }; } else { return { error: 'An unknown error occurred' }; } } } }