import { Injectable } from '@nestjs/common'; import { OracleDBService } from 'src/db/db.service'; import * as oracledb from 'oracledb' @Injectable() export class SpService { constructor(private readonly oracleDBService: OracleDBService) { } async insertNewServiceProvider( p_name: String, p_lookupcode: String, p_address1: String, p_address2: String, p_city: String, p_state: String, p_zip: String, p_country: String, p_issuingregion: String, p_replacementregion: String, p_bondsurety: String, p_cargopolicyno: String, p_cargosurety: String, p_user_id: String, P_NOTES: string, P_FILEIDS: 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.InsertNewSP( :p_name, :p_lookupcode, :p_address1, :p_address2, :p_city, :p_state, :p_zip, :p_country, :p_issuingregion, :p_replacementregion, :p_bondsurety, :p_cargopolicyno, :p_cargosurety, :p_user_id, :P_NOTES, :P_FILEIDS, :p_cursor); END;`, { p_name: { val: p_name, type: oracledb.DB_TYPE_VARCHAR }, p_lookupcode: { val: p_lookupcode, type: oracledb.DB_TYPE_VARCHAR }, p_address1: { val: p_address1, type: oracledb.DB_TYPE_VARCHAR }, p_address2: { val: p_address2, type: oracledb.DB_TYPE_VARCHAR }, p_city: { val: p_city, type: oracledb.DB_TYPE_VARCHAR }, p_state: { val: p_state, type: oracledb.DB_TYPE_VARCHAR }, p_zip: { val: p_zip, type: oracledb.DB_TYPE_VARCHAR }, p_country: { val: p_country, type: oracledb.DB_TYPE_VARCHAR }, p_issuingregion: { val: p_issuingregion, type: oracledb.DB_TYPE_VARCHAR }, p_replacementregion: { val: p_replacementregion, type: oracledb.DB_TYPE_VARCHAR }, p_bondsurety: { val: p_bondsurety, type: oracledb.DB_TYPE_VARCHAR }, p_cargopolicyno: { val: p_cargopolicyno, type: oracledb.DB_TYPE_VARCHAR }, p_cargosurety: { val: p_cargosurety, type: oracledb.DB_TYPE_VARCHAR }, p_user_id: { val: p_user_id, type: oracledb.DB_TYPE_VARCHAR }, P_NOTES: { val: P_NOTES, type: oracledb.DB_TYPE_VARCHAR }, P_FILEIDS: { val: P_FILEIDS, 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 updateServiceProvider( p_spid: Number, p_name: String, p_lookupcode: String, p_address1: String, p_address2: String, p_city: String, p_state: String, p_zip: String, p_country: String, p_issuingregion: String, p_replacementregion: String, p_bondsurety: String, p_cargopolicyno: String, p_cargosurety: String, p_user_id: String, P_NOTES: string, P_FILEIDS: 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.UpdateSP( :p_spid, :p_name, :p_lookupcode, :p_address1, :p_address2, :p_city, :p_state, :p_zip, :p_country, :p_issuingregion, :p_replacementregion, :p_bondsurety, :p_cargopolicyno, :p_cargosurety, :p_user_id, :P_NOTES, :P_FILEIDS, :p_cursor); END;`, { p_spid: { val: p_spid, type: oracledb.DB_TYPE_NUMBER }, p_name: { val: p_name, type: oracledb.DB_TYPE_VARCHAR }, p_lookupcode: { val: p_lookupcode, type: oracledb.DB_TYPE_VARCHAR }, p_address1: { val: p_address1, type: oracledb.DB_TYPE_VARCHAR }, p_address2: { val: p_address2, type: oracledb.DB_TYPE_VARCHAR }, p_city: { val: p_city, type: oracledb.DB_TYPE_VARCHAR }, p_state: { val: p_state, type: oracledb.DB_TYPE_VARCHAR }, p_zip: { val: p_zip, type: oracledb.DB_TYPE_VARCHAR }, p_country: { val: p_country, type: oracledb.DB_TYPE_VARCHAR }, p_issuingregion: { val: p_issuingregion, type: oracledb.DB_TYPE_VARCHAR }, p_replacementregion: { val: p_replacementregion, type: oracledb.DB_TYPE_VARCHAR }, p_bondsurety: { val: p_bondsurety, type: oracledb.DB_TYPE_VARCHAR }, p_cargopolicyno: { val: p_cargopolicyno, type: oracledb.DB_TYPE_VARCHAR }, p_cargosurety: { val: p_cargosurety, type: oracledb.DB_TYPE_VARCHAR }, p_user_id: { val: p_user_id, type: oracledb.DB_TYPE_VARCHAR }, P_NOTES: { val: P_NOTES, type: oracledb.DB_TYPE_VARCHAR }, P_FILEIDS: { val: P_FILEIDS, 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 getAllServiceproviders() { 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.GetAllSPs(: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); // Close the cursor after you're done await cursor.close(); return rows; } else { throw new Error('No cursor returned from the stored procedure'); } } catch (err) { console.error('Error fetching users: ', err); return { error: err.message } } finally { } } async getServiceproviderByID(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.GetSPbySPID(: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); 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 { } } }