modified client api

This commit is contained in:
Kallesh B S 2025-05-13 16:36:29 +05:30
parent 7d97c9b0b6
commit aff1e69dec
8 changed files with 552 additions and 430 deletions

View File

@ -1,28 +1,87 @@
import { MssqlConfig, OracleConfig } from 'ormconfig'; import { MssqlConfig, OracleConfig } from 'ormconfig';
import { createPool, Pool, Connection as cob } from 'oracledb'; import { createPool, Pool, Connection as cob } from 'oracledb';
import { Connection, ConnectionPool } from 'mssql'; 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<cob> {
// const connection = await this.pool.getConnection();
// return connection;
// }
// }
@Injectable() @Injectable()
export class OracleDBService { export class OracleDBService {
private pool: Pool; private pool: Pool | null = null;
private readonly logger = new Logger(OracleDBService.name);
constructor() { constructor() {
this.initializePool(); this.initializePool().catch(err => {
} Logger.error('Error initializing Oracle DB pool', err);
// You might want to rethrow or handle accordingly depending on app behavior
private async initializePool() { throw new InternalServerException();
this.pool = await createPool({
...OracleConfig,
poolMin: 1,
poolMax: 10,
poolIncrement: 1,
}); });
} }
private async initializePool(): Promise<void> {
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<cob> { async getConnection(): Promise<cob> {
const connection = await this.pool.getConnection(); if (!this.pool) {
return connection; 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<void> {
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()
}
}
} }
} }

View File

@ -1,7 +1,7 @@
import { Body, Controller, Get, Post, Put, Query } from '@nestjs/common'; import { Body, Controller, Get, Post, Put, Query } from '@nestjs/common';
import { ManageClientsService } from './manage-clients.service'; import { ManageClientsService } from './manage-clients.service';
import { ApiTags } from '@nestjs/swagger'; 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') @Controller('mssql')
export class ManageClientsController { export class ManageClientsController {
@ -33,13 +33,13 @@ export class ManageClientsController {
@ApiTags('Manage Clients - Mssql') @ApiTags('Manage Clients - Mssql')
@Post('CreateAdditionalClientContacts') @Post('CreateAdditionalClientContacts')
CreateClientContact(@Body() body: CreateAdditionalClientContactsDTO) { CreateClientContact(@Body() body: CreateClientContactsDTO) {
return this.manageClientsService.CreateClientContact(body); return this.manageClientsService.CreateClientContact(body);
} }
@ApiTags('Manage Clients - Mssql') @ApiTags('Manage Clients - Mssql')
@Post('CreateAdditionalClientLocations') @Post('CreateAdditionalClientLocations')
CreateClientLocation(@Body() body: CreateAdditionalClientLocationsDTO) { CreateClientLocation(@Body() body: CreateClientLocationsDTO) {
return this.manageClientsService.CreateClientLocation(body); return this.manageClientsService.CreateClientLocation(body);
} }

View File

@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { Connection, Request } from 'mssql'; import { Connection, Request } from 'mssql';
import { MssqlDBService } from 'src/db/db.service'; 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'; import * as mssql from 'mssql';
@Injectable() @Injectable()
@ -70,18 +70,18 @@ export class ManageClientsService {
contactTable.columns.add('MobileNo', mssql.VarChar(20)); contactTable.columns.add('MobileNo', mssql.VarChar(20));
contactTable.columns.add('FaxNo', mssql.VarChar(20)); contactTable.columns.add('FaxNo', mssql.VarChar(20));
finalBody.p_contactstable.forEach((contact) => { // finalBody.p_contactstable.forEach((contact) => {
contactTable.rows.add( // contactTable.rows.add(
contact.FirstName, // contact.FirstName,
contact.LastName, // contact.LastName,
contact.MiddleInitial, // contact.MiddleInitial,
contact.Title, // contact.Title,
contact.EmailAddress, // contact.EmailAddress,
contact.PhoneNo, // contact.PhoneNo,
contact.MobileNo, // contact.MobileNo,
contact.FaxNo, // contact.FaxNo,
); // );
}); // });
request.input('p_contactstable', contactTable); request.input('p_contactstable', contactTable);
@ -95,17 +95,17 @@ export class ManageClientsService {
clientLocAddressTable.columns.add('Zip', mssql.VarChar(10)); clientLocAddressTable.columns.add('Zip', mssql.VarChar(10));
clientLocAddressTable.columns.add('Country', mssql.VarChar(2)); clientLocAddressTable.columns.add('Country', mssql.VarChar(2));
finalBody.p_clientlocaddresstable.forEach((contact) => { // finalBody.p_clientlocaddresstable.forEach((contact) => {
clientLocAddressTable.rows.add( // clientLocAddressTable.rows.add(
contact.Nameof, // contact.Nameof,
contact.Address1, // contact.Address1,
contact.Address2, // contact.Address2,
contact.City, // contact.City,
contact.State, // contact.State,
contact.Zip, // contact.Zip,
contact.Country, // contact.Country,
); // );
}); // });
request.input('p_clientlocaddresstable', clientLocAddressTable); request.input('p_clientlocaddresstable', clientLocAddressTable);
@ -188,7 +188,7 @@ export class ManageClientsService {
} }
}; };
CreateClientContact = async (body: CreateAdditionalClientContactsDTO) => { CreateClientContact = async (body: CreateClientContactsDTO) => {
const newBody = { const newBody = {
p_spid: null, p_spid: null,
p_clientid: null, p_clientid: null,
@ -211,7 +211,7 @@ export class ManageClientsService {
setEmptyStringsToNull(reqBody); setEmptyStringsToNull(reqBody);
const finalBody: CreateAdditionalClientContactsDTO = { const finalBody: CreateClientContactsDTO = {
...newBody, ...newBody,
...reqBody, ...reqBody,
}; };
@ -371,7 +371,7 @@ export class ManageClientsService {
}; };
CreateClientLocation = async (body: CreateAdditionalClientLocationsDTO) => { CreateClientLocation = async (body: CreateClientLocationsDTO) => {
const newBody = { const newBody = {
p_spid: null, p_spid: null,
p_clientid: null, p_clientid: null,
@ -394,7 +394,7 @@ export class ManageClientsService {
setEmptyStringsToNull(reqBody); setEmptyStringsToNull(reqBody);
const finalBody: CreateAdditionalClientLocationsDTO = { const finalBody: CreateClientLocationsDTO = {
...newBody, ...newBody,
...reqBody, ...reqBody,
}; };
@ -431,7 +431,6 @@ export class ManageClientsService {
request.input('p_clientlocaddresstable', clientLocAddressTable); 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); request.input('p_userid', mssql.VarChar(100), finalBody.p_userid);
const result = await request.execute('carnetsys.CreateClientLocation'); const result = await request.execute('carnetsys.CreateClientLocation');

View File

@ -1,9 +1,9 @@
import { Body, Controller, Get, Post, Put, Query } from '@nestjs/common'; import { Body, Controller, Get, Post, Put, Query } from '@nestjs/common';
import { ManageClientsService } from './manage-clients.service'; import { ManageClientsService } from './manage-clients.service';
import { import {
CreateAdditionalClientContactsDTO, CreateClientContactsDTO,
CreateAdditionalClientLocationsDTO,
CreateClientDataDTO, CreateClientDataDTO,
CreateClientLocationsDTO,
GetPreparerByClientidContactsByClientidLocByClientidDTO, GetPreparerByClientidContactsByClientidLocByClientidDTO,
GetPreparersDTO, GetPreparersDTO,
UpdateClientContactsDTO, UpdateClientContactsDTO,
@ -41,14 +41,14 @@ export class ManageClientsController {
} }
@ApiTags('Manage Clients - Oracle') @ApiTags('Manage Clients - Oracle')
@Post('CreateAdditionalClientContacts') @Post('CreateClientContacts')
CreateClientContact(@Body() body: CreateAdditionalClientContactsDTO) { CreateClientContact(@Body() body: CreateClientContactsDTO) {
return this.manageClientsService.CreateClientContact(body); return this.manageClientsService.CreateClientContact(body);
} }
@ApiTags('Manage Clients - Oracle') @ApiTags('Manage Clients - Oracle')
@Post('CreateAdditionalClientLocations') @Post('CreateClientLocations')
CreateClientLocation(@Body() body: CreateAdditionalClientLocationsDTO) { CreateClientLocation(@Body() body: CreateClientLocationsDTO) {
return this.manageClientsService.CreateClientLocation(body); return this.manageClientsService.CreateClientLocation(body);
} }

View File

@ -171,21 +171,21 @@ export class CreateClientDataDTO {
@IsDefined({ message: 'Property p_userid is required' }) @IsDefined({ message: 'Property p_userid is required' })
p_userid: string; p_userid: string;
@ApiProperty({ required: true, type: () => [p_contactstableDTO] }) // @ApiProperty({ required: true, type: () => [p_contactstableDTO] })
@Type(() => p_contactstableDTO) // @Type(() => p_contactstableDTO)
@ValidateNested({ each: true }) // @ValidateNested({ each: true })
@IsArray({ message: 'Property p_contactstable allows only array type' }) // @IsArray({ message: 'Property p_contactstable allows only array type' })
@IsDefined({ message: 'Property p_contactstable is required' }) // @IsDefined({ message: 'Property p_contactstable is required' })
p_contactstable: p_contactstableDTO[]; // p_contactstable: p_contactstableDTO[];
@ApiProperty({ required: true, type: () => [p_clientlocaddresstableDTO] }) // @ApiProperty({ required: true, type: () => [p_clientlocaddresstableDTO] })
@Type(() => p_clientlocaddresstableDTO) // @Type(() => p_clientlocaddresstableDTO)
@ValidateNested({ each: true }) // @ValidateNested({ each: true })
@IsArray({ // @IsArray({
message: 'Property p_clientlocaddresstable allows only array type', // message: 'Property p_clientlocaddresstable allows only array type',
}) // })
@IsDefined({ message: 'Property p_clientlocaddresstable is required' }) // @IsDefined({ message: 'Property p_clientlocaddresstable is required' })
p_clientlocaddresstable: p_clientlocaddresstableDTO[]; // p_clientlocaddresstable: p_clientlocaddresstableDTO[];
} }
export class UpdateClientDTO { export class UpdateClientDTO {
@ -508,7 +508,7 @@ export class UpdateClientLocationsDTO {
p_userid: string; p_userid: string;
} }
export class CreateAdditionalClientContactsDTO { export class CreateClientContactsDTO {
@ApiProperty({ required: true }) @ApiProperty({ required: true })
@Max(999999999, { message: 'Property p_spid must not exceed 999999999' }) @Max(999999999, { message: 'Property p_spid must not exceed 999999999' })
@Min(0, { message: 'Property p_spid must be at least 0 or more' }) @Min(0, { message: 'Property p_spid must be at least 0 or more' })
@ -549,7 +549,7 @@ export class CreateAdditionalClientContactsDTO {
p_userid: string; p_userid: string;
} }
export class CreateAdditionalClientLocationsDTO { export class CreateClientLocationsDTO {
@ApiProperty({ required: true }) @ApiProperty({ required: true })
@Max(999999999, { message: 'Property p_spid must not exceed 999999999' }) @Max(999999999, { message: 'Property p_spid must not exceed 999999999' })
@Min(0, { message: 'Property p_spid must be at least 0 or more' }) @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' }) @IsDefined({ message: 'Property p_clientlocaddresstable is required' })
p_clientlocaddresstable: p_clientlocaddresstableDTO[]; p_clientlocaddresstable: p_clientlocaddresstableDTO[];
@ApiProperty({ required: true }) // @ApiProperty({ required: true })
@Length(0, 1, { // @Length(0, 1, {
message: 'Property p_defcontactflag must be exactly 1 character', // message: 'Property p_defcontactflag must be exactly 1 character',
}) // })
@IsString({ message: 'Property p_defcontactflag must be a string' }) // @IsString({ message: 'Property p_defcontactflag must be a string' })
@IsDefined({ message: 'Property p_defcontactflag is required' }) // @IsDefined({ message: 'Property p_defcontactflag is required' })
p_defcontactflag: string; // p_defcontactflag: string;
@ApiProperty({ required: true }) @ApiProperty({ required: true })
@Length(0, 100, { @Length(0, 100, {

File diff suppressed because it is too large Load Diff

View File

@ -30,6 +30,6 @@ export class RegionController {
// @ApiTags('Regions - Oracle') // @ApiTags('Regions - Oracle')
// @Get('/getDetails') // @Get('/getDetails')
selectAll() { selectAll() {
return this.regionService.selectAll(); return this.regionService.selectSP();
} }
} }

View File

@ -42,6 +42,56 @@ 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) { async insertRegions(body: InsertRegionsDto) {