completed the client api for mssql

This commit is contained in:
Kallesh B S 2025-04-18 11:05:08 +05:30
parent ae153227f6
commit 0539c17ced
2 changed files with 343 additions and 14 deletions

View File

@ -1,7 +1,7 @@
import { Body, Controller, Get, Post, Put, Query } from '@nestjs/common';
import { ManageClientsService } from './manage-clients.service';
import { ApiTags } from '@nestjs/swagger';
import { CreateAdditionalClientContactsDTO, CreateClientDataDTO, GetPreparerByClientidContactsByClientidLocByClientidDTO, UpdateClientContactsDTO, UpdateClientDTO } from 'src/oracle/manage-clients/manage-clients.dto';
import { CreateAdditionalClientContactsDTO, CreateAdditionalClientLocationsDTO, CreateClientDataDTO, GetPreparerByClientidContactsByClientidLocByClientidDTO, GetPreparersDTO, UpdateClientContactsDTO, UpdateClientDTO, UpdateClientLocationsDTO } from 'src/oracle/manage-clients/manage-clients.dto';
@Controller('mssql')
export class ManageClientsController {
@ -13,20 +13,24 @@ export class ManageClientsController {
return this.manageClientsService.CreateClientData(body);
}
@ApiTags('Manage Clients - Mssql')
@Get('GetPreparerByClientid')
GetPreparerByClientid(
@Query() body: GetPreparerByClientidContactsByClientidLocByClientidDTO,
) {
return this.manageClientsService.GetPreparerByClientid(body);
}
@ApiTags('Manage Clients - Mssql')
@Put('UpdateClient')
async UpdateClient(@Body() body: UpdateClientDTO) {
return this.manageClientsService.UpdateClient(body);
}
@ApiTags('Manage Clients - Mssql')
@Put('UpdateClientContacts')
UpdateClientContacts(@Body() body: UpdateClientContactsDTO) {
return this.manageClientsService.UpdateClientContacts(body);
}
@ApiTags('Manage Clients - Mssql')
@Put('UpdateClientLocations')
UpdateClientLocations(@Body() body: UpdateClientLocationsDTO) {
return this.manageClientsService.UpdateClientLocations(body);
}
@ApiTags('Manage Clients - Mssql')
@Post('CreateAdditionalClientContacts')
CreateClientContact(@Body() body: CreateAdditionalClientContactsDTO) {
@ -34,10 +38,42 @@ export class ManageClientsController {
}
@ApiTags('Manage Clients - Mssql')
@Put('UpdateClientContacts')
UpdateClientContacts(@Body() body: UpdateClientContactsDTO) {
return this.manageClientsService.UpdateClientContacts(body);
@Post('CreateAdditionalClientLocations')
CreateClientLocation(@Body() body: CreateAdditionalClientLocationsDTO) {
return this.manageClientsService.CreateClientLocation(body);
}
@ApiTags('Manage Clients - Mssql')
@Get('GetPreparers')
async GetPreparers(@Query() query: GetPreparersDTO) {
return await this.manageClientsService.GetPreparers(query);
}
@ApiTags('Manage Clients - Mssql')
@Get('GetPreparerByClientid')
GetPreparerByClientid(
@Query() body: GetPreparerByClientidContactsByClientidLocByClientidDTO,
) {
return this.manageClientsService.GetPreparerByClientid(body);
}
@ApiTags('Manage Clients - Mssql')
@Get('GetPreparerContactsByClientid')
GetPreparerContactsByClientid(
@Query() body: GetPreparerByClientidContactsByClientidLocByClientidDTO,
) {
return this.manageClientsService.GetPreparerContactsByClientid(body);
}
@ApiTags('Manage Clients - Mssql')
@Get('GetPreparerLocByClientid')
GetPreparerLocByClientid(
@Query() body: GetPreparerByClientidContactsByClientidLocByClientidDTO,
) {
return this.manageClientsService.GetPreparerLocByClientid(body);
}
}

View File

@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common';
import { Connection, Request } from 'mssql';
import { MssqlDBService } from 'src/db/db.service';
import { CreateAdditionalClientContactsDTO, CreateClientDataDTO, GetPreparerByClientidContactsByClientidLocByClientidDTO, UpdateClientContactsDTO, UpdateClientDTO } from 'src/oracle/manage-clients/manage-clients.dto';
import { CreateAdditionalClientContactsDTO, CreateAdditionalClientLocationsDTO, CreateClientDataDTO, GetPreparerByClientidContactsByClientidLocByClientidDTO, GetPreparersDTO, UpdateClientContactsDTO, UpdateClientDTO, UpdateClientLocationsDTO } from 'src/oracle/manage-clients/manage-clients.dto';
import * as mssql from 'mssql';
@Injectable()
@ -42,7 +42,7 @@ export class ManageClientsService {
const finalBody: CreateClientDataDTO = { ...newBody, ...reqBody };
let connection: Connection; // Fixed duplicate declaration
let connection: Connection; // Fixed duplicate declaration
try {
connection = await this.mssqlDBService.getConnection();
const request = new Request(connection);
@ -337,4 +337,297 @@ let connection: Connection; // Fixed duplicate declaration
};
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) {
console.error('Error executing stored procedure:', error);
return { error: error.message };
} finally {
if (connection) {
connection.close();
}
}
};
CreateClientLocation = async (body: CreateAdditionalClientLocationsDTO) => {
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: CreateAdditionalClientLocationsDTO = {
...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_defcontactflag', mssql.VarChar(1), finalBody.p_defcontactflag);
request.input('p_userid', mssql.VarChar(100), finalBody.p_userid);
const result = await request.execute('carnetsys.CreateClientLocation');
return { data: result.recordset };
} catch (error) {
console.error('Error executing stored procedure:', error);
return { error: error.message };
} finally {
if (connection) {
connection.close();
}
}
};
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) {
console.error('Error executing stored procedure:', error);
return { error: error.message };
} finally {
if (connection) {
connection.close();
}
}
};
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) {
console.error('Error executing stored procedure:', error);
return { error: error.message };
} finally {
if (connection) {
connection.close();
}
}
};
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) {
console.error('Error executing stored procedure:', error);
return { error: error.message };
} finally {
if (connection) {
connection.close();
}
}
};
}