Skip to content

Commit 12cb1d9

Browse files
authored
Merge pull request #26 from codeuino/membersSection
Members Timeline section - Admin Panel
2 parents c033f6b + b8f0113 commit 12cb1d9

File tree

7 files changed

+105
-72
lines changed

7 files changed

+105
-72
lines changed

.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
DATABASE_URL="mongodb://<username>:<password>@cluster0-shard-00-00.t4hho.mongodb.net:27017,cluster0-shard-00-01.t4hho.mongodb.net:27017,cluster0-shard-00-02.t4hho.mongodb.net:27017/<dbname>?ssl=true&replicaSet=atlas-a80apd-shard-0&authSource=admin&retryWrites=true&w=majority"
1+
DATABASE_URL="mongodb://MongoAdminUser:SecretKey%231@cluster0-shard-00-00.t4hho.mongodb.net:27017,cluster0-shard-00-01.t4hho.mongodb.net:27017,cluster0-shard-00-02.t4hho.mongodb.net:27017/SpansberryDb?ssl=true&replicaSet=atlas-a80apd-shard-0&authSource=admin&retryWrites=true&w=majority"
22
JWT_SECRET = "somesupersecretkey"
33
PORT=8000

__tests__/user.spec.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,27 @@ test("get all users via admin authorization", async () => {
7676
.post("/graphql")
7777
.send({
7878
query: `{ users {
79-
_id
80-
name {
81-
firstName
79+
users {
80+
_id
81+
name {
82+
firstName
83+
}
84+
email
85+
}
86+
blockedUsers {
87+
_id
88+
name {
89+
firstName
90+
}
91+
email
8292
}
83-
email
8493
}}`,
8594
})
8695
.set("Accept", "application/json")
8796
.set("Authorization", `Bearer ${firstUserToken}`);
8897
expect(response.type).toBe("application/json");
8998
expect(response.status).toBe(200);
90-
expect(response.body.data.users.length).toBe(1);
91-
expect(response.body.data.users[0].name.firstName).toBe("TestUser");
92-
expect(response.body.data.users[0].email).toBe("abc1@email.com");
99+
expect(response.body.data.users.users.length).toBe(0);
93100
});
94101

