import { Injectable } from '@nestjs/common'; import { Connection, Request } from 'mssql'; import { MssqlDBService } from 'src/db/db.service'; import { InsertNewServiceProviderDTO, UpdateServiceProviderDTO } from 'src/dto/sp/sp.dto'; import * as mssql from 'mssql' import { SPID_DTO } from 'src/dto/sp/sp-property.dto'; @Injectable() export class SpService { constructor(private readonly mssqlDBService: MssqlDBService) { } async getAllServiceproviders() { let connection: Connection; try { connection = await this.mssqlDBService.getConnection(); const request = new Request(connection); const result = await request.execute('carnetsys.GETALLSPS'); return { data: result.recordset }; } catch (error) { return { error: error.message }; } } async getSpBySpid(body: SPID_DTO) { let connection: Connection; try { connection = await this.mssqlDBService.getConnection(); const request = new Request(connection); request.input('P_SPID', mssql.Int, body.P_SPID); const result = await request.execute('carnetsys.GETSPBYSPID'); return { data: result.recordset }; } catch (error) { return { error: error.message }; } } async insertNewServiceProvider(body: InsertNewServiceProviderDTO) { let connection: Connection; try { connection = await this.mssqlDBService.getConnection(); const request = new Request(connection); request.input('P_NAME', mssql.VarChar(4000), body.P_NAME); request.input('P_LOOKUPCODE', mssql.VarChar(4000), body.P_LOOKUPCODE); request.input('P_ADDRESS1', mssql.VarChar(4000), body.P_ADDRESS1); request.input('P_ADDRESS2', mssql.VarChar(4000), body.P_ADDRESS2); request.input('P_CITY', mssql.VarChar(4000), body.P_CITY); request.input('P_STATE', mssql.VarChar(4000), body.P_STATE); request.input('P_ZIP', mssql.VarChar(4000), body.P_ZIP); request.input('P_COUNTRY', mssql.VarChar(4000), body.P_COUNTRY); request.input('P_ISSUINGREGION', mssql.VarChar(4000), body.P_ISSUINGREGION); request.input('P_REPLACEMENTREGION', mssql.VarChar(4000), body.P_REPLACEMENTREGION); request.input('P_BONDSURETY', mssql.VarChar(4000), body.P_BONDSURETY); request.input('P_CARGOPOLICYNO', mssql.VarChar(4000), body.P_CARGOPOLICYNO); request.input('P_CARGOSURETY', mssql.VarChar(4000), body.P_CARGOSURETY); request.input('P_USER_ID', mssql.VarChar(4000), body.P_USER_ID); request.input('P_NOTES', mssql.VarChar(4000), body.P_NOTES); request.input('P_FILEIDS', mssql.VarChar(4000), body.P_FILEIDS); const result = await request.execute('carnetsys.INSERTNEWSP'); return { data: result.recordset }; } catch (error) { return { error: error.message }; } } async updateServiceProvider(body: UpdateServiceProviderDTO) { let connection: Connection; try { connection = await this.mssqlDBService.getConnection(); const request = new Request(connection); request.input('P_SPID', mssql.Int, body.P_SPID); request.input('P_NAME', mssql.VarChar(4000), body.P_NAME); request.input('P_LOOKUPCODE', mssql.VarChar(4000), body.P_LOOKUPCODE); request.input('P_ADDRESS1', mssql.VarChar(4000), body.P_ADDRESS1); request.input('P_ADDRESS2', mssql.VarChar(4000), body.P_ADDRESS2); request.input('P_CITY', mssql.VarChar(4000), body.P_CITY); request.input('P_STATE', mssql.VarChar(4000), body.P_STATE); request.input('P_ZIP', mssql.VarChar(4000), body.P_ZIP); request.input('P_COUNTRY', mssql.VarChar(4000), body.P_COUNTRY); request.input('P_BONDSURETY', mssql.VarChar(4000), body.P_BONDSURETY); request.input('P_CARGOPOLICYNO', mssql.VarChar(4000), body.P_CARGOPOLICYNO); request.input('P_CARGOSURETY', mssql.VarChar(4000), body.P_CARGOSURETY); request.input('P_REPLACEMENTREGION', mssql.VarChar(4000), body.P_REPLACEMENTREGION); request.input('P_ISSUINGREGION', mssql.VarChar(4000), body.P_ISSUINGREGION); request.input('P_USER_ID', mssql.VarChar(4000), body.P_USER_ID); request.input('P_NOTES', mssql.VarChar(4000), body.P_NOTES); request.input('P_FILEIDS', mssql.VarChar(4000), body.P_FILEIDS); const result = await request.execute('carnetsys.UPDATESP'); return { data: result.recordset }; } catch (error) { return { error: error.message }; } } }