Skip to content

Commit fc1c260

Browse files
committed
Frontend requirements + Maintenance mode
1 parent 2795daa commit fc1c260

File tree

7 files changed

+64
-11
lines changed

7 files changed

+64
-11
lines changed

__tests__/tag.spec.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,16 @@ test("get all topics having a particular tag", async () => {
6868
}
6969
) {
7070
_id
71-
name
72-
description
71+
topics {
72+
_id
73+
name
74+
description
75+
}
7376
}}`,
7477
})
7578
.set("Accept", "application/json");
7679
expect(response.type).toBe("application/json");
7780
expect(response.status).toBe(200);
78-
expect(response.body.data.getTagTopics.length).toBe(1);
79-
expect(response.body.data.getTagTopics[0].name).toBe("Test Topic");
81+
expect(response.body.data.getTagTopics.topics.length).toBe(1);
82+
expect(response.body.data.getTagTopics.topics[0].name).toBe("Test Topic");
8083
});

app.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const createError = require("http-errors");
22
const express = require("express");
33
const path = require("path");
4-
const isAuth = require("./middleware/is-auth");
4+
const isAuth = require("./middleware/isAuth");
5+
const isUnderMaintenance = require("./middleware/isUnderMaintenance");
56
const bodyParser = require("body-parser");
67
const cookieParser = require("cookie-parser");
78
const morgan = require("morgan");
@@ -27,6 +28,7 @@ app.use(cookieParser());
2728
app.use(bodyParser.json());
2829
app.use(express.static(path.join(__dirname, "public")));
2930
app.use(isAuth);
31+
app.use(isUnderMaintenance);
3032

3133
io.sockets.on("connection", function (socket, client) {
3234
console.log("client connected!");

graphql/resolvers/organization.js

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,35 @@ module.exports = {
6666
if (req.currentUser.isBlocked || req.currentUser.isRemoved) {
6767
throw new Error(noAuthorizationError);
6868
}
69-
organization.name = args.organizationInput.name,
70-
organization.description = args.organizationInput.description,
71-
organization.contactInfo = args.organizationInput.contactInfo,
69+
organization.name = args.organizationInput.name;
70+
organization.description = args.organizationInput.description;
71+
organization.contactInfo = args.organizationInput.contactInfo;
72+
await organization.save();
73+
organization = await Organization.findOne().lean();
74+
return {
75+
...organization,
76+
exists: true,
77+
};
78+
} else {
79+
throw new Error(adminAccessError);
80+
}
81+
} catch (err) {
82+
console.log(err);
83+
throw err;
84+
}
85+
},
86+
87+
toggleMaintenanceMode: async (args, req) => {
88+
if (!req.isAuth) {
89+
throw new Error(authenticationError);
90+
}
91+
try {
92+
let organization = await Organization.findOne({});
93+
if (req.currentUser.isAdmin && organization) {
94+
if (req.currentUser.isBlocked || req.currentUser.isRemoved) {
95+
throw new Error(noAuthorizationError);
96+
}
97+
organization.isUnderMaintenance = !organization.isUnderMaintenance;
7298
await organization.save();
7399
organization = await Organization.findOne().lean();
74100
return {
@@ -174,7 +200,7 @@ module.exports = {
174200
if (user.isFirstAdmin) {
175201
throw new Error(firstAdminDemoteError);
176202
}
177-
if(!user.isAdmin) {
203+
if (!user.isAdmin) {
178204
throw new Error(noAdminError);
179205
}
180206
user.isAdmin = false;

graphql/resolvers/tag.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = {
1313
if (!tag) {
1414
throw new Error(tagRemovedError);
1515
}
16-
return tag.topics;
16+
return tag;
1717
} catch (err) {
1818
console.log(err);
1919
throw err;

graphql/schema/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type RootQuery {
3939
getTopicChats(topicFindInput: topicFindInput!): [message!]!
4040
getTask(taskFindInput: taskFindInput!): task!
4141
getTopicTasks(topicFindInput: topicFindInput!): [task!]!
42-
getTagTopics(tagFindInput: tagFindInput!): [topic!]!
42+
getTagTopics(tagFindInput: tagFindInput!): tag!
4343
}
4444
4545
type RootMutation {
@@ -51,6 +51,7 @@ type RootMutation {
5151
getUserProfile(userFindInput: userFindInput!): user!
5252
createOrganization(organizationInput: organizationInput!): resultData!
5353
updateOrganization(organizationInput: organizationInput!): organization!
54+
toggleMaintenanceMode: organization!
5455
makeAdmin(userFindInput: userFindInput!): resultData!
5556
makeModerator(userFindInput: userFindInput!): resultData!
5657
removeAdmin(userFindInput: userFindInput!): resultData!
File renamed without changes.

middleware/isUnderMaintenance.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const Organization = require("../models/organization");
2+
const createError = require("http-errors");
3+
4+
module.exports = async (req, res, next) => {
5+
const organization = await Organization.findOne({}).lean();
6+
if (organization && organization.isUnderMaintenance) {
7+
if (req.headers["access-level"] == -1 || (req.currentUser && req.currentUser.isAdmin)) {
8+
return next();
9+
} else {
10+
return res.status(500).json({
11+
errors: [
12+
{
13+
message: "We are currently under maintenance. Please refresh to continue.",
14+
},
15+
],
16+
data: null,
17+
});
18+
}
19+
}
20+
next();
21+
}

0 commit comments

Comments
 (0)