BE/src/oracle/uscib-managed-sp/carnet-sequence/carnet-sequence.service.ts

131 lines
3.4 KiB
TypeScript
Raw Normal View History

2025-03-25 17:10:33 +05:30
import { Injectable } from '@nestjs/common';
2025-04-02 13:03:48 +05:30
import * as oracledb from 'oracledb';
import { OracleDBService } from 'src/db/db.service';
2025-04-02 13:29:06 +05:30
import {
CreateCarnetSequenceDTO,
GetCarnetSequenceDTO,
} from './carnet-sequence.dto';
2025-03-25 17:10:33 +05:30
@Injectable()
2025-04-02 13:03:48 +05:30
export class CarnetSequenceService {
2025-04-02 13:29:06 +05:30
constructor(private readonly oracleDBService: OracleDBService) {}
async createCarnetSequence(body: CreateCarnetSequenceDTO) {
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.CreateCarnetSequence(
:p_spid,
:p_regionid,
:p_startnumber,
:p_endnumber,
:p_carnettype,
:p_cursor);
2025-04-02 13:29:06 +05:30
END;`,
{
p_spid: {
val: body.p_spid,
type: oracledb.DB_TYPE_NUMBER,
},
p_regionid: {
val: body.p_regionid,
type: oracledb.DB_TYPE_NUMBER,
},
p_startnumber: {
val: body.p_startnumber,
type: oracledb.DB_TYPE_NUMBER,
},
p_endnumber: {
val: body.p_endnumber,
type: oracledb.DB_TYPE_NUMBER,
},
p_carnettype: {
val: body.p_carnettype,
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();
await result.outBinds.p_cursor.close();
return fres;
} catch (err) {
if (err instanceof Error) {
return { error: err.message };
} else {
return { error: 'An unknown error occurred' };
}
2025-04-02 13:03:48 +05:30
}
2025-04-02 13:29:06 +05:30
}
async getCarnetSequence(body: GetCarnetSequenceDTO) {
let connection;
let rows = [];
try {
// Connect to the Oracle database using oracledb
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.GetCarnetSequence(:p_spid,:p_cursor);
END;`,
2025-04-02 13:29:06 +05:30
{
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); // Fetch 100 rows at a time
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-04-02 13:03:48 +05:30
}
2025-04-02 13:29:06 +05:30
}
2025-04-02 13:03:48 +05:30
}