2025-04-07 12:28:39 +05:30
|
|
|
import { Injectable } from '@nestjs/common';
|
2025-04-07 15:39:23 +05:30
|
|
|
import { Connection, Request } from 'mssql';
|
2025-04-07 12:28:39 +05:30
|
|
|
import { MssqlDBService } from 'src/db/db.service';
|
2025-04-07 15:39:23 +05:30
|
|
|
import * as mssql from 'mssql';
|
|
|
|
|
import { InsertRegionsDto } from 'src/oracle/uscib-managed-sp/region/region.dto';
|
2025-04-07 12:28:39 +05:30
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class MssqlService {
|
|
|
|
|
|
|
|
|
|
constructor(private readonly mssqlDBService: MssqlDBService) { }
|
|
|
|
|
|
|
|
|
|
async checkConnection() {
|
|
|
|
|
let connection;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
connection = await this.mssqlDBService.getConnection();
|
|
|
|
|
|
|
|
|
|
// Create a new Request object
|
|
|
|
|
const request = new Request(connection);
|
|
|
|
|
|
|
|
|
|
// Execute the query
|
|
|
|
|
const result = await request.query('SELECT GETDATE() AS CurrentDateTime');
|
|
|
|
|
|
|
|
|
|
console.log("Connection successful. Current date and time from the server:");
|
|
|
|
|
console.log(result.recordset[0].CurrentDateTime);
|
|
|
|
|
return { data: result.recordset[0].CurrentDateTime }
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error checking connection:', error);
|
|
|
|
|
} finally {
|
|
|
|
|
if (connection) {
|
|
|
|
|
connection.close(); // Ensure the connection is closed
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-04-07 15:39:23 +05:30
|
|
|
|
|
|
|
|
async getRegions() {
|
|
|
|
|
let connection: Connection;
|
|
|
|
|
try {
|
|
|
|
|
connection = await this.mssqlDBService.getConnection();
|
|
|
|
|
const request = new Request(connection);
|
|
|
|
|
const result = await request.execute('carnetsys.GETREGIONS');
|
|
|
|
|
return { data: result.recordsets };
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error executing stored procedure:', error);
|
|
|
|
|
} finally {
|
|
|
|
|
if (connection) {
|
|
|
|
|
connection.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async insetNewRegions(body:InsertRegionsDto) {
|
|
|
|
|
|
|
|
|
|
let connection: Connection;
|
|
|
|
|
try {
|
|
|
|
|
connection = await this.mssqlDBService.getConnection();
|
|
|
|
|
const request = new Request(connection);
|
|
|
|
|
request.input('P_REGION', mssql.VarChar(30), body.p_region);
|
|
|
|
|
request.input('P_NAME', mssql.VarChar(30), body.p_name);
|
|
|
|
|
const result = await request.execute('carnetsys.INSERTNEWREGION');
|
|
|
|
|
return { data: result.recordsets };
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error executing stored procedure:', error);
|
|
|
|
|
} finally {
|
|
|
|
|
if (connection) {
|
|
|
|
|
connection.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-04-07 12:28:39 +05:30
|
|
|
}
|