filter workflows user can see based on entities
This commit is contained in:
@@ -2,7 +2,7 @@ import { ApprovalWorkflow } from "@/interfaces/approval.workflow";
|
|||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import { useCallback, useEffect, useState } from "react";
|
import { useCallback, useEffect, useState } from "react";
|
||||||
|
|
||||||
export default function useApprovalWorkflows() {
|
export default function useApprovalWorkflows(entitiesString?: string) {
|
||||||
const [workflows, setWorkflows] = useState<ApprovalWorkflow[]>([]);
|
const [workflows, setWorkflows] = useState<ApprovalWorkflow[]>([]);
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
const [isError, setIsError] = useState(false);
|
const [isError, setIsError] = useState(false);
|
||||||
@@ -10,7 +10,7 @@ export default function useApprovalWorkflows() {
|
|||||||
const getData = useCallback(() => {
|
const getData = useCallback(() => {
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
axios
|
axios
|
||||||
.get<ApprovalWorkflow[]>(`/api/approval-workflows`)
|
.get<ApprovalWorkflow[]>(`/api/approval-workflows`, {params: { entityIds: entitiesString }})
|
||||||
.then((response) => setWorkflows(response.data))
|
.then((response) => setWorkflows(response.data))
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
setIsError(true);
|
setIsError(true);
|
||||||
|
|||||||
@@ -19,5 +19,9 @@ async function get(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
return res.status(403).json({ ok: false });
|
return res.status(403).json({ ok: false });
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.status(200).json(await getApprovalWorkflows("active-workflows"));
|
const entityIdsString = req.query.entityIds as string;
|
||||||
|
|
||||||
|
const entityIdsArray = entityIdsString.split(",");
|
||||||
|
|
||||||
|
return res.status(200).json(await getApprovalWorkflows("active-workflows", entityIdsArray));
|
||||||
}
|
}
|
||||||
@@ -69,7 +69,11 @@ export const getServerSideProps = withIronSessionSsr(async ({ req, res }) => {
|
|||||||
|
|
||||||
if (shouldRedirectHome(user) || !["admin", "developer", "teacher", "corporate", "mastercorporate"].includes(user.type)) return redirect("/");
|
if (shouldRedirectHome(user) || !["admin", "developer", "teacher", "corporate", "mastercorporate"].includes(user.type)) return redirect("/");
|
||||||
|
|
||||||
const workflows = await getApprovalWorkflows("active-workflows");
|
const entityIDS = mapBy(user.entities, "id");
|
||||||
|
const entities = await getEntitiesWithRoles(isAdmin(user) ? undefined : entityIDS);
|
||||||
|
const allowedEntities = findAllowedEntities(user, entities, "view_workflows");
|
||||||
|
|
||||||
|
const workflows = await getApprovalWorkflows("active-workflows", allowedEntities.map(entity => entity.id));
|
||||||
|
|
||||||
const allAssigneeIds: string[] = [
|
const allAssigneeIds: string[] = [
|
||||||
...new Set(
|
...new Set(
|
||||||
@@ -81,10 +85,6 @@ export const getServerSideProps = withIronSessionSsr(async ({ req, res }) => {
|
|||||||
)
|
)
|
||||||
];
|
];
|
||||||
|
|
||||||
const entityIDS = mapBy(user.entities, "id");
|
|
||||||
const entities = await getEntitiesWithRoles(isAdmin(user) ? undefined : entityIDS);
|
|
||||||
const allowedEntities = findAllowedEntities(user, entities, "view_workflows");
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
props: serialize({
|
props: serialize({
|
||||||
user,
|
user,
|
||||||
@@ -103,7 +103,8 @@ interface Props {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default function ApprovalWorkflows({ user, initialWorkflows, workflowsAssignees, userEntitiesWithLabel }: Props) {
|
export default function ApprovalWorkflows({ user, initialWorkflows, workflowsAssignees, userEntitiesWithLabel }: Props) {
|
||||||
const { workflows, reload } = useApprovalWorkflows();
|
const entitiesString = userEntitiesWithLabel.map(entity => entity.id).join(",");
|
||||||
|
const { workflows, reload } = useApprovalWorkflows(entitiesString);
|
||||||
const currentWorkflows = workflows || initialWorkflows;
|
const currentWorkflows = workflows || initialWorkflows;
|
||||||
|
|
||||||
const [filteredWorkflows, setFilteredWorkflows] = useState<ApprovalWorkflow[]>([]);
|
const [filteredWorkflows, setFilteredWorkflows] = useState<ApprovalWorkflow[]>([]);
|
||||||
|
|||||||
@@ -4,11 +4,18 @@ import { ObjectId } from "mongodb";
|
|||||||
|
|
||||||
const db = client.db(process.env.MONGODB_DB);
|
const db = client.db(process.env.MONGODB_DB);
|
||||||
|
|
||||||
export const getApprovalWorkflows = async (collection: string, ids?: string[]) => {
|
export const getApprovalWorkflows = async (collection: string, entityIds?: string[], ids?: string[]) => {
|
||||||
return await db
|
const filters: any = {};
|
||||||
.collection<ApprovalWorkflow>(collection)
|
|
||||||
.find(ids ? { _id: { $in: ids.map((id) => new ObjectId(id)) } } : {})
|
if (ids && ids.length > 0) {
|
||||||
.toArray();
|
filters.id = { $in: ids };
|
||||||
|
}
|
||||||
|
|
||||||
|
if (entityIds && entityIds.length > 0) {
|
||||||
|
filters.entityId = { $in: entityIds };
|
||||||
|
}
|
||||||
|
|
||||||
|
return await db.collection<ApprovalWorkflow>(collection).find(filters).toArray();
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getApprovalWorkflow = async (collection: string, id: string) => {
|
export const getApprovalWorkflow = async (collection: string, id: string) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user