95102
test("should not update user details if logged out", async () => {

graphql/resolvers/organization.js

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,15 @@ module.exports = {
128128
if (!user) {
129129
throw new Error(noUserError);
130130
}
131+
const organization = await Organization.findOne({});
132+
if (user.isModerator) {
133+
organization.moderatorIds = organization.moderatorIds.filter(
134+
(moderatorId) => moderatorId.toString() != user.id
135+
);
136+
}
131137
user.isAdmin = true;
132138
user.isModerator = true;
133139
await user.save();
134-
const organization = await Organization.findOne({});
135140
organization.adminIds.push(user);
136141
await organization.save();
137142
return { result: madeAdminResult };
@@ -162,9 +167,15 @@ module.exports = {
162167
if (!user) {
163168
throw new Error(noUserError);
164169
}
170+
const organization = await Organization.findOne({});
171+
if (user.isAdmin) {
172+
organization.adminIds = organization.adminIds.filter(
173+
(adminId) => adminId.toString() != user.id
174+
);
175+
}
176+
user.isAdmin = false;
165177
user.isModerator = true;
166178
await user.save();
167-
const organization = await Organization.findOne({});
168179
organization.moderatorIds.push(user);
169180
await organization.save();
170181
return {
@@ -242,6 +253,7 @@ module.exports = {
242253
if (!user.isModerator) {
243254
throw new Error(noModeratorError);
244255
}
256+
user.isAdmin = false;
245257
user.isModerator = false;
246258
await user.save();
247259
const organization = await Organization.findOne({});
@@ -271,31 +283,19 @@ module.exports = {
271283
throw new Error(noAuthorizationError);
272284
}
273285
const organization = await Organization.findOne({})
274-
.populate("adminIds", [
275-
"_id",
276-
"name",
277-
"email",
278-
"info",
279-
"isAdmin",
280-
"isModerator",
281-
"isActivated",
282-
"isRemoved",
283-
"isFirstAdmin",
284-
])
285-
.populate("moderatorIds", [
286-
"_id",
287-
"name",
288-
"email",
289-
"info",
290-
"isAdmin",
291-
"isModerator",
292-
"isActivated",
293-
"isRemoved",
294-
"isFirstAdmin",
295-
]);
286+
.populate({
287+
path: "adminIds",
288+
options: { sort: { createdAt: -1 } },
289+
})
290+
.populate({
291+
path: "moderatorIds",
292+
options: { sort: { createdAt: -1 } },
293+
});
294+
let admins = organization.adminIds.filter(admin => !admin.isBlocked);
295+
let moderators = organization.moderatorIds.filter(moderator => !moderator.isAdmin && !moderator.isBlocked);
296296
return {
297-
admins: organization.adminIds,
298-
moderators: organization.moderatorIds,
297+
admins,
298+
moderators,
299299
};
300300
} else {
301301
throw new Error(adminAccessError);

graphql/resolvers/topic.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const Message = require("../../models/message");
55
const Tag = require("../../models/tag");
66
const {
77
authenticationError,
8+
categoryRemovedError,
89
topicRemovedError,
910
noAuthorizationError,
1011
categoryArchivedError,
@@ -36,10 +37,13 @@ module.exports = {
3637
throw new Error(noAuthorizationError);
3738
}
3839
try {
39-
let category = await Category.findById(
40+
const category = await Category.findById(
4041
args.topicInput.parentCategory
41-
).lean();
42-
if (category.isArchived == false) {
42+
);
43+
if (!category) {
44+
throw new Error(categoryRemovedError);
45+
}
46+
if (category.isArchived === false) {
4347
let topic = new Topic({
4448
name: args.topicInput.name,
4549
description: args.topicInput.description,
@@ -79,11 +83,8 @@ module.exports = {
7983
}
8084
}
8185
await topic.save();
82-
const saveCategory = await Category.findById(
83-
args.topicInput.parentCategory
84-
);
85-
saveCategory.topics.push(topic);
86-
await saveCategory.save();
86+
category.topics.push(topic);
87+
await category.save();
8788
const user = await User.findById(req.currentUser.id);
8889
user.topicsCreated.push(topic);
8990
await user.save();
@@ -109,6 +110,9 @@ module.exports = {
109110
}
110111
try {
111112
let topic = await Topic.findById(args.topicInput._id);
113+
if (!topic) {
114+
throw new Error(topicRemovedError);
115+
}
112116
if (
113117
topic.createdBy.toString() == req.currentUser.id ||
114118
req.currentUser.isModerator
@@ -205,6 +209,9 @@ module.exports = {
205209
}
206210
try {
207211
const topic = await Topic.findById(args.topicFindInput._id);
212+
if (!topic) {
213+
throw new Error(topicRemovedError);
214+
}
208215
if (
209216
topic.createdBy.toString() == req.currentUser.id ||
210217
req.currentUser.isModerator
@@ -228,6 +235,9 @@ module.exports = {
228235
);
229236
await user.save();
230237
const category = await Category.findById(topic.parentCategory);
238+
if (!category) {
239+
throw new Error(categoryRemovedError);
240+
}
231241
category.topics = category.topics.filter(
232242
(topicId) => topicId.toString() != args.topicFindInput._id
233243
);
@@ -250,6 +260,9 @@ module.exports = {
250260
}
251261
try {
252262
const topic = await Topic.findById(args.topicFindInput._id);
263+
if (!topic) {
264+
throw new Error(topicRemovedError);
265+
}
253266
if (
254267
topic.createdBy.toString() == req.currentUser.id ||
255268
req.currentUser.isModerator

graphql/resolvers/user.js

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,25 @@ module.exports = {
2525
throw new Error(authenticationError);
2626
}
2727
try {
28-
if (req.currentUser.isAdmin) {
29-
const users = await User.find(
30-
{ isRemoved: false },
31-
"name email info isFirstAdmin isAdmin isModerator isBlocked isRemoved"
32-
);
33-
return users;
34-
} else {
35-
throw new Error(adminAccessError);
28+
const users = await User.find(
29+
{
30+
isRemoved: false,
31+
},
32+
"name email info isBlocked isAdmin isModerator createdAt"
33+
).sort([["createdAt", -1]]);
34+
let blockedUsers = [];
35+
let normalUsers = [];
36+
for (const user of users) {
37+
if (user.isBlocked === true) {
38+
blockedUsers.push(user);
39+
} else if (user.isAdmin == false && user.isModerator == false) {
40+
normalUsers.push(user);
41+
}
3642
}
43+
return {
44+
users: normalUsers,
45+
blockedUsers,
46+
};
3747
} catch (err) {
3848
console.log(err);
3949
throw err;
@@ -45,20 +55,18 @@ module.exports = {
4555
throw new Error(emailPasswordError);
4656
}
4757
try {
58+
let user;
4859
let existingUser = await User.findOne({
4960
email: args.userInput.email,
5061
});
51-
let user, organization;
52-
const users = await User.find({}).lean();
53-
const organizations = await Organization.find({}).lean();
62+
const organization = await Organization.findOne({});
5463
if (existingUser) {
5564
if (existingUser.isBlocked) {
5665
throw new Error(userBlockedError);
5766
}
5867
if (existingUser.isRemoved) {
59-
organization = await Organization.findOne();
6068
existingUser.name = args.userInput.name;
61-
existingUser.password = args.userInput.password,
69+
existingUser.password = args.userInput.password;
6270
existingUser.phone = args.userInput.phone;
6371
existingUser.info = args.userInput.info;
6472
existingUser.info.about.designation = "";
@@ -73,11 +81,11 @@ module.exports = {
7381
);
7482
}
7583
} else {
84+
const users = await User.find({}).lean();
7685
if (users.length === 0) {
77-
if (organizations.length === 0) {
86+
if (!organization) {
7887
throw new Error(noOrganizationError);
7988
} else {
80-
organization = await Organization.findOne();
8189
user = new User({
8290
name: args.userInput.name,
8391
email: args.userInput.email,
@@ -91,7 +99,6 @@ module.exports = {
9199
organization.adminIds.push(user);
92100
}
93101
} else {
94-
organization = await Organization.findOne();
95102
user = new User({
96103
name: args.userInput.name,
97104
email: args.userInput.email,
@@ -100,7 +107,7 @@ module.exports = {
100107
info: args.userInput.info,
101108
});
102109
}
103-
const saveUser = await user.save();
110+
await user.save();
104111
}
105112
organization.totalUsers += 1;
106113
await organization.save();
@@ -124,6 +131,12 @@ module.exports = {
124131
throw new Error(noAuthorizationError);
125132
}
126133
let user = await User.findOne({ _id: req.currentUser.id });
134+
if (!user || user.isRemoved) {
135+
throw new Error(noUserError);
136+
}
137+
if (user.isBlocked) {
138+
throw new Error(userBlockedError);
139+
}
127140
user.name = args.userInput.name;
128141
user.phone = args.userInput.phone;
129142
user.info = args.userInput.info;
@@ -151,18 +164,15 @@ module.exports = {
151164
} else if (args.userFindInput._id) {
152165
user = await User.findById(args.userFindInput._id);
153166
}
154-
if (!user) {
167+
if (!user || user.isRemoved) {
155168
throw new Error(noUserError);
156169
}
157170
if (user.isFirstAdmin) {
158171
throw new Error(firstAdminBlockError);
159172
}
160-
if (user.isRemoved) {
161-
throw new Error(noUserError);
162-
}
163173
user.isBlocked = true;
164174
await user.save();
165-
const organization = await Organization.findOne();
175+
const organization = await Organization.findOne({});
166176
organization.blockedUsers.push(user);
167177
organization.totalUsers -= 1;
168178
await organization.save();
@@ -191,15 +201,12 @@ module.exports = {
191201
} else if (args.userFindInput._id) {
192202
user = await User.findById(args.userFindInput._id);
193203
}
194-
if (!user) {
204+
if (!user || user.isRemoved) {
195205
throw new Error(noUserError);
196206
}
197207
if (user.isFirstAdmin) {
198208
throw new Error(noAuthorizationError);
199209
}
200-
if (user.isRemoved) {
201-
throw new Error(noUserError);
202-
}
203210
user.isBlocked = false;
204211
await user.save();
205212
const organization = await Organization.findOne();
@@ -227,7 +234,7 @@ module.exports = {
227234
const organization = await Organization.findOne();
228235
if (!args.userFindInput.email && !args.userFindInput._id) {
229236
user = await User.findById(req.currentUser.id);
230-
if (!user) {
237+
if (!user || user.isRemoved) {
231238
throw new Error(noUserError);
232239
}
233240
if (user.isFirstAdmin) {
@@ -255,7 +262,7 @@ module.exports = {
255262
} else if (args.userFindInput._id) {
256263
user = await User.findById(args.userFindInput._id);
257264
}
258-
if (!user) {
265+
if (!user || user.isRemoved) {
259266
throw new Error(noUserError);
260267
}
261268
if (user.isFirstAdmin) {

graphql/schema/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ type resultData {
2323
}
2424
2525
type RootQuery {
26-
users: [user!]!
26+
users: usersData!
2727
login(email: String!, password: String!): authData!
2828
getCurrentUser(_id: String!, token: String!): authData!
29+
getUserProfile(userFindInput: userFindInput!): user!
2930
getSelfCategories: [category!]!
3031
getSelfTopics: [topic!]!
3132
getAssignedTasks: [task!]!
@@ -48,7 +49,6 @@ type RootMutation {
4849
blockUser(userFindInput: userFindInput!): resultData!
4950
unblockUser(userFindInput: userFindInput!): resultData!
5051
removeUser(userFindInput: userFindInput!): resultData!
51-
getUserProfile(userFindInput: userFindInput!): user!
5252
createOrganization(organizationInput: organizationInput!): resultData!
5353
updateOrganization(organizationInput: organizationInput!): organization!
5454
toggleMaintenanceMode: organization!

0 commit comments

Comments
 (0)