593 lines
19 KiB
TypeScript
593 lines
19 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { Connection, Request } from 'mssql';
|
|
import { MssqlDBService } from 'src/db/db.service';
|
|
import { CreateClientContactsDTO, CreateClientDataDTO, CreateClientLocationsDTO, GetPreparerByClientidContactsByClientidLocByClientidDTO, GetPreparersDTO, UpdateClientContactsDTO, UpdateClientDTO, UpdateClientLocationsDTO } 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; // Fixed duplicate declaration
|
|
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) {
|
|
|
|
return { error: error.message };
|
|
}
|
|
};
|
|
|
|
GetPreparerByClientid = async (body: GetPreparerByClientidContactsByClientidLocByClientidDTO) => {
|
|
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_clientid', mssql.Int, body.p_clientid);
|
|
const result = await request.execute('carnetsys.GetPreparerbyClientID');
|
|
return { data: result.recordset };
|
|
} catch (error) {
|
|
return { error: error.message };
|
|
}
|
|
};
|
|
|
|
UpdateClient = async (body: UpdateClientDTO) => {
|
|
const newBody = {
|
|
p_spid: null,
|
|
p_clientid: null,
|
|
p_preparername: null,
|
|
p_address1: null,
|
|
p_address2: null,
|
|
p_city: null,
|
|
p_state: null,
|
|
p_zip: null,
|
|
p_country: null,
|
|
p_revenuelocation: null,
|
|
p_userid: null,
|
|
};
|
|
|
|
const reqBody = JSON.parse(JSON.stringify(body));
|
|
|
|
function setEmptyStringsToNull(obj) {
|
|
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: UpdateClientDTO = { ...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_clientid', mssql.Int, finalBody.p_clientid);
|
|
request.input('p_preparername', mssql.VarChar(50), finalBody.p_preparername);
|
|
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_revenuelocation', mssql.VarChar(2), finalBody.p_revenuelocation);
|
|
request.input('p_userid', mssql.VarChar(100), finalBody.p_userid);
|
|
|
|
|
|
const result = await request.execute('carnetsys.UpdateClient');
|
|
return { data: result.recordset };
|
|
} catch (error) {
|
|
|
|
return { error: error.message };
|
|
}
|
|
};
|
|
|
|
CreateClientContact = async (body: CreateClientContactsDTO) => {
|
|
const newBody = {
|
|
p_spid: null,
|
|
p_clientid: null,
|
|
p_contactstable: null,
|
|
p_defcontactflag: null,
|
|
p_userid: null,
|
|
};
|
|
|
|
const reqBody = JSON.parse(JSON.stringify(body));
|
|
|
|
function setEmptyStringsToNull(obj) {
|
|
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: CreateClientContactsDTO = {
|
|
...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_clientid', mssql.Int, finalBody.p_clientid);
|
|
|
|
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.P_FIRSTNAME,
|
|
contact.P_LASTNAME,
|
|
contact.P_MIDDLEINITIAL,
|
|
contact.P_TITLE,
|
|
contact.P_EMAILADDRESS,
|
|
contact.P_PHONENO,
|
|
contact.P_MOBILENO,
|
|
contact.P_FAXNO,
|
|
);
|
|
});
|
|
|
|
request.input('p_contactstable', contactTable);
|
|
|
|
request.input('p_defcontactflag', mssql.VarChar(1), finalBody.p_defcontactflag);
|
|
request.input('p_userid', mssql.VarChar(100), finalBody.p_userid);
|
|
|
|
|
|
const result = await request.execute('carnetsys.CreateClientContact');
|
|
return { data: result.recordset };
|
|
} catch (error) {
|
|
|
|
return { error: error.message };
|
|
}
|
|
};
|
|
|
|
UpdateClientContacts = async (body: UpdateClientContactsDTO) => {
|
|
const newBody = {
|
|
p_spid: null,
|
|
p_clientcontactid: null,
|
|
p_firstname: null,
|
|
p_lastname: null,
|
|
p_middleinitial: null,
|
|
p_title: null,
|
|
p_phone: null,
|
|
p_fax: null,
|
|
p_mobileno: null,
|
|
p_emailaddress: null,
|
|
p_userid: null,
|
|
};
|
|
|
|
const reqBody = JSON.parse(JSON.stringify(body));
|
|
|
|
function setEmptyStringsToNull(obj) {
|
|
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: UpdateClientContactsDTO = { ...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_clientcontactid', mssql.Int, finalBody.p_clientcontactid);
|
|
request.input('p_firstname', mssql.VarChar(50), finalBody.p_firstname);
|
|
request.input('p_lastname', mssql.VarChar(50), finalBody.p_lastname);
|
|
request.input('p_middleinitial', mssql.VarChar(3), finalBody.p_middleinitial);
|
|
request.input('p_title', mssql.VarChar(50), finalBody.p_title);
|
|
request.input('p_phone', mssql.VarChar(20), finalBody.p_phone);
|
|
request.input('p_fax', mssql.VarChar(20), finalBody.p_fax);
|
|
request.input('p_mobileno', mssql.VarChar(20), finalBody.p_mobileno);
|
|
request.input('p_emailaddress', mssql.VarChar(100), finalBody.p_emailaddress);
|
|
request.input('p_userid', mssql.VarChar(100), finalBody.p_userid);
|
|
|
|
const result = await request.execute('carnetsys.UpdateClientContacts');
|
|
return { data: result.recordset };
|
|
} catch (error) {
|
|
|
|
return { error: error.message };
|
|
}
|
|
|
|
};
|
|
|
|
UpdateClientLocations = async (body: UpdateClientLocationsDTO) => {
|
|
const newBody = {
|
|
p_spid: null,
|
|
p_clientlocationid: null,
|
|
p_lcoationname: null,
|
|
p_address1: null,
|
|
p_address2: null,
|
|
p_city: null,
|
|
p_state: null,
|
|
p_zip: null,
|
|
p_country: null,
|
|
p_userid: null,
|
|
};
|
|
|
|
const reqBody = JSON.parse(JSON.stringify(body));
|
|
|
|
function setEmptyStringsToNull(obj) {
|
|
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: UpdateClientLocationsDTO = { ...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_clientlocationid', mssql.Int, finalBody.p_clientlocationid);
|
|
request.input('p_lcoationname', mssql.VarChar(50), finalBody.p_lcoationname);
|
|
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_userid', mssql.VarChar(100), finalBody.p_userid);
|
|
|
|
const result = await request.execute('carnetsys.UpdateClientlocations');
|
|
return { data: result.recordset };
|
|
} catch (error) {
|
|
|
|
return { error: error.message };
|
|
}
|
|
|
|
};
|
|
|
|
CreateClientLocation = async (body: CreateClientLocationsDTO) => {
|
|
const newBody = {
|
|
p_spid: null,
|
|
p_clientid: null,
|
|
p_clientlocaddresstable: null,
|
|
p_defcontactflag: null,
|
|
p_userid: null,
|
|
};
|
|
|
|
const reqBody = JSON.parse(JSON.stringify(body));
|
|
|
|
function setEmptyStringsToNull(obj) {
|
|
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: CreateClientLocationsDTO = {
|
|
...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_clientid', mssql.Int, finalBody.p_clientid);
|
|
|
|
|
|
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);
|
|
|
|
request.input('p_userid', mssql.VarChar(100), finalBody.p_userid);
|
|
|
|
const result = await request.execute('carnetsys.CreateClientLocation');
|
|
return { data: result.recordset };
|
|
} catch (error) {
|
|
|
|
return { error: error.message };
|
|
}
|
|
|
|
};
|
|
|
|
GetPreparers = async (body: GetPreparersDTO) => {
|
|
|
|
const newBody = {
|
|
p_spid: null,
|
|
p_name: null,
|
|
p_lookupcode: null,
|
|
p_city: null,
|
|
p_state: null,
|
|
p_status: null
|
|
};
|
|
|
|
const reqBody = JSON.parse(JSON.stringify(body));
|
|
|
|
function setEmptyStringsToNull(obj) {
|
|
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: GetPreparersDTO = {
|
|
...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_name', mssql.VarChar(50), finalBody.p_name);
|
|
request.input('p_lookupcode', mssql.VarChar(30), finalBody.p_lookupcode);
|
|
request.input('p_city', mssql.VarChar(20), finalBody.p_city);
|
|
request.input('p_state', mssql.VarChar(2), finalBody.p_state);
|
|
request.input('p_status', mssql.VarChar(10), finalBody.p_status);
|
|
|
|
const result = await request.execute('carnetsys.GetPreparers');
|
|
return { data: result.recordset };
|
|
|
|
} catch (error) {
|
|
|
|
return { error: error.message };
|
|
}
|
|
|
|
};
|
|
|
|
GetPreparerContactsByClientid = async (
|
|
body: GetPreparerByClientidContactsByClientidLocByClientidDTO,
|
|
) => {
|
|
|
|
const newBody = {
|
|
p_spid: null,
|
|
p_name: null,
|
|
p_lookupcode: null,
|
|
p_city: null,
|
|
p_state: null,
|
|
p_status: null
|
|
};
|
|
|
|
const reqBody = JSON.parse(JSON.stringify(body));
|
|
|
|
function setEmptyStringsToNull(obj) {
|
|
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: GetPreparerByClientidContactsByClientidLocByClientidDTO = {
|
|
...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_clientid', mssql.Int, finalBody.p_clientid);
|
|
|
|
const result = await request.execute('carnetsys.GetPreparerContactsbyClientID');
|
|
return { data: result.recordset };
|
|
|
|
} catch (error) {
|
|
|
|
return { error: error.message };
|
|
}
|
|
|
|
};
|
|
|
|
GetPreparerLocByClientid = async (
|
|
body: GetPreparerByClientidContactsByClientidLocByClientidDTO,
|
|
) => {
|
|
const newBody = {
|
|
p_spid: null,
|
|
p_name: null,
|
|
p_lookupcode: null,
|
|
p_city: null,
|
|
p_state: null,
|
|
p_status: null
|
|
};
|
|
|
|
const reqBody = JSON.parse(JSON.stringify(body));
|
|
|
|
function setEmptyStringsToNull(obj) {
|
|
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: GetPreparerByClientidContactsByClientidLocByClientidDTO = {
|
|
...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_clientid', mssql.Int, finalBody.p_clientid);
|
|
|
|
const result = await request.execute('carnetsys.GetPreparerLocbyClientID');
|
|
return { data: result.recordset };
|
|
|
|
} catch (error) {
|
|
|
|
return { error: error.message };
|
|
}
|
|
};
|
|
|
|
}
|