Skip to content

Commit

Permalink
Merge 1d9da66 into ac4be16
Browse files Browse the repository at this point in the history
  • Loading branch information
phembarl committed Jul 18, 2019
2 parents ac4be16 + 1d9da66 commit 64a285a
Show file tree
Hide file tree
Showing 21 changed files with 470 additions and 275 deletions.
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"build": "babel src -d lib",
"pretest": "npm run undo:migrate && npm run migrate && npm run seed",
"test": "NODE_ENV=test mocha --timeout 50000 --require @babel/register --exit",
"test": "NODE_ENV=test nyc mocha --timeout 50000 --require @babel/register --exit",
"generate-lcov": "nyc report --reporter=text-lcov > lcov.info",
"coveralls-coverage": "coveralls < lcov.info",
"coverage": "nyc npm test && npm run generate-lcov && npm run coveralls-coverage",
Expand All @@ -18,6 +18,11 @@
"migrate": "sequelize db:migrate",
"undo:migrate": "sequelize db:migrate:undo:all",
"seed": "sequelize db:seed:all"
},"nyc": {
"exclude": [
"src/helpers/mail/*.js",
"src/server.js"
]
},
"author": "Andela Simulations Programme",
"license": "MIT",
Expand Down Expand Up @@ -46,6 +51,7 @@
"method-override": "^2.3.10",
"methods": "^1.1.2",
"mocha": "^6.1.4",
"moment": "^2.24.0",
"mongoose": "^5.2.2",
"mongoose-unique-validator": "^2.0.1",
"multer": "^1.4.1",
Expand Down
2 changes: 2 additions & 0 deletions src/controllers/Auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,15 @@ class AuthController {
const url = `${req.protocol}://${req.get('host')}/api/v1/auth/resetPassword?resetToken=${token}&email=${user.email}`;
const html = resetTemplate(user.userName, url);

if (!isTest) {
await sendEmail
.sendEmail(
'do_not_reply@authorhaven.com',
user.email,
'Password Reset',
html
);
}

// RETURN SUCCESS IF SUCCESSFUL
return res.status(200).json({
Expand Down
89 changes: 88 additions & 1 deletion src/controllers/CommentController.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Comment } from '../db/models';
import { Comment, CommentHistory } from '../db/models';
/**
* @description This controller handles comment request
* @class CommentController
Expand Down Expand Up @@ -50,6 +50,14 @@ class CommentController {
}
}

/**
* @static
* @description The get all comment method
* @param {object} req The req object
* @param {object} res The res object
* @returns {object} json res
* @memberof CommentController
*/
static async getAllArticleComments(req, res) {
const { article } = res.locals;
const articleId = article.id;
Expand Down Expand Up @@ -85,6 +93,85 @@ class CommentController {
});
}
}

/**
*@description This function gets a specific comment
* @static
* @param {object} req
* @param {object} res
* @returns {object} res
* @memberof CommentController
*/
static async getSingleComment(req, res) {
try {
const { comment } = res.locals;
return res.status(200).json({
status: 200,
comment,
});
} catch (error) {
return res.status(500).json({
status: 500,
error: error.message,
});
}
}

/**
* @description This function edits and updates a comment
* @static
* @param {object} req
* @param {object} res
* @returns {object} res
* @memberof CommentController
*/
static async updateComment(req, res) {
try {
const { comment } = res.locals;
const { commentBody } = req.body;
const userId = req.user;

await CommentHistory.create({ userId, commentId: comment.id, commentBody: comment.commentBody });

await comment.update({commentBody},
{ where: {
id: comment.id,
}
});
return res.status(200).json({
status: 200,
comment,
message: 'Comment updated successfully',
});
} catch (error) {
return res.status(500).json({
status: 500,
error: error.message,
});
}
}

/**
* @description This function gets the edit history of a comment
* @static
* @param {object} req
* @param {object} res
* @returns {object} res
* @memberof CommentController
*/
static async getCommentHistory(req, res) {
const { id } = req.params;

const comments = await CommentHistory.findAll({
where: { commentId: id },
attributes: {exclude: ['createdAt']},
});

return res.status(200).json({
status: 200,
comments,
});
}
}

export default CommentController;
75 changes: 38 additions & 37 deletions src/db/migrations/20190716060554-create-report.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,39 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Reports', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
body: {
type: Sequelize.TEXT,
required: true,
allowNull: false
},
reportType: {
type: Sequelize.ENUM(['article', 'comment']),
required: true,
allowNull: false
},
reportTypeId: {
type: Sequelize.INTEGER,
required: true,
allowNull: false
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Reports');
}
};
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Reports', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
body: {
type: Sequelize.TEXT,
required: true,
allowNull: false
},
reportType: {
type: Sequelize.ENUM(['article', 'comment']),
required: true,
allowNull: false
},
reportTypeId: {
type: Sequelize.INTEGER,
required: true,
allowNull: false
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Reports');
}
};
83 changes: 42 additions & 41 deletions src/db/migrations/20190716062433-create-user-report.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,44 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('UserReports', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
reporterId: {
type: Sequelize.INTEGER,
onUpdate: 'CASCADE',
onDelete: 'CASCADE',
references: {
model: 'Users',
key: 'id',
as: 'reporterId'
}
},
reportId: {
type: Sequelize.INTEGER,
onUpdate: 'CASCADE',
onDelete: 'CASCADE',
references: {
model: 'Reports',
key: 'id',
as: 'reportId'
}
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('UserReports', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
reporterId: {
type: Sequelize.INTEGER,
onUpdate: 'CASCADE',
onDelete: 'CASCADE',
references: {
model: 'Users',
key: 'id',
as: 'reporterId'
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('UserReports');
}
};
},
reportId: {
type: Sequelize.INTEGER,
onUpdate: 'CASCADE',
onDelete: 'CASCADE',
references: {
model: 'Reports',
key: 'id',
as: 'reportId'
}
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('UserReports');
}
};
2 changes: 1 addition & 1 deletion src/db/migrations/20190716203009-create-comment-history.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ module.exports = {
return queryInterface.dropTable('CommentHistories');
}
};


Loading

0 comments on commit 64a285a

Please sign in to comment.