BE/src/mssql/param-table/param-table.service.ts
2025-07-21 20:34:23 +05:30

158 lines
7.2 KiB
TypeScript

import { Injectable } from '@nestjs/common';
import { MssqlDBService } from 'src/db/db.service';
import * as mssql from 'mssql';
import { Request } from 'mssql';
import { Connection } from 'mssql';
import { InternalServerException } from 'src/exceptions/internalServerError.exception';
import { BadRequestException } from 'src/exceptions/badRequest.exception';
import { ActivateOrInactivateParamRecordDTO, CreateParamRecordDTO, CreateTableRecordDTO, getParamValuesDTO, UpdateParamRecordDTO } from 'src/dto/param-table/param-table.dto';
@Injectable()
export class ParamTableService {
constructor(private readonly mssqlDBService: MssqlDBService) { }
async GETPARAMVALUES(body: getParamValuesDTO) {
const finalBody = {
P_SPID: body.P_SPID === 0 ? body.P_SPID : body.P_SPID ? body.P_SPID : null,
P_PARAMTYPE: body.P_PARAMTYPE ? body.P_PARAMTYPE : null,
};
console.log(finalBody);
let connection: mssql.ConnectionPool;
try {
connection = await this.mssqlDBService.getConnection();
const request = new Request(connection);
request.input('P_SPID', mssql.Int, finalBody.P_SPID);
request.input('P_PARAMTYPE', mssql.VarChar(100), finalBody.P_PARAMTYPE);
const result = await request.execute('carnetsys.GETPARAMVALUES');
return result.recordset;
} catch (error) {
throw new InternalServerException(error.message);
}
}
async CREATETABLERECORD(body: CreateTableRecordDTO) {
let connection: mssql.ConnectionPool;
try {
connection = await this.mssqlDBService.getConnection();
const request = new Request(connection);
request.input('P_USERID', mssql.VarChar(100), body.P_USERID);
request.input('P_TABLEFULLDESC', mssql.NVarChar, body.P_TABLEFULLDESC);
const result = await request.execute('carnetsys.CREATETABLERECORD');
// return { data: result.recordset };
if (result['recordset'][0].ERRORMSG) {
throw new BadRequestException(result[0].ERRORMSG);
}
else {
return { statusCode: 201, message: "Created Successfully" }
}
} catch (error) {
console.log(error);
throw new InternalServerException(error.message);
}
}
async CREATEPARAMRECORD(body: CreateParamRecordDTO) {
let connection: mssql.ConnectionPool;
try {
connection = await this.mssqlDBService.getConnection();
const request = new Request(connection);
request.input('P_SPID', mssql.Int, body.P_SPID);
request.input('P_PARAMTYPE', mssql.VarChar(255), body.P_PARAMTYPE);
request.input('P_PARAMDESC', mssql.VarChar(255), body.P_PARAMDESC);
request.input('P_PARAMVALUE', mssql.VarChar(255), body.P_PARAMVALUE);
request.input('P_ADDLPARAMVALUE1', mssql.VarChar(255), body.P_ADDLPARAMVALUE1);
request.input('P_ADDLPARAMVALUE2', mssql.VarChar(255), body.P_ADDLPARAMVALUE2);
request.input('P_ADDLPARAMVALUE3', mssql.VarChar(255), body.P_ADDLPARAMVALUE3);
request.input('P_ADDLPARAMVALUE4', mssql.VarChar(255), body.P_ADDLPARAMVALUE4);
request.input('P_ADDLPARAMVALUE5', mssql.VarChar(255), body.P_ADDLPARAMVALUE5);
request.input('P_SORTSEQ', mssql.Int, body.P_SORTSEQ);
request.input('P_USERID', mssql.VarChar(100), body.P_USERID);
const result = await request.execute('carnetsys.CREATEPARAMRECORD');
// return { data: result.recordset };
if (result['recordset'][0].ERRORMSG) {
throw new BadRequestException(result[0].ERRORMSG);
}
else {
return { statusCode: 201, message: "Created Successfully" }
}
} catch (error) {
// return { error: error.message };
throw new InternalServerException()
}
}
async UPDATEPARAMRECORD(body: UpdateParamRecordDTO) {
let connection: mssql.ConnectionPool;
try {
connection = await this.mssqlDBService.getConnection();
const request = new Request(connection);
request.input('P_SPID', mssql.Int, body.P_SPID);
request.input('P_PARAMID', mssql.Int, body.P_PARAMID);
request.input('P_PARAMDESC', mssql.VarChar(255), body.P_PARAMDESC);
request.input('P_ADDLPARAMVALUE1', mssql.VarChar(255), body.P_ADDLPARAMVALUE1);
request.input('P_ADDLPARAMVALUE2', mssql.VarChar(255), body.P_ADDLPARAMVALUE2);
request.input('P_ADDLPARAMVALUE3', mssql.VarChar(255), body.P_ADDLPARAMVALUE3);
request.input('P_ADDLPARAMVALUE4', mssql.VarChar(255), body.P_ADDLPARAMVALUE4);
request.input('P_ADDLPARAMVALUE5', mssql.VarChar(255), body.P_ADDLPARAMVALUE5);
request.input('P_SORTSEQ', mssql.Int, body.P_SORTSEQ);
request.input('P_USERID', mssql.VarChar(100), body.P_USERID);
const result = await request.execute('carnetsys.UPDATEPARAMRECORD');
// return { data: result.recordset };
if (result['recordset'][0].ERRORMSG) {
throw new BadRequestException(result[0].ERRORMSG);
}
else {
return { statusCode: 200, message: "Updated Successfully" }
}
} catch (error) {
throw new InternalServerException(error.message);
}
}
async INACTIVATEPARAMRECORD(body: ActivateOrInactivateParamRecordDTO) {
let connection: mssql.ConnectionPool;
try {
connection = await this.mssqlDBService.getConnection();
const request = new Request(connection);
request.input('P_PARAMID', mssql.Int, body.P_PARAMID);
request.input('P_USERID', mssql.VarChar(100), body.P_USERID);
const result = await request.execute('carnetsys.INACTIVATEPARAMRECORD');
// return { data: result.recordset };
if (result['recordset'][0].ERRORMSG) {
throw new BadRequestException(result[0].ERRORMSG);
}
else {
return { statusCode: 200, message: result['recordset'][0].message }
}
} catch (error) {
throw new InternalServerException(error.message);
}
}
async REACTIVATEPARAMRECORD(body: ActivateOrInactivateParamRecordDTO) {
let connection: mssql.ConnectionPool;
try {
connection = await this.mssqlDBService.getConnection();
const request = new Request(connection);
request.input('P_PARAMID', mssql.Int, body.P_PARAMID);
request.input('P_USERID', mssql.VarChar(100), body.P_USERID);
const result = await request.execute('carnetsys.REACTIVATEPARAMRECORD');
// return { data: result.recordset };
if (result['recordset'][0].ERRORMSG) {
throw new BadRequestException(result[0].ERRORMSG);
}
else {
return { statusCode: 200, message: result['recordset'][0].message }
}
} catch (error) {
throw new InternalServerException(error.message);
}
}
}