Skip to content

Commit

Permalink
MDMS integration for targetConfigs
Browse files Browse the repository at this point in the history
  • Loading branch information
ashish-egov committed Dec 27, 2024
1 parent 407c61c commit 3c58171
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 122 deletions.
12 changes: 0 additions & 12 deletions health-services/project-factory/src/server/api/campaignApis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1848,21 +1848,9 @@ async function projectCreate(projectCreateBody: any, request: any) {
logger.info(
`for boundary type ${projectCreateResponse?.Project[0]?.address?.boundaryType} and code ${projectCreateResponse?.Project[0]?.address?.boundary}`
);
// if (
// !request.body.newlyCreatedBoundaryProjectMap[
// projectCreateBody?.Projects?.[0]?.address?.boundary
// ]
// ) {
// request.body.newlyCreatedBoundaryProjectMap[
// projectCreateBody?.Projects?.[0]?.address?.boundary
// ] = {};
// }
request.body.boundaryProjectMapping[
projectCreateBody?.Projects?.[0]?.address?.boundary
].projectId = projectCreateResponse?.Project[0]?.id;
// request.body.newlyCreatedBoundaryProjectMap[
// projectCreateBody?.Projects?.[0]?.address?.boundary
// ].projectId = projectCreateResponse?.Project[0]?.id;
} else {
throwError(
"PROJECT",
Expand Down
62 changes: 2 additions & 60 deletions health-services/project-factory/src/server/api/genericApis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import FormData from "form-data"; // Import FormData for handling multipart/form
import { defaultheader, httpRequest } from "../utils/request"; // Import httpRequest function for making HTTP requests
import { getFormattedStringForDebug, logger } from "../utils/logger"; // Import logger for logging
import { correctParentValues, findMapValue, generateActivityMessage, getBoundaryRelationshipData, getDataSheetReady, getLocalizedHeaders, sortCampaignDetails, throwError } from "../utils/genericUtils"; // Import utility functions
import { extractCodesFromBoundaryRelationshipResponse, generateFilteredBoundaryData, getConfigurableColumnHeadersBasedOnCampaignType, getFiltersFromCampaignSearchResponse, getLocalizedName } from '../utils/campaignUtils'; // Import utility functions
import { extractCodesFromBoundaryRelationshipResponse, generateFilteredBoundaryData, getConfigurableColumnHeadersBasedOnCampaignType, getFiltersFromCampaignSearchResponse, getLocalizedName, processDataForTargetCalculation } from '../utils/campaignUtils'; // Import utility functions
import { getCampaignSearchResponse, getHierarchy } from './campaignApis';
const _ = require('lodash'); // Import lodash library
import { getExcelWorkbookFromFileURL } from "../utils/excelUtils";
import { processMapping } from "../utils/campaignMappingUtils";
import { targetConfigs } from "../config/targetConfigs";


//Function to get Workbook with different tabs (for type target)
const getTargetWorkbook = async (fileUrl: string, localizationMap?: any) => {
Expand Down Expand Up @@ -226,63 +224,7 @@ const getTargetSheetDataAfterCode = async (
console.warn(`Column "${codeColumnName}" not found in sheet "${sheetName}".`);
continue;
}

// Process each row of the sheet data
const processedData = jsonData.map((row: any, rowIndex: any) => {

// Initialize an object to hold row-specific data
let rowData: any = { [codeColumnName]: row[codeColumnName] };

// Add placeholder fields for Parent Target and Current Target data
rowData['Parent Target at the Selected Boundary level'] = {};
rowData['Target at the Selected Boundary level'] = {};
const beneficiaries = targetConfigs?.[request?.body?.CampaignDetails?.projectType]?.beneficiaries;

// Check if a parent campaign exists in the request body
if (request?.body?.parentCampaign) {
// Loop through the beneficiaries for the specified campaign type
if (beneficiaries?.length > 0) {
for (const beneficiary of beneficiaries) {
const beneficiaryType = beneficiary?.beneficiaryType;
const columns = beneficiary?.columns;
let totalParentValue = 0;

// Loop through each column to calculate the total parent value
for (const col of columns) {
// Get the parent value from the column and add it if it's an integer
const parentValue = row[`${getLocalizedName(col, localizationMap)}(OLD)`];
if (typeof parentValue === 'number' && Number.isInteger(parentValue)) {
totalParentValue += parentValue;
}
}
// Assign the total parent value to the corresponding beneficiary type
rowData['Parent Target at the Selected Boundary level'][beneficiaryType] = totalParentValue;
}
}
}

// Loop through the beneficiaries again to calculate the current target values
if (beneficiaries?.length > 0) {
for (const beneficiary of beneficiaries) {
const beneficiaryType = beneficiary?.beneficiaryType;
const columns = beneficiary?.columns;
let totalCurrentValue = 0;

// Loop through each column to calculate the total current value
for (const col of columns) {
const currentValue = row[getLocalizedName(col, localizationMap)];
if (typeof currentValue === 'number' && Number.isInteger(currentValue)) {
totalCurrentValue += currentValue;
}
}
// Assign the total current value to the corresponding beneficiary type
rowData['Target at the Selected Boundary level'][beneficiaryType] = totalCurrentValue;
}
}

// Return the processed row data
return rowData;
}).filter(Boolean); // Remove any null entries from the map (i.e., skip the header row)
const processedData = await processDataForTargetCalculation(request, jsonData, codeColumnName, localizationMap);

workbookData[sheetName] = processedData;
}
Expand Down
50 changes: 0 additions & 50 deletions health-services/project-factory/src/server/config/targetConfigs.ts

This file was deleted.

79 changes: 79 additions & 0 deletions health-services/project-factory/src/server/utils/campaignUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
createAndUploadJsonFile,
createBoundaryRelationship,
getSheetData,
searchMDMS
} from "../api/genericApis";
import { getFormattedStringForDebug, logger } from "./logger";
import createAndSearch from "../config/createAndSearch";
Expand Down Expand Up @@ -3023,6 +3024,84 @@ async function updateAndPersistResourceDetails(
);
}

