163 lines
4.4 KiB
TypeScript
163 lines
4.4 KiB
TypeScript
import { Injectable, Logger } from '@nestjs/common';
|
|
import * as oracledb from 'oracledb';
|
|
import { OracleDBService } from 'src/db/db.service';
|
|
import {
|
|
CreateCarnetSequenceDTO,
|
|
GetCarnetSequenceDTO,
|
|
} from './carnet-sequence.dto';
|
|
|
|
import { BadRequestException } from 'src/exceptions/badRequest.exception';
|
|
import { InternalServerException } from 'src/exceptions/internalServerError.exception';
|
|
|
|
@Injectable()
|
|
export class CarnetSequenceService {
|
|
private readonly logger = new Logger(CarnetSequenceService.name);
|
|
|
|
constructor(private readonly oracleDBService: OracleDBService) {}
|
|
|
|
async createCarnetSequence(body: CreateCarnetSequenceDTO) {
|
|
let connection;
|
|
try {
|
|
connection = await this.oracleDBService.getConnection();
|
|
if (!connection) {
|
|
throw new InternalServerException();
|
|
}
|
|
|
|
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();
|
|
|
|
const fres = await result.outBinds.p_cursor.getRows();
|
|
|
|
await result.outBinds.p_cursor.close();
|
|
|
|
if (fres.length>0 && fres[0].ERRORMESG) {
|
|
this.logger.warn(fres[0].ERRORMESG);
|
|
throw new BadRequestException(fres[0].ERRORMESG)
|
|
}
|
|
|
|
return { statusCode: 201, message: "Createdted Successfully", ...fres[0] };
|
|
} catch (error) {
|
|
if (error instanceof BadRequestException) {
|
|
this.logger.warn(error.message);
|
|
throw error;
|
|
}
|
|
this.logger.error('createCarnetSequence failed', error.stack || error);
|
|
throw new InternalServerException();
|
|
} finally {
|
|
if (connection) {
|
|
try {
|
|
await connection.close();
|
|
} catch (closeErr) {
|
|
this.logger.error('Failed to close DB connection', closeErr);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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 InternalServerException();
|
|
}
|
|
|
|
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);
|
|
rows = rows.concat(rowsBatch);
|
|
} while (rowsBatch.length > 0);
|
|
|
|
await cursor.close();
|
|
} else {
|
|
throw new BadRequestException();
|
|
}
|
|
|
|
return rows;
|
|
} catch (error) {
|
|
if (error instanceof BadRequestException) {
|
|
this.logger.warn(error.message);
|
|
throw error;
|
|
}
|
|
else if (error.message === "NJS-107: invalid cursor") {
|
|
this.logger.warn(error.message);
|
|
throw new BadRequestException();
|
|
}
|
|
this.logger.error('getCarnetSequence failed', error.stack || error);
|
|
throw new InternalServerException();
|
|
} finally {
|
|
if (connection) {
|
|
try {
|
|
await connection.close();
|
|
} catch (closeErr) {
|
|
this.logger.error('Failed to close DB connection', closeErr);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|