Skip to content

Commit 6fd2a27

Browse files
authored
fix-coordinator-table (#432)
1 parent 27f9fb6 commit 6fd2a27

File tree

8 files changed

+262
-124
lines changed

8 files changed

+262
-124
lines changed

src/models/cohort.model.ts

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

55
export interface CohortInterface {
6-
_id: mongoose.Types.ObjectId;
7-
name: string;
8-
phase: PhaseInterface;
9-
coordinator: mongoose.Types.ObjectId;
10-
members: mongoose.Types.ObjectId[];
11-
program: mongoose.Types.ObjectId;
12-
teams: number;
13-
active: boolean;
14-
startDate: Date;
15-
endDate?: Date; // Optional
16-
organization: mongoose.Types.ObjectId;
6+
_id: mongoose.Types.ObjectId
7+
name: string
8+
phase: PhaseInterface
9+
coordinator: mongoose.Types.ObjectId | null
10+
members: mongoose.Types.ObjectId[]
11+
program: mongoose.Types.ObjectId
12+
teams: number
13+
active: boolean
14+
startDate: Date
15+
endDate?: Date // Optional
16+
organization: mongoose.Types.ObjectId
1717
}
1818

1919
const cohortSchema = new Schema(
@@ -29,7 +29,7 @@ const cohortSchema = new Schema(
2929
ref: 'Phase',
3030
},
3131
coordinator: {
32-
type: mongoose.Types.ObjectId,
32+
type: mongoose.Schema.Types.ObjectId,
3333
ref: 'User',
3434
required: true,
3535
},
@@ -91,4 +91,4 @@ cohortSchema.pre('findOneAndRemove', findActive)
9191
cohortSchema.pre('findOneAndUpdate', findActive)
9292

9393
const Cohort = mongoose.model('Cohort', cohortSchema)
94-
export default Cohort
94+
export default Cohort

src/resolvers/2fa.resolvers.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ const resolvers = {
3838

3939
await user.save()
4040

41-
4241
await sendEmail(
4342
email,
4443
' Two-Factor Authentication enabled ',
@@ -76,7 +75,6 @@ const resolvers = {
7675
}
7776
},
7877

79-
8078
loginWithTwoFactorAuthentication: async (
8179
_: any,
8280
{ id, email, otp, TwoWayVerificationToken }: { id?: string; email?: string; otp: string; TwoWayVerificationToken: string }, context: any
@@ -125,7 +123,6 @@ const resolvers = {
125123
},
126124

127125
},
128-
};
129-
126+
}
130127

131-
export default resolvers
128+
export default resolvers

src/resolvers/cohort.resolvers.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ const resolvers = {
108108
coordinator: user._id,
109109
organization: org._id,
110110
}).populate(['coordinator', 'phase', 'program'])
111-
112111
return coordinatorCohorts
113112
}
114113
case RoleOfUser.TTL:

src/resolvers/coordinatorResolvers.ts

Lines changed: 149 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { GraphQLError } from 'graphql'
22
import * as jwt from 'jsonwebtoken'
3-
import { Types } from 'mongoose'
3+
import mongoose, { Types } from 'mongoose'
44
import { checkLoggedInOrganization } from '../helpers/organization.helper'
55
import { checkUserLoggedIn } from '../helpers/user.helpers'
66
import { Attendance } from '../models/attendance.model'
@@ -44,8 +44,8 @@ interface Team extends Document {
4444
}
4545
const team = {
4646
cohort: { name: 'Your Cohort Name' },
47-
name: 'Team Name'
48-
};
47+
name: 'Team Name',
48+
}
4949

