import { Injectable } from '@nestjs/common'; import { OracleDBService } from 'src/db/db.service'; import * as oracledb from 'oracledb' @Injectable() export class SpContactsService { constructor(private readonly oracleDBService: OracleDBService) { } async insertSPContacts( p_spid: number, p_defcontactflag: string, p_firstname: string, p_lastname: string, P_MIDDLEINITIAL: string, p_title: string, p_phoneno: string, p_mobileno: string, p_faxno: string, p_emailaddress: string, p_user_id: string ) { let connection; try { // Connect to the Oracle database using oracledb 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: p_spid, type: oracledb.DB_TYPE_NUMBER }, p_defcontactflag: { val: p_defcontactflag, type: oracledb.DB_TYPE_VARCHAR }, p_firstname: { val: p_firstname, type: oracledb.DB_TYPE_VARCHAR }, p_lastname: { val: p_lastname, type: oracledb.DB_TYPE_VARCHAR }, P_MIDDLEINITIAL: { val: P_MIDDLEINITIAL, type: oracledb.DB_TYPE_VARCHAR }, p_title: { val: p_title, type: oracledb.DB_TYPE_VARCHAR }, p_phoneno: { val: p_phoneno, type: oracledb.DB_TYPE_VARCHAR }, p_mobileno: { val: p_mobileno, type: oracledb.DB_TYPE_VARCHAR }, p_faxno: { val: p_faxno, type: oracledb.DB_TYPE_VARCHAR }, p_emailaddress: { val: p_emailaddress, type: oracledb.DB_TYPE_VARCHAR }, p_user_id: { val: 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(); let fres = await result.outBinds.p_cursor.getRows(); return fres } catch (err) { console.error('Error fetching users: ', err); return { error: err.message } } finally { } } async setSPDefaultcontact(p_spcontactid: number) { let connection; try { // Connect to the Oracle database using oracledb 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: p_spcontactid, type: oracledb.DB_TYPE_NUMBER }, } ); await connection.commit(); return "SP executed successfully" } catch (err) { console.error('Error fetching users: ', err); return { error: err.message } } finally { } } async updateSPContacts( p_spcontactid: number, p_firstname: string, p_lastname: string, P_MIDDLEINITIAL: string, p_title: string, p_phoneno: string, p_mobileno: string, p_faxno: string, p_emailaddress: string, p_user_id: string, ) { let connection; try { // Connect to the Oracle database using oracledb 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: p_spcontactid, type: oracledb.DB_TYPE_NUMBER }, p_firstname: { val: p_firstname, type: oracledb.DB_TYPE_VARCHAR }, p_lastname: { val: p_lastname, type: oracledb.DB_TYPE_VARCHAR }, P_MIDDLEINITIAL: { val: P_MIDDLEINITIAL, type: oracledb.DB_TYPE_VARCHAR }, p_title: { val: p_title, type: oracledb.DB_TYPE_VARCHAR }, p_phoneno: { val: p_phoneno, type: oracledb.DB_TYPE_VARCHAR }, p_mobileno: { val: p_mobileno, type: oracledb.DB_TYPE_VARCHAR }, p_faxno: { val: p_faxno, type: oracledb.DB_TYPE_VARCHAR }, p_emailaddress: { val: p_emailaddress, type: oracledb.DB_TYPE_VARCHAR }, p_user_id: { val: 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(); let fres = await result.outBinds.p_cursor.getRows(); return fres } catch (err) { console.error('Error fetching users: ', err); return { error: err.message } } finally { } } async inactivateSPContact(p_spcontactid: number) { let connection; try { // Connect to the Oracle database using oracledb 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: p_spcontactid, type: oracledb.DB_TYPE_NUMBER } } ); await connection.commit(); return "SP executed successfully" } catch (err) { console.error('Error fetching users: ', err); return { error: err.message } } finally { } } async getSPDefaultcontacts(p_SPid: number) { let connection; let rows = []; try { // Connect to the Oracle database using oracledb 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: 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; // The OUT cursor let rowsBatch; do { rowsBatch = await cursor.getRows(100); // Fetch 100 rows at a time rows = rows.concat(rowsBatch); // Append fetched rows to the main array } while (rowsBatch.length > 0); // Close the cursor after you're done await cursor.close(); } else { throw new Error('No cursor returned from the stored procedure'); } return rows; } catch (err) { console.error('Error fetching users: ', err); return { error: err.message } } finally { } } async getSPcontacts() { let connection; let rows = []; try { // Connect to the Oracle database using oracledb connection = await this.oracleDBService.getConnection() if (!connection) { throw new Error('No DB Connected') } const result = await connection.execute( `BEGIN USCIB_Managed_Pkg.GetSPAllContacts(:p_cursor); END;`, { 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; // The OUT cursor let rowsBatch; do { rowsBatch = await cursor.getRows(100); // Fetch 100 rows at a time rows = rows.concat(rowsBatch); // Append fetched rows to the main array } while (rowsBatch.length > 0); console.log('Rows fetched:', rows); // Close the cursor after you're done await cursor.close(); } else { throw new Error('No cursor returned from the stored procedure'); } return rows; } catch (err) { console.error('Error fetching users: ', err); return { error: err.message } } finally { } } }