|
1 | 1 | import { GraphQLError } from 'graphql' |
2 | 2 | import * as jwt from 'jsonwebtoken' |
3 | | -import { Types } from 'mongoose' |
| 3 | +import mongoose, { Types } from 'mongoose' |
4 | 4 | import { checkLoggedInOrganization } from '../helpers/organization.helper' |
5 | 5 | import { checkUserLoggedIn } from '../helpers/user.helpers' |
6 | 6 | import { Attendance } from '../models/attendance.model' |
@@ -44,8 +44,8 @@ interface Team extends Document { |
44 | 44 | } |
45 | 45 | const team = { |
46 | 46 | cohort: { name: 'Your Cohort Name' }, |
47 | | - name: 'Team Name' |
48 | | -}; |
| 47 | + name: 'Team Name', |
| 48 | +} |
49 | 49 |
|
50 | 50 | const manageStudentResolvers = { |
51 | 51 | Query: { |
@@ -455,7 +455,7 @@ const manageStudentResolvers = { |
455 | 455 | org!.name, |
456 | 456 | `${process.env.FRONTEND_LINK}/login/org`, |
457 | 457 | team.cohort.name, |
458 | | - team.name |
| 458 | + team.name |
459 | 459 | ) |
460 | 460 | await sendEmailOnMembershipActions( |
461 | 461 | role, |
@@ -507,7 +507,6 @@ const manageStudentResolvers = { |
507 | 507 |
|
508 | 508 | // CATCH ERROR |
509 | 509 | } catch (error: any) { |
510 | | - console.log(error) |
511 | 510 | throw new GraphQLError(error.message, { |
512 | 511 | extensions: { |
513 | 512 | code: '500', |
@@ -607,6 +606,139 @@ const manageStudentResolvers = { |
607 | 606 | } |
608 | 607 | }, |
609 | 608 |
|
| 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 | + |
610 | 742 | async removeMemberFromCohort( |
611 | 743 | _: any, |
612 | 744 | { teamName, email, orgToken }: any, |
@@ -676,23 +808,23 @@ const manageStudentResolvers = { |
676 | 808 | const Attendances: any[] = await Attendance.find({ |
677 | 809 | coordinatorId: userId, |
678 | 810 | }) |
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 | + ) |
683 | 815 |
|
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 | + } |
690 | 823 | } |
691 | 824 | } |
692 | 825 | } |
693 | 826 | } |
694 | 827 | } |
695 | | - } |
696 | 828 |
|
697 | 829 | checkMember.team.members = checkMember.team?.members.filter( |
698 | 830 | (member: any) => { |
@@ -914,7 +1046,7 @@ async function sendEmailOnMembershipActions( |
914 | 1046 | org!.name, |
915 | 1047 | `${process.env.FRONTEND_LINK}/login/org`, |
916 | 1048 | team.cohort.name, |
917 | | - team.name |
| 1049 | + team.name |
918 | 1050 | ) |
919 | 1051 | const link: any = process.env.FRONTEND_LINK + '/login/org' |
920 | 1052 | await sendEmail( |
@@ -964,4 +1096,3 @@ export default manageStudentResolvers |
964 | 1096 | function inviteUserTemplate(arg0: string, link: string, arg2: string) { |
965 | 1097 | throw new Error('Function not implemented.') |
966 | 1098 | } |
967 | | - |
|
0 commit comments