BE/src/oracle/uscib-managed-sp/sp-contacts/sp-contacts.service.ts

361 lines
10 KiB
TypeScript
Raw Normal View History

2025-03-25 17:10:33 +05:30
import { Injectable } from '@nestjs/common';
import { OracleDBService } from 'src/db/db.service';
import * as oracledb from 'oracledb'
2025-04-02 13:03:48 +05:30
import {
getSPDefaultcontactDTO,
inactivateSPContactDTO,
InsertSPContactsDTO,
setSPDefaultcontactDTO,
UpdateSPContactsDTO
} from './sp-contacts.dto';
2025-03-25 17:10:33 +05:30
@Injectable()
export class SpContactsService {
constructor(private readonly oracleDBService: OracleDBService) { }
2025-04-02 13:03:48 +05:30
async insertSPContacts(body: InsertSPContactsDTO) {
2025-03-25 17:10:33 +05:30
let connection;
try {
2025-04-02 13:03:48 +05:30
2025-03-25 17:10:33 +05:30
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: {
2025-04-02 13:03:48 +05:30
val: body.p_spid,
2025-03-25 17:10:33 +05:30
type: oracledb.DB_TYPE_NUMBER
},
p_defcontactflag: {
2025-04-02 13:03:48 +05:30
val: body.p_defcontactflag,
2025-03-25 17:10:33 +05:30
type: oracledb.DB_TYPE_VARCHAR
},
p_firstname: {
2025-04-02 13:03:48 +05:30
val: body.p_firstname,
2025-03-25 17:10:33 +05:30
type: oracledb.DB_TYPE_VARCHAR
},
p_lastname: {
2025-04-02 13:03:48 +05:30
val: body.p_lastname,
2025-03-25 17:10:33 +05:30
type: oracledb.DB_TYPE_VARCHAR
},
P_MIDDLEINITIAL: {
2025-04-02 13:03:48 +05:30
val: body.P_MIDDLEINITIAL,
2025-03-25 17:10:33 +05:30
type: oracledb.DB_TYPE_VARCHAR
},
p_title: {
2025-04-02 13:03:48 +05:30
val: body.p_title,
2025-03-25 17:10:33 +05:30
type: oracledb.DB_TYPE_VARCHAR
},
p_phoneno: {
2025-04-02 13:03:48 +05:30
val: body.p_phoneno,
2025-03-25 17:10:33 +05:30
type: oracledb.DB_TYPE_VARCHAR
},
p_mobileno: {
2025-04-02 13:03:48 +05:30
val: body.p_mobileno,
2025-03-25 17:10:33 +05:30
type: oracledb.DB_TYPE_VARCHAR
},
p_faxno: {
2025-04-02 13:03:48 +05:30
val: body.p_faxno,
2025-03-25 17:10:33 +05:30
type: oracledb.DB_TYPE_VARCHAR
},
p_emailaddress: {
2025-04-02 13:03:48 +05:30
val: body.p_emailaddress,
2025-03-25 17:10:33 +05:30
type: oracledb.DB_TYPE_VARCHAR
},
p_user_id: {
2025-04-02 13:03:48 +05:30
val: body.p_user_id,
2025-03-25 17:10:33 +05:30
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) {
2025-04-02 13:03:48 +05:30
2025-03-25 17:10:33 +05:30
return { error: err.message }
} finally { }
}
2025-04-02 13:03:48 +05:30
async setSPDefaultcontact(body: setSPDefaultcontactDTO) {
2025-03-25 17:10:33 +05:30
let connection;
try {
2025-04-02 13:03:48 +05:30
2025-03-25 17:10:33 +05:30
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: {
2025-04-02 13:03:48 +05:30
val: body.p_spcontactid,
2025-03-25 17:10:33 +05:30
type: oracledb.DB_TYPE_NUMBER
},
}
);
await connection.commit();
return "SP executed successfully"
} catch (err) {
2025-04-02 13:03:48 +05:30
2025-03-25 17:10:33 +05:30
return { error: err.message }
} finally { }
}
2025-04-02 13:03:48 +05:30
async updateSPContacts(body: UpdateSPContactsDTO) {
2025-03-25 17:10:33 +05:30
let connection;
try {
2025-04-02 13:03:48 +05:30
2025-03-25 17:10:33 +05:30
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: {
2025-04-02 13:03:48 +05:30
val: body.p_spcontactid,
2025-03-25 17:10:33 +05:30
type: oracledb.DB_TYPE_NUMBER
},
p_firstname: {
2025-04-02 13:03:48 +05:30
val: body.p_firstname,
2025-03-25 17:10:33 +05:30
type: oracledb.DB_TYPE_VARCHAR
},
p_lastname: {
2025-04-02 13:03:48 +05:30
val: body.p_lastname,
2025-03-25 17:10:33 +05:30
type: oracledb.DB_TYPE_VARCHAR
},
P_MIDDLEINITIAL: {
2025-04-02 13:03:48 +05:30
val: body.P_MIDDLEINITIAL,
2025-03-25 17:10:33 +05:30
type: oracledb.DB_TYPE_VARCHAR
},
p_title: {
2025-04-02 13:03:48 +05:30
val: body.p_title,
2025-03-25 17:10:33 +05:30
type: oracledb.DB_TYPE_VARCHAR
},
p_phoneno: {
2025-04-02 13:03:48 +05:30
val: body.p_phoneno,
2025-03-25 17:10:33 +05:30
type: oracledb.DB_TYPE_VARCHAR
},
p_mobileno: {
2025-04-02 13:03:48 +05:30
val: body.p_mobileno,
2025-03-25 17:10:33 +05:30
type: oracledb.DB_TYPE_VARCHAR
},
p_faxno: {
2025-04-02 13:03:48 +05:30
val: body.p_faxno,
2025-03-25 17:10:33 +05:30
type: oracledb.DB_TYPE_VARCHAR
},
p_emailaddress: {
2025-04-02 13:03:48 +05:30
val: body.p_emailaddress,
2025-03-25 17:10:33 +05:30
type: oracledb.DB_TYPE_VARCHAR
},
p_user_id: {
2025-04-02 13:03:48 +05:30
val: body.p_user_id,
2025-03-25 17:10:33 +05:30
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) {
2025-04-02 13:03:48 +05:30
2025-03-25 17:10:33 +05:30
return { error: err.message }
} finally { }
}
2025-04-02 13:03:48 +05:30
async inactivateSPContact(body: inactivateSPContactDTO) {
2025-03-25 17:10:33 +05:30
let connection;
try {
2025-04-02 13:03:48 +05:30
2025-03-25 17:10:33 +05:30
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: {
2025-04-02 13:03:48 +05:30
val: body.p_spcontactid,
2025-03-25 17:10:33 +05:30
type: oracledb.DB_TYPE_NUMBER
}
}
);
await connection.commit();
return "SP executed successfully"
} catch (err) {
2025-04-02 13:03:48 +05:30
2025-03-25 17:10:33 +05:30
return { error: err.message }
} finally { }
}
2025-04-02 13:03:48 +05:30
async getSPDefaultcontacts(body: getSPDefaultcontactDTO) {
2025-03-25 17:10:33 +05:30
let connection;
let rows = [];
try {
2025-04-02 13:03:48 +05:30
2025-03-25 17:10:33 +05:30
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: {
2025-04-02 13:03:48 +05:30
val: body.p_SPid,
2025-03-25 17:10:33 +05:30
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) {
2025-04-02 13:03:48 +05:30
const cursor = result.outBinds.p_cursor;
2025-03-25 17:10:33 +05:30
let rowsBatch;
do {
2025-04-02 13:03:48 +05:30
rowsBatch = await cursor.getRows(100);
rows = rows.concat(rowsBatch);
2025-03-25 17:10:33 +05:30
} while (rowsBatch.length > 0);
2025-04-02 13:03:48 +05:30
2025-03-25 17:10:33 +05:30
await cursor.close();
} else {
throw new Error('No cursor returned from the stored procedure');
}
return rows;
} catch (err) {
2025-04-02 13:03:48 +05:30
2025-03-25 17:10:33 +05:30
return { error: err.message }
} finally { }
}
async getSPcontacts() {
let connection;
let rows = [];
try {
2025-04-02 13:03:48 +05:30
2025-03-25 17:10:33 +05:30
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) {
2025-04-02 13:03:48 +05:30
const cursor = result.outBinds.p_cursor;
2025-03-25 17:10:33 +05:30
let rowsBatch;
do {
2025-04-02 13:03:48 +05:30
rowsBatch = await cursor.getRows(100);
rows = rows.concat(rowsBatch);
2025-03-25 17:10:33 +05:30
} while (rowsBatch.length > 0);
2025-04-02 13:03:48 +05:30
2025-03-25 17:10:33 +05:30
await cursor.close();
} else {
throw new Error('No cursor returned from the stored procedure');
}
return rows;
} catch (err) {
2025-04-02 13:03:48 +05:30
2025-03-25 17:10:33 +05:30
return { error: err.message }
} finally { }
}
}