Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: CE-1296 add Inspector and Province-wide roles #857

Merged
merged 4 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 25 additions & 19 deletions frontend/src/app/components/containers/complaints/complaints.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
selectActiveTab,
selectActiveComplaintsViewType,
setActiveComplaintsViewType,
selectDefaultRegion,
} from "../../../store/reducers/app";

import { ComplaintMapWithServerSideClustering } from "./complaint-map-with-server-side-clustering";
Expand All @@ -28,6 +29,8 @@ import UserService from "@service/user-service";
import Roles from "@apptypes/app/roles";
import Option from "@apptypes/app/option";
import { resetComplaintSearchParameters, selectComplaintSearchParameters } from "@/app/store/reducers/complaints";
import { AgencyType } from "@/app/types/app/agency-types";
import { DropdownOption } from "@/app/types/app/drop-down-option";

type Props = {
defaultComplaintType: string;
Expand All @@ -45,7 +48,7 @@ export const Complaints: FC<Props> = ({ defaultComplaintType }) => {
if (!storedComplaintType) dispatch(setActiveTab(defaultComplaintType));
}, [storedComplaintType, dispatch, defaultComplaintType]);
const [complaintType, setComplaintType] = useState(
UserService.hasRole([Roles.CEEB]) ? CEEB_TYPES.ERS : (storedComplaintType ?? defaultComplaintType),
UserService.hasRole([Roles.CEEB]) ? CEEB_TYPES.ERS : storedComplaintType ?? defaultComplaintType,
);

