import { Injectable, InternalServerErrorException } from '@nestjs/common'; import { Connection, Request } from 'mssql'; import { MssqlDBService } from 'src/db/db.service'; import { InsertRegionsDto, UpdateRegionDto } from 'src/dto/uscib-managed-sp/region/region.dto'; import * as mssql from 'mssql'; import { InternalServerException } from 'src/exceptions/internalServerError.exception'; @Injectable() export class RegionService { constructor(private readonly mssqlDBService: MssqlDBService) { } async getRegions() { let connection: mssql.ConnectionPool; try { connection = await this.mssqlDBService.getConnection(); const request = new Request(connection); const result = await request.execute('carnetsys.GETREGIONS'); return result.recordset; } catch (error) { throw new InternalServerException(); } } async insetNewRegions(body: InsertRegionsDto) { let connection: mssql.ConnectionPool; try { connection = await this.mssqlDBService.getConnection(); const request = new Request(connection); request.input('P_REGION', mssql.VarChar(mssql.MAX), body.P_REGION); request.input('P_NAME', mssql.VarChar(mssql.MAX), body.P_NAME); const result = await request.execute('carnetsys.INSERTNEWREGION'); return { statusCode: 201, message: "Created Successfully", ...result.recordset[0] }; } catch (error) { throw new InternalServerException(); } } async updateRegions(body: UpdateRegionDto){ let connection: mssql.ConnectionPool; try { connection = await this.mssqlDBService.getConnection(); const request = new Request(connection); request.input('p_regionID', mssql.Int, body.P_REGIONID); request.input('P_NAME', mssql.VarChar(mssql.MAX), body.P_NAME); const result = await request.execute('carnetsys.UpdateRegion'); return { statusCode: 200, message: "Updated Successfully", ...result.recordset[0] }; } catch (error) { throw new InternalServerException(); } } }