-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
946 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
import 'dotenv/config'; | ||
import models from '../models'; | ||
import reportService from '../services/report.service'; | ||
import Util from '../helpers/util'; | ||
|
||
const util = new Util(); | ||
|
||
const db = models.reporting; | ||
/** | ||
* | ||
* | ||
* @class Report | ||
*/ | ||
class Report { | ||
/** | ||
* | ||
* | ||
* @static | ||
* @param {*} req | ||
* @param {*} res | ||
* @returns {object} Reports | ||
* @memberof Report | ||
*/ | ||
static async getAllReport(req, res) { | ||
const counter = await db.count(); | ||
if (counter === 0) { | ||
util.setError(200, 'No reports found'); | ||
return util.send(res); | ||
} | ||
if (req.offset >= counter) { | ||
req.offset = 0; | ||
} | ||
const { offset, limit } = req; | ||
const reports = await reportService.getAllReport(offset, limit); | ||
const response = { | ||
count: counter, | ||
reports, | ||
}; | ||
util.setSuccess(200, 'Reports retrieved successfully', response); | ||
return util.send(res); | ||
} | ||
|
||
/** | ||
* | ||
* | ||
* @static | ||
* @param {*} req | ||
* @param {*} res | ||
* @returns {object} Reports | ||
* @memberof Report | ||
*/ | ||
static async getMyReport(req, res) { | ||
try { | ||
const counter = await db.count({ where: { userId: req.auth.id } }); | ||
if (counter === 0) { | ||
util.setError(200, 'No reports found'); | ||
return util.send(res); | ||
} | ||
if (req.offset >= counter) { | ||
req.offset = 0; | ||
} | ||
const { offset, limit } = req; | ||
const reports = await reportService.getMyReport(offset, limit, req.auth.id,); | ||
const response = { | ||
count: counter, | ||
yourReport: reports, | ||
}; | ||
util.setSuccess(200, 'Your reports retrieved successfully', response); | ||
return util.send(res); | ||
} catch (error) { | ||
util.setError(500, 'server error contact admin'); | ||
return util.send(res); | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* | ||
* @static | ||
* @param {*} req | ||
* @param {*} res | ||
* @returns {object} Report for an article | ||
* @memberof Report | ||
*/ | ||
static async getReportsForArticle(req, res) { | ||
try { | ||
const counter = await db.count({ where: { articleSlug: req.params.Article } }); | ||
if (counter === 0) { | ||
util.setError(200, 'This article is not yet Reported'); | ||
return util.send(res); | ||
} | ||
if (req.offset >= counter) { | ||
req.offset = 0; | ||
} | ||
const { offset, limit } = req; | ||
const articleSlug = req.params.Article; | ||
const reports = await reportService.getAllReportForOneArticle(offset, limit, articleSlug); | ||
const response = { | ||
count: counter, | ||
reports, | ||
}; | ||
util.setSuccess(200, 'Reports retrieved successfully', response); | ||
return util.send(res); | ||
} catch (error) { | ||
util.setError(500, 'server error contact admin'); | ||
return util.send(res); | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* | ||
* @static | ||
* @param {*} req | ||
* @param {*} res | ||
* @returns {object} delete your report | ||
* @memberof Report | ||
*/ | ||
static async deleteReport(req, res) { | ||
try { | ||
const findReport = await db.findOne({ | ||
where: { id: req.params.reportId } | ||
}); | ||
if (!findReport) { | ||
return res.status(200).json({ | ||
status: 200, | ||
message: 'That report does not exist!' | ||
}); | ||
} | ||
if (req.auth.id !== findReport.userId && req.auth.role !== 'admin') { | ||
return res.status(403).json({ | ||
status: 403, | ||
message: 'Sorry you have no access to delete this Report.' | ||
}); | ||
} | ||
await db.destroy({ | ||
where: { id: req.params.reportId } | ||
}); | ||
util.setSuccess(200, 'Report deleted succussfully!'); | ||
return util.send(res); | ||
} catch (error) { | ||
util.setError(500, 'server error contact admin'); | ||
return util.send(res); | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* | ||
* @static | ||
* @param {*} req | ||
* @param {*} res | ||
* @returns {Object} Report an article | ||
* @memberof Report | ||
*/ | ||
static async reportArticle(req, res) { | ||
try { | ||
const report = { | ||
reason: req.body.reason, | ||
userId: req.auth.id, | ||
articleSlug: req.params.Article | ||
}; | ||
const reportArticle = await reportService.report(report); | ||
const newReport = { | ||
reason: reportArticle.reason, | ||
articleSlug: reportArticle.articleSlug, | ||
id: reportArticle.id | ||
}; | ||
util.setSuccess(200, 'Article Successfully reported', newReport); | ||
return util.send(res); | ||
} catch (error) { | ||
util.setError(500, 'server error contact admin'); | ||
return util.send(res); | ||
} | ||
} | ||
} | ||
|
||
export default Report; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
|
||
|
||
export default async (page, limit, counter) => { | ||
let pageN = parseInt(page, 10); | ||
if (isNaN(pageN) || page < 1) { | ||
pageN = 1; | ||
} | ||
let limitS = parseInt(limit, 10); | ||
if (isNaN(limitS)) { | ||
limitS = 10; | ||
} else if (limitS > 50) { | ||
limitS = 50; | ||
} else if (limitS < 1) { | ||
limitS = 1; | ||
} | ||
let offset = (page - 1) * limit; | ||
if (offset >= counter) { | ||
offset = 0; | ||
} | ||
return { | ||
limit, | ||
offset | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import Util from '../helpers/util'; | ||
import models from '../models/index'; | ||
|
||
const util = new Util(); | ||
|
||
export default async (req, res, next) => { | ||
try { | ||
const ArticleSlug = req.params.Article; | ||
const post = await models.Article.findOne({ where: { slug: ArticleSlug } }); | ||
|
||
if (!post) { | ||
util.setError(404, 'post not found'); | ||
return util.send(res); | ||
} | ||
const user = await models.user.findOne({ where: { email: req.auth.email } }); | ||
const userId = user.id; | ||
if (userId === post.authorId) { | ||
util.setError(403, 'Sorry you can not report your article.'); | ||
return util.send(res); | ||
} | ||
|
||
const Report = await models.reporting.findOne({ where: { articleSlug: ArticleSlug, userId } }); | ||
if (Report) { | ||
util.setError(403, 'You arleady Reported this article.'); | ||
return util.send(res); | ||
} | ||
next(); | ||
} catch (error) { | ||
util.setError(500, 'server error contact admin'); | ||
return util.send(res); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import Joi from 'joi'; | ||
import Util from '../../helpers/util'; | ||
|
||
const util = new Util(); | ||
|
||
export default (req, res, next) => { | ||
const { reason } = req.body; | ||
|
||
const schema = { | ||
reason: Joi.string().valid('Rules Violation', 'Spam', 'Harassment').required(), | ||
}; | ||
const { error } = Joi.validate({ | ||
reason | ||
}, schema); | ||
|
||
if (!error) return next(); | ||
const errorMessageFromJoi = error.details[0].message; | ||
const removeEscapeCharacters = errorMessageFromJoi.replace(/['"]+/g, ''); | ||
util.setError(400, removeEscapeCharacters); | ||
return util.send(res); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
'use strict'; | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => { | ||
return queryInterface.createTable('reportings', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
userId: { | ||
type: Sequelize.INTEGER, | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE', | ||
references: { | ||
model: 'users', | ||
key: 'id' | ||
} | ||
}, | ||
articleSlug: { | ||
type: Sequelize.STRING, | ||
allowNull: false, | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE', | ||
references: { | ||
model: 'Articles', | ||
key: 'slug' | ||
} | ||
}, | ||
reason: { | ||
type: Sequelize.ENUM, | ||
values: ['Rules Violation', 'Spam', 'Harassment'], | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}); | ||
}, | ||
down: (queryInterface, Sequelize) => { | ||
return queryInterface.dropTable('reportings'); | ||
} | ||
}; |
Oops, something went wrong.