diff --git a/health/micro-ui/web/micro-ui-internals/packages/modules/campaign-manager/src/components/CampaignSummary.js b/health/micro-ui/web/micro-ui-internals/packages/modules/campaign-manager/src/components/CampaignSummary.js index a1ba93faff8..71872484ff9 100644 --- a/health/micro-ui/web/micro-ui-internals/packages/modules/campaign-manager/src/components/CampaignSummary.js +++ b/health/micro-ui/web/micro-ui-internals/packages/modules/campaign-manager/src/components/CampaignSummary.js @@ -85,44 +85,92 @@ function reverseDeliveryRemap(data, t) { let currentCycleIndex = null; let currentCycle = null; - // data.forEach((item, index) => { - // if (currentCycleIndex !== item.cycleNumber) { - // currentCycleIndex = item.cycleNumber; - // currentCycle = { - // cycleIndex: currentCycleIndex.toString(), - // startDate: item?.startDate ? Digit.Utils.date.convertEpochToDate(item?.startDate) : null, - // endDate: item?.endDate ? Digit.Utils.date.convertEpochToDate(item?.endDate) : null, - // active: index === 0, // Initialize active to false - // deliveries: [], - // }; - // reversedData.push(currentCycle); - // } + const operatorMapping = { + "<=": "LESS_THAN_EQUAL_TO", + ">=": "GREATER_THAN_EQUAL_TO", + "<": "LESS_THAN", + ">": "GREATER_THAN", + "==": "EQUAL_TO", + "!=": "NOT_EQUAL_TO", + "IN_BETWEEN": "IN_BETWEEN" + }; + + const cycles = data?.[0]?.cycles || []; + const transformedCycles = cycles.map((cycle) => { + const deliveries = cycle.deliveries?.map((delivery, deliveryIndex) => { + const doseCriteria = delivery.doseCriteria?.flatMap((criteria, ruleKey) => { + const products = criteria.ProductVariants.map((variant, key) => ({ + key: key + 1, + count: 1, + value: variant.productVariantId, + name: variant.name + })); + + const condition = criteria.condition; + let conditionParts = condition.split("and").map(part => part.trim()); + let rules = []; + conditionParts.forEach((part) => { + const parts = part.split(' ').filter(Boolean); + + let attributes = []; + if (parts.length === 5 && (parts[1] === "<=" || parts[1] === "<") && (parts[3] === "<" || parts[3] === "<=")) { + const toValue = parts[0]; + const fromValue = parts[4]; + attributes.push({ + key: 1, // Incrementing key for each attribute + operator: { code: operatorMapping["IN_BETWEEN"] }, + attribute: { code: parts[2] }, + fromValue, + toValue + }); + } else { + // Parse single conditions using regex + const match = part.match(/(.*?)\s*(<=|>=|<|>|==|!=)\s*(.*)/); + if (match) { + const attributeCode = match[1].trim(); + const operatorSymbol = match[2].trim(); + const value = match[3].trim(); + attributes.push({ + key: attributes.length + 1, // Incrementing key for each attribute + value, + operator: { code: operatorMapping[operatorSymbol] }, + attribute: { code: attributeCode } + }); + } + } - // const deliveryIndex = item.deliveryNumber.toString(); + // Add each part as a new delivery rule + rules.push({ + ruleKey: ruleKey + 1, + delivery: {}, + products, + attributes + }); + }); - // let delivery = currentCycle.deliveries.find((delivery) => delivery.deliveryIndex === deliveryIndex); + return rules; + }); - // if (!delivery) { - // delivery = { - // deliveryIndex: deliveryIndex, - // active: item.deliveryNumber === 1, // Set active to true only for the first delivery - // deliveryRules: [], - // }; - // currentCycle.deliveries.push(delivery); - // } + return { + active: true, + deliveryIndex: String(deliveryIndex + 1), + deliveryRules: doseCriteria + }; + }); + + return { + active: true, + cycleIndex: String(cycle.id), + deliveries: deliveries + }; +}); + +return transformedCycles; - // delivery.deliveryRules.push({ - // ruleKey: item.deliveryRuleNumber, - // delivery: {}, - // attributes: loopAndReturn(item.conditions, t), - // products: [...item.products], - // }); - // }); - return data; - return reversedData; } + function boundaryDataGrp(boundaryData) { // Create an empty object to hold grouped data by type const groupedData = {}; @@ -406,16 +454,16 @@ const CampaignSummary = (props) => { { key: "CAMPAIGN_NO_OF_CYCLES", value: - data?.[0]?.deliveryRules && data?.[0]?.deliveryRules.map((item) => item.cycleIndex)?.length > 0 - ? Math.max(...data?.[0]?.deliveryRules.map((item) => item.cycleIndex)) + data?.[0]?.additionalDetails?.cycleData?.cycleConfgureDate?.cycle ? + data?.[0]?.additionalDetails?.cycleData?.cycleConfgureDate?.cycle : t("CAMPAIGN_SUMMARY_NA"), }, { key: "CAMPAIGN_NO_OF_DELIVERIES", value: - data?.[0]?.deliveryRules && data?.[0]?.deliveryRules?.flatMap((rule) => rule?.deliveries?.map((delivery) => delivery?.deliveryIndex))?.length > 0 - ? Math.max(...data?.[0]?.deliveryRules?.flatMap((rule) => rule?.deliveries?.map((delivery) => delivery?.deliveryIndex))) - : t("CAMPAIGN_SUMMARY_NA"), + data?.[0]?.additionalDetails?.cycleData?.cycleConfgureDate?.deliveries ? + data?.[0]?.additionalDetails?.cycleData?.cycleConfgureDate?.deliveries + : t("CAMPAIGN_SUMMARY_NA"), }, ], }, diff --git a/health/micro-ui/web/micro-ui-internals/packages/modules/campaign-manager/src/components/CampaignUpdateSummary.js b/health/micro-ui/web/micro-ui-internals/packages/modules/campaign-manager/src/components/CampaignUpdateSummary.js index a4d590bdc62..9897d5140e5 100644 --- a/health/micro-ui/web/micro-ui-internals/packages/modules/campaign-manager/src/components/CampaignUpdateSummary.js +++ b/health/micro-ui/web/micro-ui-internals/packages/modules/campaign-manager/src/components/CampaignUpdateSummary.js @@ -214,7 +214,7 @@ const CampaignUpdateSummary = (props) => { }, cardHeader: { value: t("TARGET_DETAILS"), inlineStyles: { marginTop: 0, fontSize: "1.5rem" } }, cardSecondaryAction: noAction !== "false" && ( -
handleRedirect(2)}> +
handleRedirect(4)}> {t(`CAMPAIGN_EDIT`)}
@@ -234,7 +234,7 @@ const CampaignUpdateSummary = (props) => { }, cardHeader: { value: t("FACILITY_DETAILS"), inlineStyles: { marginTop: 0, fontSize: "1.5rem" } }, cardSecondaryAction: noAction !== "false" && ( -
handleRedirect(3)}> +
handleRedirect(2)}> {t(`CAMPAIGN_EDIT`)}
@@ -254,7 +254,7 @@ const CampaignUpdateSummary = (props) => { }, cardHeader: { value: t("USER_DETAILS"), inlineStyles: { marginTop: 0, fontSize: "1.5rem" } }, cardSecondaryAction: noAction !== "false" && ( -
handleRedirect(4)}> +
handleRedirect(3)}> {t(`CAMPAIGN_EDIT`)}
diff --git a/health/micro-ui/web/micro-ui-internals/packages/modules/campaign-manager/src/components/DeliveryDetailsSummary.js b/health/micro-ui/web/micro-ui-internals/packages/modules/campaign-manager/src/components/DeliveryDetailsSummary.js index 266805bcf59..640acef3bb9 100644 --- a/health/micro-ui/web/micro-ui-internals/packages/modules/campaign-manager/src/components/DeliveryDetailsSummary.js +++ b/health/micro-ui/web/micro-ui-internals/packages/modules/campaign-manager/src/components/DeliveryDetailsSummary.js @@ -2,10 +2,11 @@ import React, { Fragment, useEffect, useRef, useState } from "react"; import { useTranslation } from "react-i18next"; import { useHistory } from "react-router-dom"; import { Button, EditIcon, Header, Loader, ViewComposer } from "@egovernments/digit-ui-react-components"; -import { InfoBannerIcon, Toast , Card , Stepper , TextBlock } from "@egovernments/digit-ui-components"; +import { InfoBannerIcon, Toast, Card, Stepper, TextBlock } from "@egovernments/digit-ui-components"; import { DownloadIcon } from "@egovernments/digit-ui-react-components"; import { PRIMARY_COLOR, downloadExcelWithCustomName } from "../utils"; import getProjectServiceUrl from "../utils/getProjectServiceUrl"; +import getDeliveryConfig from "../utils/getDeliveryConfig"; function mergeObjects(item) { const arr = item; @@ -74,7 +75,7 @@ function loopAndReturn(dataa, t) { } return { ...i, - }; + }; }); return format; } @@ -85,45 +86,93 @@ function reverseDeliveryRemap(data, t) { let currentCycleIndex = null; let currentCycle = null; - // data.forEach((item, index) => { - // if (currentCycleIndex !== item.cycleNumber) { - // currentCycleIndex = item.cycleNumber; - // currentCycle = { - // // cycleIndex: currentCycleIndex.toString(), - // cycleIndex: item?.cycleIndex, - // startDate: item?.startDate ? Digit.Utils.date.convertEpochToDate(item?.startDate) : null, - // endDate: item?.endDate ? Digit.Utils.date.convertEpochToDate(item?.endDate) : null, - // active: index === 0, // Initialize active to false - // deliveries: [], - // }; - // reversedData.push(currentCycle); - // } - - // // const deliveryIndex = item.deliveryNumber.toString(); - // const deliveryIndex = item?.deliveryIndex; - - // let delivery = currentCycle.deliveries.find((delivery) => delivery.deliveryIndex === deliveryIndex); - - // if (!delivery) { - // delivery = { - // deliveryIndex: deliveryIndex, - // active: item.deliveryNumber === 1, // Set active to true only for the first delivery - // deliveryRules: [], - // }; - // currentCycle.deliveries.push(delivery); - // } - - // delivery.deliveryRules.push({ - // ruleKey: item.deliveryRuleNumber, - // delivery: {}, - // attributes: loopAndReturn(item.conditions, t), - // products: [...item.products], - // }); - // }); - - // return reversedData; - - return data; + const operatorMapping = { + "<=": "LESS_THAN_EQUAL_TO", + ">=": "GREATER_THAN_EQUAL_TO", + "<": "LESS_THAN", + ">": "GREATER_THAN", + "==": "EQUAL_TO", + "!=": "NOT_EQUAL_TO", + "IN_BETWEEN": "IN_BETWEEN" + }; + + const cycles = data?.[0]?.cycles || []; + const transformedCycles = cycles.map((cycle) => { + const deliveries = cycle.deliveries?.map((delivery, deliveryIndex) => { + const doseCriteria = delivery.doseCriteria?.flatMap((criteria, ruleKey) => { + const products = criteria.ProductVariants.map((variant, key) => ({ + key: key + 1, + count: 1, + value: variant.productVariantId, + name: variant.name + })); + + const condition = criteria.condition; + let conditionParts = condition.split("and").map(part => part.trim()); // Split by 'and' and trim spaces + let rules = []; + + // Iterate over each part of the condition if split by 'and' + conditionParts.forEach((part) => { + const parts = part.split(' ').filter(Boolean); // Split by space and remove empty strings + + let attributes = []; + if (parts.length === 5 && (parts[1] === "<=" || parts[1] === "<") && (parts[3] === "<" || parts[3] === "<=")) { + // Handle IN_BETWEEN case + const toValue = parts[0]; + const fromValue = parts[4]; + attributes.push({ + key: 1, // Incrementing key for each attribute + operator: { code: operatorMapping["IN_BETWEEN"] }, + attribute: { code: parts[2] }, + fromValue, + toValue + }); + } else { + // Parse single conditions using regex + const match = part.match(/(.*?)\s*(<=|>=|<|>|==|!=)\s*(.*)/); + if (match) { + const attributeCode = match[1].trim(); + const operatorSymbol = match[2].trim(); + const value = match[3].trim(); + attributes.push({ + key: attributes.length + 1, // Incrementing key for each attribute + value, + operator: { code: operatorMapping[operatorSymbol] }, + attribute: { code: attributeCode } + }); + } + } + + // Add each part as a new delivery rule + rules.push({ + ruleKey: ruleKey + 1, + delivery: {}, + products, + attributes + }); + }); + + return rules; + }); + + return { + active: true, + deliveryIndex: String(deliveryIndex + 1), + deliveryRules: doseCriteria + }; + }); + + return { + active: true, + cycleIndex: String(cycle.id), // Include cycleIndex as string + deliveries: deliveries + }; +}); + +// Return the transformed cycles data +return transformedCycles; + + } const fetchResourceFile = async (tenantId, resourceIdArr) => { @@ -178,7 +227,7 @@ const DeliveryDetailsSummary = (props) => { const [cycles, setCycles] = useState([]); const [cards, setCards] = useState([]); const isPreview = searchParams.get("preview"); - const [currentStep , setCurrentStep] = useState(1); + const [currentStep, setCurrentStep] = useState(1); const currentKey = searchParams.get("key"); const [key, setKey] = useState(() => { const keyParam = searchParams.get("key"); @@ -204,7 +253,6 @@ const DeliveryDetailsSummary = (props) => { window.history.replaceState({}, "", url); } - useEffect(() => { updateUrlParams({ key: key }); window.dispatchEvent(new Event("checking")); @@ -266,7 +314,7 @@ const DeliveryDetailsSummary = (props) => { ss(); const target = data?.[0]?.deliveryRules; const cycleData = reverseDeliveryRemap(target, t); - + return { cards: [ { @@ -284,15 +332,15 @@ const DeliveryDetailsSummary = (props) => { { key: "CAMPAIGN_NO_OF_CYCLES", value: - data?.[0]?.deliveryRules && data?.[0]?.deliveryRules.map((item) => item.cycleIndex)?.length > 0 - ? Math.max(...data?.[0]?.deliveryRules.map((item) => item.cycleIndex)) + data?.[0]?.additionalDetails?.cycleData?.cycleConfgureDate?.cycle ? + data?.[0]?.additionalDetails?.cycleData?.cycleConfgureDate?.cycle : t("CAMPAIGN_SUMMARY_NA"), }, { key: "CAMPAIGN_NO_OF_DELIVERIES", value: - data?.[0]?.deliveryRules && data?.[0]?.deliveryRules?.flatMap((rule) => rule?.deliveries.map((delivery) => delivery?.deliveryIndex))?.length > 0 - ? Math.max(...data?.[0]?.deliveryRules?.flatMap((rule) => rule?.deliveries.map((delivery) => delivery?.deliveryIndex))) + data?.[0]?.additionalDetails?.cycleData?.cycleConfgureDate?.deliveries ? + data?.[0]?.additionalDetails?.cycleData?.cycleConfgureDate?.deliveries : t("CAMPAIGN_SUMMARY_NA"), }, ], @@ -361,23 +409,22 @@ const DeliveryDetailsSummary = (props) => { const updatedObject = { ...data }; const onStepClick = (currentStep) => { - if(currentStep === 0){ + if (currentStep === 0) { setKey(7); - } - else if(currentStep === 2) setKey(9); + } else if (currentStep === 2) setKey(9); else setKey(8); }; return ( <> -
+
{
-
-
{t("HCM_DELIVERY_DETAILS_SUMMARY")}
-
-
- - {showToast && ( - - )} -
-
+
+
{t("HCM_DELIVERY_DETAILS_SUMMARY")}
+
+
+ + {showToast && ( + + )} +
+
); diff --git a/health/micro-ui/web/micro-ui-internals/packages/modules/campaign-manager/src/pages/employee/SetupCampaign.js b/health/micro-ui/web/micro-ui-internals/packages/modules/campaign-manager/src/pages/employee/SetupCampaign.js index 6f44719ef90..24875e7e339 100644 --- a/health/micro-ui/web/micro-ui-internals/packages/modules/campaign-manager/src/pages/employee/SetupCampaign.js +++ b/health/micro-ui/web/micro-ui-internals/packages/modules/campaign-manager/src/pages/employee/SetupCampaign.js @@ -70,94 +70,80 @@ function cycleDataRemap(data) { }); } -// function reverseDeliveryRemap(data) { -// if (!data) return null; -// const reversedData = []; -// let currentCycleIndex = null; -// let currentCycle = null; - -// data.forEach((item, index) => { -// if (currentCycleIndex !== item.cycleNumber) { -// currentCycleIndex = item.cycleNumber; -// currentCycle = { -// cycleIndex: currentCycleIndex.toString(), -// active: index === 0, // Initialize active to false -// deliveries: [], -// }; -// reversedData.push(currentCycle); -// } - -// const deliveryIndex = item.deliveryNumber.toString(); - -// let delivery = currentCycle.deliveries.find((delivery) => delivery.deliveryIndex === deliveryIndex); - -// if (!delivery) { -// delivery = { -// deliveryIndex: deliveryIndex, -// active: item.deliveryNumber === 1, // Set active to true only for the first delivery -// deliveryRules: [], -// }; -// currentCycle.deliveries.push(delivery); -// } - -// delivery.deliveryRules.push({ -// ruleKey: item.deliveryRuleNumber, -// delivery: {}, -// deliveryType: item?.deliveryType, -// attributes: loopAndReturn(item.conditions), -// products: [...item.products], -// }); -// }); - -// return reversedData; -// } - -// function restructureData(data) { -// return data.map(cycle => ({ -// mandatoryWaitSinceLastCycleInDays: null, -// id: parseInt(cycle.cycleIndex), -// deliveries: cycle.deliveries.map(delivery => ({ -// id: parseInt(delivery.deliveryIndex), -// mandatoryWaitSinceLastDeliveryInDays: null, -// doseCriteria: delivery.deliveryRules.map(rule => ({ -// condition: parseCondition(rule.attributes), -// ProductVariants: rule.products.map(product => ({ -// productVariantId: product.value, -// name: product.name -// })) -// })) -// })) -// })); -// } - -// function parseCondition(attributes) { -// const conditions = attributes.map(attr => { -// const attributeCode = attr.attribute.code; -// const operator = attr.operator.code; -// if (operator === "IN_BETWEEN") { -// const fromValue = attr.fromValue; -// const toValue = attr.toValue; -// return `${fromValue} <= ${attributeCode} < ${toValue}`; -// } - -// const value = attr.value; -// return `${attributeCode}${convertOperator(operator)}${value}`; -// }); - -// // Join conditions into a single string -// return conditions.join(' and '); -// } - -// function convertOperator(operatorCode) { -// const operators = { -// "LESS_THAN_EQUAL_TO": "<=", -// "GREATER_THAN_EQUAL_TO": ">=", -// "LESS_THAN": "<", -// "GREATER_THAN": ">", -// "EQUAL_TO": "=", -// }; -// return operators[operatorCode] || "="; -// } +function getOperatorSymbol(operator) { + const operatorMapping = { + "LESS_THAN": "<", + "LESS_THAN_EQUAL_TO": "<=", + "GREATER_THAN": ">", + "GREATER_THAN_EQUAL_TO": ">=", + "EQUAL_TO": "==", + "NOT_EQUAL_TO": "!=", + "IN_BETWEEN": "IN" // Map IN_BETWEEN to a keyword if needed + }; + return operatorMapping[operator] || ""; // Default to empty if not found +} + +function restructureData(data, cycle, DeliveryConfig, projectType) { + const tt = DeliveryConfig?.find((e) => e.code === String(projectType)); + + if (tt) { + delete tt.cycles; // Remove old cycles + delete tt.resources; // Remove old resources + } + + const resourcesMap = new Map(); + + const cycles = data.map(cycle => ({ + mandatoryWaitSinceLastCycleInDays: null, + startDate: Digit.Utils.pt.convertDateToEpoch(cycle?.cycleData?.[0]?.fromDate), + endDate: Digit.Utils.pt.convertDateToEpoch(cycle?.cycleData?.[0]?.toDate), + id: parseInt(cycle.cycleIndex, 10), + deliveries: cycle.deliveries.map(delivery => ({ + id: parseInt(delivery.deliveryIndex, 10), + deliveryStrategy: delivery.deliveryStrategy || "DIRECT", + mandatoryWaitSinceLastDeliveryInDays: null, + doseCriteria: delivery.deliveryRules.map(rule => { + // Consolidate product variants for the resources array + rule.products.forEach(product => { + if (resourcesMap.has(product.value)) { + resourcesMap.get(product.value).count += product.count; + } else { + resourcesMap.set(product.value, { + productVariantId: product.value, + isBaseUnitVariant: false, + name: product.name + }); + } + }); + + // Build the condition string, handling IN_BETWEEN separately + const conditions = rule.attributes.map(attr => { + if (attr?.operator?.code === "IN_BETWEEN") { + return `${attr.toValue} <= ${attr.attribute.code} < ${attr.fromValue}`; + } else { + return `${attr?.attribute?.code}${getOperatorSymbol(attr?.operator?.code)}${attr?.value}`; + } + }); + + return { + condition: conditions.join(" and "), + ProductVariants: rule.products.map(product => ({ + productVariantId: product.value, + name: product.name + })) + }; + }) + })) + })); + + const resources = Array.from(resourcesMap.values()); + + if (tt) { + tt.cycles = cycles; + tt.resources = resources; + } + return [tt]; +} function groupByTypeRemap(data) { if (!data) return null; @@ -245,6 +231,17 @@ const SetupCampaign = ({ hierarchyType, hierarchyData }) => { return hierarchyConfig?.["HCM-ADMIN-CONSOLE"]?.hierarchyConfig?.find((item) => item.isActive)?.lowestHierarchy; }, [hierarchyConfig]); + const { data: DeliveryConfig } = Digit.Hooks.useCustomMDMS( + tenantId, + "HCM-PROJECT-TYPES", + [{ name: "projectTypes" }], + { + select: (data) => { + return data?.["HCM-PROJECT-TYPES"]?.projectTypes; + }, + } + ); + const reqCriteria = { url: `/boundary-service/boundary-hierarchy-definition/_search`, changeQueryName: `${hierarchyType}`, @@ -417,110 +414,6 @@ const SetupCampaign = ({ hierarchyType, hierarchyData }) => { } }, [currentKey]); - // function restructureData(data) { - // const dateData = totalFormData?.HCM_CAMPAIGN_CYCLE_CONFIGURE?.cycleConfigure?.cycleData; - // const restructuredData = []; - - // data.forEach((cycle) => { - // cycle.deliveries.forEach((delivery, index) => { - // delivery.deliveryRules.forEach((rule) => { - // const restructuredRule = { - // startDate: Digit.Utils.pt.convertDateToEpoch(dateData?.find((i) => i.key == cycle.cycleIndex)?.fromDate, "daystart"), // Hardcoded for now - // endDate: Digit.Utils.pt.convertDateToEpoch(dateData?.find((i) => i?.key == cycle?.cycleIndex)?.toDate), // Hardcoded for now - // cycleNumber: parseInt(cycle.cycleIndex), - // deliveryNumber: parseInt(delivery.deliveryIndex), - // deliveryType: rule?.deliveryType, - // deliveryRuleNumber: parseInt(rule.ruleKey), // New key added - // products: [], - // conditions: [], - // }; - - // rule.attributes.forEach((attribute) => { - // if (attribute?.operator?.code === "IN_BETWEEN") { - // restructuredRule.conditions.push({ - // attribute: attribute?.attribute?.code - // ? attribute?.attribute?.code - // : typeof attribute?.attribute === "string" - // ? attribute?.attribute - // : null, - // operator: "LESS_THAN_EQUAL_TO", - // value: attribute.fromValue ? Number(attribute.fromValue) : null, - // }); - - // restructuredRule.conditions.push({ - // attribute: attribute?.attribute?.code - // ? attribute?.attribute?.code - // : typeof attribute?.attribute === "string" - // ? attribute?.attribute - // : null, - // operator: "GREATER_THAN_EQUAL_TO", - // value: attribute.toValue ? Number(attribute.toValue) : null, - // }); - // } else { - // restructuredRule.conditions.push({ - // attribute: attribute?.attribute?.code - // ? attribute?.attribute?.code - // : typeof attribute?.attribute === "string" - // ? attribute?.attribute - // : null, - // operator: attribute.operator ? attribute.operator.code : null, - // value: - // attribute?.attribute?.code === "Gender" && attribute?.value?.length > 0 - // ? attribute?.value - // : attribute?.attribute?.code === "TYPE_OF_STRUCTURE" - // ? attribute?.value - // : attribute?.value - // ? Number(attribute?.value) - // : null, - // }); - // } - // }); - - // rule.products.forEach((prod) => { - // restructuredRule.products.push({ - // value: prod?.value, - // name: prod?.name, - // count: prod?.count, - // }); - // }); - - // restructuredData.push(restructuredRule); - // }); - // }); - // }); - - // return restructuredData; - // } - - function restructureData(inputData) { - const result = { - cycleIndex: inputData?.cycleIndex, - active: inputData?.active, - deliveries: inputData?.deliveries?.map((delivery) => ({ - deliveryIndex: delivery.deliveryIndex, - active: delivery.active, - deliveryRules: delivery.deliveryRules.map((rule) => ({ - ruleKey: rule.ruleKey, - delivery: rule.delivery, - attributes: rule.attributes.map((attr) => ({ - key: attr.key, - attribute: attr.attribute.code, // Directly using code from attribute - operator: attr.operator.code, // Directly using code from operator - value: attr.value, - })), - products: rule?.products.map((product) => ({ - key: product.key, - value: product.value, - count: product.count, - name: product.name, - })), - })), - })), - }; - - return [result]; - } - function resourceData(facilityData, boundaryData, userData) { const resources = [facilityData, boundaryData, userData].filter((data) => data !== null && data !== undefined); @@ -561,8 +454,8 @@ const SetupCampaign = ({ hierarchyType, hierarchyData }) => { payloadData.additionalDetails.cycleData = {}; } if (totalFormData?.HCM_CAMPAIGN_DELIVERY_DATA?.deliveryRule) { - const temp = restructureData(totalFormData?.HCM_CAMPAIGN_DELIVERY_DATA?.deliveryRule?.[0]); - payloadData.deliveryRules = temp; + const temp = restructureData(totalFormData?.HCM_CAMPAIGN_DELIVERY_DATA?.deliveryRule , totalFormData?.HCM_CAMPAIGN_CYCLE_CONFIGURE?.cycleConfigure , DeliveryConfig); + payloadData.deliveryRules = temp?.[0]; // payloadData.deliveryRules = totalFormData?.HCM_CAMPAIGN_DELIVERY_DATA?.deliveryRule; } else { payloadData.deliveryRules = []; @@ -633,7 +526,7 @@ const SetupCampaign = ({ hierarchyType, hierarchyData }) => { userId: dataParams?.userId, }; if (totalFormData?.HCM_CAMPAIGN_DELIVERY_DATA?.deliveryRule) { - const temp = restructureData(totalFormData?.HCM_CAMPAIGN_DELIVERY_DATA?.deliveryRule?.[0]); + const temp = restructureData(totalFormData?.HCM_CAMPAIGN_DELIVERY_DATA?.deliveryRule, totalFormData?.HCM_CAMPAIGN_CYCLE_CONFIGURE?.cycleConfigure , DeliveryConfig , totalFormData?.HCM_CAMPAIGN_TYPE?.projectType?.code); payloadData.deliveryRules = temp; // payloadData.deliveryRules = totalFormData?.HCM_CAMPAIGN_DELIVERY_DATA?.deliveryRule; } @@ -698,8 +591,8 @@ const SetupCampaign = ({ hierarchyType, hierarchyData }) => { userId: dataParams?.userId, }; if (totalFormData?.HCM_CAMPAIGN_DELIVERY_DATA?.deliveryRule) { - const temp = restructureData(totalFormData?.HCM_CAMPAIGN_DELIVERY_DATA?.deliveryRule?.[0]); - payloadData.deliveryRules = temp; + const temp = restructureData(totalFormData?.HCM_CAMPAIGN_DELIVERY_DATA?.deliveryRule , totalFormData?.HCM_CAMPAIGN_CYCLE_CONFIGURE?.cycleConfigure ,DeliveryConfig ,totalFormData?.HCM_CAMPAIGN_TYPE?.projectType?.code); + payloadData.deliveryRules = temp?.[0]; // payloadData.deliveryRules = totalFormData?.HCM_CAMPAIGN_DELIVERY_DATA?.deliveryRule; } @@ -762,8 +655,8 @@ const SetupCampaign = ({ hierarchyType, hierarchyData }) => { payloadData.additionalDetails.cycleData = {}; } if (totalFormData?.HCM_CAMPAIGN_DELIVERY_DATA?.deliveryRule) { - const temp = restructureData(totalFormData?.HCM_CAMPAIGN_DELIVERY_DATA?.deliveryRule?.[0]); - payloadData.deliveryRules = temp; + const temp = restructureData(totalFormData?.HCM_CAMPAIGN_DELIVERY_DATA?.deliveryRule , totalFormData?.HCM_CAMPAIGN_CYCLE_CONFIGURE?.cycleConfigure , DeliveryConfig ,totalFormData?.HCM_CAMPAIGN_TYPE?.projectType?.code); + payloadData.deliveryRules = [temp?.[0]]; // payloadData.deliveryRules = totalFormData?.HCM_CAMPAIGN_DELIVERY_DATA?.deliveryRule; } else { payloadData.deliveryRules = []; @@ -1551,8 +1444,6 @@ const SetupCampaign = ({ hierarchyType, hierarchyData }) => { {showToast && ( - cycle.deliveries.flatMap((delivery) => delivery.deliveryRules.flatMap((rule) => rule.attributes.map((attribute) => attribute.value))) + cycle?.deliveries?.flatMap((delivery) => delivery?.deliveryRules?.flatMap((rule) => rule?.attributes?.map((attribute) => attribute?.value))) ); const filteredStructureConfig = structureConfig?.filter((structure) => !selectedStructureCodes.includes(structure?.code)); diff --git a/health/micro-ui/web/micro-ui-internals/packages/modules/campaign-manager/src/pages/employee/deliveryRule/index.js b/health/micro-ui/web/micro-ui-internals/packages/modules/campaign-manager/src/pages/employee/deliveryRule/index.js index 09d31117a2f..8c374479448 100644 --- a/health/micro-ui/web/micro-ui-internals/packages/modules/campaign-manager/src/pages/employee/deliveryRule/index.js +++ b/health/micro-ui/web/micro-ui-internals/packages/modules/campaign-manager/src/pages/employee/deliveryRule/index.js @@ -301,13 +301,13 @@ function DeliverySetup({ onSelect, config, formData, control, tabCount = 2, subT saved.forEach((cycle) => { // Remove deliveries if there are more deliveries than the specified number - if (cycle.deliveries.length > subTabs) { - cycle.deliveries.splice(subTabs); + if (cycle?.deliveries?.length > subTabs) { + cycle?.deliveries.splice(subTabs); } // Add deliveries if there are fewer deliveries than the specified number - if (subTabs > cycle.deliveries.length) { - for (let i = cycle.deliveries.length + 1; i <= subTabs; i++) { + if (subTabs > cycle?.deliveries?.length) { + for (let i = cycle?.deliveries.length + 1; i <= subTabs; i++) { const newIndex = i.toString(); cycle.deliveries.push({ deliveryIndex: newIndex, diff --git a/health/micro-ui/web/micro-ui-internals/packages/modules/campaign-manager/src/utils/getDeliveryConfig.js b/health/micro-ui/web/micro-ui-internals/packages/modules/campaign-manager/src/utils/getDeliveryConfig.js index fff1d939ec5..5777535e130 100644 --- a/health/micro-ui/web/micro-ui-internals/packages/modules/campaign-manager/src/utils/getDeliveryConfig.js +++ b/health/micro-ui/web/micro-ui-internals/packages/modules/campaign-manager/src/utils/getDeliveryConfig.js @@ -47,59 +47,6 @@ const getDeliveryConfig = ({ data, projectType }) => { return { operatorValue, value }; } - const generateMRDNConfig = (deliveries, projectType) => { - return deliveries?.map((delivery) => { - return { - delivery: delivery.id, - conditionConfig: delivery.doseCriteria.map((criteria, index) => { - const conditions = criteria.condition.split("and"); - - const attributeConfig = conditions.map((conditionStr, conditionIndex) => { - const { operatorValue, value } = parseCondition(conditionStr); - - let fromValue = null; - let toValue = null; - if (operatorValue === "IN_BETWEEN") { - fromValue = Number(value.minValue); - toValue = Number(value.maxValue); - } else { - fromValue = Number(value.comparisonValue); - toValue = null; - } - - return { - key: index + conditionIndex + 1, - label: "Custom", - attrType: criteria.attrType || "dropdown", - attrValue: value?.variable, - operatorValue: operatorValue, - fromValue: fromValue, - toValue: toValue ? toValue - 1 : null, - }; - }); - const productConfig = criteria.ProductVariants.map(async (variant, i) => { - const productName = await useProductVariantSearch({ variantId: variant.productVariantId, tenantId: "mz" }); - - return { - key: i + 1, - count: variant.quantity, - value: variant.productVariantId, - name: productName || "Product Name", - }; - }); - - return { - disableDeliveryType: delivery.deliveryStrategy === "DIRECT", - deliveryType: delivery.deliveryStrategy, - attributeConfig: attributeConfig, - productConfig: productConfig, - }; - }), - }; - }); - }; - - const generateBednetConfig = (deliveries, projectType) => { return deliveries?.map((delivery) => { const productSet = new Set(); @@ -150,10 +97,54 @@ const getDeliveryConfig = ({ data, projectType }) => { }); }; + + const generateMRDNConfig = (deliveries) => { + const deliveryConfig = deliveries.map((delivery, deliveryIndex) => { + return { + delivery: delivery.id, + conditionConfig: delivery.deliveries.map((deliveryItem, conditionIndex) => { + return { + disableDeliveryType: deliveryItem.deliveryStrategy === "DIRECT", + deliveryType: deliveryItem.deliveryStrategy, + attributeConfig: deliveryItem.doseCriteria.map((criteria, attrIndex) => { + const { operatorValue, value } = parseCondition(criteria.condition); + + return { + key: `${conditionIndex + 1}-${attrIndex + 1}`, + label: "Custom", + attrType: criteria.attrType || "dropdown", + attrValue: value.variable, + operatorValue, + fromValue: Number(value.minValue), + toValue: Number(value.maxValue) - 1 + }; + }), + productConfig: deliveryItem.doseCriteria.flatMap((criteria, prodIndex) => { + return criteria.ProductVariants.map((variant, varIndex) => { + // if(variant?.productVariantId){ + // productName = useProductVariantSearch({ variantId: variant.productVariantId, tenantId: "mz" }); + // } + // const productName = useProductVariantSearch({ variantId: variant.productVariantId, tenantId: "mz" }); + + return { + key: `${conditionIndex + 1}-${varIndex + 1}`, + count: variant.quantity, + value: variant.productVariantId, + name: "Product Name" + }; + }); + }) + }; + }) + }; + }); + return deliveryConfig; + }; + const deliveryConfig = ({ data, projectType }) => { switch (projectType) { case "MR-DN": - return generateMRDNConfig(data?.cycles?.[0]?.deliveries, projectType); + return generateMRDNConfig(data?.cycles, projectType); case "LLIN-mz": case "IRS-mz": return generateBednetConfig(data?.cycles?.[0]?.deliveries, projectType); @@ -164,11 +155,17 @@ const getDeliveryConfig = ({ data, projectType }) => { function convertToConfig(data) { return { + beneficiaryType: data?.beneficiaryType, + code: data?.code, projectType: data?.code, attrAddDisable: data?.attrAddDisable || false, deliveryAddDisable: data?.deliveryAddDisable || false, customAttribute: data?.customAttribute || true, productCountHide: data?.productCountHide || false, + eligibilityCriteria: data?.eligibilityCriteria, + dashboardUrls: data?.dashboardUrls, + taskProcedure: data?.taskProcedure, + resources: data?.resources, cycleConfig: { cycle: data?.cycles?.length || 1, deliveries: data?.cycles?.[0]?.deliveries?.length || 1,