Skip to content

Commit

Permalink
OM-248: filter out bills if eu config enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
olewandowski1 committed Aug 21, 2024
1 parent 14b2a47 commit 261c1b3
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,6 @@ None
* `state.core.user`, to access user info (rights,...)

## Configurations Options
None

- `App.economicUnitConfig`:
This configuration option determines whether the Bill page should be filtered based on the selected Economic Unit. When set to **true**, the Bill page will display data only for the chosen Economic Unit, ensuring that users view information specific to their assigned unit. If set to **false** (the default setting), the Bill page will not be filtered by any Economic Unit, and users can view all available data regardless of their unit association.
60 changes: 57 additions & 3 deletions src/components/BillSearcher.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useState, useCallback, useEffect, useRef } from "react";
import { bindActionCreators } from "redux";
import { connect } from "react-redux";
import { connect, useSelector } from "react-redux";
import { injectIntl } from "react-intl";

import { IconButton, Tooltip, Button, Dialog, DialogActions, DialogTitle, DialogContent } from "@material-ui/core";
Expand All @@ -18,6 +18,7 @@ import {
withHistory,
historyPush,
downloadExport,
decodeId,
} from "@openimis/fe-core";
import { fetchBills, deleteBill, fetchBillsExport } from "../actions";
import {
Expand All @@ -27,6 +28,9 @@ import {
RIGHT_BILL_SEARCH,
RIGHT_BILL_DELETE,
STATUS,
DEFAULT,
INSPECTOR_RIGHT,
ADMIN_RIGHT,
} from "../constants";
import BillFilter from "./BillFilter";
import InvoiceStatusPicker from "../pickers/InvoiceStatusPicker";
Expand Down Expand Up @@ -56,10 +60,14 @@ const BillSearcher = ({
actions,
actionsContributionKey,
}) => {
const prevSubmittingMutationRef = useRef();
const [billToDelete, setBillToDelete] = useState(null);
const [deletedBillUuids, setDeletedBillUuids] = useState([]);
const [failedExport, setFailedExport] = useState(false);
const prevSubmittingMutationRef = useRef();
const [queryParams, setQueryParams] = useState([]);
const { economicUnit } = useSelector((state) => state.policyHolder);
const economicUnitConfig = modulesManager.getConf("fe-core", "App.economicUnitConfig", DEFAULT.ECONOMIC_UNIT_CONFIG);
const isAdminOrInspector = rights.includes(INSPECTOR_RIGHT) || rights.includes(ADMIN_RIGHT);

useEffect(() => billToDelete && openConfirmDialog(), [billToDelete]);

Expand Down Expand Up @@ -115,7 +123,24 @@ const BillSearcher = ({
[billToDelete],
);

const fetch = (params) => fetchBills(params);
const fetch = useCallback(
(params) => {
try {
const actionParams = [...params];

const decodedId = decodeId(economicUnit?.id ?? EMPTY_STRING);

if (economicUnitConfig && economicUnit?.id && !isAdminOrInspector) {
actionParams.push(`subjectId:"${decodedId}"`);
}

fetchBills(actionParams);
} catch (error) {
throw new Error(`[BILL_SEARCHER]: Fetching bills failed. ${error}`);
}
},
[economicUnit],
);

const headers = () => {
const headers = [
Expand Down Expand Up @@ -198,6 +223,34 @@ const BillSearcher = ({
},
});

const filtersToQueryParams = ({ filters, pageSize, beforeCursor, afterCursor, orderBy }) => {
const queryParams = Object.keys(filters)
.filter((f) => !!filters[f].filter)
.map((f) => filters[f].filter);
if (!beforeCursor && !afterCursor) {
queryParams.push(`first: ${pageSize}`);
}
if (afterCursor) {
queryParams.push(`after: "${afterCursor}"`);
queryParams.push(`first: ${pageSize}`);
}
if (beforeCursor) {
queryParams.push(`before: "${beforeCursor}"`);
queryParams.push(`last: ${pageSize}`);
}
if (orderBy) {
queryParams.push(`orderBy: ["${orderBy}"]`);
}
setQueryParams(queryParams);
return queryParams;
};

useEffect(() => {
if (queryParams.length) {
fetch(queryParams);
}
}, [economicUnit, queryParams]);

return (
<div>
<Searcher
Expand All @@ -209,6 +262,7 @@ const BillSearcher = ({
fetchingItems={fetchingBills}
fetchedItems={fetchedBills}
errorItems={errorBills}
filtersToQueryParams={filtersToQueryParams}
tableTitle={formatMessageWithValues(intl, "bill", "bills.searcherResultsTitle", {
billsTotalCount,
})}
Expand Down
3 changes: 3 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export const RIGHT_BILL_EVENT_DELETE = 156304;
export const RIGHT_BILL_EVENT_CREATE_MESSAGE = 156306;
export const RIGHT_BILL_EVENT_DELETE_MY_MESSAGE = 156307;
export const RIGHT_BILL_EVENT_DELETE_ALL_MESSAGE = 156308;
export const INSPECTOR_RIGHT = 204005;
export const ADMIN_RIGHT = 204004;
export const DEFAULT_PAGE_SIZE = 10;
export const ROWS_PER_PAGE_OPTIONS = [10, 20, 50, 100];
export const EMPTY_STRING = "";
Expand Down Expand Up @@ -177,4 +179,5 @@ export const EMPTY_EVENT_MESSAGE = {

export const DEFAULT = {
IS_WORKER: false,
ECONOMIC_UNIT_CONFIG: false,
};

0 comments on commit 261c1b3

Please sign in to comment.