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

124 lines
3.8 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';
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 {
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
USCIB_Managed_Pkg.CreateCarnetSequence(
:p_spid,
:p_regionid,
:p_startnumber,
:p_endnumber,
:p_carnettype,
:p_cursor);
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();
let fres = await result.outBinds.p_cursor.getRows();
await result.outBinds.p_cursor.close()
return fres
} catch (err) {
return { error: err.message }
} finally { }
}
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
USCIB_Managed_Pkg.GetCarnetSequence(: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); // 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) {
return { error: err.message }
} finally { }
}
}