Skip to content

Commit

Permalink
created function to find roles in a project
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiebones committed Oct 20, 2022
1 parent c69b595 commit e81d8b2
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
50 changes: 50 additions & 0 deletions server/graphql/resolvers/projects/query/findProjectRoles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Projects } from "../../../../models/projectModel";
import { FindProjectRolesInput, ProjectRole, Project } from "../../../../generated";
import { ApolloError } from "apollo-server-express";

const findProjectRoles = async (
parent: any,
args: {
request: FindProjectRolesInput;
},
context: any,
info: any,
) => {
try {
const { projectID, roleID } = args.request;
console.log("Query > findProjectRole > args.request = ", args.request);

if (!projectID || !roleID) throw new ApolloError("The projectID and the roleID is required");

const projects: Project = await Projects.findOne({ _id: projectID });

if (!projects) throw new ApolloError("The project with the supplied projectID does not exist");

const roles = projects?.roles as ProjectRole[];

if (roles?.length) {
//find the role.
let rolesArray: ProjectRole[] = [];
roles.forEach((role: any) => {
if (roleID.includes(role._id)) {
rolesArray.push(role);
}
});

if (rolesArray.length) {
console.log("roles array 🔥 ", rolesArray);
return rolesArray;
} else {
throw new ApolloError("The roleID does not exist on the project");
}
} else {
throw new ApolloError("No roles exist on the project yet");
}
} catch (err: any) {
throw new ApolloError(err.message, err.extensions?.code || "findProjectRoles", {
component: "ProjectQuery > findProjectRoles",
});
}
};

export default findProjectRoles;
7 changes: 5 additions & 2 deletions server/graphql/resolvers/projects/query/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import findProject from "./findProject";
import findProjects from "./findProjects";

import findProjectRole from "./findProjectRole";
import findProjectRoles from "./findProjectRoles";

export default {
findProject,
findProjects
findProjects,
findProjectRole,
findProjectRoles
}

0 comments on commit e81d8b2

Please sign in to comment.