|
| 1 | +import { faker } from '@faker-js/faker'; |
| 2 | +import { INestApplication } from '@nestjs/common'; |
| 3 | +import { |
| 4 | + OrganizationsService, |
| 5 | + RolesService, |
| 6 | + SchoolsService, |
| 7 | + UsersService, |
| 8 | +} from '@vidya/api/edu/services'; |
| 9 | +import { Organization, Role, School, User } from '@vidya/entities'; |
| 10 | + |
| 11 | +export type Context = { |
| 12 | + one: { |
| 13 | + org: Organization; |
| 14 | + roles: { |
| 15 | + orgAdmin: Role; |
| 16 | + }; |
| 17 | + schoolOne: { |
| 18 | + school: School; |
| 19 | + roles: { |
| 20 | + schoolOneAdmin: Role; |
| 21 | + }; |
| 22 | + }; |
| 23 | + schoolTwo: { |
| 24 | + school: School; |
| 25 | + roles: { |
| 26 | + schoolTwoAdmin: Role; |
| 27 | + }; |
| 28 | + }; |
| 29 | + users: { |
| 30 | + orgAdmin: User; |
| 31 | + schoolOneAdmin: User; |
| 32 | + schoolTwoAdmin: User; |
| 33 | + }; |
| 34 | + }; |
| 35 | + two: { |
| 36 | + org: Organization; |
| 37 | + roles: { |
| 38 | + orgAdmin: Role; |
| 39 | + }; |
| 40 | + users: { |
| 41 | + orgAdmin: User; |
| 42 | + }; |
| 43 | + }; |
| 44 | +}; |
| 45 | + |
| 46 | +export const createContext = async ( |
| 47 | + app: INestApplication, |
| 48 | +): Promise<Context> => { |
| 49 | + const orgsService = app.get(OrganizationsService); |
| 50 | + const rolesService = app.get(RolesService); |
| 51 | + const schoolsService = app.get(SchoolsService); |
| 52 | + const usersService = app.get(UsersService); |
| 53 | + |
| 54 | + /* -------------------------------------------------------------------------- */ |
| 55 | + /* Organizations */ |
| 56 | + /* -------------------------------------------------------------------------- */ |
| 57 | + |
| 58 | + const orgOne = await orgsService.create({ |
| 59 | + name: 'Org One', |
| 60 | + }); |
| 61 | + |
| 62 | + const orgTwo = await orgsService.create({ |
| 63 | + name: 'Org Two', |
| 64 | + }); |
| 65 | + |
| 66 | + /* -------------------------------------------------------------------------- */ |
| 67 | + /* Schools */ |
| 68 | + /* -------------------------------------------------------------------------- */ |
| 69 | + |
| 70 | + const schoolOne = await schoolsService.create({ |
| 71 | + name: 'School Two', |
| 72 | + organizationId: orgOne.id, |
| 73 | + }); |
| 74 | + |
| 75 | + const schoolTwo = await schoolsService.create({ |
| 76 | + name: 'School Two', |
| 77 | + organizationId: orgOne.id, |
| 78 | + }); |
| 79 | + |
| 80 | + /* -------------------------------------------------------------------------- */ |
| 81 | + /* Roles */ |
| 82 | + /* -------------------------------------------------------------------------- */ |
| 83 | + |
| 84 | + const orgAdmin = await rolesService.create({ |
| 85 | + name: 'Org Admin', |
| 86 | + description: 'Organization Admin', |
| 87 | + organizationId: orgOne.id, |
| 88 | + permissions: ['users:read'], |
| 89 | + }); |
| 90 | + |
| 91 | + const schoolOneAdmin = await rolesService.create({ |
| 92 | + name: 'School One Admin', |
| 93 | + description: 'School One Admin', |
| 94 | + organizationId: orgOne.id, |
| 95 | + schoolId: schoolOne.id, |
| 96 | + permissions: ['users:read'], |
| 97 | + }); |
| 98 | + |
| 99 | + const schoolTwoAdmin = await rolesService.create({ |
| 100 | + name: 'School Two Admin', |
| 101 | + description: 'School Two Admin', |
| 102 | + organizationId: orgOne.id, |
| 103 | + schoolId: schoolTwo.id, |
| 104 | + permissions: ['users:read'], |
| 105 | + }); |
| 106 | + |
| 107 | + const orgTwoAdmin = await rolesService.create({ |
| 108 | + name: 'Org Two Admin', |
| 109 | + description: 'Organization Two Admin', |
| 110 | + organizationId: orgTwo.id, |
| 111 | + permissions: ['users:read'], |
| 112 | + }); |
| 113 | + |
| 114 | + /* -------------------------------------------------------------------------- */ |
| 115 | + /* Users */ |
| 116 | + /* -------------------------------------------------------------------------- */ |
| 117 | + |
| 118 | + const orgAdminUser = await usersService.create({ |
| 119 | + name: 'Org Admin', |
| 120 | + email: faker.internet.email(), |
| 121 | + roles: [orgAdmin], |
| 122 | + }); |
| 123 | + |
| 124 | + const schoolOneAdminUser = await usersService.create({ |
| 125 | + name: 'School One Admin', |
| 126 | + email: faker.internet.email(), |
| 127 | + roles: [schoolOneAdmin], |
| 128 | + }); |
| 129 | + |
| 130 | + const schoolTwoAdminUser = await usersService.create({ |
| 131 | + name: 'School Two Admin', |
| 132 | + email: faker.internet.email(), |
| 133 | + roles: [schoolTwoAdmin], |
| 134 | + }); |
| 135 | + |
| 136 | + const orgTwoAdminUser = await usersService.create({ |
| 137 | + name: 'Org Two Admin', |
| 138 | + email: faker.internet.email(), |
| 139 | + roles: [orgTwoAdmin], |
| 140 | + }); |
| 141 | + |
| 142 | + /* -------------------------------------------------------------------------- */ |
| 143 | + /* Return */ |
| 144 | + /* -------------------------------------------------------------------------- */ |
| 145 | + |
| 146 | + return { |
| 147 | + one: { |
| 148 | + org: orgOne, |
| 149 | + roles: { |
| 150 | + orgAdmin: orgAdmin, |
| 151 | + }, |
| 152 | + schoolOne: { |
| 153 | + school: schoolOne, |
| 154 | + roles: { |
| 155 | + schoolOneAdmin: schoolOneAdmin, |
| 156 | + }, |
| 157 | + }, |
| 158 | + schoolTwo: { |
| 159 | + school: schoolTwo, |
| 160 | + roles: { |
| 161 | + schoolTwoAdmin: schoolTwoAdmin, |
| 162 | + }, |
| 163 | + }, |
| 164 | + users: { |
| 165 | + orgAdmin: orgAdminUser, |
| 166 | + schoolOneAdmin: schoolOneAdminUser, |
| 167 | + schoolTwoAdmin: schoolTwoAdminUser, |
| 168 | + }, |
| 169 | + }, |
| 170 | + two: { |
| 171 | + org: orgTwo, |
| 172 | + roles: { |
| 173 | + orgAdmin: orgTwoAdmin, |
| 174 | + }, |
| 175 | + users: { |
| 176 | + orgAdmin: orgTwoAdminUser, |
| 177 | + }, |
| 178 | + }, |
| 179 | + }; |
| 180 | +}; |
0 commit comments