diff --git a/health-services/project-factory/src/server/api/campaignApis.ts b/health-services/project-factory/src/server/api/campaignApis.ts index 0e02024452..841ffa7b51 100644 --- a/health-services/project-factory/src/server/api/campaignApis.ts +++ b/health-services/project-factory/src/server/api/campaignApis.ts @@ -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", diff --git a/health-services/project-factory/src/server/api/genericApis.ts b/health-services/project-factory/src/server/api/genericApis.ts index d6cc8d0807..37eab0ba05 100644 --- a/health-services/project-factory/src/server/api/genericApis.ts +++ b/health-services/project-factory/src/server/api/genericApis.ts @@ -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) => { @@ -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; } diff --git a/health-services/project-factory/src/server/config/targetConfigs.ts b/health-services/project-factory/src/server/config/targetConfigs.ts deleted file mode 100644 index 20ae616b36..0000000000 --- a/health-services/project-factory/src/server/config/targetConfigs.ts +++ /dev/null @@ -1,50 +0,0 @@ -export const targetConfigs: any = { - "LLIN-mz": { - "beneficiaries": [ - { - "beneficiaryType": "INDIVIDUAL", - "columns": ["HCM_ADMIN_CONSOLE_TARGET_BEDNET_COLUMN_2"] - }, - { - "beneficiaryType": "HOUSEHOLD", - "columns": ["HCM_ADMIN_CONSOLE_TARGET"] - }, - { - "beneficiaryType": "PRODUCT", - "columns": ["HCM_ADMIN_CONSOLE_TARGET_BEDNET_COLUMN_3"] - } - ] - }, - "MR-DN": { - "beneficiaries": [ - { - "beneficiaryType": "INDIVIDUAL", - "columns": ["HCM_ADMIN_CONSOLE_TARGET_SMC_AGE_3_TO_11", "HCM_ADMIN_CONSOLE_TARGET_SMC_AGE_12_TO_59"] - }, - { - "beneficiaryType": "HOUSEHOLD", - "columns": ["HCM_ADMIN_CONSOLE_TARGET_SMC_COLUUM_3"] - }, - { - "beneficiaryType": "PRODUCT", - "columns": ["HCM_ADMIN_CONSOLE_TARGET_SMC_COLUUM_4"] - } - ] - }, - "IRS-mz": { - "beneficiaries": [ - { - "beneficiaryType": "INDIVIDUAL", - "columns": ["HCM_ADMIN_CONSOLE_TARGET_IRS_4"] - }, - { - "beneficiaryType": "HOUSEHOLD", - "columns": ["HCM_ADMIN_CONSOLE_TARGET_IRS_1"] - }, - { - "beneficiaryType": "PRODUCT", - "columns": ["HCM_ADMIN_CONSOLE_TARGET_IRS_3"] - } - ] - } -} \ No newline at end of file diff --git a/health-services/project-factory/src/server/utils/campaignUtils.ts b/health-services/project-factory/src/server/utils/campaignUtils.ts index 2b5bc52dd9..821c75ae2f 100644 --- a/health-services/project-factory/src/server/utils/campaignUtils.ts +++ b/health-services/project-factory/src/server/utils/campaignUtils.ts @@ -25,6 +25,7 @@ import { createAndUploadJsonFile, createBoundaryRelationship, getSheetData, + searchMDMS } from "../api/genericApis"; import { getFormattedStringForDebug, logger } from "./logger"; import createAndSearch from "../config/createAndSearch"; @@ -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;