BE/src/oracle/uscib-managed-sp/sp-contacts/sp-contacts.service.ts
2025-05-16 12:48:57 +05:30

417 lines
11 KiB
TypeScript

import { Injectable, Logger } 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';
import { BadRequestException } from 'src/exceptions/badRequest.exception';
import { InternalServerException } from 'src/exceptions/internalServerError.exception';
@Injectable()
export class SpContactsService {
private readonly logger = new Logger(SpContactsService.name);
constructor(private readonly oracleDBService: OracleDBService) { }
async insertSPContacts(body: InsertSPContactsDTO) {
let connection;
try {
connection = await this.oracleDBService.getConnection();
if (!connection) {
throw new InternalServerException();
}
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();
if (fres[0].ERRORMESG) {
this.logger.warn(fres[0].ERRORMESG);
throw new BadRequestException(fres[0].ERRORMESG)
}
return { statusCode: 201, message: "Createdted Successfully", ...fres[0] };
} catch (error) {
if (error instanceof BadRequestException) {
throw error;
}
this.logger.error('insertSPContacts 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 setSPDefaultcontact(body: setSPDefaultcontactDTO) {
let connection;
try {
connection = await this.oracleDBService.getConnection();
if (!connection) {
throw new InternalServerException();
}
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 { statusCode: 200, message: 'Default contact was added successfully for SP' };
} catch (error) {
this.logger.error('setSPDefaultcontact 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 updateSPContacts(body: UpdateSPContactsDTO) {
let connection;
try {
connection = await this.oracleDBService.getConnection();
if (!connection) {
throw new InternalServerException();
}
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();
if (fres[0].ERRORMESG) {
this.logger.warn(fres[0].ERRORMESG);
throw new BadRequestException(fres[0].ERRORMESG)
}
return { statusCode: 200, message: "Updated Successfully" };
} catch (error) {
if (error instanceof BadRequestException) {
this.logger.warn(error.message);
throw error;
}
this.logger.error('updateSPContacts 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 inactivateSPContact(body: inactivateSPContactDTO) {
let connection;
try {
connection = await this.oracleDBService.getConnection();
if (!connection) {
throw new InternalServerException();
}
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 { statusCode: 200, message: 'Inactivated SP contact successfully' };
} catch (error) {
this.logger.error('inactivateSPContact 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 getSPDefaultcontacts(body: getSPDefaultcontactDTO) {
let connection;
let rows = [];
try {
connection = await this.oracleDBService.getConnection();
if (!connection) {
throw new InternalServerException();
}
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 BadRequestException();
}
return rows;
} catch (error) {
this.logger.error('getSPDefaultcontacts 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 getSPAllContacts(body: getSPAllContactsDTO) {
let connection;
let rows = [];
try {
connection = await this.oracleDBService.getConnection();
if (!connection) {
throw new InternalServerException();
}
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 BadRequestException();
}
return rows;
} catch (error) {
this.logger.error('getSPAllContacts 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);
}
}
}
}
}