|
| 1 | +import { GraphQLError } from 'graphql' |
1 | 2 | import Cohort from '../models/cohort.model' |
2 | | -import { Organization } from '../models/organization.model' |
| 3 | +import { IOrganization, Organization } from '../models/organization.model' |
3 | 4 | import Program from '../models/program.model' |
4 | | -import { User } from '../models/user' |
| 5 | +import { RoleOfUser, User, UserInterface } from '../models/user' |
5 | 6 |
|
6 | | -export default async function isAssigned(organName: string, userId: string) { |
7 | | - // Fetch programs and populate organization |
8 | | - const programs: any = await Program.find().populate({ |
9 | | - path: 'organization', |
10 | | - model: Organization, |
11 | | - strictPopulate: false, |
12 | | - }) |
13 | | - |
14 | | - const cohorts: any = await Cohort.find().populate({ |
15 | | - path: 'program', |
16 | | - model: Program, |
17 | | - strictPopulate: false, |
18 | | - populate: [ |
19 | | - { |
20 | | - path: 'organization', |
21 | | - model: Organization, |
22 | | - strictPopulate: false, |
23 | | - }, |
24 | | - { |
25 | | - path: 'users', |
26 | | - model: User, |
27 | | - strictPopulate: false, |
28 | | - }, |
29 | | - ], |
30 | | - }) |
31 | | - let isAssignedToProgramOrCohort = false |
32 | | - |
33 | | - // Check if the user is assigned to any program associated with the organization |
34 | | - for (const element of programs) { |
35 | | - if (element.organization?.name === organName) { |
36 | | - isAssignedToProgramOrCohort = true |
37 | | - return isAssignedToProgramOrCohort |
38 | | - } |
| 7 | +export default async function isAssignedToAnEntity(user: UserInterface, org: IOrganization) { |
| 8 | + const orgUserData = user.organizations.find(data=>data.orgId.toString()===org._id.toString()) |
| 9 | + if(!orgUserData){ |
| 10 | + throw new GraphQLError(`User ${user.email} is not part of ${org.name}`,{ |
| 11 | + extensions:{ |
| 12 | + code: "FORBIDDEN" |
| 13 | + } |
| 14 | + }) |
39 | 15 | } |
40 | | - |
41 | | - // Check if the user is assigned to any cohort associated with the organization |
42 | | - for (const cohort of cohorts) { |
43 | | - if (cohort.program.organization?.name === organName) { |
44 | | - if ( |
45 | | - cohort.users.some( |
46 | | - (user: { _id: { toString: () => string } }) => |
47 | | - user._id.toString() === userId |
48 | | - ) |
49 | | - ) { |
50 | | - isAssignedToProgramOrCohort = true |
51 | | - return isAssignedToProgramOrCohort |
52 | | - } |
53 | | - } |
| 16 | + switch(orgUserData.role){ |
| 17 | + case RoleOfUser.SUPER_ADMIN: |
| 18 | + case RoleOfUser.ADMIN: |
| 19 | + break |
| 20 | + case RoleOfUser.MANAGER: |
| 21 | + const programs = await Program.find({ |
| 22 | + manager: user._id, |
| 23 | + organization: org._id, |
| 24 | + }) |
| 25 | + if(!programs.length){ |
| 26 | + throw new GraphQLError(`User ${user.email} does not manage any programs`,{ |
| 27 | + extensions: { |
| 28 | + code: "FORBIDDEN" |
| 29 | + } |
| 30 | + }) |
| 31 | + } |
| 32 | + break |
| 33 | + case RoleOfUser.COORDINATOR: |
| 34 | + // if(!orgUserData.program){ |
| 35 | + // throw new GraphQLError(`User ${user.email} is not assigned to a program`,{ |
| 36 | + // extensions: { |
| 37 | + // code: "FORBIDDEN" |
| 38 | + // } |
| 39 | + // }) |
| 40 | + // } |
| 41 | + const cohorts = await Cohort.find({ |
| 42 | + coordinator: user._id, |
| 43 | + organization: org._id, |
| 44 | + }) |
| 45 | + if(!cohorts.length){ |
| 46 | + throw new GraphQLError(`User ${user.email} does not manage any cohorts`,{ |
| 47 | + extensions: { |
| 48 | + code: "FORBIDDEN" |
| 49 | + } |
| 50 | + }) |
| 51 | + } |
| 52 | + break |
| 53 | + case RoleOfUser.TTL: |
| 54 | + if(!orgUserData.program){ |
| 55 | + throw new GraphQLError(`User ${user.email} is not assigned to a program`,{ |
| 56 | + extensions: { |
| 57 | + code: "FORBIDDEN" |
| 58 | + } |
| 59 | + }) |
| 60 | + } |
| 61 | + if(!orgUserData.cohort){ |
| 62 | + throw new GraphQLError(`User ${user.email} is not assigned to a cohort`,{ |
| 63 | + extensions: { |
| 64 | + code: "FORBIDDEN" |
| 65 | + } |
| 66 | + }) |
| 67 | + } |
| 68 | + if(!orgUserData.team){ |
| 69 | + throw new GraphQLError(`User ${user.email} does not manage any team`,{ |
| 70 | + extensions: { |
| 71 | + code: "FORBIDDEN" |
| 72 | + } |
| 73 | + }) |
| 74 | + } |
| 75 | + break |
| 76 | + case RoleOfUser.TRAINEE: |
| 77 | + if(!orgUserData.program){ |
| 78 | + throw new GraphQLError(`User ${user.email} is not assigned to a program`,{ |
| 79 | + extensions: { |
| 80 | + code: "FORBIDDEN" |
| 81 | + } |
| 82 | + }) |
| 83 | + } |
| 84 | + if(!orgUserData.cohort){ |
| 85 | + throw new GraphQLError(`User ${user.email} is not assigned to a cohort`,{ |
| 86 | + extensions: { |
| 87 | + code: "FORBIDDEN" |
| 88 | + } |
| 89 | + }) |
| 90 | + } |
| 91 | + if(!orgUserData.team){ |
| 92 | + throw new GraphQLError(`User ${user.email} is not assigned to a team`,{ |
| 93 | + extensions: { |
| 94 | + code: "FORBIDDEN" |
| 95 | + } |
| 96 | + }) |
| 97 | + } |
| 98 | + break |
| 99 | + default: |
| 100 | + throw new GraphQLError("User role is not valid",{ |
| 101 | + extensions: { |
| 102 | + code: "FORBIDDEN" |
| 103 | + } |
| 104 | + }) |
54 | 105 | } |
55 | | - return isAssignedToProgramOrCohort |
56 | 106 | } |
0 commit comments