|
| 1 | +require("dotenv").config(); |
| 2 | +const User = require("../../models/user"); |
| 3 | +const Organization = require("../../models/organization"); |
| 4 | +const { |
| 5 | + organizationCreatedResult, |
| 6 | + madeAdminResult, |
| 7 | + madeModeratorResult, |
| 8 | + removeAdminResult, |
| 9 | + removeModeratorResult } = require("./resultMessages"); |
| 10 | +const { authenticationError, |
| 11 | + adminAccessError, |
| 12 | + noUserError, |
| 13 | + organizationExistError, |
| 14 | + firstAdminDemoteError, |
| 15 | + noAdminError, |
| 16 | + noModeratorError } = require("./errorMessages"); |
| 17 | + |
| 18 | +module.exports = { |
| 19 | + createOrganization: async (args) => { |
| 20 | + try { |
| 21 | + const organizations = await Organization.find({}).lean(); |
| 22 | + if (organizations.length === 0) { |
| 23 | + const organization = new Organization({ |
| 24 | + name: args.organizationInput.name, |
| 25 | + description: args.organizationInput.description, |
| 26 | + contactInfo: args.organizationInput.contactInfo, |
| 27 | + }); |
| 28 | + await organization.save(); |
| 29 | + return { result: organizationCreatedResult }; |
| 30 | + } else { |
| 31 | + throw new Error(organizationExistError); |
| 32 | + } |
| 33 | + } catch (err) { |
| 34 | + console.log(err); |
| 35 | + throw err; |
| 36 | + } |
| 37 | + }, |
| 38 | + |
| 39 | + getOrganization: async () => { |
| 40 | + try { |
| 41 | + const organization = await Organization.findOne().lean(); |
| 42 | + return organization; |
| 43 | + } catch (err) { |
| 44 | + console.log(err); |
| 45 | + throw err; |
| 46 | + } |
| 47 | + }, |
| 48 | + |
| 49 | + updateOrganization: async (req, args) => { |
| 50 | + if (!req.isAuth) { |
| 51 | + throw new Error(authenticationError); |
| 52 | + } |
| 53 | + try { |
| 54 | + const organizations = await Organization.find({}).lean(); |
| 55 | + if (req.currentUser.isAdmin && organizations.length !== 0) { |
| 56 | + const organization = await Organization.updateOne( |
| 57 | + {}, |
| 58 | + { |
| 59 | + $set: { |
| 60 | + name: args.organizationInput.name, |
| 61 | + description: args.organizationInput.description, |
| 62 | + contactInfo: args.organizationInput.contactInfo, |
| 63 | + }, |
| 64 | + } |
| 65 | + ); |
| 66 | + return { ...organization._doc }; |
| 67 | + } else { |
| 68 | + throw new Error(adminAccessError); |
| 69 | + } |
| 70 | + } catch (err) { |
| 71 | + console.log(err); |
| 72 | + throw err; |
| 73 | + } |
| 74 | + }, |
| 75 | + |
| 76 | + makeAdmin: async (req, args) => { |
| 77 | + if (!req.isAuth) { |
| 78 | + throw new Error(authenticationError); |
| 79 | + } |
| 80 | + try { |
| 81 | + if (req.currentUser.isAdmin) { |
| 82 | + let user; |
| 83 | + if (args.userFindInput.email) { |
| 84 | + user = await User.findOne({ email: args.userFindInput.email }); |
| 85 | + } else if (args.userFindInput._id) { |
| 86 | + user = await User.findById(args.userFindInput._id); |
| 87 | + } |
| 88 | + if (!user) { |
| 89 | + throw new Error(noUserError); |
| 90 | + } |
| 91 | + user.isAdmin = true; |
| 92 | + await user.save(); |
| 93 | + const organization = await Organization.findOne({}); |
| 94 | + organization.adminInfo.adminIds.push(user); |
| 95 | + await organization.save(); |
| 96 | + return { result: madeAdminResult }; |
| 97 | + } else { |
| 98 | + throw new Error(adminAccessError); |
| 99 | + } |
| 100 | + } catch (err) { |
| 101 | + console.log(err); |
| 102 | + throw err; |
| 103 | + } |
| 104 | + }, |
| 105 | + |
| 106 | + makeModerator: async (req, args) => { |
| 107 | + if (!req.isAuth) { |
| 108 | + throw new Error(authenticationError); |
| 109 | + } |
| 110 | + try { |
| 111 | + if (req.currentUser.isAdmin) { |
| 112 | + let user; |
| 113 | + if (args.userFindInput.email) { |
| 114 | + user = await User.findOne({ email: args.userFindInput.email }); |
| 115 | + } else if (args.userFindInput._id) { |
| 116 | + user = await User.findById(args.userFindInput._id); |
| 117 | + } |
| 118 | + if (!user) { |
| 119 | + throw new Error(noUserError); |
| 120 | + } |
| 121 | + user.isModerator = true; |
| 122 | + await user.save(); |
| 123 | + const organization = await Organization.findOne({}); |
| 124 | + organization.moderatorInfo.moderatorIds.push(user); |
| 125 | + await organization.save(); |
| 126 | + return { |
| 127 | + result: madeModeratorResult, |
| 128 | + }; |
| 129 | + } else { |
| 130 | + throw new Error(adminAccessError); |
| 131 | + } |
| 132 | + } catch (err) { |
| 133 | + console.log(err); |
| 134 | + throw err; |
| 135 | + } |
| 136 | + }, |
| 137 | + |
| 138 | + removeAdmin: async (req, args) => { |
| 139 | + if (!req.isAuth) { |
| 140 | + throw new Error(authenticationError); |
| 141 | + } |
| 142 | + console.log(args.userFindInput); |
| 143 | + try { |
| 144 | + if (req.currentUser.isAdmin) { |
| 145 | + let user; |
| 146 | + if (args.userFindInput.email) { |
| 147 | + user = await User.findOne({ email: args.userFindInput.email }); |
| 148 | + } else if (args.userFindInput._id) { |
| 149 | + user = await User.findById(args.userFindInput._id); |
| 150 | + } |
| 151 | + if (!user) { |
| 152 | + throw new Error(noUserError); |
| 153 | + } |
| 154 | + if (user.isFirstAdmin) { |
| 155 | + throw new Error(firstAdminDemoteError); |
| 156 | + } |
| 157 | + if(!user.isAdmin) { |
| 158 | + throw new Error(noAdminError); |
| 159 | + } |
| 160 | + user.isAdmin = false; |
| 161 | + user.isModerator = false; |
| 162 | + await user.save(); |
| 163 | + const organization = await Organization.findOne({}); |
| 164 | + console.log(organization.adminInfo.adminIds); |
| 165 | + organization.adminInfo.adminIds = organization.adminInfo.adminIds.filter( |
| 166 | + (adminId) => adminId.toString() !== user.id |
| 167 | + ); |
| 168 | + await organization.save(); |
| 169 | + return { result: removeAdminResult }; |
| 170 | + } else { |
| 171 | + throw new Error(adminAccessError); |
| 172 | + } |
| 173 | + } catch (err) { |
| 174 | + console.log(err); |
| 175 | + throw err; |
| 176 | + } |
| 177 | + }, |
| 178 | + |
| 179 | + removeModerator: async (req, args) => { |
| 180 | + if (!req.isAuth) { |
| 181 | + throw new Error(authenticationError); |
| 182 | + } |
| 183 | + try { |
| 184 | + if (req.currentUser.isAdmin) { |
| 185 | + let user; |
| 186 | + if (args.email) { |
| 187 | + user = await User.findOne({ email: args.userFindInput.email }); |
| 188 | + } else if (args.userFindInput._id) { |
| 189 | + user = await User.findById(args.userFindInput._id); |
| 190 | + } |
| 191 | + if (!user) { |
| 192 | + throw new Error(noUserError); |
| 193 | + } |
| 194 | + if (!user.isModerator) { |
| 195 | + throw new Error(noModeratorError); |
| 196 | + } |
| 197 | + user.isModerator = false; |
| 198 | + await user.save(); |
| 199 | + const organization = await Organization.findOne({}); |
| 200 | + organization.moderatorInfo.moderatorIds = organization.moderatorInfo.moderatorIds.filter( |
| 201 | + (moderatorId) => moderatorId.toString() !== user.id |
| 202 | + ); |
| 203 | + await organization.save(); |
| 204 | + return { |
| 205 | + result: removeModeratorResult, |
| 206 | + }; |
| 207 | + } else { |
| 208 | + throw new Error(adminAccessError); |
| 209 | + } |
| 210 | + } catch (err) { |
| 211 | + console.log(err); |
| 212 | + throw err; |
| 213 | + } |
| 214 | + }, |
| 215 | + |
| 216 | + getAdminModerators: async (req) => { |
| 217 | + if (!req.isAuth) { |
| 218 | + throw new Error(authenticationError); |
| 219 | + } |
| 220 | + try { |
| 221 | + if (req.currentUser.isAdmin) { |
| 222 | + const organization = await Organization.findOne({}).populate(); |
| 223 | + return { |
| 224 | + admins: organization.adminInfo.adminIds, |
| 225 | + moderators: organization.moderatorInfo.moderatorIds, |
| 226 | + }; |
| 227 | + } else { |
| 228 | + throw new Error(adminAccessError); |
| 229 | + } |
| 230 | + } catch (err) { |
| 231 | + console.log(err); |
| 232 | + throw err; |
| 233 | + } |
| 234 | + }, |
| 235 | +}; |
0 commit comments