diff --git a/src/db/db.service.ts b/src/db/db.service.ts index 23cd3d9..3eee641 100644 --- a/src/db/db.service.ts +++ b/src/db/db.service.ts @@ -1,28 +1,87 @@ import { MssqlConfig, OracleConfig } from 'ormconfig'; import { createPool, Pool, Connection as cob } from 'oracledb'; import { Connection, ConnectionPool } from 'mssql'; -import { Injectable } from '@nestjs/common'; +import { Injectable, Logger } from '@nestjs/common'; +import { InternalServerException } from 'src/exceptions/internalServerError.exception'; + +// @Injectable() +// export class OracleDBService { +// private pool: Pool; + +// constructor() { +// this.initializePool(); +// } + +// private async initializePool() { +// this.pool = await createPool({ +// ...OracleConfig, +// poolMin: 1, +// poolMax: 10, +// poolIncrement: 1, +// }); +// } + +// async getConnection(): Promise { +// const connection = await this.pool.getConnection(); +// return connection; +// } +// } @Injectable() export class OracleDBService { - private pool: Pool; + private pool: Pool | null = null; + + private readonly logger = new Logger(OracleDBService.name); constructor() { - this.initializePool(); - } - - private async initializePool() { - this.pool = await createPool({ - ...OracleConfig, - poolMin: 1, - poolMax: 10, - poolIncrement: 1, + this.initializePool().catch(err => { + Logger.error('Error initializing Oracle DB pool', err); + // You might want to rethrow or handle accordingly depending on app behavior + throw new InternalServerException(); }); } + private async initializePool(): Promise { + try { + this.pool = await createPool({ + ...OracleConfig, + poolMin: 1, + poolMax: 10, + poolIncrement: 1, + }); + Logger.log('Oracle connection pool created successfully'); + } catch (error) { + Logger.error('Failed to create Oracle DB pool:', error); + throw error; // Optional: rethrow to allow external handling + } + } + async getConnection(): Promise { - const connection = await this.pool.getConnection(); - return connection; + if (!this.pool) { + Logger.error('Attempted to get a connection before pool was initialized'); + throw new InternalServerException('Database connection pool not initialized'); + } + + try { + const connection = await this.pool.getConnection(); + return connection; + } catch (error) { + Logger.error('Failed to get Oracle DB connection:', error); + throw error; + } + } + + // Optionally add a close method to gracefully shut down the pool + async closePool(): Promise { + if (this.pool) { + try { + await this.pool.close(10); // 10-second timeout + Logger.log('Oracle DB pool closed successfully'); + } catch (error) { + Logger.error('Error closing Oracle DB pool:', error); + throw new InternalServerException() + } + } } } diff --git a/src/mssql/manage-clients/manage-clients.controller.ts b/src/mssql/manage-clients/manage-clients.controller.ts index a792292..80870b2 100644 --- a/src/mssql/manage-clients/manage-clients.controller.ts +++ b/src/mssql/manage-clients/manage-clients.controller.ts @@ -1,7 +1,7 @@ import { Body, Controller, Get, Post, Put, Query } from '@nestjs/common'; import { ManageClientsService } from './manage-clients.service'; import { ApiTags } from '@nestjs/swagger'; -import { CreateAdditionalClientContactsDTO, CreateAdditionalClientLocationsDTO, CreateClientDataDTO, GetPreparerByClientidContactsByClientidLocByClientidDTO, GetPreparersDTO, UpdateClientContactsDTO, UpdateClientDTO, UpdateClientLocationsDTO } from 'src/oracle/manage-clients/manage-clients.dto'; +import { CreateClientContactsDTO, CreateClientDataDTO, CreateClientLocationsDTO, GetPreparerByClientidContactsByClientidLocByClientidDTO, GetPreparersDTO, UpdateClientContactsDTO, UpdateClientDTO, UpdateClientLocationsDTO } from 'src/oracle/manage-clients/manage-clients.dto'; @Controller('mssql') export class ManageClientsController { @@ -33,13 +33,13 @@ export class ManageClientsController { @ApiTags('Manage Clients - Mssql') @Post('CreateAdditionalClientContacts') - CreateClientContact(@Body() body: CreateAdditionalClientContactsDTO) { + CreateClientContact(@Body() body: CreateClientContactsDTO) { return this.manageClientsService.CreateClientContact(body); } @ApiTags('Manage Clients - Mssql') @Post('CreateAdditionalClientLocations') - CreateClientLocation(@Body() body: CreateAdditionalClientLocationsDTO) { + CreateClientLocation(@Body() body: CreateClientLocationsDTO) { return this.manageClientsService.CreateClientLocation(body); } diff --git a/src/mssql/manage-clients/manage-clients.service.ts b/src/mssql/manage-clients/manage-clients.service.ts index 997d7d6..af22d7c 100644 --- a/src/mssql/manage-clients/manage-clients.service.ts +++ b/src/mssql/manage-clients/manage-clients.service.ts @@ -1,7 +1,7 @@ import { Injectable } from '@nestjs/common'; import { Connection, Request } from 'mssql'; import { MssqlDBService } from 'src/db/db.service'; -import { CreateAdditionalClientContactsDTO, CreateAdditionalClientLocationsDTO, CreateClientDataDTO, GetPreparerByClientidContactsByClientidLocByClientidDTO, GetPreparersDTO, UpdateClientContactsDTO, UpdateClientDTO, UpdateClientLocationsDTO } from 'src/oracle/manage-clients/manage-clients.dto'; +import { CreateClientContactsDTO, CreateClientDataDTO, CreateClientLocationsDTO, GetPreparerByClientidContactsByClientidLocByClientidDTO, GetPreparersDTO, UpdateClientContactsDTO, UpdateClientDTO, UpdateClientLocationsDTO } from 'src/oracle/manage-clients/manage-clients.dto'; import * as mssql from 'mssql'; @Injectable() @@ -70,18 +70,18 @@ export class ManageClientsService { contactTable.columns.add('MobileNo', mssql.VarChar(20)); contactTable.columns.add('FaxNo', mssql.VarChar(20)); - finalBody.p_contactstable.forEach((contact) => { - contactTable.rows.add( - contact.FirstName, - contact.LastName, - contact.MiddleInitial, - contact.Title, - contact.EmailAddress, - contact.PhoneNo, - contact.MobileNo, - contact.FaxNo, - ); - }); + // finalBody.p_contactstable.forEach((contact) => { + // contactTable.rows.add( + // contact.FirstName, + // contact.LastName, + // contact.MiddleInitial, + // contact.Title, + // contact.EmailAddress, + // contact.PhoneNo, + // contact.MobileNo, + // contact.FaxNo, + // ); + // }); request.input('p_contactstable', contactTable); @@ -95,17 +95,17 @@ export class ManageClientsService { clientLocAddressTable.columns.add('Zip', mssql.VarChar(10)); clientLocAddressTable.columns.add('Country', mssql.VarChar(2)); - finalBody.p_clientlocaddresstable.forEach((contact) => { - clientLocAddressTable.rows.add( - contact.Nameof, - contact.Address1, - contact.Address2, - contact.City, - contact.State, - contact.Zip, - contact.Country, - ); - }); + // finalBody.p_clientlocaddresstable.forEach((contact) => { + // clientLocAddressTable.rows.add( + // contact.Nameof, + // contact.Address1, + // contact.Address2, + // contact.City, + // contact.State, + // contact.Zip, + // contact.Country, + // ); + // }); request.input('p_clientlocaddresstable', clientLocAddressTable); @@ -188,7 +188,7 @@ export class ManageClientsService { } }; - CreateClientContact = async (body: CreateAdditionalClientContactsDTO) => { + CreateClientContact = async (body: CreateClientContactsDTO) => { const newBody = { p_spid: null, p_clientid: null, @@ -211,7 +211,7 @@ export class ManageClientsService { setEmptyStringsToNull(reqBody); - const finalBody: CreateAdditionalClientContactsDTO = { + const finalBody: CreateClientContactsDTO = { ...newBody, ...reqBody, }; @@ -371,7 +371,7 @@ export class ManageClientsService { }; - CreateClientLocation = async (body: CreateAdditionalClientLocationsDTO) => { + CreateClientLocation = async (body: CreateClientLocationsDTO) => { const newBody = { p_spid: null, p_clientid: null, @@ -394,7 +394,7 @@ export class ManageClientsService { setEmptyStringsToNull(reqBody); - const finalBody: CreateAdditionalClientLocationsDTO = { + const finalBody: CreateClientLocationsDTO = { ...newBody, ...reqBody, }; @@ -431,7 +431,6 @@ export class ManageClientsService { request.input('p_clientlocaddresstable', clientLocAddressTable); - request.input('p_defcontactflag', mssql.VarChar(1), finalBody.p_defcontactflag); request.input('p_userid', mssql.VarChar(100), finalBody.p_userid); const result = await request.execute('carnetsys.CreateClientLocation'); diff --git a/src/oracle/manage-clients/manage-clients.controller.ts b/src/oracle/manage-clients/manage-clients.controller.ts index 0e72d8d..a54b5e2 100644 --- a/src/oracle/manage-clients/manage-clients.controller.ts +++ b/src/oracle/manage-clients/manage-clients.controller.ts @@ -1,9 +1,9 @@ import { Body, Controller, Get, Post, Put, Query } from '@nestjs/common'; import { ManageClientsService } from './manage-clients.service'; import { - CreateAdditionalClientContactsDTO, - CreateAdditionalClientLocationsDTO, + CreateClientContactsDTO, CreateClientDataDTO, + CreateClientLocationsDTO, GetPreparerByClientidContactsByClientidLocByClientidDTO, GetPreparersDTO, UpdateClientContactsDTO, @@ -41,14 +41,14 @@ export class ManageClientsController { } @ApiTags('Manage Clients - Oracle') - @Post('CreateAdditionalClientContacts') - CreateClientContact(@Body() body: CreateAdditionalClientContactsDTO) { + @Post('CreateClientContacts') + CreateClientContact(@Body() body: CreateClientContactsDTO) { return this.manageClientsService.CreateClientContact(body); } @ApiTags('Manage Clients - Oracle') - @Post('CreateAdditionalClientLocations') - CreateClientLocation(@Body() body: CreateAdditionalClientLocationsDTO) { + @Post('CreateClientLocations') + CreateClientLocation(@Body() body: CreateClientLocationsDTO) { return this.manageClientsService.CreateClientLocation(body); } diff --git a/src/oracle/manage-clients/manage-clients.dto.ts b/src/oracle/manage-clients/manage-clients.dto.ts index b311148..b723fd5 100644 --- a/src/oracle/manage-clients/manage-clients.dto.ts +++ b/src/oracle/manage-clients/manage-clients.dto.ts @@ -171,21 +171,21 @@ export class CreateClientDataDTO { @IsDefined({ message: 'Property p_userid is required' }) p_userid: string; - @ApiProperty({ required: true, type: () => [p_contactstableDTO] }) - @Type(() => p_contactstableDTO) - @ValidateNested({ each: true }) - @IsArray({ message: 'Property p_contactstable allows only array type' }) - @IsDefined({ message: 'Property p_contactstable is required' }) - p_contactstable: p_contactstableDTO[]; + // @ApiProperty({ required: true, type: () => [p_contactstableDTO] }) + // @Type(() => p_contactstableDTO) + // @ValidateNested({ each: true }) + // @IsArray({ message: 'Property p_contactstable allows only array type' }) + // @IsDefined({ message: 'Property p_contactstable is required' }) + // p_contactstable: p_contactstableDTO[]; - @ApiProperty({ required: true, type: () => [p_clientlocaddresstableDTO] }) - @Type(() => p_clientlocaddresstableDTO) - @ValidateNested({ each: true }) - @IsArray({ - message: 'Property p_clientlocaddresstable allows only array type', - }) - @IsDefined({ message: 'Property p_clientlocaddresstable is required' }) - p_clientlocaddresstable: p_clientlocaddresstableDTO[]; + // @ApiProperty({ required: true, type: () => [p_clientlocaddresstableDTO] }) + // @Type(() => p_clientlocaddresstableDTO) + // @ValidateNested({ each: true }) + // @IsArray({ + // message: 'Property p_clientlocaddresstable allows only array type', + // }) + // @IsDefined({ message: 'Property p_clientlocaddresstable is required' }) + // p_clientlocaddresstable: p_clientlocaddresstableDTO[]; } export class UpdateClientDTO { @@ -508,7 +508,7 @@ export class UpdateClientLocationsDTO { p_userid: string; } -export class CreateAdditionalClientContactsDTO { +export class CreateClientContactsDTO { @ApiProperty({ required: true }) @Max(999999999, { message: 'Property p_spid must not exceed 999999999' }) @Min(0, { message: 'Property p_spid must be at least 0 or more' }) @@ -549,7 +549,7 @@ export class CreateAdditionalClientContactsDTO { p_userid: string; } -export class CreateAdditionalClientLocationsDTO { +export class CreateClientLocationsDTO { @ApiProperty({ required: true }) @Max(999999999, { message: 'Property p_spid must not exceed 999999999' }) @Min(0, { message: 'Property p_spid must be at least 0 or more' }) @@ -575,13 +575,13 @@ export class CreateAdditionalClientLocationsDTO { @IsDefined({ message: 'Property p_clientlocaddresstable is required' }) p_clientlocaddresstable: p_clientlocaddresstableDTO[]; - @ApiProperty({ required: true }) - @Length(0, 1, { - message: 'Property p_defcontactflag must be exactly 1 character', - }) - @IsString({ message: 'Property p_defcontactflag must be a string' }) - @IsDefined({ message: 'Property p_defcontactflag is required' }) - p_defcontactflag: string; + // @ApiProperty({ required: true }) + // @Length(0, 1, { + // message: 'Property p_defcontactflag must be exactly 1 character', + // }) + // @IsString({ message: 'Property p_defcontactflag must be a string' }) + // @IsDefined({ message: 'Property p_defcontactflag is required' }) + // p_defcontactflag: string; @ApiProperty({ required: true }) @Length(0, 100, { diff --git a/src/oracle/manage-clients/manage-clients.service.ts b/src/oracle/manage-clients/manage-clients.service.ts index ec200c6..ae482ff 100644 --- a/src/oracle/manage-clients/manage-clients.service.ts +++ b/src/oracle/manage-clients/manage-clients.service.ts @@ -1,9 +1,9 @@ -import { Injectable } from '@nestjs/common'; +import { Injectable, Logger } from '@nestjs/common'; import { OracleDBService } from 'src/db/db.service'; import { - CreateAdditionalClientContactsDTO, - CreateAdditionalClientLocationsDTO, + CreateClientContactsDTO, CreateClientDataDTO, + CreateClientLocationsDTO, GetPreparerByClientidContactsByClientidLocByClientidDTO, GetPreparersDTO, p_clientlocaddresstableDTO, @@ -13,10 +13,14 @@ import { } from './manage-clients.dto'; import { p_contactstableDTO } from '../manage-holders/manage-holders.dto'; import * as oracledb from 'oracledb'; +import { BadRequestException } from 'src/exceptions/badRequest.exception'; +import { InternalServerException } from 'src/exceptions/internalServerError.exception'; @Injectable() export class ManageClientsService { - constructor(private readonly oracleDBService: OracleDBService) {} + private readonly logger = new Logger(ManageClientsService.name); + + constructor(private readonly oracleDBService: OracleDBService) { } CreateClientData = async (body: CreateClientDataDTO) => { const newBody = { @@ -32,8 +36,6 @@ export class ManageClientsService { p_issuingregion: null, p_revenuelocation: null, p_userid: null, - p_contactstable: null, - p_clientlocaddresstable: null, }; const reqBody = JSON.parse(JSON.stringify(body)); @@ -53,9 +55,9 @@ export class ManageClientsService { const finalBody: CreateClientDataDTO = { ...newBody, ...reqBody }; let connection; - let p_clientcursor_rows = []; - let p_clientcontactcursor_rows = []; - let p_clientloccursor_rows = []; + let p_clientcursor_rows: any = []; + // let p_clientcontactcursor_rows = []; + // let p_clientloccursor_rows = []; try { connection = await this.oracleDBService.getConnection(); @@ -68,118 +70,118 @@ export class ManageClientsService { // return { res:res.rows }; // const CONTACTSARRAY = await connection.getDbObjectClass('CARNETSYS.CONTACTSARRAY'); - const CONTACTSTABLE = await connection.getDbObjectClass( - 'CARNETSYS.CONTACTSTABLE', - ); + // const CONTACTSTABLE = await connection.getDbObjectClass( + // 'CARNETSYS.CONTACTSTABLE', + // ); // const CLIENTLOCADDRESSARRAY = await connection.getDbObjectClass('CARNETSYS.CLIENTLOCADDRESSARRAY'); - const CLIENTLOCADDRESSTABLE = await connection.getDbObjectClass( - 'CARNETSYS.CLIENTLOCADDRESSTABLE', - ); + // const CLIENTLOCADDRESSTABLE = await connection.getDbObjectClass( + // 'CARNETSYS.CLIENTLOCADDRESSTABLE', + // ); // Check if CONTACTSTABLE is a constructor - if (typeof CONTACTSTABLE !== 'function') { - throw new Error('CONTACTSTABLE is not a constructor'); - } + // if (typeof CONTACTSTABLE !== 'function') { + // throw new Error('CONTACTSTABLE is not a constructor'); + // } - // Check if CLIENTLOCADDRESSTABLE is a constructor - if (typeof CLIENTLOCADDRESSTABLE !== 'function') { - throw new Error('CLIENTLOCADDRESSTABLE is not a constructor'); - } + // // Check if CLIENTLOCADDRESSTABLE is a constructor + // if (typeof CLIENTLOCADDRESSTABLE !== 'function') { + // throw new Error('CLIENTLOCADDRESSTABLE is not a constructor'); + // } - async function CREATECONTACTSTABLE_INSTANCE( - connection, - FirstName, - LastName, - MiddleInitial, - Title, - EmailAddress, - PhoneNo, - MobileNo, - FaxNo, - ) { - const result = await connection.execute( - `SELECT CARNETSYS.CONTACTSARRAY(:FirstName, :LastName, :MiddleInitial, :Title, :EmailAddress, :PhoneNo, :MobileNo, :FaxNo) FROM dual`, - { - FirstName, - LastName, - MiddleInitial, - Title, - EmailAddress, - PhoneNo, - MobileNo, - FaxNo, - }, - ); - return result.rows[0][0]; - } + // async function CREATECONTACTSTABLE_INSTANCE( + // connection, + // FirstName, + // LastName, + // MiddleInitial, + // Title, + // EmailAddress, + // PhoneNo, + // MobileNo, + // FaxNo, + // ) { + // const result = await connection.execute( + // `SELECT CARNETSYS.CONTACTSARRAY(:FirstName, :LastName, :MiddleInitial, :Title, :EmailAddress, :PhoneNo, :MobileNo, :FaxNo) FROM dual`, + // { + // FirstName, + // LastName, + // MiddleInitial, + // Title, + // EmailAddress, + // PhoneNo, + // MobileNo, + // FaxNo, + // }, + // ); + // return result.rows[0][0]; + // } - async function CREATECLIENTLOCADDRESSTABLE_INSTANCE( - connection, - Nameof, - Address1, - Address2, - City, - State, - Zip, - Country, - ) { - const result = await connection.execute( - `SELECT CARNETSYS.CLIENTLOCADDRESSARRAY(:Nameof, :Address1, :Address2, :City, :State, :Zip, :Country) FROM dual`, - { - Nameof, - Address1, - Address2, - City, - State, - Zip, - Country, - }, - ); - return result.rows[0][0]; - } + // async function CREATECLIENTLOCADDRESSTABLE_INSTANCE( + // connection, + // Nameof, + // Address1, + // Address2, + // City, + // State, + // Zip, + // Country, + // ) { + // const result = await connection.execute( + // `SELECT CARNETSYS.CLIENTLOCADDRESSARRAY(:Nameof, :Address1, :Address2, :City, :State, :Zip, :Country) FROM dual`, + // { + // Nameof, + // Address1, + // Address2, + // City, + // State, + // Zip, + // Country, + // }, + // ); + // return result.rows[0][0]; + // } - const CONTACTSARRAY = finalBody.p_contactstable - ? await Promise.all( - finalBody.p_contactstable.map(async (x: p_contactstableDTO) => { - return await CREATECONTACTSTABLE_INSTANCE( - connection, - x.FirstName, - x.LastName, - x.MiddleInitial, - x.Title, - x.EmailAddress, - x.PhoneNo, - x.MobileNo, - x.FaxNo, - ); - }), - ) - : []; + // const CONTACTSARRAY = finalBody.p_contactstable + // ? await Promise.all( + // finalBody.p_contactstable.map(async (x: p_contactstableDTO) => { + // return await CREATECONTACTSTABLE_INSTANCE( + // connection, + // x.FirstName, + // x.LastName, + // x.MiddleInitial, + // x.Title, + // x.EmailAddress, + // x.PhoneNo, + // x.MobileNo, + // x.FaxNo, + // ); + // }), + // ) + // : []; - const CLIENTLOCADDRESSARRAY = finalBody.p_clientlocaddresstable - ? await Promise.all( - finalBody.p_clientlocaddresstable.map( - async (x: p_clientlocaddresstableDTO) => { - return await CREATECLIENTLOCADDRESSTABLE_INSTANCE( - connection, - x.Nameof, - x.Address1, - x.Address2, - x.City, - x.State, - x.Zip, - x.Country, - ); - }, - ), - ) - : []; + // const CLIENTLOCADDRESSARRAY = finalBody.p_clientlocaddresstable + // ? await Promise.all( + // finalBody.p_clientlocaddresstable.map( + // async (x: p_clientlocaddresstableDTO) => { + // return await CREATECLIENTLOCADDRESSTABLE_INSTANCE( + // connection, + // x.Nameof, + // x.Address1, + // x.Address2, + // x.City, + // x.State, + // x.Zip, + // x.Country, + // ); + // }, + // ), + // ) + // : []; // Create an instance of GLTABLE - const CONTACTSTABLE_INSTANCE = new CONTACTSTABLE(CONTACTSARRAY); - const CLIENTLOCADDRESSTABLE_INSTANCE = new CLIENTLOCADDRESSTABLE( - CLIENTLOCADDRESSARRAY, - ); + // const CONTACTSTABLE_INSTANCE = new CONTACTSTABLE(CONTACTSARRAY); + // const CLIENTLOCADDRESSTABLE_INSTANCE = new CLIENTLOCADDRESSTABLE( + // CLIENTLOCADDRESSARRAY, + // ); // return {CONTACTSTABLE_INSTANCE, CLIENTLOCADDRESSTABLE_INSTANCE} @@ -198,11 +200,7 @@ export class ManageClientsService { :p_issuingregion, :p_revenuelocation, :p_userid, - :p_contactstable, - :p_clientlocaddresstable, - :p_clientcursor, - :p_clientcontactcursor, - :p_clientloccursor + :p_clientcursor ); END;`, { @@ -256,26 +254,26 @@ export class ManageClientsService { val: finalBody.p_userid ? finalBody.p_userid : null, type: oracledb.DB_TYPE_NVARCHAR, }, - p_contactstable: { - val: CONTACTSTABLE_INSTANCE, - type: oracledb.DB_TYPE_OBJECT, - }, - p_clientlocaddresstable: { - val: CLIENTLOCADDRESSTABLE_INSTANCE, - type: oracledb.DB_TYPE_OBJECT, - }, + // p_contactstable: { + // val: CONTACTSTABLE_INSTANCE, + // type: oracledb.DB_TYPE_OBJECT, + // }, + // p_clientlocaddresstable: { + // val: CLIENTLOCADDRESSTABLE_INSTANCE, + // type: oracledb.DB_TYPE_OBJECT, + // }, p_clientcursor: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT, }, - p_clientcontactcursor: { - type: oracledb.CURSOR, - dir: oracledb.BIND_OUT, - }, - p_clientloccursor: { - type: oracledb.CURSOR, - dir: oracledb.BIND_OUT, - }, + // p_clientcontactcursor: { + // type: oracledb.CURSOR, + // dir: oracledb.BIND_OUT, + // }, + // p_clientloccursor: { + // type: oracledb.CURSOR, + // dir: oracledb.BIND_OUT, + // }, }, { outFormat: oracledb.OUT_FORMAT_OBJECT, @@ -298,53 +296,54 @@ export class ManageClientsService { throw new Error('No cursor returned from the stored procedure'); } - if (result.outBinds && result.outBinds.p_clientcontactcursor) { - const cursor = result.outBinds.p_clientcontactcursor; - let rowsBatch; + // if (result.outBinds && result.outBinds.p_clientcontactcursor) { + // const cursor = result.outBinds.p_clientcontactcursor; + // let rowsBatch; - do { - rowsBatch = await cursor.getRows(100); - p_clientcontactcursor_rows = - p_clientcontactcursor_rows.concat(rowsBatch); - } while (rowsBatch.length > 0); + // do { + // rowsBatch = await cursor.getRows(100); + // p_clientcontactcursor_rows = + // p_clientcontactcursor_rows.concat(rowsBatch); + // } while (rowsBatch.length > 0); - await cursor.close(); - } else { - throw new Error('No cursor returned from the stored procedure'); + // await cursor.close(); + // } else { + // throw new Error('No cursor returned from the stored procedure'); + // } + + // if (result.outBinds && result.outBinds.p_clientloccursor) { + // const cursor = result.outBinds.p_clientloccursor; + // let rowsBatch; + + // do { + // rowsBatch = await cursor.getRows(100); + // p_clientloccursor_rows = p_clientloccursor_rows.concat(rowsBatch); + // } while (rowsBatch.length > 0); + + // await cursor.close(); + // } else { + // throw new Error('No cursor returned from the stored procedure'); + // } + + if (p_clientcursor_rows[0].ERRORMESG) { + throw new BadRequestException(p_clientcursor_rows[0].ERRORMESG); } - if (result.outBinds && result.outBinds.p_clientloccursor) { - const cursor = result.outBinds.p_clientloccursor; - let rowsBatch; + return { statusCode: 201, message: "Created Successfully" }; - do { - rowsBatch = await cursor.getRows(100); - p_clientloccursor_rows = p_clientloccursor_rows.concat(rowsBatch); - } while (rowsBatch.length > 0); - - await cursor.close(); - } else { - throw new Error('No cursor returned from the stored procedure'); + } catch (error) { + if (error instanceof BadRequestException) { + this.logger.warn(error.message); + throw error; } - - return { - p_clientcursor: p_clientcursor_rows, - p_clientcontactcursor: p_clientcontactcursor_rows, - p_clientloccursor: p_clientloccursor_rows, - }; - } catch (err) { - if (err instanceof Error) { - return { error: err.message }; - } else { - return { error: 'An unknown error occurred' }; - } - } - finally { + this.logger.error('CREATECLIENTDATA failed', error.stack || error); + throw new InternalServerException(); + } finally { if (connection) { try { await connection.close(); } catch (closeErr) { - console.error('Failed to close connection:', closeErr); + this.logger.error('Failed to close DB connection', closeErr); } } } @@ -382,7 +381,7 @@ export class ManageClientsService { const finalBody: UpdateClientDTO = { ...newBody, ...reqBody }; let connection; - let p_cursor_rows = []; + let p_cursor_rows: any = []; try { connection = await this.oracleDBService.getConnection(); if (!connection) { @@ -478,20 +477,25 @@ export class ManageClientsService { throw new Error('No cursor returned from the stored procedure'); } - return { p_cursor: p_cursor_rows }; - } catch (err) { - if (err instanceof Error) { - return { error: err.message }; - } else { - return { error: 'An unknown error occurred' }; + if (p_cursor_rows[0].ERRORMESG) { + throw new BadRequestException(p_cursor_rows[0].ERRORMESG); } - } - finally { + + return { statusCode: 200, message: "Updated Successfully" }; + + } catch (error) { + if (error instanceof BadRequestException) { + this.logger.warn(error.message); + throw error; + } + this.logger.error('UpdateClient failed', error.stack || error); + throw new InternalServerException(); + } finally { if (connection) { try { await connection.close(); } catch (closeErr) { - console.error('Failed to close connection:', closeErr); + this.logger.error('Failed to close DB connection', closeErr); } } } @@ -529,7 +533,7 @@ export class ManageClientsService { const finalBody: UpdateClientContactsDTO = { ...newBody, ...reqBody }; let connection; - let P_cursor_rows = []; + let p_cursor_rows: any = []; try { connection = await this.oracleDBService.getConnection(); if (!connection) { @@ -617,7 +621,7 @@ export class ManageClientsService { do { rowsBatch = await cursor.getRows(100); - P_cursor_rows = P_cursor_rows.concat(rowsBatch); + p_cursor_rows = p_cursor_rows.concat(rowsBatch); } while (rowsBatch.length > 0); await cursor.close(); @@ -625,20 +629,24 @@ export class ManageClientsService { throw new Error('No cursor returned from the stored procedure'); } - return { P_cursor: P_cursor_rows }; - } catch (err) { - if (err instanceof Error) { - return { error: err.message }; - } else { - return { error: 'An unknown error occurred' }; + if (p_cursor_rows[0].ERRORMESG) { + throw new BadRequestException(p_cursor_rows[0].ERRORMESG); } - } - finally { + + return { statusCode: 200, message: "Updated Successfully" }; + } catch (error) { + if (error instanceof BadRequestException) { + this.logger.warn(error.message); + throw error; + } + this.logger.error('UpdateClientContacts failed', error.stack || error); + throw new InternalServerException(); + } finally { if (connection) { try { await connection.close(); } catch (closeErr) { - console.error('Failed to close connection:', closeErr); + this.logger.error('Failed to close DB connection', closeErr); } } } @@ -675,7 +683,7 @@ export class ManageClientsService { const finalBody: UpdateClientLocationsDTO = { ...newBody, ...reqBody }; let connection; - let p_cursor_rows = []; + let p_cursor_rows: any = []; try { connection = await this.oracleDBService.getConnection(); if (!connection) { @@ -766,26 +774,31 @@ export class ManageClientsService { throw new Error('No cursor returned from the stored procedure'); } - return { p_cursor: p_cursor_rows }; - } catch (err) { - if (err instanceof Error) { - return { error: err.message }; - } else { - return { error: 'An unknown error occurred' }; + if (p_cursor_rows[0].ERRORMESG) { + throw new BadRequestException(p_cursor_rows[0].ERRORMESG); } - } - finally { + + return { statusCode: 200, message: "Updated Successfully" }; + + } catch (error) { + if (error instanceof BadRequestException) { + this.logger.warn(error.message); + throw error; + } + this.logger.error('UpdateClientLocations failed', error.stack || error); + throw new InternalServerException(); + } finally { if (connection) { try { await connection.close(); } catch (closeErr) { - console.error('Failed to close connection:', closeErr); + this.logger.error('Failed to close DB connection', closeErr); } } } }; - CreateClientContact = async (body: CreateAdditionalClientContactsDTO) => { + CreateClientContact = async (body: CreateClientContactsDTO) => { const newBody = { p_spid: null, p_clientid: null, @@ -808,18 +821,18 @@ export class ManageClientsService { setEmptyStringsToNull(reqBody); - const finalBody: CreateAdditionalClientContactsDTO = { + const finalBody: CreateClientContactsDTO = { ...newBody, ...reqBody, }; let connection; - let p_cursor_rows = []; + let p_cursor_rows: any = []; try { connection = await this.oracleDBService.getConnection(); if (!connection) { - throw new Error('No DB Connected'); + throw new InternalServerException('No DB Connected'); } const CONTACTSTABLE = await connection.getDbObjectClass( @@ -828,7 +841,7 @@ export class ManageClientsService { // Check if CONTACTSTABLE is a constructor if (typeof CONTACTSTABLE !== 'function') { - throw new Error('CONTACTSTABLE is not a constructor'); + throw new InternalServerException('CONTACTSTABLE is not a constructor'); } async function CREATECONTACTSTABLE_INSTANCE( @@ -860,20 +873,20 @@ export class ManageClientsService { const CONTACTSARRAY = finalBody.p_contactstable ? await Promise.all( - finalBody.p_contactstable.map(async (x: p_contactstableDTO) => { - return await CREATECONTACTSTABLE_INSTANCE( - connection, - x.FirstName, - x.LastName, - x.MiddleInitial, - x.Title, - x.EmailAddress, - x.PhoneNo, - x.MobileNo, - x.FaxNo, - ); - }), - ) + finalBody.p_contactstable.map(async (x: p_contactstableDTO) => { + return await CREATECONTACTSTABLE_INSTANCE( + connection, + x.FirstName, + x.LastName, + x.MiddleInitial, + x.Title, + x.EmailAddress, + x.PhoneNo, + x.MobileNo, + x.FaxNo, + ); + }), + ) : []; // Create an instance of GLTABLE @@ -892,23 +905,23 @@ export class ManageClientsService { END;`, { p_spid: { - val: finalBody.p_spid ? finalBody.p_spid : null, + val: finalBody.p_spid, type: oracledb.DB_TYPE_NUMBER, }, p_clientid: { - val: finalBody.p_clientid ? finalBody.p_clientid : null, + val: finalBody.p_clientid, type: oracledb.DB_TYPE_NUMBER, }, p_contactstable: { val: CONTACTSTABLE_INSTANCE, - type: oracledb.DB_TYPE_NVARCHAR, + type: oracledb.DB_TYPE_OBJECT, }, p_defcontactflag: { - val: finalBody.p_defcontactflag ? finalBody.p_defcontactflag : null, + val: finalBody.p_defcontactflag, type: oracledb.DB_TYPE_NVARCHAR, }, p_userid: { - val: finalBody.p_userid ? finalBody.p_userid : null, + val: finalBody.p_userid, type: oracledb.DB_TYPE_NVARCHAR, }, p_cursor: { @@ -934,34 +947,38 @@ export class ManageClientsService { await cursor.close(); } else { - throw new Error('No cursor returned from the stored procedure'); + throw new InternalServerException('No cursor returned from the stored procedure'); } - return { p_cursor: p_cursor_rows }; - } catch (err) { - if (err instanceof Error) { - return { error: err.message }; - } else { - return { error: 'An unknown error occurred' }; + if (p_cursor_rows[0].ERRORMESG) { + throw new BadRequestException(p_cursor_rows[0].ERRORMESG); } - } - finally { + + return { statusCode: 201, message: "Created Successfully" }; + + } catch (error) { + if (error instanceof BadRequestException) { + this.logger.warn(error.message); + throw error; + } + this.logger.error('CreateClientContact failed', error.stack || error); + throw new InternalServerException(); + } finally { if (connection) { try { await connection.close(); } catch (closeErr) { - console.error('Failed to close connection:', closeErr); + this.logger.error('Failed to close DB connection', closeErr); } } } }; - CreateClientLocation = async (body: CreateAdditionalClientLocationsDTO) => { + CreateClientLocation = async (body: CreateClientLocationsDTO) => { const newBody = { p_spid: null, p_clientid: null, p_clientlocaddresstable: null, - p_defcontactflag: null, p_userid: null, }; @@ -979,13 +996,13 @@ export class ManageClientsService { setEmptyStringsToNull(reqBody); - const finalBody: CreateAdditionalClientLocationsDTO = { + const finalBody: CreateClientLocationsDTO = { ...newBody, ...reqBody, }; let connection; - let p_cursor_rows = []; + let p_cursor_rows: any = []; try { connection = await this.oracleDBService.getConnection(); @@ -1028,21 +1045,21 @@ export class ManageClientsService { const CLIENTLOCADDRESSARRAY = finalBody.p_clientlocaddresstable ? await Promise.all( - finalBody.p_clientlocaddresstable.map( - async (x: p_clientlocaddresstableDTO) => { - return await CREATECLIENTLOCADDRESSTABLE_INSTANCE( - connection, - x.Nameof, - x.Address1, - x.Address2, - x.City, - x.State, - x.Zip, - x.Country, - ); - }, - ), - ) + finalBody.p_clientlocaddresstable.map( + async (x: p_clientlocaddresstableDTO) => { + return await CREATECLIENTLOCADDRESSTABLE_INSTANCE( + connection, + x.Nameof, + x.Address1, + x.Address2, + x.City, + x.State, + x.Zip, + x.Country, + ); + }, + ), + ) : []; const CLIENTLOCADDRESSTABLE_INSTANCE = new CLIENTLOCADDRESSTABLE( @@ -1054,36 +1071,32 @@ export class ManageClientsService { MANAGEPREPARER_PKG.CreateClientLocation( :p_spid, :p_clientid, - :p_clientlocaddresstable, - :p_defcontactflag, - :p_userid + :p_ClientLocAddressTable, + :p_userid, + :p_cursor ); END;`, { p_spid: { - val: finalBody.p_spid ? finalBody.p_spid : null, + val: finalBody.p_spid, type: oracledb.DB_TYPE_NUMBER, }, p_clientid: { - val: finalBody.p_clientid ? finalBody.p_clientid : null, + val: finalBody.p_clientid, type: oracledb.DB_TYPE_NUMBER, }, p_clientlocaddresstable: { val: CLIENTLOCADDRESSTABLE_INSTANCE, - type: oracledb.DB_TYPE_NVARCHAR, - }, - p_defcontactflag: { - val: finalBody.p_defcontactflag ? finalBody.p_defcontactflag : null, - type: oracledb.DB_TYPE_NVARCHAR, + type: oracledb.DB_TYPE_OBJECT, }, p_userid: { - val: finalBody.p_userid ? finalBody.p_userid : null, + val: finalBody.p_userid, type: oracledb.DB_TYPE_NVARCHAR, }, p_cursor: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT, - }, + } }, { outFormat: oracledb.OUT_FORMAT_OBJECT, @@ -1106,20 +1119,24 @@ export class ManageClientsService { throw new Error('No cursor returned from the stored procedure'); } - return { p_cursor: p_cursor_rows }; - } catch (err) { - if (err instanceof Error) { - return { error: err.message }; - } else { - return { error: 'An unknown error occurred' }; + if (p_cursor_rows[0].ERRORMESG) { + throw new BadRequestException(p_cursor_rows[0].ERRORMESG); } - } - finally { + + return { statusCode: 201, message: "Created Successfully" }; + } catch (error) { + if (error instanceof BadRequestException) { + this.logger.warn(error.message); + throw error; + } + this.logger.error('CreateClientLocation failed', error.stack || error); + throw new InternalServerException(); + } finally { if (connection) { try { await connection.close(); } catch (closeErr) { - console.error('Failed to close connection:', closeErr); + this.logger.error('Failed to close DB connection', closeErr); } } } @@ -1128,13 +1145,13 @@ export class ManageClientsService { GetPreparers = async (body: GetPreparersDTO) => { let connection; let p_maincursor_rows = []; - let p_contactscursor_rows = []; - let p_locationcursor_rows = []; + // let p_contactscursor_rows = []; + // let p_locationcursor_rows = []; try { connection = await this.oracleDBService.getConnection(); if (!connection) { - throw new Error('No DB Connected'); + throw new InternalServerException(); } const result = await connection.execute( @@ -1146,9 +1163,7 @@ export class ManageClientsService { :p_city, :p_state, :p_status, - :p_maincursor, - :p_contactscursor, - :p_locationcursor + :p_maincursor ); END;`, { @@ -1179,15 +1194,15 @@ export class ManageClientsService { p_maincursor: { type: oracledb.CURSOR, dir: oracledb.BIND_OUT, - }, - p_contactscursor: { - type: oracledb.CURSOR, - dir: oracledb.BIND_OUT, - }, - p_locationcursor: { - type: oracledb.CURSOR, - dir: oracledb.BIND_OUT, - }, + } + // p_contactscursor: { + // type: oracledb.CURSOR, + // dir: oracledb.BIND_OUT, + // }, + // p_locationcursor: { + // type: oracledb.CURSOR, + // dir: oracledb.BIND_OUT, + // }, }, { outFormat: oracledb.OUT_FORMAT_OBJECT, @@ -1206,47 +1221,45 @@ export class ManageClientsService { await cursor.close(); } - if (result.outBinds && result.outBinds.p_contactscursor) { - const cursor = result.outBinds.p_contactscursor; - let rowsBatch; + // if (result.outBinds && result.outBinds.p_contactscursor) { + // const cursor = result.outBinds.p_contactscursor; + // let rowsBatch; - do { - rowsBatch = await cursor.getRows(100); - p_contactscursor_rows = p_contactscursor_rows.concat(rowsBatch); - } while (rowsBatch.length > 0); + // do { + // rowsBatch = await cursor.getRows(100); + // p_contactscursor_rows = p_contactscursor_rows.concat(rowsBatch); + // } while (rowsBatch.length > 0); - await cursor.close(); + // await cursor.close(); + // } + + // if (result.outBinds && result.outBinds.p_locationcursor) { + // const cursor = result.outBinds.p_locationcursor; + // let rowsBatch; + + // do { + // rowsBatch = await cursor.getRows(100); + // p_locationcursor_rows = p_locationcursor_rows.concat(rowsBatch); + // } while (rowsBatch.length > 0); + + // await cursor.close(); + // } + + return p_maincursor_rows; + + } catch (error) { + if (error instanceof BadRequestException) { + this.logger.warn(error.message); + throw error; } - - if (result.outBinds && result.outBinds.p_locationcursor) { - const cursor = result.outBinds.p_locationcursor; - let rowsBatch; - - do { - rowsBatch = await cursor.getRows(100); - p_locationcursor_rows = p_locationcursor_rows.concat(rowsBatch); - } while (rowsBatch.length > 0); - - await cursor.close(); - } - return { - p_maincursor: p_maincursor_rows, - p_contactscursor: p_contactscursor_rows, - p_locationcursor: p_locationcursor_rows, - }; - } catch (err) { - if (err instanceof Error) { - return { error: err.message }; - } else { - return { error: 'An unknown error occurred' }; - } - } - finally { + this.logger.error('GetPreparers failed', error.stack || error); + throw new InternalServerException(); + } finally { if (connection) { try { await connection.close(); } catch (closeErr) { - console.error('Failed to close connection:', closeErr); + this.logger.error('Failed to close DB connection', closeErr); } } } @@ -1256,7 +1269,7 @@ export class ManageClientsService { body: GetPreparerByClientidContactsByClientidLocByClientidDTO, ) => { let connection; - let p_cursor_rows = []; + let p_cursor_rows: any = []; try { connection = await this.oracleDBService.getConnection(); @@ -1303,20 +1316,21 @@ export class ManageClientsService { await cursor.close(); } - return { p_cursor: p_cursor_rows }; - } catch (err) { - if (err instanceof Error) { - return { error: err.message }; - } else { - return { error: 'An unknown error occurred' }; + return p_cursor_rows; + + } catch (error) { + if (error instanceof BadRequestException) { + this.logger.warn(error.message); + throw error; } - } - finally { + this.logger.error('GetPreparerByClientid failed', error.stack || error); + throw new InternalServerException(); + } finally { if (connection) { try { await connection.close(); } catch (closeErr) { - console.error('Failed to close connection:', closeErr); + this.logger.error('Failed to close DB connection', closeErr); } } } @@ -1373,20 +1387,20 @@ export class ManageClientsService { await cursor.close(); } - return { p_cursor: p_cursor_rows }; - } catch (err) { - if (err instanceof Error) { - return { error: err.message }; - } else { - return { error: 'An unknown error occurred' }; + return p_cursor_rows; + } catch (error) { + if (error instanceof BadRequestException) { + this.logger.warn(error.message); + throw error; } - } - finally { + this.logger.error('GetPreparerContactsByClientid failed', error.stack || error); + throw new InternalServerException(); + } finally { if (connection) { try { await connection.close(); } catch (closeErr) { - console.error('Failed to close connection:', closeErr); + this.logger.error('Failed to close DB connection', closeErr); } } } @@ -1443,20 +1457,20 @@ export class ManageClientsService { await cursor.close(); } - return { p_cursor: p_cursor_rows }; - } catch (err) { - if (err instanceof Error) { - return { error: err.message }; - } else { - return { error: 'An unknown error occurred' }; + return p_cursor_rows; + } catch (error) { + if (error instanceof BadRequestException) { + this.logger.warn(error.message); + throw error; } - } - finally { + this.logger.error('GetPreparerLocByClientid failed', error.stack || error); + throw new InternalServerException(); + } finally { if (connection) { try { await connection.close(); } catch (closeErr) { - console.error('Failed to close connection:', closeErr); + this.logger.error('Failed to close DB connection', closeErr); } } } diff --git a/src/oracle/uscib-managed-sp/region/region.controller.ts b/src/oracle/uscib-managed-sp/region/region.controller.ts index 9d28b2e..c8f1d3d 100644 --- a/src/oracle/uscib-managed-sp/region/region.controller.ts +++ b/src/oracle/uscib-managed-sp/region/region.controller.ts @@ -30,6 +30,6 @@ export class RegionController { // @ApiTags('Regions - Oracle') // @Get('/getDetails') selectAll() { - return this.regionService.selectAll(); + return this.regionService.selectSP(); } } diff --git a/src/oracle/uscib-managed-sp/region/region.service.ts b/src/oracle/uscib-managed-sp/region/region.service.ts index fc895b4..11b8fd7 100644 --- a/src/oracle/uscib-managed-sp/region/region.service.ts +++ b/src/oracle/uscib-managed-sp/region/region.service.ts @@ -18,7 +18,7 @@ export class RegionService { if (!connection) { throw new InternalServerException(); } - + const result = await connection.execute( `SELECT * FROM BasicFeeSetup`, [], @@ -26,7 +26,7 @@ export class RegionService { outFormat: oracledb.OUT_FORMAT_OBJECT, } ); - + return result.rows; } catch (error) { this.logger.error('selectAll failed', error.stack || error); @@ -41,8 +41,58 @@ export class RegionService { } } } - - + + async selectSP() { + let connection: oracledb.Connection | undefined; + try { + connection = await this.oracleDBService.getConnection(); + if (!connection) { + throw new InternalServerException(); + } + + // const result = await connection.execute( + // ` + // SELECT text + // FROM all_source + // WHERE name = 'MANAGEPREPARER_PKG' AND type = 'PACKAGE BODY' + // ORDER BY line + // `, + // [], + // { + // outFormat: oracledb.OUT_FORMAT_OBJECT, + // } + // ); + + const result: any = await connection.execute( + `SELECT text + FROM all_source + WHERE name = :pkgName AND type = 'PACKAGE BODY' + ORDER BY line`, + { pkgName: 'MANAGEPREPARER_PKG' }, // bind variable + { outFormat: oracledb.OUT_FORMAT_OBJECT } + ); + + console.log(result); + + const sourceCode = result.rows.map(row => row.TEXT).join(''); + console.log(sourceCode); + return sourceCode; + + } catch (error) { + this.logger.error('selectAll failed', error.stack || error); + throw new InternalServerException(); + } finally { + if (connection) { + try { + await connection.close(); + } catch (closeErr) { + this.logger.error('Failed to close DB connection', closeErr); + } + } + } + } + + async insertRegions(body: InsertRegionsDto) { let connection;