5050
const manageStudentResolvers = {
5151
Query: {
@@ -455,7 +455,7 @@ const manageStudentResolvers = {
455455
org!.name,
456456
`${process.env.FRONTEND_LINK}/login/org`,
457457
team.cohort.name,
458-
team.name
458+
team.name
459459
)
460460
await sendEmailOnMembershipActions(
461461
role,
@@ -507,7 +507,6 @@ const manageStudentResolvers = {
507507

508508
// CATCH ERROR
509509
} catch (error: any) {
510-
console.log(error)
511510
throw new GraphQLError(error.message, {
512511
extensions: {
513512
code: '500',
@@ -607,6 +606,139 @@ const manageStudentResolvers = {
607606
}
608607
},
609608

609+
async giveCoordinatorCohort(
610+
_: any,
611+
{ coordinatorId, cohortId }: { coordinatorId: string; cohortId: string },
612+
context: Context
613+
) {
614+
;(await checkUserLoggedIn(context))([RoleOfUser.ADMIN])
615+
616+
const user = await User.findOne({
617+
_id: coordinatorId,
618+
role: 'coordinator',
619+
}).exec()
620+
621+
let cohortGiven
622+
let cohortFound = false
623+
624+
if (!user) {
625+
throw new Error('Coordinator not found')
626+
}
627+
628+
const allCohorts = await Cohort.find()
629+
630+
allCohorts.forEach((cohort) => {
631+
if (
632+
cohort.coordinator &&
633+
cohort.coordinator.toString() === coordinatorId
634+
) {
635+
cohort.set('coordinator', null)
636+
}
637+
638+
const cohortIdStr = cohort._id?.toString()
639+
640+
if (cohortIdStr === cohortId) {
641+
cohortFound = true
642+
cohort.coordinator = new mongoose.Types.ObjectId(coordinatorId)
643+
cohortGiven = cohort.name
644+
}
645+
})
646+
647+
if (!cohortFound) {
648+
throw new Error('Cohort not found')
649+
}
650+
651+
for (const cohort of allCohorts) {
652+
await cohort.save({ validateBeforeSave: false })
653+
}
654+
655+
await sendEmail(
656+
user.email,
657+
'Given new Cohort',
658+
`Hello, Congaturations!! You have been assigned a new cohort to coordinate, Cohort name: ${cohortGiven}`,
659+
'Best Regards.',
660+
process.env.ADMIN_EMAIL,
661+
process.env.ADMIN_PASS
662+
)
663+
664+
return `Coordinator with email ${user.email} has been given cohort: ${cohortGiven}`
665+
},
666+
667+
async dropCordinator(
668+
_: any,
669+
{ id, reason }: { id: string; reason: string },
670+
context: Context
671+
) {
672+
;(await checkUserLoggedIn(context))([RoleOfUser.ADMIN])
673+
674+
const user = await User.findOne({ _id: id, role: 'coordinator' }).exec()
675+
if (!user) {
676+
throw new Error('Coordinator not found')
677+
}
678+
679+
if (user.status?.status !== undefined) {
680+
user.status.status = 'drop'
681+
} else {
682+
throw new Error('User status property does not exist')
683+
}
684+
685+
const cohorts = await Cohort.find()
686+
const modifiedCohorts = cohorts.filter((cohort) => {
687+
if (
688+
cohort.coordinator &&
689+
(cohort.coordinator as mongoose.Types.ObjectId).toString() === id
690+
) {
691+
cohort.set('coordinator', null)
692+
return true
693+
}
694+
return false
695+
})
696+
for (const cohort of modifiedCohorts) {
697+
await cohort.save({ validateBeforeSave: false })
698+
}
699+
700+
await user.save()
701+
702+
await sendEmail(
703+
user.email,
704+
'Dropped',
705+
`${reason}. If you think this is a mistake reach out to us!`,
706+
'',
707+
process.env.ADMIN_EMAIL,
708+
process.env.ADMIN_PASS
709+
)
710+
711+
return `Coordinator with email ${user.email} has been dropped. Reason: ${reason}`
712+
},
713+
714+
async undropCordinator(_: any, { id }: { id: string }, context: Context) {
715+
;(await checkUserLoggedIn(context))([RoleOfUser.ADMIN])
716+
717+
const user = await User.findOne({ _id: id, role: 'coordinator' }).exec()
718+
if (!user) {
719+
throw new Error('Coordinator not found')
720+
}
721+
722+
if (user.status?.status !== undefined) {
723+
user.status.status = 'active'
724+
} else {
725+
throw new Error('User status property does not exist')
726+
}
727+
728+
await user.save()
729+
730+
await sendEmail(
731+
user.email,
732+
'Undropped',
733+
'Welcome back To our Organisation',
734+
'',
735+
process.env.ADMIN_EMAIL,
736+
process.env.ADMIN_PASS
737+
)
738+
739+
return `Coordinator with email ${user.email} has been undropped.`
740+
},
741+
610742
async removeMemberFromCohort(
611743
_: any,
612744
{ teamName, email, orgToken }: any,
@@ -676,23 +808,23 @@ const manageStudentResolvers = {
676808
const Attendances: any[] = await Attendance.find({
677809
coordinatorId: userId,
678810
})
679-
if(traineeAttendance?.trainees){
680-
const traineeExist = traineeAttendance.trainees.findIndex(
681-
(data: any) => data.traineeEmail === email
682-
)
811+
if (traineeAttendance?.trainees) {
812+
const traineeExist = traineeAttendance.trainees.findIndex(
813+
(data: any) => data.traineeEmail === email
814+
)
683815

684-
if (traineeExist !== -1) {
685-
for (const attendance of Attendances) {
686-
for (let i = 0; i < attendance.trainees.length; i++) {
687-
if (attendance.trainees[i].traineeEmail === email) {
688-
attendance.trainees.splice(i, 1)
689-
await attendance.save()
816+
if (traineeExist !== -1) {
817+
for (const attendance of Attendances) {
818+
for (let i = 0; i < attendance.trainees.length; i++) {
819+
if (attendance.trainees[i].traineeEmail === email) {
820+
attendance.trainees.splice(i, 1)
821+
await attendance.save()
822+
}
690823
}
691824
}
692825
}
693826
}
694827
}
695-
}
696828

697829
checkMember.team.members = checkMember.team?.members.filter(
698830
(member: any) => {
@@ -914,7 +1046,7 @@ async function sendEmailOnMembershipActions(
9141046
org!.name,
9151047
`${process.env.FRONTEND_LINK}/login/org`,
9161048
team.cohort.name,
917-
team.name
1049+
team.name
9181050
)
9191051
const link: any = process.env.FRONTEND_LINK + '/login/org'
9201052
await sendEmail(
@@ -964,4 +1096,3 @@ export default manageStudentResolvers
9641096
function inviteUserTemplate(arg0: string, link: string, arg2: string) {
9651097
throw new Error('Function not implemented.')
9661098
}
967-

src/resolvers/team.resolvers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ const resolvers = {
293293
},
294294
})
295295
}
296+
296297
const org = await checkLoggedInOrganization(orgToken)
297298
if (!org) {
298299
throw new GraphQLError('No organization found', {
@@ -311,6 +312,7 @@ const resolvers = {
311312
}
312313
)
313314
}
315+
314316
const cohort = await Cohort.findById(cohortId)
315317
if (!cohort) {
316318
throw new GraphQLError('No such cohort found', {

0 commit comments

Comments
 (0)