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

143 lines
4.0 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 { InsertRegionsDto, UpdateRegionDto } from './region.dto';
2025-03-25 17:10:33 +05:30
@Injectable()
export class RegionService {
constructor(private readonly oracleDBService: OracleDBService) { }
2025-04-02 13:03:48 +05:30
async insertRegions(body: InsertRegionsDto) {
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
2025-04-02 13:03:48 +05:30
USCIB_Managed_Pkg.InsertNewRegion(:p_region,:p_name,:p_cursor);
END;`, {
2025-03-25 17:10:33 +05:30
p_region: {
2025-04-02 13:03:48 +05:30
val: body.p_region,
2025-03-25 17:10:33 +05:30
type: oracledb.DB_TYPE_VARCHAR
},
p_name: {
2025-04-02 13:03:48 +05:30
val: body.p_name,
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 updateRegions(body: UpdateRegionDto) {
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
2025-04-02 13:03:48 +05:30
USCIB_Managed_Pkg.UpdateRegion(:p_regionID,:p_name,:p_cursor);
END;`, {
2025-03-25 17:10:33 +05:30
p_regionID: {
2025-04-02 13:03:48 +05:30
val: body.p_regionID,
2025-03-25 17:10:33 +05:30
type: oracledb.DB_TYPE_NUMBER
},
p_name: {
2025-04-02 13:03:48 +05:30
val: body.p_name,
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 { }
}
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
USCIB_Managed_Pkg.GetRegions(: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);
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 { }
}
}