Skip to content

Commit

Permalink
Merge pull request #1067 from egovernments/ashish-egov-patch-2
Browse files Browse the repository at this point in the history
Update boundaryUtils.ts
  • Loading branch information
ashish-egov authored Jul 15, 2024
2 parents ef1e287 + 67372f8 commit 46df829
Showing 1 changed file with 40 additions and 11 deletions.
51 changes: 40 additions & 11 deletions utilities/boundary-bulk-bff/src/server/utils/boundaryUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,18 +312,22 @@ async function getAutoGeneratedBoundaryCodesHandler(boundaryList: any, childPare

async function createBoundaryEntities(request: any, boundaryMap: Map<any, any>) {
const updatedBoundaryMap: Array<{ key: string, value: string }> = Array.from(boundaryMap).map(([key, value]) => ({ key: key.value, value: value }));

// Create boundary entities
const requestBody = { "RequestInfo": request.body.RequestInfo } as { RequestInfo: any; Boundary?: any };
const boundaries: any[] = [];
const codesFromResponse: any = [];
const boundaryCodes: any[] = [];

Array.from(boundaryMap.entries()).forEach(([, boundaryCode]) => {
boundaryCodes.push(boundaryCode);
});

const boundaryEntitiesCreated: any[] = [];
const boundaryEntityCreateChunkSize = 200;
const chunkSize = 20;
const boundaryCodeChunks = [];

for (let i = 0; i < boundaryCodes.length; i += chunkSize) {
boundaryCodeChunks.push(boundaryCodes.slice(i, i + chunkSize));
}
Expand All @@ -332,14 +336,26 @@ async function createBoundaryEntities(request: any, boundaryMap: Map<any, any>)
const boundaryCodeString = chunk.join(', ');
if (chunk.length > 0 && boundaryCodeString) {
logger.info(`Creating boundary entities for codes: ${boundaryCodeString}`);
const boundaryEntityResponse = await httpRequest(config.host.boundaryHost + config.paths.boundaryEntitySearch, request.body, { tenantId: request?.query?.tenantId, codes: boundaryCodeString });
const boundaryCodesFromResponse = await boundaryEntityResponse.Boundary.flatMap((boundary: any) => boundary.code.toString());
await codesFromResponse.push(...boundaryCodesFromResponse);

try {
const boundaryEntityResponse = await httpRequest(
config.host.boundaryHost + config.paths.boundaryEntitySearch,
request.body,
{ tenantId: request?.query?.tenantId, codes: boundaryCodeString }
);
const boundaryCodesFromResponse = boundaryEntityResponse.Boundary.flatMap((boundary: any) => boundary.code.toString());
codesFromResponse.push(...boundaryCodesFromResponse);
} catch (error: any) {
console.log(error);
logger.error(`Failed to fetch boundary entities for codes: ${boundaryCodeString}, Error: ${error.message}`);
throw error;
}
} else {
logger.debug(`Skipping empty or invalid chunk: ${boundaryCodeString}`);
}
}


const codeSet = new Set(codesFromResponse);// Creating a set and filling it with the codes from the response
const codeSet = new Set(codesFromResponse); // Creating a set and filling it with the codes from the response
for (const { key: boundaryName, value: boundaryCode } of updatedBoundaryMap) {
if (!codeSet.has(boundaryCode.toString())) {
const boundary = {
Expand All @@ -352,22 +368,35 @@ async function createBoundaryEntities(request: any, boundaryMap: Map<any, any>)
};
boundaries.push(boundary);
}
};
}

const totalBoundaryCount = boundaries.length;
if (!(totalBoundaryCount === 0)) {
if (totalBoundaryCount !== 0) {
let createdCount = 0;
for (let i = 0; i < totalBoundaryCount; i += boundaryEntityCreateChunkSize) {
requestBody.Boundary = boundaries.slice(i, i + boundaryEntityCreateChunkSize);
const response = await httpRequest(`${config.host.boundaryHost}${config.paths.boundaryEntityCreate}`, requestBody, {}, 'POST');
boundaryEntitiesCreated.push(response)
createdCount += requestBody.Boundary.length;
persistEntityCreate(request, createdCount, totalBoundaryCount);
try {
const response = await httpRequest(
`${config.host.boundaryHost}${config.paths.boundaryEntityCreate}`,
requestBody,
{},
'POST'
);
boundaryEntitiesCreated.push(response);
createdCount += requestBody.Boundary.length;
persistEntityCreate(request, createdCount, totalBoundaryCount);
} catch (error: any) {
console.log(error);
logger.error(`Failed to create boundary entities, Error: ${error.message}`);
throw error;
}
}
logger.info('Boundary entities created');
logger.debug('Boundary entities response: ' + getFormattedStringForDebug(boundaryEntitiesCreated));
}
}


function findMapValue(map: Map<any, any>, key: any): any | null {
let foundValue = null;
map.forEach((value, mapKey) => {
Expand Down

0 comments on commit 46df829

Please sign in to comment.