Skip to content

Commit

Permalink
Merge branch 'campaign' into HLM-5988/HLM-6008
Browse files Browse the repository at this point in the history
  • Loading branch information
nabeelmd-eGov committed May 28, 2024
2 parents dc38b7f + aebf04e commit 8e8b0e1
Show file tree
Hide file tree
Showing 18 changed files with 842 additions and 1,082 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState, Fragment } from "react";
import React, { useEffect, useState, Fragment ,useMemo} from "react";
import { CardText, LabelFieldPair, Card, Header, CardLabel, Modal } from "@egovernments/digit-ui-react-components";
import { useTranslation } from "react-i18next";
import { Dropdown, InfoCard, MultiSelectDropdown, Toast } from "@egovernments/digit-ui-components";
Expand Down Expand Up @@ -39,10 +39,17 @@ function SelectingBoundaries({ onSelect, formData, ...props }) {
const [hierarchyTypeDataresult, setHierarchyTypeDataresult] = useState(params?.hierarchy);
const [executionCount, setExecutionCount] = useState(0);
// State variable to store the lowest hierarchy level
const [lowestHierarchy, setLowestHierarchy] = useState(null);
// const [lowestHierarchy, setLowestHierarchy] = useState(null);
const [showPopUp, setShowPopUp] = useState(null);
const [restrictSelection, setRestrictSelection] = useState(null);
const [updateBoundary, setUpdateBoundary] = useState(null);
const { isLoading, data: hierarchyConfig } = Digit.Hooks.useCustomMDMS(tenantId, "HCM-ADMIN-CONSOLE", [{ name: "hierarchyConfig" }]);

// const lowestHierarchy = hierarchyConfig?.["HCM-ADMIN-CONSOLE"]?.hierarchyConfig?.[0]?.lowestHierarchy;
const lowestHierarchy = useMemo(() => hierarchyConfig?.["HCM-ADMIN-CONSOLE"]?.hierarchyConfig?.[0]?.lowestHierarchy, [hierarchyConfig]);
const lowestChild = hierarchyTypeDataresult?.boundaryHierarchy.filter((item => item.parentBoundaryType === lowestHierarchy))?.[0]?.boundaryType;
const searchParams = new URLSearchParams(location.search);
const isDraft = searchParams.get("draft");

useEffect(() => {
if (!updateBoundary) {
Expand Down Expand Up @@ -117,11 +124,6 @@ function SelectingBoundaries({ onSelect, formData, ...props }) {
}
createHierarchyStructure(hierarchyTypeDataresult);

setLowestHierarchy(
hierarchyTypeDataresult?.boundaryHierarchy?.filter(
(e) => !hierarchyTypeDataresult?.boundaryHierarchy?.find((e1) => e1?.parentBoundaryType == e?.boundaryType)
)
);
}
}, [hierarchyTypeDataresult]);

Expand Down Expand Up @@ -159,7 +161,7 @@ function SelectingBoundaries({ onSelect, formData, ...props }) {

const newData = [];
const fetchBoundaryTypeData = async () => {
if (boundaryType === undefined) {
if (boundaryType === undefined || boundaryType === lowestChild) {
// Do nothing if boundaryType is undefined
return;
}
Expand Down Expand Up @@ -271,37 +273,40 @@ function SelectingBoundaries({ onSelect, formData, ...props }) {
res.push(ob?.[1]);
});

const transformedRes = res?.map((item) => ({
// const transformedRes = res?.map((item) => ({
// code: item.code,
// type: item.type || item.boundaryType,
// isRoot: item.boundaryType === parentBoundaryTypeRoot,
// includeAllChildren: item.type === lowestHierarchy || item.boundaryType === lowestHierarchy,
// parent: item?.parent,
// }));

let transformedRes =[];
if(!isDraft){
transformedRes = res?.map((item) => ({
code: item.code,
type: item.type || item.boundaryType,
isRoot: item.boundaryType === parentBoundaryTypeRoot,
includeAllChildren: item.type === lowestHierarchy?.[0]?.boundaryType,
includeAllChildren: item.type === lowestHierarchy || item.boundaryType === lowestHierarchy,
parent: item?.parent,
}));

// res.forEach((boundary) => {
// const index = transformedRes?.findIndex((item) => item?.code === boundary?.code);
// if (index !== -1) {
// transformedRes[index].includeAllChildren = true;
// }
// // Find the parent boundary type using the hierarchy data
// const parentBoundaryType = hierarchyTypeDataresult?.boundaryHierarchy?.find((e) => e?.boundaryType === boundary?.boundaryType)
// ?.parentBoundaryType;

// // If the selected boundary has a parent, set includeAllChildren to false for the parent
// if (parentBoundaryType) {
// const parentIndexes = selectedData?.reduce((acc, item, index) => {
// if (item?.type === parentBoundaryType) {
// acc.push(index);
// }
// return acc;
// }, []);

// parentIndexes?.forEach((parentIndex) => {
// selectedData[parentIndex].includeAllChildren = true;
// });
// }
// });
}
else{
// transformedRes = selectedData.filter((item) => item?.type === boundary?.boundaryType)
const filteredData = selectedData.filter((item) => item?.type === boundary?.boundaryType);
if (filteredData.length === 0) {
// If no selected data for the particular boundary type, run the transformation logic
transformedRes = res?.map((item) => ({
code: item.code,
type: item.type || item.boundaryType,
isRoot: item.boundaryType === parentBoundaryTypeRoot,
includeAllChildren: item.type === lowestHierarchy || item.boundaryType === lowestHierarchy,
parent: item?.parent,
}));
} else {
transformedRes = filteredData;
}
}

const newBoundaryType = transformedRes?.[0]?.type;
const existingBoundaryType = selectedData?.length > 0 ? selectedData?.[0]?.type : null;
Expand Down Expand Up @@ -358,11 +363,20 @@ function SelectingBoundaries({ onSelect, formData, ...props }) {
<div className="selecting-boundary-div">
<Header>{t(`CAMPAIGN_SELECT_BOUNDARY`)}</Header>
<CardText>{t(`CAMPAIGN_SELECT_BOUNDARIES_DESCRIPTION`)}</CardText>
{hierarchyTypeDataresult?.boundaryHierarchy.map((boundary, index) =>
{hierarchyTypeDataresult?.boundaryHierarchy
.filter((boundary, index, array) => {
// Find the index of the lowest hierarchy
const lowestIndex = array.findIndex((b) => b.boundaryType === lowestHierarchy);
// Include only those boundaries that are above or equal to the lowest hierarchy
return index <= lowestIndex;
})
.map((boundary, index) =>
boundary?.parentBoundaryType == null ? (
<LabelFieldPair key={index}>
<CardLabel>
{t(`${hierarchy}_${boundary?.boundaryType}`?.toUpperCase())}
{/* {t(`${hierarchy}_${boundary?.boundaryType}`?.toUpperCase())} */}
{t((hierarchy + "_" + boundary?.boundaryType).toUpperCase())}

<span className="mandatory-span">*</span>
</CardLabel>
<div className="digit-field">
Expand All @@ -382,7 +396,7 @@ function SelectingBoundaries({ onSelect, formData, ...props }) {
) : (
<LabelFieldPair key={index}>
<CardLabel>
{t(`${hierarchy}_${boundary?.boundaryType}`?.toUpperCase())}
{t((hierarchy + "_" + boundary?.boundaryType).toUpperCase())}
<span className="mandatory-span">*</span>
</CardLabel>
<div className="digit-field">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Loader, FormComposerV2, Header, MultiUploadWrapper, Button, Close, LogoutIcon } from "@egovernments/digit-ui-react-components";
import React, { useState, useEffect } from "react";
import React, { useState, useEffect , useMemo} from "react";
import { useTranslation } from "react-i18next";
import { useHistory, useParams } from "react-router-dom";
import { CampaignConfig } from "../../configs/CampaignConfig";
Expand Down Expand Up @@ -251,10 +251,16 @@ const SetupCampaign = () => {
return keyParam ? parseInt(keyParam) : 1;
});

const [lowest, setLowest] = useState(null);
// const [lowest, setLowest] = useState(null);
const [fetchBoundary, setFetchBoundary] = useState(() => Boolean(searchParams.get("fetchBoundary")));
const [fetchUpload, setFetchUpload] = useState(false);
const [enabled, setEnabled] = useState(false);
const { data: hierarchyConfig } = Digit.Hooks.useCustomMDMS(tenantId, "HCM-ADMIN-CONSOLE", [
{ name: "hierarchyConfig" },
]);

// const lowestHierarchy = hierarchyConfig?.["HCM-ADMIN-CONSOLE"]?.hierarchyConfig?.[0]?.lowestHierarchy;
const lowestHierarchy = useMemo(() => hierarchyConfig?.["HCM-ADMIN-CONSOLE"]?.hierarchyConfig?.[0]?.lowestHierarchy, [hierarchyConfig]);

const reqCriteria = {
url: `/boundary-service/boundary-hierarchy-definition/_search`,
Expand All @@ -271,15 +277,15 @@ const SetupCampaign = () => {

const { data: hierarchyDefinition } = Digit.Hooks.useCustomAPIHook(reqCriteria);

useEffect(() => {
if (hierarchyDefinition) {
setLowest(
hierarchyDefinition?.BoundaryHierarchy?.[0]?.boundaryHierarchy?.filter(
(e) => !hierarchyDefinition?.BoundaryHierarchy?.[0]?.boundaryHierarchy?.find((e1) => e1?.parentBoundaryType == e?.boundaryType)
)
);
}
}, [hierarchyDefinition]);
// useEffect(() => {
// if (hierarchyDefinition) {
// setLowest(
// hierarchyDefinition?.BoundaryHierarchy?.[0]?.boundaryHierarchy?.filter(
// (e) => !hierarchyDefinition?.BoundaryHierarchy?.[0]?.boundaryHierarchy?.find((e1) => e1?.parentBoundaryType == e?.boundaryType)
// )
// );
// }
// }, [hierarchyDefinition]);
const { isLoading: draftLoading, data: draftData, error: draftError, refetch: draftRefetch } = Digit.Hooks.campaign.useSearchCampaign({
tenantId: tenantId,
filter: {
Expand Down Expand Up @@ -835,18 +841,63 @@ const SetupCampaign = () => {
return false;
}

// function validateBoundaryLevel(data) {
// // Extracting boundary types from hierarchy response
// const boundaryTypes = new Set(hierarchyDefinition?.BoundaryHierarchy?.[0]?.boundaryHierarchy.map((item) => item?.boundaryType));

// // Extracting unique boundary types from data
// const uniqueDataBoundaryTypes = new Set(data?.map((item) => item.type));

// // Checking if all unique boundary types from hierarchy response are present in data
// const allBoundaryTypesPresent = [...boundaryTypes].every((type) => uniqueDataBoundaryTypes.has(type));

// return allBoundaryTypesPresent;
// }

function validateBoundaryLevel(data) {
// Extracting boundary types from hierarchy response
const boundaryTypes = new Set(hierarchyDefinition?.BoundaryHierarchy?.[0]?.boundaryHierarchy.map((item) => item?.boundaryType));

// Extracting boundary hierarchy from hierarchy definition
const boundaryHierarchy = hierarchyDefinition?.BoundaryHierarchy?.[0]?.boundaryHierarchy || [];

// Find the index of the lowest hierarchy
const lowestIndex = boundaryHierarchy.findIndex((item) => item?.boundaryType === lowestHierarchy);

// Create a set of boundary types including only up to the lowest hierarchy
const boundaryTypes = new Set(
boundaryHierarchy
.filter((_, index) => index <= lowestIndex)
.map((item) => item?.boundaryType)
);

// Extracting unique boundary types from data
const uniqueDataBoundaryTypes = new Set(data?.map((item) => item.type));

// Checking if all unique boundary types from hierarchy response are present in data
// Checking if all boundary types from the filtered hierarchy are present in data
const allBoundaryTypesPresent = [...boundaryTypes].every((type) => uniqueDataBoundaryTypes.has(type));

return allBoundaryTypesPresent;
}
}


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

// // Build the parent-children map
// filteredData?.forEach((item) => {
// if (item?.parent) {
// if (!parentChildrenMap[item?.parent]) {
// parentChildrenMap[item?.parent] = [];
// }
// parentChildrenMap[item?.parent].push(item.code);
// }
// });

// // Check for missing children
// const missingParents = filteredData?.filter((item) => item?.parent && !parentChildrenMap[item.code]);
// const extraParent = missingParents?.filter((i) => i?.type !== lowest?.[0]?.boundaryType);

// return extraParent;
// }

function recursiveParentFind(filteredData) {
const parentChildrenMap = {};
Expand All @@ -863,11 +914,12 @@ const SetupCampaign = () => {

// Check for missing children
const missingParents = filteredData?.filter((item) => item?.parent && !parentChildrenMap[item.code]);
const extraParent = missingParents?.filter((i) => i?.type !== lowest?.[0]?.boundaryType);
const extraParent = missingParents?.filter((i) => i?.type !== lowestHierarchy);

return extraParent;
}


// validating the screen data on clicking next button
const handleValidate = (formData) => {
const key = Object.keys(formData)?.[0];
Expand Down
32 changes: 32 additions & 0 deletions utilities/project-factory/LOCALSETUP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Local Setup

To set up the ProjectFactory service in your local system, clone the [Digit Frontend repository](https://github.com/egovernments/DIGIT-Frontend).

## Dependencies

### Infra Dependency

- [x] Postgres DB
- [ ] Redis
- [ ] Elasticsearch
- [x] Kafka
- [x] Consumer
- [x] Producer

## Running Locally

### Local setup
1. To setup the ProjectFactory service, clone the [Digit Frontend repository](https://github.com/egovernments/DIGIT-Frontend).
2. Install Node.js version 20 using nvm (Node Version Manager).
3. Update the configs in [utilities/project-factory/src/server/config/index.ts](utilities/project-factory/src/server/config/index.ts), change HOST to "http://localhost:8080/" and KAFKA_BROKER_HOST to "localhost:9092".
4. Also update DB config values as per your local system config.
5. Update all dependency service host either on any unified-env or port-forward.
6. Open the terminal and run the following command

`cd utilities/project-factory/`

`yarn install` (run this command only once when you clone the repo)

`yarn dev`

> Note: After running the above command, if a Kafka error occurs, ensure that Kafka and Zookeeper are running in the background. If a connection error with another microservice occurs, ensure that the URL mentioned in the external mapping of the data config is correct, or you can port-forward that particular service.
Loading

0 comments on commit 8e8b0e1

Please sign in to comment.