Skip to content

Commit

Permalink
Check the eligibility criteria for the details on the benefits applic…
Browse files Browse the repository at this point in the history
…ation page. (#62)

* fix:Check the eligibility criteria for the details on the benefits application page.

* fix:check Eligibility Criteria change

* fix:checkEligibilityCriteria optimize

* fix: checkEligibilityCriteria build issue

* fix:error

* fix:code rabbit issue

---------

Co-authored-by: sagar <sagarkoshti1990@gmila.com>
  • Loading branch information
sagarkoshti1990 and sagar authored Dec 3, 2024
1 parent 4ad68b2 commit daee796
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 34 deletions.
90 changes: 56 additions & 34 deletions src/screens/benefit/Details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { MdCurrencyRupee } from "react-icons/md";
import WebViewFormSubmitWithRedirect from "../../components/WebView";
import { useTranslation } from "react-i18next";
import Loader from "../../components/common/Loader";
import { checkEligibilityCriteria } from "../../utils/jsHelper/helper";
import { termsAndConditions } from "../../assets/termsAndCondition";
import CommonDialogue from "../../components/common/Dialogue";

Expand Down Expand Up @@ -97,34 +98,32 @@ const BenefitsDetails: React.FC = () => {
const navigate = useNavigate();
const { id } = useParams<{ id: string }>();
const { t } = useTranslation();
const [isEligible, setIsEligible] = useState<any[]>();

const handleConfirmation = async () => {
setLoading(true);

// try {
const result = await applyApplication({ id, context });
const url = (result as { data: { responses: Array<any> } }).data
?.responses?.[0]?.message?.order?.items?.[0]?.xinput?.form?.url;
const formData = authUser ?? undefined; // Ensure authUser is used or fallback to undefined
setLoading(false);
// Only set WebFormProps if the url exists
if (url) {
setWebFormProp({
url,
formData,
});
if (isEligible?.length > 0) {
setError(
`You cannot proceed further because the criteria are not matching, such as ${isEligible.join(
", "
)}.`
);
} else {
setError("URL not found in response");
setLoading(true);
const result = await applyApplication({ id, context });
const url = (result as { data: { responses: Array<any> } }).data
?.responses?.[0]?.message?.order?.items?.[0]?.xinput?.form?.url;
const formData = authUser ?? undefined; // Ensure authUser is used or fallback to undefined
setLoading(false);
// Only set WebFormProps if the url exists
if (url) {
setWebFormProp({
url,
formData,
});
} else {
setError("URL not found in response");
}
}
// } catch (error: unknown) {
// if (error instanceof Error) {
// setError(`Failed to apply application: ${error.message}`);
// } else {
// setError("An unknown error occurred");
// }
// } finally {
// setLoading(false);
// }
};

const handleBack = () => {
Expand Down Expand Up @@ -158,9 +157,38 @@ const BenefitsDetails: React.FC = () => {
const token = localStorage.getItem("authToken");
if (token) {
const user = await getUser();

const eligibilityArr = [];
if (Array.isArray(resultItem?.tags)) {
resultItem?.tags?.forEach((e: any) => {
if (e?.descriptor?.code === "@eligibility") {
if (Array.isArray(e.list)) {
e.list.forEach((item: any) => {
const code = item?.descriptor?.code;
try {
const valueObj = JSON.parse(item.value || "{}");
const payload = {
...valueObj,
value: user?.data?.[code],
};
const result = checkEligibilityCriteria(payload);
if (!result) {
eligibilityArr.push(code);
}
} catch (error) {
console.error(
`Failed to parse eligibility criteria: ${error}`
);
eligibilityArr.push(code);
}
});
}
}
});
}
setIsEligible(
eligibilityArr.length > 0 ? eligibilityArr : undefined
);
setAuthUser(user?.data || {});

const appResult = await getApplication({
user_id: user?.data?.user_id,
benefit_id: id,
Expand All @@ -180,6 +208,7 @@ const BenefitsDetails: React.FC = () => {
setError("An unexpected error occurred");
}
}
setLoading(false);
}
};
init();
Expand Down Expand Up @@ -302,14 +331,7 @@ const BenefitsDetails: React.FC = () => {
</Heading>
<UnorderedList mt={4}>
{item?.tags
?.filter((tag) =>
[
"educational-eligibility",
"personal-eligibility",
"economical-eligibility",
"geographical-eligibility",
].includes(tag.descriptor?.code)
)
?.filter((tag) => ["@eligibility"].includes(tag.descriptor?.code))
.map((tag, index) => (
<ListItem key={"detail" + index}>
{tag.descriptor?.short_desc}
Expand Down
84 changes: 84 additions & 0 deletions src/utils/jsHelper/helper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,87 @@ export function getSubmmitedDoc(userData, document) {
}
return result;
}

/**
* Checks if the given value satisfies the given condition with respect to the given condition values.
* The condition can be any of the following:
* - "equals": checks if value equals any of the condition values
* - "lessThan": checks if value is less than the first condition value
* - "lessThanOrEquals": checks if value is less than or equals the first condition value
* - "greaterThan": checks if value is greater than the first condition value
* - "greaterThanOrEquals": checks if value is greater than or equals the first condition value
* - "in": checks if value is in the condition values
* - "notIn": checks if value is not in the condition values
*
* @param {string|number} value The value to check
* @param {string} condition The condition to evaluate
* @param {string|number|string[]|number[]} conditionValues The values to check against
* @returns {boolean} true if the value satisfies the condition, false otherwise
*/
export function checkEligibilityCriteria({
value,
condition,
conditionValues,
}: {
value: string | number | null | undefined;
condition: string;
conditionValues: string | number | (string | number)[];
}): boolean {
if (value == null) return false;
// Convert value to string if it's a number
const val = typeof value === "string" ? value : value?.toString();
if (!val) return false;

// Convert conditionValues to an array of strings
const conditionVals: string[] =
typeof conditionValues === "string"
? [conditionValues]
: (conditionValues as (string | number)[]).map((cv) => cv?.toString());

// Evaluate the condition
switch (condition.trim()) {
case "equals":
// Check if value equals any of the condition values
return conditionVals.includes(val);
case "lessThan":
case "less than":
// Check if value is less than the first condition value
return (
conditionVals.length > 0 &&
parseInt(conditionVals[0], 10) > parseInt(val, 10)
);
case "lessThanOrEquals":
case "less than or equals":
case "less than equals":
// Check if value is less than or equals the first condition value
return (
conditionVals.length > 0 &&
parseInt(conditionVals[0], 10) >= parseInt(val, 10)
);
case "greaterThan":
case "greater than":
// Check if value is greater than the first condition value
return (
conditionVals.length > 0 &&
parseInt(conditionVals[0], 10) < parseInt(val, 10)
);
case "greaterThanOrEquals":
case "greater than or equals":
case "greater than equals":
// Check if value is greater than or equals the first condition value
return (
conditionVals.length > 0 &&
parseInt(conditionVals[0], 10) <= parseInt(val, 10)
);
case "in":
// Check if value is in the condition values
return conditionVals.includes(val);
case "notIn":
case "not in":
// Check if value is not in the condition values
return !conditionVals.includes(val);
default:
// Return false for unrecognized conditions
return false;
}
}

0 comments on commit daee796

Please sign in to comment.