export async function processDataForTargetCalculation(request: any, jsonData: any, codeColumnName: string, localizationMap?: any) {
// Retrieve targetConfigs from MDMS
const targetConfigs = await searchMDMS([request?.body?.CampaignDetails?.projectType], "HCM-ADMIN-CONSOLE.targetConfigs", request?.body?.RequestInfo);

// Process each row of the sheet data
const resultantData = jsonData.map((row: any) => {

// Initialize an object to hold row-specific data
let rowData: any = { [codeColumnName]: row[codeColumnName] };

// Add placeholder fields for Parent Target and Current Target data
rowData['Parent Target at the Selected Boundary level'] = {};
rowData['Target at the Selected Boundary level'] = {};
const beneficiaries = targetConfigs?.mdms?.[0]?.data?.beneficiaries;
// Calculate the parent target values
calculateTargetsAtParentLevel(request, row, rowData, beneficiaries, localizationMap);
// Calculate the current target values
calculateTargetsAtCurrentLevel(row, rowData, beneficiaries, localizationMap);

// Return the processed row data
return rowData;
}).filter(Boolean); // Remove any null entries from the map (i.e., skip the header row)

return resultantData;
}

export function calculateTargetsAtParentLevel(request: any, row: any, rowData: any, beneficiaries: any, localizationMap?: any) {
// Check if a parent campaign exists in the request body
if (request?.body?.parentCampaign) {
// Loop through the beneficiaries for the specified campaign type
if (Array.isArray(beneficiaries) && beneficiaries?.length > 0) {
for (const beneficiary of beneficiaries) {
const beneficiaryType = beneficiary?.beneficiaryType;
const columns = beneficiary?.columns;
let totalParentValue = 0;

// Loop through each column to calculate the total parent value
for (const col of columns) {
// Get the parent value from the column and add it if it's an integer
const parentValue = row[`${getLocalizedName(col, localizationMap)}(OLD)`];
if (typeof parentValue === 'number' && Number.isInteger(parentValue)) {
totalParentValue += parentValue;
}
}
// Assign the total parent value to the corresponding beneficiary type
rowData['Parent Target at the Selected Boundary level'][beneficiaryType] = totalParentValue;
}
}
else {
logger.warn("No beneficiaries config found for the specified campaign type");
}
}
}

export function calculateTargetsAtCurrentLevel(row: any, rowData: any, beneficiaries: any, localizationMap?: any) {
// Loop through the beneficiaries again to calculate the current target values
if (Array.isArray(beneficiaries) && beneficiaries?.length > 0) {
for (const beneficiary of beneficiaries) {
const beneficiaryType = beneficiary?.beneficiaryType;
const columns = beneficiary?.columns;
let totalCurrentValue = 0;

// Loop through each column to calculate the total current value
for (const col of columns) {
const currentValue = row[getLocalizedName(col, localizationMap)];
if (typeof currentValue === 'number' && Number.isInteger(currentValue)) {
totalCurrentValue += currentValue;
}
}
// Assign the total current value to the corresponding beneficiary type
rowData['Target at the Selected Boundary level'][beneficiaryType] = totalCurrentValue;
}
}
else {
logger.warn("No beneficiaries config found for the specified campaign type");
}
}

async function getResourceDetails(request: any) {
const { tenantId, type, hierarchyType } =
request?.body?.ResourceDetails || request?.query;
Expand Down

0 comments on commit 3c58171

Please sign in to comment.