267 lines
11 KiB
TypeScript
267 lines
11 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { Connection, Request } from 'mssql';
|
|
import { MssqlDBService } from 'src/db/db.service';
|
|
import { CreateHoldersDTO, GetHolderDTO, HolderActivateOrInactivateDTO, HolderContactActivateOrInactivateDTO, UpdateHolderContactDTO, UpdateHolderDTO } from 'src/oracle/manage-holders/manage-holders.dto';
|
|
import * as mssql from 'mssql';
|
|
|
|
@Injectable()
|
|
export class ManageHoldersService {
|
|
constructor(private readonly mssqlDBService: MssqlDBService) { }
|
|
|
|
CreateHolders = async (body: CreateHoldersDTO) => {
|
|
const newBody = {
|
|
p_spid: null,
|
|
p_clientlocationid: null,
|
|
p_holderno: null,
|
|
p_holdertype: null,
|
|
p_uscibmemberflag: null,
|
|
p_govagencyflag: null,
|
|
p_holdername: null,
|
|
p_namequalifier: null,
|
|
p_addlname: null,
|
|
p_address1: null,
|
|
p_address2: null,
|
|
p_city: null,
|
|
p_state: null,
|
|
p_zip: null,
|
|
p_country: null,
|
|
p_userid: null,
|
|
p_contactstable: 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: CreateHoldersDTO = { ...newBody, ...reqBody };
|
|
|
|
let connection: Connection;
|
|
try {
|
|
connection = await this.mssqlDBService.getConnection();
|
|
const request: 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_holderno', mssql.VarChar(15), finalBody.p_holderno);
|
|
request.input('p_holdertype', mssql.VarChar(3), finalBody.p_holdertype);
|
|
request.input('p_uscibmemberflag', mssql.VarChar(1), finalBody.p_uscibmemberflag);
|
|
request.input('p_govagencyflag', mssql.VarChar(1), finalBody.p_govagencyflag);
|
|
request.input('p_holdername', mssql.VarChar(50), finalBody.p_holdername);
|
|
request.input('p_namequalifier', mssql.VarChar(10), finalBody.p_namequalifier);
|
|
request.input('p_addlname', mssql.VarChar(50), finalBody.p_addlname);
|
|
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(50), 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 && 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 result = await request.execute('carnetsys.CreateHolderData');
|
|
return { data: result.recordset };
|
|
} catch (error) {
|
|
|
|
return { error: error.message };
|
|
}
|
|
|
|
};
|
|
|
|
GetHolderRecord = async (body: GetHolderDTO) => {
|
|
let connection: Connection;
|
|
try {
|
|
connection = await this.mssqlDBService.getConnection();
|
|
const request: Request = new Request(connection);
|
|
request.input('p_spid', mssql.Int, body.p_spid);
|
|
request.input('p_holderid', mssql.Int, body.p_holderid);
|
|
|
|
const result = await request.execute('carnetsys.GetHolderMaster');
|
|
return { data: result.recordset };
|
|
} catch (error) {
|
|
|
|
return { error: error.message };
|
|
}
|
|
};
|
|
|
|
UpdateHolder = async (body: UpdateHolderDTO) => {
|
|
let connection: Connection;
|
|
try {
|
|
connection = await this.mssqlDBService.getConnection();
|
|
const request: Request = new mssql.Request(connection);
|
|
|
|
request.input('p_holderid', mssql.Int, body.p_holderid);
|
|
request.input('p_spid', mssql.Int, body.p_spid);
|
|
request.input('p_locationid', mssql.Int, body.p_locationid);
|
|
request.input('p_holderno', mssql.VarChar(15), body.p_holderno);
|
|
request.input('p_holdertype', mssql.VarChar(3), body.p_holdertype);
|
|
request.input('p_uscibmemberflag', mssql.VarChar(1), body.p_uscibmemberflag);
|
|
request.input('p_govagencyflag', mssql.VarChar(1), body.p_govagencyflag);
|
|
request.input('p_holdername', mssql.VarChar(50), body.p_holdername);
|
|
request.input('p_namequalifier', mssql.VarChar(10), body.p_namequalifier);
|
|
request.input('p_addlname', mssql.VarChar(50), body.p_addlname);
|
|
request.input('p_address1', mssql.VarChar(50), body.p_address1);
|
|
request.input('p_address2', mssql.VarChar(50), body.p_address2);
|
|
request.input('p_city', mssql.VarChar(30), body.p_city);
|
|
request.input('p_state', mssql.VarChar(2), body.p_state);
|
|
request.input('p_zip', mssql.VarChar(10), body.p_zip);
|
|
request.input('p_country', mssql.VarChar(2), body.p_country);
|
|
request.input('p_userid', mssql.VarChar(100), body.p_userid);
|
|
|
|
const result = await request.execute('carnetsys.UpdateHolders');
|
|
return { data: result.recordset };
|
|
} catch (error) {
|
|
|
|
return { error: error.message };
|
|
}
|
|
};
|
|
|
|
UpdateHolderContact = async (body: UpdateHolderContactDTO) => {
|
|
|
|
let connection: Connection;
|
|
try {
|
|
connection = await this.mssqlDBService.getConnection();
|
|
const request: Request = new mssql.Request(connection);
|
|
|
|
request.input('p_holdercontactid', mssql.Int, body.p_holdercontactid);
|
|
request.input('p_spid', mssql.Int, body.p_spid);
|
|
request.input('p_firstname', mssql.VarChar(50), body.p_firstname);
|
|
request.input('p_lastname', mssql.VarChar(50), body.p_lastname);
|
|
request.input('p_middleinitial', mssql.VarChar(3), body.p_middleinitial);
|
|
request.input('p_title', mssql.VarChar(50), body.p_title);
|
|
request.input('p_phone', mssql.VarChar(20), body.p_phone);
|
|
request.input('p_mobile', mssql.VarChar(20), body.p_mobile);
|
|
request.input('p_fax', mssql.VarChar(20), body.p_fax);
|
|
request.input('p_emailaddress', mssql.VarChar(100), body.p_emailaddress);
|
|
request.input('p_userid', mssql.VarChar(100), body.p_userid);
|
|
|
|
|
|
const result = await request.execute('carnetsys.UpdateHolderContacts');
|
|
return { data: result.recordset };
|
|
} catch (error) {
|
|
|
|
return { error: error.message };
|
|
}
|
|
};
|
|
|
|
GetHolderContacts = async (body: GetHolderDTO) => {
|
|
|
|
let connection: Connection;
|
|
try {
|
|
connection = await this.mssqlDBService.getConnection();
|
|
const request: Request = new Request(connection);
|
|
request.input('p_spid', mssql.Int, body.p_spid);
|
|
request.input('p_holderid', mssql.Int, body.p_holderid);
|
|
|
|
const result = await request.execute('carnetsys.GetHolderContacts');
|
|
return { data: result.recordset };
|
|
} catch (error) {
|
|
|
|
return { error: error.message };
|
|
}
|
|
};
|
|
|
|
InactivateHolder = async (body: HolderActivateOrInactivateDTO) => {
|
|
|
|
console.log(body);
|
|
|
|
let connection: Connection;
|
|
try {
|
|
connection = await this.mssqlDBService.getConnection();
|
|
const request: Request = new Request(connection);
|
|
request.input('p_spid', mssql.Int, body.p_spid);
|
|
request.input('p_holderid', mssql.Int, body.p_holderid);
|
|
request.input('p_userid', mssql.VarChar(100), body.p_userid);
|
|
const result = await request.execute('carnetsys.InactivateHolder');
|
|
return { data: result.recordset };
|
|
} catch (error) {
|
|
|
|
return { error: error.message };
|
|
}
|
|
};
|
|
|
|
ReactivateHolder = async (body: HolderActivateOrInactivateDTO) => {
|
|
let connection: Connection;
|
|
try {
|
|
connection = await this.mssqlDBService.getConnection();
|
|
const request: Request = new Request(connection);
|
|
request.input('p_spid', mssql.Int, body.p_spid);
|
|
request.input('p_holderid', mssql.Int, body.p_holderid);
|
|
request.input('p_userid', mssql.VarChar(100), body.p_userid);
|
|
|
|
const result = await request.execute('carnetsys.ReactivateHolder');
|
|
return { data: result.recordset };
|
|
} catch (error) {
|
|
|
|
return { error: error.message };
|
|
}
|
|
};
|
|
|
|
InactivateHolderContact = async (body: HolderContactActivateOrInactivateDTO) => {
|
|
let connection: Connection;
|
|
try {
|
|
connection = await this.mssqlDBService.getConnection();
|
|
const request: Request = new Request(connection);
|
|
request.input('p_spid', mssql.Int, body.p_spid);
|
|
request.input('p_holderContactid', mssql.Int, body.p_holderContactid);
|
|
request.input('p_userid', mssql.VarChar(100), body.p_userid);
|
|
|
|
const result = await request.execute('carnetsys.InactivateHolderContact');
|
|
return { data: result.recordset };
|
|
} catch (error) {
|
|
|
|
return { error: error.message };
|
|
}
|
|
};
|
|
|
|
ReactivateHolderContact = async (body: HolderContactActivateOrInactivateDTO) => {
|
|
let connection: Connection;
|
|
try {
|
|
connection = await this.mssqlDBService.getConnection();
|
|
const request: Request = new Request(connection);
|
|
request.input('p_spid', mssql.Int, body.p_spid);
|
|
request.input('p_holderContactid', mssql.Int, body.p_holderContactid);
|
|
request.input('p_userid', mssql.VarChar(100), body.p_userid);
|
|
|
|
const result = await request.execute('carnetsys.ReactivateHolderContact');
|
|
return { data: result.recordset };
|
|
} catch (error) {
|
|
|
|
return { error: error.message };
|
|
}
|
|
};
|
|
}
|