Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions __tests__/tag.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,16 @@ test("get all topics having a particular tag", async () => {
}
) {
_id
name
description
topics {
_id
name
description
}
}}`,
})
.set("Accept", "application/json");
expect(response.type).toBe("application/json");
expect(response.status).toBe(200);
expect(response.body.data.getTagTopics.length).toBe(1);
expect(response.body.data.getTagTopics[0].name).toBe("Test Topic");
expect(response.body.data.getTagTopics.topics.length).toBe(1);
expect(response.body.data.getTagTopics.topics[0].name).toBe("Test Topic");
});
4 changes: 3 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const createError = require("http-errors");
const express = require("express");
const path = require("path");
const isAuth = require("./middleware/is-auth");
const isAuth = require("./middleware/isAuth");
const isUnderMaintenance = require("./middleware/isUnderMaintenance");
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const morgan = require("morgan");
Expand All @@ -27,6 +28,7 @@ app.use(cookieParser());
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, "public")));
app.use(isAuth);
app.use(isUnderMaintenance);

io.sockets.on("connection", function (socket, client) {
console.log("client connected!");
Expand Down
34 changes: 30 additions & 4 deletions graphql/resolvers/organization.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,35 @@ module.exports = {
if (req.currentUser.isBlocked || req.currentUser.isRemoved) {
throw new Error(noAuthorizationError);
}
organization.name = args.organizationInput.name,
organization.description = args.organizationInput.description,
organization.contactInfo = args.organizationInput.contactInfo,
organization.name = args.organizationInput.name;
organization.description = args.organizationInput.description;
organization.contactInfo = args.organizationInput.contactInfo;
await organization.save();
organization = await Organization.findOne().lean();
return {
...organization,
exists: true,
};
} else {
throw new Error(adminAccessError);
}
} catch (err) {
console.log(err);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CO

throw err;
}
},

toggleMaintenanceMode: async (args, req) => {
if (!req.isAuth) {
throw new Error(authenticationError);
}
try {
let organization = await Organization.findOne({});
if (req.currentUser.isAdmin && organization) {
if (req.currentUser.isBlocked || req.currentUser.isRemoved) {
throw new Error(noAuthorizationError);
}
organization.isUnderMaintenance = !organization.isUnderMaintenance;
await organization.save();
organization = await Organization.findOne().lean();
return {
Expand Down Expand Up @@ -174,7 +200,7 @@ module.exports = {
if (user.isFirstAdmin) {
throw new Error(firstAdminDemoteError);
}
if(!user.isAdmin) {
if (!user.isAdmin) {
throw new Error(noAdminError);
}
user.isAdmin = false;
Expand Down
2 changes: 1 addition & 1 deletion graphql/resolvers/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = {
if (!tag) {
throw new Error(tagRemovedError);
}
return tag.topics;
return tag;
} catch (err) {
console.log(err);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

=> code observability

throw err;
Expand Down
3 changes: 2 additions & 1 deletion graphql/schema/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type RootQuery {
getTopicChats(topicFindInput: topicFindInput!): [message!]!
getTask(taskFindInput: taskFindInput!): task!
getTopicTasks(topicFindInput: topicFindInput!): [task!]!
getTagTopics(tagFindInput: tagFindInput!): [topic!]!
getTagTopics(tagFindInput: tagFindInput!): tag!
}

type RootMutation {
Expand All @@ -51,6 +51,7 @@ type RootMutation {
getUserProfile(userFindInput: userFindInput!): user!
createOrganization(organizationInput: organizationInput!): resultData!
updateOrganization(organizationInput: organizationInput!): organization!
toggleMaintenanceMode: organization!
makeAdmin(userFindInput: userFindInput!): resultData!
makeModerator(userFindInput: userFindInput!): resultData!
removeAdmin(userFindInput: userFindInput!): resultData!
Expand Down
File renamed without changes.
21 changes: 21 additions & 0 deletions middleware/isUnderMaintenance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const Organization = require("../models/organization");
const createError = require("http-errors");

module.exports = async (req, res, next) => {
const organization = await Organization.findOne({}).lean();
if (organization && organization.isUnderMaintenance) {
if (req.headers["access-level"] == -1 || (req.currentUser && req.currentUser.isAdmin)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it is no longer recommended or is a necessity to prefix custom headers with X-; this name will be good to go

return next();
} else {
return res.status(500).json({
errors: [
{
message: "We are currently under maintenance. Please refresh to continue.",
},
],
data: null,
});
}
}
next();
}