143 lines
5.2 KiB
TypeScript
143 lines
5.2 KiB
TypeScript
|
|
import { Injectable } from '@nestjs/common';
|
||
|
|
import { Connection, Request } from 'mssql';
|
||
|
|
import { MssqlDBService } from 'src/db/db.service';
|
||
|
|
import { CreateClientDataDTO, GetPreparerByClientidContactsByClientidLocByClientidDTO } from 'src/oracle/manage-clients/manage-clients.dto';
|
||
|
|
import * as mssql from 'mssql';
|
||
|
|
|
||
|
|
@Injectable()
|
||
|
|
export class ManageClientsService {
|
||
|
|
constructor(private readonly mssqlDBService: MssqlDBService) {}
|
||
|
|
|
||
|
|
CreateClientData = async (body: CreateClientDataDTO) => {
|
||
|
|
const newBody = {
|
||
|
|
p_spid: null,
|
||
|
|
p_clientname: null,
|
||
|
|
p_lookupcode: null,
|
||
|
|
p_address1: null,
|
||
|
|
p_address2: null,
|
||
|
|
p_city: null,
|
||
|
|
p_state: null,
|
||
|
|
p_zip: null,
|
||
|
|
p_country: null,
|
||
|
|
p_issuingregion: null,
|
||
|
|
p_revenuelocation: null,
|
||
|
|
p_userid: null,
|
||
|
|
p_contactstable: null,
|
||
|
|
p_clientlocaddresstable: null,
|
||
|
|
};
|
||
|
|
|
||
|
|
const reqBody = JSON.parse(JSON.stringify(body));
|
||
|
|
|
||
|
|
function setEmptyStringsToNull(obj: any) {
|
||
|
|
Object.keys(obj).forEach((key) => {
|
||
|
|
if (typeof obj[key] === 'object' && obj[key] !== null) {
|
||
|
|
setEmptyStringsToNull(obj[key]);
|
||
|
|
} else if (obj[key] === '') {
|
||
|
|
obj[key] = null;
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
setEmptyStringsToNull(reqBody);
|
||
|
|
|
||
|
|
const finalBody: CreateClientDataDTO = { ...newBody, ...reqBody };
|
||
|
|
|
||
|
|
let connection: Connection;
|
||
|
|
try {
|
||
|
|
connection = await this.mssqlDBService.getConnection();
|
||
|
|
const request = new Request(connection);
|
||
|
|
request.input('p_spid', mssql.Int, finalBody.p_spid);
|
||
|
|
request.input('p_clientname', mssql.VarChar(50), finalBody.p_clientname);
|
||
|
|
request.input('p_lookupcode', mssql.VarChar(20), finalBody.p_lookupcode);
|
||
|
|
request.input('p_address1', mssql.VarChar(50), finalBody.p_address1);
|
||
|
|
request.input('p_address2', mssql.VarChar(50), finalBody.p_address2);
|
||
|
|
request.input('p_city', mssql.VarChar(30), finalBody.p_city);
|
||
|
|
request.input('p_state', mssql.VarChar(2), finalBody.p_state);
|
||
|
|
request.input('p_zip', mssql.VarChar(10), finalBody.p_zip);
|
||
|
|
request.input('p_country', mssql.VarChar(2), finalBody.p_country);
|
||
|
|
request.input('p_issuingregion', mssql.VarChar(2), finalBody.p_issuingregion);
|
||
|
|
request.input('p_revenuelocation', mssql.VarChar(2), finalBody.p_revenuelocation);
|
||
|
|
request.input('p_userid', mssql.VarChar(100), finalBody.p_userid);
|
||
|
|
|
||
|
|
const contactTable = new mssql.Table('carnetsys.ContactsTable');
|
||
|
|
contactTable.create = true;
|
||
|
|
contactTable.columns.add('FirstName', mssql.VarChar(50));
|
||
|
|
contactTable.columns.add('LastName', mssql.VarChar(50));
|
||
|
|
contactTable.columns.add('MiddleInitial', mssql.VarChar(3));
|
||
|
|
contactTable.columns.add('Title', mssql.VarChar(50));
|
||
|
|
contactTable.columns.add('EmailAddress', mssql.VarChar(100));
|
||
|
|
contactTable.columns.add('PhoneNo', mssql.VarChar(20));
|
||
|
|
contactTable.columns.add('MobileNo', mssql.VarChar(20));
|
||
|
|
contactTable.columns.add('FaxNo', mssql.VarChar(20));
|
||
|
|
|
||
|
|
finalBody.p_contactstable.forEach((contact) => {
|
||
|
|
contactTable.rows.add(
|
||
|
|
contact.FirstName,
|
||
|
|
contact.LastName,
|
||
|
|
contact.MiddleInitial,
|
||
|
|
contact.Title,
|
||
|
|
contact.EmailAddress,
|
||
|
|
contact.PhoneNo,
|
||
|
|
contact.MobileNo,
|
||
|
|
contact.FaxNo,
|
||
|
|
);
|
||
|
|
});
|
||
|
|
|
||
|
|
request.input('p_contactstable', contactTable);
|
||
|
|
|
||
|
|
const clientLocAddressTable = new mssql.Table('carnetsys.ClientLocAddressTable');
|
||
|
|
clientLocAddressTable.create = true;
|
||
|
|
clientLocAddressTable.columns.add('Nameof', mssql.VarChar(50));
|
||
|
|
clientLocAddressTable.columns.add('Address1', mssql.VarChar(50));
|
||
|
|
clientLocAddressTable.columns.add('Address2', mssql.VarChar(50));
|
||
|
|
clientLocAddressTable.columns.add('City', mssql.VarChar(30));
|
||
|
|
clientLocAddressTable.columns.add('State', mssql.VarChar(2));
|
||
|
|
clientLocAddressTable.columns.add('Zip', mssql.VarChar(10));
|
||
|
|
clientLocAddressTable.columns.add('Country', mssql.VarChar(2));
|
||
|
|
|
||
|
|
finalBody.p_clientlocaddresstable.forEach((contact) => {
|
||
|
|
clientLocAddressTable.rows.add(
|
||
|
|
contact.Nameof,
|
||
|
|
contact.Address1,
|
||
|
|
contact.Address2,
|
||
|
|
contact.City,
|
||
|
|
contact.State,
|
||
|
|
contact.Zip,
|
||
|
|
contact.Country,
|
||
|
|
);
|
||
|
|
});
|
||
|
|
|
||
|
|
request.input('p_clientlocaddresstable', clientLocAddressTable);
|
||
|
|
|
||
|
|
const result = await request.execute('carnetsys.CreateClientData');
|
||
|
|
return { data: result.recordset };
|
||
|
|
} catch (error) {
|
||
|
|
console.error('Error executing stored procedure:', error);
|
||
|
|
return { error: error.message };
|
||
|
|
} finally {
|
||
|
|
if (connection) {
|
||
|
|
connection.close();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
GetPreparerByClientid = async (body: GetPreparerByClientidContactsByClientidLocByClientidDTO) => {
|
||
|
|
let connection;
|
||
|
|
|
||
|
|
try {
|
||
|
|
connection = await this.mssqlDBService.getConnection();
|
||
|
|
const request = new Request(connection);
|
||
|
|
request.input('p_spid', mssql.Int, body.p_spid);
|
||
|
|
request.input('p_clientid', mssql.Int, body.p_clientid);
|
||
|
|
const result = await request.execute('carnetsys.GetPreparerbyClientID');
|
||
|
|
return { data: result.recordset };
|
||
|
|
} catch (error) {
|
||
|
|
console.error('Error executing stored procedure:', error);
|
||
|
|
} finally {
|
||
|
|
if (connection) {
|
||
|
|
connection.close();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
};
|
||
|
|
}
|