const storedComplaintViewType = useAppSelector(selectActiveComplaintsViewType);
Expand All @@ -57,6 +60,7 @@ export const Complaints: FC<Props> = ({ defaultComplaintType }) => {
const currentOfficer = useAppSelector(selectCurrentOfficer(), shallowEqual);

const defaultZone = useAppSelector(selectDefaultZone);
const defaultRegion = useAppSelector(selectDefaultRegion);

//-- this is used to apply the search to the pager component
const storedSearchParams = useAppSelector(selectComplaintSearchParameters);
Expand All @@ -65,7 +69,7 @@ export const Complaints: FC<Props> = ({ defaultComplaintType }) => {
const handleComplaintTabChange = (complaintType: string) => {
setComplaintType(complaintType);
dispatch(resetComplaintSearchParameters());
let filters = getFilters(currentOfficer, defaultZone);
let filters = getFilters(currentOfficer, defaultZone, defaultRegion);

const payload: Array<ComplaintFilterPayload> = [
...Object.entries(filters).map(([filter, value]) => ({
Expand Down Expand Up @@ -180,14 +184,13 @@ export const Complaints: FC<Props> = ({ defaultComplaintType }) => {

export const ComplaintsWrapper: FC<Props> = ({ defaultComplaintType }) => {
const defaultZone = useAppSelector(selectDefaultZone, shallowEqual);
const defaultRegion = useAppSelector(selectDefaultRegion);
const currentOfficer = useAppSelector(selectCurrentOfficer(), shallowEqual);
const storedSearchParams = useAppSelector(selectComplaintSearchParameters);
// If the search is fresh, there are only 2 default parameters set. If more than 2 exist,
// this is not a fresh search as the search funtion itself sets more filters, even if blank.
const freshSearch = Object.keys(storedSearchParams).length === 2;
const defaultFilters = getFilters(currentOfficer, defaultZone);
const complaintFilters = freshSearch ? defaultFilters : storedSearchParams;

const complaintFilters = freshSearch ? getFilters(currentOfficer, defaultZone, defaultRegion) : storedSearchParams;
return (
<>
{currentOfficer && (
Expand All @@ -205,24 +208,27 @@ const getComplaintTypes = () => {
return UserService.hasRole(Roles.CEEB) ? CEEB_TYPES : COMPLAINT_TYPES;
};

const getFilters = (currentOfficer: any, defaultZone: any) => {
const getFilters = (currentOfficer: any, defaultZone: DropdownOption | null, defaultRegion: DropdownOption | null) => {
let filters: any = {};

if (UserService.hasRole([Roles.CEEB]) && !UserService.hasRole([Roles.CEEB_COMPLIANCE_COORDINATOR])) {
if (currentOfficer) {
let {
person: { firstName, lastName, id },
} = currentOfficer;
const officer = { label: `${lastName}, ${firstName}`, value: id };

filters = { ...filters, officer };
}
// Province-wide role defaults to only "Open" so skip the other checks
if (UserService.hasRole(Roles.PROVINCE_WIDE)) {
return filters;
}

if (UserService.hasRole([Roles.CEEB, Roles.CEEB_COMPLIANCE_COORDINATOR])) {
// No additional filters needed, default will apply
} else if (defaultZone) {
filters = { ...filters, zone: defaultZone };
const userAgency = UserService.getUserAgency();
if (userAgency === AgencyType.COS) {
if (UserService.hasRole(Roles.INSPECTOR)) {
filters = { ...filters, region: defaultRegion };
} else {
filters = { ...filters, zone: defaultZone };
}
} else if (currentOfficer && !UserService.hasRole(Roles.CEEB_COMPLIANCE_COORDINATOR)) {
const {
person: { firstName, lastName, id },
} = currentOfficer;
const officer = { label: `${lastName}, ${firstName}`, value: id };
filters = { ...filters, officer };
}

return filters;
Expand Down
6 changes: 2 additions & 4 deletions frontend/src/app/components/containers/layout/side-bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,19 @@ export const SideBar: FC = () => {
name: "Complaints",
icon: "bi bi-file-earmark-medical",
route: "/complaints",
roles: ["COS", "CEEB"],
},
{
id: "create-complaints-link",
name: "Create Complaint",
icon: "bi bi-plus-circle",
route: "complaint/createComplaint",
roles: ["COS", "CEEB"],
},
{
id: "zone-at-a-glance-link",
name: "Zone at a Glance",
icon: "bi bi-buildings",
route: "/zone/at-a-glance",
roles: ["COS"],
excludedRoles: [Roles.CEEB, Roles.PROVINCE_WIDE],
},
];

Expand Down Expand Up @@ -103,7 +101,7 @@ export const SideBar: FC = () => {
{/* <!-- menu items for the organization --> */}
<ul className="nav nav-pills flex-column mb-auto comp-sidenav-list">
{menueItems.map((item, idx) => {
if (UserService.hasRole(Roles.CEEB) && !item.roles.includes("CEEB")) {
if (item.excludedRoles && UserService.hasRole(item.excludedRoles)) {
// Do not display this hence return null
return null;
}
Expand Down
8 changes: 3 additions & 5 deletions frontend/src/app/providers/complaint-filter-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ let initialState: ComplaintFilters = {
outcomeAnimalEndDate: undefined,
};

const mapFilters = (complaintFilters: Partial<ComplaintFilters>) => {
const convertFilterNames = (complaintFilters: Partial<ComplaintFilters>) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that I oppose the change, but why?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing because it neither calls the map function nor has anything to do with the map in the application.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^ It was me who named it map in the first place I think, but when I got in there this time it felt wrong. This feels like a more accurate name.

/**
* This funtion takes a partial set of filters in the shape of the ComplaintSearchParameters from
* the store, and maps them into the initial state for this provider. This enables the search page
Expand Down Expand Up @@ -119,11 +119,9 @@ const ComplaintFilterContext = createContext<ComplaintFilterContextType>({
const ComplaintFilterProvider: FC<ProviderProps> = ({ children, freshSearch, complaintFilters }) => {
let startingState = { ...initialState };
if (freshSearch) {
startingState = complaintFilters.zone
? { ...startingState, status: { value: "OPEN", label: "Open" }, zone: complaintFilters.zone }
: { ...startingState, status: { value: "OPEN", label: "Open" } };
startingState = { ...startingState, ...complaintFilters, status: { value: "OPEN", label: "Open" } };
} else {
const activeFilters = mapFilters(complaintFilters);
const activeFilters = convertFilterNames(complaintFilters);
startingState = { ...startingState, ...activeFilters };
}

Expand Down
13 changes: 13 additions & 0 deletions frontend/src/app/store/reducers/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,14 @@ export const selectDefaultZone = (state: RootState): DropdownOption | null => {
return value && label ? { value, label } : null;
};

export const selectDefaultRegion = (state: RootState): DropdownOption | null => {
const {
profile: { region: value, regionDescription: label },
} = state.app;

return value && label ? { value, label } : null;
};

export const selectModalOpenState = (state: RootState): boolean => {
const { app } = state;
return app.modalIsOpen;
Expand Down Expand Up @@ -366,6 +374,7 @@ export const getTokenProfile = (): AppThunk => async (dispatch) => {

let office = "";
let region = "";
let regionDescription = "";
let zone = "";
let zoneDescription = "";
let agency = "";
Expand All @@ -380,6 +389,7 @@ export const getTokenProfile = (): AppThunk => async (dispatch) => {

office = unit.office_location_code;
region = unit.region_code;
regionDescription = unit.region_name;
zone = unit.zone_code;
zoneDescription = unit.zone_name;
agency = agencyCode.agency_code;
Expand All @@ -402,6 +412,7 @@ export const getTokenProfile = (): AppThunk => async (dispatch) => {
idir_username: idir_username,
office: office,
region: region,
regionDescription: regionDescription,
zone: zone,
zoneDescription: zoneDescription,
agency,
Expand Down Expand Up @@ -537,6 +548,7 @@ const initialState: AppState = {
idir_username: "",
office: "",
region: "",
regionDescription: "",
zone: "",
zoneDescription: "",
agency: "",
Expand Down Expand Up @@ -592,6 +604,7 @@ const reducer = (state: AppState = initialState, action: any): AppState => {
idir_username: payload.idir_username,
office: payload.office,
region: payload.region,
regionDescription: payload.regionDescription,
zone: payload.zone,
zoneDescription: payload.zoneDescription,
agency: payload.agency,
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/app/types/app/menu-item.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import Roles from "@/app/types/app/roles";

export default interface MenuItem {
id?: string;
name: string;
icon: string;
route?: string;
roles: Array<string>;
excludedRoles?: Array<Roles>;
}
1 change: 1 addition & 0 deletions frontend/src/app/types/app/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default interface Profile {
idir_username: string;
office: string;
region: string;
regionDescription: string;
zone: string;
zoneDescription: string;
agency: string;
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/app/types/app/roles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ enum Roles {
COS_OFFICER = "COS Officer",
TEMPORARY_TEST_ADMIN = "TEMPORARY_TEST_ADMIN",
READ_ONLY = "READ ONLY",
INSPECTOR = "Inspector",
PROVINCE_WIDE = "Province-wide",
}

export default Roles;