Skip to content

Commit 983179f

Browse files
committed
deleteUser, updateUserRole, requestOrganization, types, validations, fixed helpers
1 parent 9a2d87c commit 983179f

File tree

15 files changed

+539
-481
lines changed

15 files changed

+539
-481
lines changed

package-lock.json

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@
8888
"ts-node-dev": "^2.0.0",
8989
"tslog": "^4.9.2",
9090
"ws": "^8.11.0",
91-
"xlsx": "^0.18.5"
91+
"xlsx": "^0.18.5",
92+
"zod": "^3.23.8"
9293
},
9394
"devDependencies": {
9495
"@istanbuljs/nyc-config-typescript": "^1.0.1",

src/helpers/organization.helper.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { GraphQLError } from 'graphql'
22
import 'dotenv/config'
33
import { JwtPayload, verify } from 'jsonwebtoken'
4-
import { Organization } from '../models/organization.model'
4+
import { IOrganization, Organization } from '../models/organization.model'
5+
import { OrgUserDataInterface, UserInterface } from '../models/user'
56

67
export async function checkLoggedInOrganization(token?: string) {
78
const SECRET = process.env.SECRET as string
@@ -50,3 +51,15 @@ export async function checkLoggedInOrganization(token?: string) {
5051
}
5152
}
5253
}
54+
55+
export function isPartOfOrganization(user: UserInterface, org: IOrganization): OrgUserDataInterface{
56+
const orgUserData = user.organizations.find(data=>data.orgId.toString()===org._id.toString())
57+
if(!orgUserData){
58+
throw new GraphQLError(`User ${user.email} is not part of ${org.name}`,{
59+
extensions: {
60+
code: "FORBIDDEN"
61+
}
62+
})
63+
}
64+
return orgUserData
65+
}

src/helpers/user.helpers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export const emailExpression =
2727
export async function checkUserLoggedIn(
2828
org: IOrganization,
2929
context: Context
30-
): Promise<(a?: Array<string>) => {user: UserInterface, orgUserData: OrgUserDataInterface}> {
30+
): Promise<(a?: Array<string>) => UserInterface> {
3131
const { userId, role } = context
3232

3333
if (!userId) {
@@ -76,6 +76,6 @@ export async function checkUserLoggedIn(
7676
)
7777
}
7878

79-
return {user, orgUserData}
79+
return user
8080
}
8181
}

src/models/cohort.model.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import mongoose, { Schema } from 'mongoose'
1+
import mongoose, { Schema, Document } from 'mongoose'
22
import { User } from './user'
33
import { PhaseInterface } from './phase.model';
44

