Skip to content

Commit

Permalink
fixed summary and campaign creation (#1479)
Browse files Browse the repository at this point in the history
* fixed summary and campaign creation

* changes
  • Loading branch information
Bhavya-egov authored Oct 9, 2024
1 parent 06ada1e commit 8a051cc
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,39 +85,40 @@ 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);
}
// 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 deliveryIndex = item.deliveryNumber.toString();
// const deliveryIndex = item.deliveryNumber.toString();

let delivery = currentCycle.deliveries.find((delivery) => delivery.deliveryIndex === 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);
}
// 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],
});
});
// delivery.deliveryRules.push({
// ruleKey: item.deliveryRuleNumber,
// delivery: {},
// attributes: loopAndReturn(item.conditions, t),
// products: [...item.products],
// });
// });
return data;

return reversedData;
}
Expand Down Expand Up @@ -402,16 +403,16 @@ const CampaignSummary = (props) => {
{
key: "CAMPAIGN_NO_OF_CYCLES",
value:
data?.[0]?.deliveryRules && data?.[0]?.deliveryRules.map((item) => item.cycleNumber)?.length > 0
? Math.max(...data?.[0]?.deliveryRules.map((item) => item.cycleNumber))
data?.[0]?.deliveryRules && data?.[0]?.deliveryRules.map((item) => item.cycleIndex)?.length > 0
? Math.max(...data?.[0]?.deliveryRules.map((item) => item.cycleIndex))
: t("CAMPAIGN_SUMMARY_NA"),
},
{
key: "CAMPAIGN_NO_OF_DELIVERIES",
value:
data?.[0]?.deliveryRules && data?.[0]?.deliveryRules.map((item) => item.deliveryNumber)?.length > 0
? Math.max(...data?.[0]?.deliveryRules.map((item) => item.deliveryNumber))
: t("CAMPAIGN_SUMMARY_NA"),
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"),
},
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,6 @@ const DeliveryDetailsSummary = (props) => {
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"),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ function loopAndReturn(dataa) {
toValue: existingItem.value && item.value ? Math.min(existingItem.value, item.value) : null,
fromValue: existingItem.value && item.value ? Math.max(existingItem.value, item.value) : null,
};
}
else {
} else {
newArray.push(item);
}
});
Expand Down Expand Up @@ -113,7 +112,6 @@ function cycleDataRemap(data) {
// return reversedData;
// }


// function restructureData(data) {
// return data.map(cycle => ({
// mandatoryWaitSinceLastCycleInDays: null,
Expand All @@ -137,7 +135,7 @@ function cycleDataRemap(data) {
// const attributeCode = attr.attribute.code;
// const operator = attr.operator.code;
// if (operator === "IN_BETWEEN") {
// const fromValue = attr.fromValue;
// const fromValue = attr.fromValue;
// const toValue = attr.toValue;
// return `${fromValue} <= ${attributeCode} < ${toValue}`;
// }
Expand All @@ -158,11 +156,9 @@ function cycleDataRemap(data) {
// "GREATER_THAN": ">",
// "EQUAL_TO": "=",
// };
// return operators[operatorCode] || "=";
// return operators[operatorCode] || "=";
// }



function groupByTypeRemap(data) {
if (!data) return null;

Expand Down Expand Up @@ -204,14 +200,14 @@ function updateUrlParams(params) {
window.history.replaceState({}, "", url);
}

const SetupCampaign = ({ hierarchyType ,hierarchyData }) => {
const SetupCampaign = ({ hierarchyType, hierarchyData }) => {
const tenantId = Digit.ULBService.getCurrentTenantId();
const { t } = useTranslation();
const history = useHistory();
const [currentStep, setCurrentStep] = useState(0);
const [totalFormData, setTotalFormData] = useState({});
const [isSubmitting, setIsSubmitting] = useState(false);
const [campaignConfig, setCampaignConfig] = useState(CampaignConfig(totalFormData, null, isSubmitting ));
const [campaignConfig, setCampaignConfig] = useState(CampaignConfig(totalFormData, null, isSubmitting));
const [shouldUpdate, setShouldUpdate] = useState(false);
const [params, setParams, clearParams] = Digit.Hooks.useSessionStorage("HCM_CAMPAIGN_MANAGER_FORM_DATA", {});
const [dataParams, setDataParams] = Digit.Hooks.useSessionStorage("HCM_CAMPAIGN_MANAGER_UPLOAD_ID", {});
Expand Down Expand Up @@ -282,7 +278,6 @@ const SetupCampaign = ({ hierarchyType ,hierarchyData }) => {
};

useEffect(() => {

window.addEventListener("checking", getCurrentKey);

return () => {
Expand Down Expand Up @@ -356,7 +351,7 @@ const SetupCampaign = ({ hierarchyType ,hierarchyData }) => {
},
HCM_CAMPAIGN_DELIVERY_DATA: {
// deliveryRule: reverseDeliveryRemap(delivery),
deliveryRule: delivery
deliveryRule: delivery,
},
HCM_CAMPAIGN_SELECTING_BOUNDARY_DATA: {
boundaryType: {
Expand Down Expand Up @@ -409,8 +404,8 @@ const SetupCampaign = ({ hierarchyType ,hierarchyData }) => {
}
}, [hierarchyDefinition?.BoundaryHierarchy?.[0], draftData]);
useEffect(() => {
setCampaignConfig(CampaignConfig(totalFormData, dataParams, isSubmitting, summaryErrors , hierarchyData));
}, [totalFormData, dataParams, isSubmitting, summaryErrors , hierarchyData]);
setCampaignConfig(CampaignConfig(totalFormData, dataParams, isSubmitting, summaryErrors, hierarchyData));
}, [totalFormData, dataParams, isSubmitting, summaryErrors, hierarchyData]);

useEffect(() => {
setIsSubmitting(false);
Expand Down Expand Up @@ -497,6 +492,36 @@ const SetupCampaign = ({ hierarchyType ,hierarchyData }) => {
// 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);
return resources;
Expand Down Expand Up @@ -536,9 +561,9 @@ const SetupCampaign = ({ hierarchyType ,hierarchyData }) => {
payloadData.additionalDetails.cycleData = {};
}
if (totalFormData?.HCM_CAMPAIGN_DELIVERY_DATA?.deliveryRule) {
// const temp = restructureData(totalFormData?.HCM_CAMPAIGN_DELIVERY_DATA?.deliveryRule);
// payloadData.deliveryRules = temp;
payloadData.deliveryRules = totalFormData?.HCM_CAMPAIGN_DELIVERY_DATA?.deliveryRule;
const temp = restructureData(totalFormData?.HCM_CAMPAIGN_DELIVERY_DATA?.deliveryRule?.[0]);
payloadData.deliveryRules = temp;
// payloadData.deliveryRules = totalFormData?.HCM_CAMPAIGN_DELIVERY_DATA?.deliveryRule;
} else {
payloadData.deliveryRules = [];
}
Expand Down Expand Up @@ -608,9 +633,9 @@ const SetupCampaign = ({ hierarchyType ,hierarchyData }) => {
userId: dataParams?.userId,
};
if (totalFormData?.HCM_CAMPAIGN_DELIVERY_DATA?.deliveryRule) {
// const temp = restructureData(totalFormData?.HCM_CAMPAIGN_DELIVERY_DATA?.deliveryRule);
// payloadData.deliveryRules = temp;
payloadData.deliveryRules = totalFormData?.HCM_CAMPAIGN_DELIVERY_DATA?.deliveryRule;
const temp = restructureData(totalFormData?.HCM_CAMPAIGN_DELIVERY_DATA?.deliveryRule?.[0]);
payloadData.deliveryRules = temp;
// payloadData.deliveryRules = totalFormData?.HCM_CAMPAIGN_DELIVERY_DATA?.deliveryRule;
}
if (compareIdentical(draftData, payloadData) === false) {
await updateCampaign(payloadData, {
Expand Down Expand Up @@ -673,9 +698,9 @@ const SetupCampaign = ({ hierarchyType ,hierarchyData }) => {
userId: dataParams?.userId,
};
if (totalFormData?.HCM_CAMPAIGN_DELIVERY_DATA?.deliveryRule) {
// const temp = restructureData(totalFormData?.HCM_CAMPAIGN_DELIVERY_DATA?.deliveryRule);
// payloadData.deliveryRules = temp;
payloadData.deliveryRules = totalFormData?.HCM_CAMPAIGN_DELIVERY_DATA?.deliveryRule;
const temp = restructureData(totalFormData?.HCM_CAMPAIGN_DELIVERY_DATA?.deliveryRule?.[0]);
payloadData.deliveryRules = temp;
// payloadData.deliveryRules = totalFormData?.HCM_CAMPAIGN_DELIVERY_DATA?.deliveryRule;
}

await mutate(payloadData, {
Expand Down Expand Up @@ -737,9 +762,9 @@ const SetupCampaign = ({ hierarchyType ,hierarchyData }) => {
payloadData.additionalDetails.cycleData = {};
}
if (totalFormData?.HCM_CAMPAIGN_DELIVERY_DATA?.deliveryRule) {
// const temp = restructureData(totalFormData?.HCM_CAMPAIGN_DELIVERY_DATA?.deliveryRule);
// payloadData.deliveryRules = temp;
payloadData.deliveryRules = totalFormData?.HCM_CAMPAIGN_DELIVERY_DATA?.deliveryRule;
const temp = restructureData(totalFormData?.HCM_CAMPAIGN_DELIVERY_DATA?.deliveryRule?.[0]);
payloadData.deliveryRules = temp;
// payloadData.deliveryRules = totalFormData?.HCM_CAMPAIGN_DELIVERY_DATA?.deliveryRule;
} else {
payloadData.deliveryRules = [];
}
Expand Down Expand Up @@ -936,7 +961,6 @@ const SetupCampaign = ({ hierarchyType ,hierarchyData }) => {
return allBoundaryTypesPresent;
}


function recursiveParentFind(filteredData) {
const parentChildrenMap = {};

Expand Down Expand Up @@ -1084,7 +1108,7 @@ const SetupCampaign = ({ hierarchyType ,hierarchyData }) => {
}
setShowToast(null);
return;
case "DeliveryDetailsSummary":
case "DeliveryDetailsSummary":
const cycleConfigureData = totalFormData?.HCM_CAMPAIGN_CYCLE_CONFIGURE;
const isCycleError = validateCycleData(cycleConfigureData);
const deliveryCycleData = totalFormData?.HCM_CAMPAIGN_DELIVERY_DATA;
Expand All @@ -1103,7 +1127,7 @@ const SetupCampaign = ({ hierarchyType ,hierarchyData }) => {
}
setShowToast(null);
return true;
case "DataUploadSummary":
case "DataUploadSummary":
const isTargetError = totalFormData?.HCM_CAMPAIGN_UPLOAD_BOUNDARY_DATA?.uploadBoundary?.uploadedFile?.[0]?.filestoreId
? false
: (setSummaryErrors((prev) => {
Expand Down Expand Up @@ -1494,7 +1518,7 @@ const SetupCampaign = ({ hierarchyType ,hierarchyData }) => {
actionClassName={"actionBarClass"}
className="setup-campaign"
cardClassName="setup-campaign-card"
noCardStyle = {true}
noCardStyle={true}
// noCardStyle={currentStep === 7 ? false : true}
onSecondayActionClick={onSecondayActionClick}
label={
Expand Down

0 comments on commit 8a051cc

Please sign in to comment.