Skip to content

Commit 0dafb7c

Browse files
committed
Add Insights API
1 parent b8f0113 commit 0dafb7c

24 files changed

+106
-72
lines changed

.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
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"
3-
PORT=8000
3+
PORT=8000

.env.travis

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
NODE_ENV = "travis"
22
DATABASE_URL="mongodb://127.0.0.1:27017"
33
JWT_SECRET = "somesupersecretkey"
4-
PORT=8080
4+
PORT=8080

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ install:
88
before_script:
99
- sleep 15
1010
script:
11-
- npm run travisTest
11+
- npm run travisTest

__tests__/category.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,4 +259,4 @@ test("should delete a category with admin/moderator/creator access", async () =>
259259
expect(categories.length).toBe(0);
260260
const topics = await Topic.find().lean();
261261
expect(topics.length).toBe(0);
262-
});
262+
});

__tests__/organization.spec.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ test("demote provided moderator to user by another admin using Id", async () =>
337337
expect(demotedUser.isModerator).toBe(false);
338338
});
339339

340-
test("get all admins and moderators", async () => {
340+
test("get all admins and moderators with admin authorization", async () => {
341341
const userLoginResponse = await testLoginUser(1);
342342
const token = userLoginResponse.body.data.login.token;
343343
const response = await request
@@ -369,3 +369,22 @@ test("get all admins and moderators", async () => {
369369
expect(response.body.data.getAdminModerators.moderators.length).toBe(0);
370370
expect(response.body.data.getAdminModerators.admins[0].isFirstAdmin).toBe(true);
371371
});
372+
373+
test("get more organization details with admin authorization", async () => {
374+
const userLoginResponse = await testLoginUser(1);
375+
const token = userLoginResponse.body.data.login.token;
376+
const response = await request
377+
.post("/graphql")
378+
.send({
379+
query: `{ getOrganizationData {
380+
categories
381+
topics
382+
}}`,
383+
})
384+
.set("Accept", "application/json")
385+
.set("Authorization", `Bearer ${token}`);
386+
expect(response.type).toBe("application/json");
387+
expect(response.status).toBe(200);
388+
expect(response.body.data.getOrganizationData.categories).toBe(0);
389+
expect(response.body.data.getOrganizationData.topics).toBe(0);
390+
});

__tests__/user.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ test("should signup new user", async () => {
6969
userId = response.body.data.createUser._id;
7070
});
7171

72-
test("get all users via admin authorization", async () => {
72+
test("get all users", async () => {
7373
firstUserLoginResponse = await testLoginUser(1);
7474
firstUserToken = firstUserLoginResponse.body.data.login.token;
7575
const response = await request
@@ -336,4 +336,4 @@ test("current user should be able to get tasks assigned to him", async () => {
336336
expect(response.body.data.getAssignedTasks.length).toBe(1);
337337
expect(response.body.data.getAssignedTasks[0].description).toBe("Lorem Ipsum");
338338
expect(response.body.data.getAssignedTasks[0].userId).toBe(userId);
339-
});
339+
});

config/testVariables.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module.exports = {
2323
.set("Accept", "application/json");
2424
return response;
2525
},
26+
2627
testCreateUser: async (userNo) => {
2728
const response = await request
2829
.post("/graphql")
@@ -64,6 +65,7 @@ module.exports = {
6465
.set("Accept", "application/json");
6566
return response;
6667
},
68+
6769
testLoginUser: async (userNo) => {
6870
const response = await request
6971
.post("/graphql")
@@ -95,6 +97,7 @@ module.exports = {
9597
.set("Accept", "application/json");
9698
return response;
9799
},
100+
98101
testCreateCategory: async (token) => {
99102
const response = await request
100103
.post("/graphql")
@@ -120,6 +123,7 @@ module.exports = {
120123
.set("Authorization", `Bearer ${token}`);
121124
return response;
122125
},
126+
123127
testCreateTopic: async (token, categoryId) => {
124128
const response = await request
125129
.post("/graphql")
@@ -152,6 +156,7 @@ module.exports = {
152156
.set("Authorization", `Bearer ${token}`);
153157
return response;
154158
},
159+
155160
testCreateMessage: async (token, topicId) => {
156161
const response = await request
157162
.post("/graphql")
@@ -171,6 +176,7 @@ module.exports = {
171176
.set("Authorization", `Bearer ${token}`);
172177
return response;
173178
},
179+
174180
testCreateTask: async (
175181
token,
176182
topicId,
@@ -273,4 +279,4 @@ module.exports = {
273279
}
274280
return response;
275281
},
276-
};
282+
};

example.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
NODE_ENV = "development"
22
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"
33
JWT_SECRET = "somesupersecretkey"
4-
PORT=8000
4+
PORT=8000

example.env.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
NODE_ENV = "testing"
22
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"
33
JWT_SECRET = "somesupersecretkey"
4-
PORT=8080
4+
PORT=8080

graphql/resolvers/organization.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
require("dotenv").config();
22
const User = require("../../models/user");
33
const Organization = require("../../models/organization");
4+
const Category = require("../../models/category");
5+
const Topic = require("../../models/topic");
6+
47
const {
58
organizationCreatedResult,
69
madeAdminResult,
@@ -292,7 +295,9 @@ module.exports = {
292295
options: { sort: { createdAt: -1 } },
293296
});
294297
let admins = organization.adminIds.filter(admin => !admin.isBlocked);
295-
let moderators = organization.moderatorIds.filter(moderator => !moderator.isAdmin && !moderator.isBlocked);
298+
let moderators = organization.moderatorIds.filter(
299+
moderator => !moderator.isAdmin && !moderator.isBlocked
300+
);
296301
return {
297302
admins,
298303
moderators,
@@ -305,4 +310,28 @@ module.exports = {
305310
throw err;
306311
}
307312
},
313+
314+
getOrganizationData: async (args, req) => {
315+
if (!req.isAuth) {
316+
throw new Error(authenticationError);
317+
}
318+
try {
319+
if (req.currentUser.isAdmin) {
320+
if (req.currentUser.isBlocked || req.currentUser.isRemoved) {
321+
throw new Error(noAuthorizationError);
322+
}
323+
const categories = await Category.estimatedDocumentCount();
324+
const topics = await Topic.estimatedDocumentCount();
325+
return {
326+
categories,
327+
topics,
328+
};
329+
} else {
330+
throw new Error(adminAccessError);
331+
}
332+
} catch (err) {
333+
console.log(err);
334+
throw err;
335+
}
336+
}
308337
};

0 commit comments

Comments
 (0)