5-
export interface CohortInterface {
6-
_id: mongoose.Types.ObjectId;
5+
export interface CohortInterface extends Document {
6+
id?: string;
77
name: string;
88
phase: PhaseInterface;
99
coordinator: mongoose.Types.ObjectId;
@@ -24,21 +24,20 @@ const cohortSchema = new Schema(
2424
required: true,
2525
},
2626
phase: {
27-
type: mongoose.Types.ObjectId,
27+
type: Schema.Types.ObjectId,
2828
required: true,
2929
ref: 'Phase',
3030
},
3131
coordinator: {
32-
type: mongoose.Types.ObjectId,
32+
type: Schema.Types.ObjectId,
3333
ref: 'User',
34-
required: true,
3534
},
3635
members: {
37-
type: [mongoose.Types.ObjectId],
36+
type: [Schema.Types.ObjectId],
3837
ref: 'User',
3938
},
4039
program: {
41-
type: mongoose.Types.ObjectId,
40+
type: Schema.Types.ObjectId,
4241
required: true,
4342
ref: 'Program',
4443
},
@@ -59,7 +58,7 @@ const cohortSchema = new Schema(
5958
type: Date,
6059
},
6160
organization: {
62-
type: mongoose.Types.ObjectId,
61+
type: Schema.Types.ObjectId,
6362
ref: 'Organization',
6463
required: true,
6564
},

src/models/profile.model.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ interface IActivity extends mongoose.Types.Subdocument{
1616

1717
export interface IProfile extends Document{
1818
id?: string,
19+
orgId: mongoose.Types.ObjectId,
1920
biography?: string,
2021
avatar?: string,
2122
cover?: string,
@@ -40,6 +41,11 @@ const ActivitySchema = new mongoose.Schema({
4041

4142
const profileSchema = new Schema(
4243
{
44+
orgId: {
45+
type: Schema.Types.ObjectId,
46+
ref: 'Organization',
47+
required: true,
48+
},
4349
biography: {
4450
type: String,
4551
},
@@ -51,7 +57,7 @@ const profileSchema = new Schema(
5157
},
5258
activity: [ActivitySchema],
5359
user: {
54-
type: mongoose.Types.ObjectId,
60+
type: Schema.Types.ObjectId,
5561
ref: 'User',
5662
required: true,
5763
},

src/models/program.model.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ const programSchema = new Schema(
1515
manager: {
1616
type: mongoose.Types.ObjectId,
1717
ref: 'User',
18-
required: true,
1918
},
2019
organization: {
2120
type: mongoose.Types.ObjectId,

src/models/team.model.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { CohortInterface } from './cohort.model';
44
import { PhaseInterface } from './phase.model';
55

66
export interface TeamInterface {
7-
_id: mongoose.Types.ObjectId;
7+
id?: string;
88
name: string;
99
cohort?: CohortInterface;
1010
phase?: PhaseInterface;
@@ -23,15 +23,15 @@ const teamSchema = new Schema(
2323
required: true,
2424
},
2525
cohort: {
26-
type: mongoose.Types.ObjectId,
26+
type: Schema.Types.ObjectId,
2727
ref: 'Cohort',
2828
},
2929
ttl: {
30-
type: mongoose.Types.ObjectId,
30+
type: Schema.Types.ObjectId,
3131
ref: 'User',
3232
},
3333
members: {
34-
type: [mongoose.Types.ObjectId],
34+
type: [Schema.Types.ObjectId],
3535
ref: 'User',
3636
},
3737
startingPhase: {
@@ -44,20 +44,20 @@ const teamSchema = new Schema(
4444
default: true,
4545
},
4646
organization: {
47-
type: mongoose.Types.ObjectId,
47+
type: Schema.Types.ObjectId,
4848
ref: 'Organization',
4949
required: true,
5050
},
5151
manager: {
52-
type: mongoose.Types.ObjectId,
52+
type: Schema.Types.ObjectId,
5353
ref: 'User',
5454
},
5555
phase: {
56-
type: mongoose.Types.ObjectId,
56+
type: Schema.Types.ObjectId,
5757
ref: 'Phase',
5858
},
5959
program: {
60-
type: mongoose.Types.ObjectId,
60+
type: Schema.Types.ObjectId,
6161
ref: 'Program',
6262
},
6363
isJobActive: {

src/models/user.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import bcrypt from 'bcryptjs'
22
import mongoose, { model, Schema, Document } from 'mongoose'
33
import { Profile } from './profile.model'
44

5-
enum STATUS_ENUM{
5+
export enum USER_STATUS_ENUM{
66
ACTIVE="active",
77
DROP="drop",
88
SUSPENDED="suspended"
99
}
1010

1111
export interface UserStatus {
12-
status: STATUS_ENUM,
12+
status: USER_STATUS_ENUM,
1313
reason?: string,
1414
date?: Date,
1515
}
@@ -77,8 +77,8 @@ const orgUserDataSchema = new Schema({
7777
status: {
7878
status: {
7979
type: String,
80-
enum: Object.values(STATUS_ENUM),
81-
default: STATUS_ENUM.ACTIVE,
80+
enum: Object.values(USER_STATUS_ENUM),
81+
default: USER_STATUS_ENUM.ACTIVE,
8282
},
8383
reason: String,
8484
date: {

0 commit comments

Comments
 (0)