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

176 lines
4.3 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';
2025-04-02 13:29:06 +05:30
import * as oracledb from 'oracledb';
2025-04-02 13:03:48 +05:30
import { InsertRegionsDto, UpdateRegionDto } from './region.dto';
2025-03-25 17:10:33 +05:30
@Injectable()
export class RegionService {
2025-04-02 13:29:06 +05:30
constructor(private readonly oracleDBService: OracleDBService) {}
async insertRegions(body: InsertRegionsDto) {
let connection;
try {
connection = await this.oracleDBService.getConnection();
if (!connection) {
throw new Error('No DB Connected');
}
const result = await connection.execute(
`BEGIN
2025-04-02 13:03:48 +05:30
USCIB_Managed_Pkg.InsertNewRegion(:p_region,:p_name,:p_cursor);
2025-04-02 13:29:06 +05:30
END;`,
{
p_region: {
val: body.p_region,
type: oracledb.DB_TYPE_VARCHAR,
},
p_name: {
val: body.p_name,
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();
return fres;
} catch (err) {
if (err instanceof Error) {
return { error: err.message };
} else {
return { error: 'An unknown error occurred' };
}
2025-03-25 17:10:33 +05:30
}
2025-04-30 17:28:09 +05:30
finally {
if (connection) {
try {
await connection.close();
} catch (closeErr) {
console.error('Failed to close connection:', closeErr);
}
}
}
2025-04-02 13:29:06 +05:30
}
async updateRegions(body: UpdateRegionDto) {
let connection;
try {
connection = await this.oracleDBService.getConnection();
if (!connection) {
throw new Error('No DB Connected');
}
const result = await connection.execute(
`BEGIN
2025-04-02 13:03:48 +05:30
USCIB_Managed_Pkg.UpdateRegion(:p_regionID,:p_name,:p_cursor);
2025-04-02 13:29:06 +05:30
END;`,
{
p_regionID: {
val: body.p_regionID,
type: oracledb.DB_TYPE_NUMBER,
},
p_name: {
val: body.p_name,
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();
return fres;
} catch (err) {
if (err instanceof Error) {
return { error: err.message };
} else {
return { error: 'An unknown error occurred' };
}
2025-03-25 17:10:33 +05:30
}
2025-04-30 17:28:09 +05:30
finally {
if (connection) {
try {
await connection.close();
} catch (closeErr) {
console.error('Failed to close connection:', closeErr);
}
}
}
2025-04-02 13:29:06 +05:30
}
async getRegions() {
let connection;
let rows = [];
try {
try {
connection = await this.oracleDBService.getConnection();
} catch (err) {
console.log('DB ERROR: ', err);
return { error: 'Error while connecting to DB' };
}
const result = await connection.execute(
`BEGIN
2025-03-25 17:10:33 +05:30
USCIB_Managed_Pkg.GetRegions(:p_cursor);
END;`,
2025-04-02 13:29:06 +05:30
{
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 Error('No cursor returned from the stored procedure');
}
return rows;
} catch (err) {
if (err instanceof Error) {
return { error: err.message };
} else {
return { error: 'An unknown error occurred' };
}
2025-03-25 17:10:33 +05:30
}
2025-04-30 17:06:16 +05:30
finally {
if (connection) {
try {
await connection.close();
} catch (closeErr) {
console.error('Failed to close connection:', closeErr);
}
}
}
2025-04-02 13:29:06 +05:30
}
2025-03-25 17:10:33 +05:30
}