More tests and move settings stuff to routes and out of models.

This commit is contained in:
gaba
2016-11-09 12:35:59 -08:00
parent 9ff2613ab4
commit eae2a0285b
3 changed files with 30 additions and 45 deletions
+15 -22
View File
@@ -1,7 +1,6 @@
const mongoose = require('../mongoose');
const uuid = require('uuid');
const Action = require('./action');
const Setting = require('./setting');
const Schema = mongoose.Schema;
@@ -115,27 +114,21 @@ CommentSchema.statics.findByStatus = function(status) {
* Find comments that need to be moderated (aka moderation queue).
* @param {String} moderationValue pre or post moderation setting. If it is undefined then look at the settings.
*/
CommentSchema.statics.moderationQueue = function(moderationValue) {
return Setting.getModerationSetting().then(function({moderation}){
if (typeof moderationValue === 'undefined' || moderationValue === undefined) {
moderationValue = moderation;
}
switch(moderationValue){
// Pre-moderation: New comments are shown in the moderator queues immediately.
case 'pre':
return Comment.findByStatus('').then((comments) => {
return comments;
});
// Post-moderation: New comments do not appear in moderation queues unless they are flagged by other users.
case 'post':
return Comment.findByStatusByActionType('', 'flag').then((comments) => {
return comments;
});
default:
throw new Error('Moderation setting not found.');
}
});
CommentSchema.statics.moderationQueue = function(moderation) {
switch(moderation){
// Pre-moderation: New comments are shown in the moderator queues immediately.
case 'pre':
return Comment.findByStatus('').then((comments) => {
return comments;
});
// Post-moderation: New comments do not appear in moderation queues unless they are flagged by other users.
case 'post':
return Comment.findByStatusByActionType('', 'flag').then((comments) => {
return comments;
});
default:
throw new Error('Moderation setting not found.');
}
};
//==============================================================================
+10 -9
View File
@@ -1,6 +1,8 @@
const express = require('express');
const Comment = require('../../../models/comment');
const Setting = require('../../../models/setting');
const router = express.Router();
//==============================================================================
@@ -50,8 +52,14 @@ router.get('/status/rejected', (req, res, next) => {
// Pre-moderation: New comments are shown in the moderator queues immediately.
// Post-moderation: New comments do not appear in moderation queues unless they are flagged by other users.
router.get('/status/pending', (req, res, next) => {
Comment.moderationQueue(req.query.moderation).then((comments) => {
res.status(200).json(comments);
Setting.getModerationSetting().then(function({moderation}){
let moderationValue = req.query.moderation;
if (typeof moderationValue === 'undefined' || moderationValue === undefined) {
moderationValue = moderation;
}
Comment.moderationQueue(moderationValue).then((comments) => {
res.status(200).json(comments);
});
}).catch(error => {
next(error);
});
@@ -68,13 +76,6 @@ router.post('/', (req, res, next) => {
}).catch(error => {
next(error);
});
// let comment = new Comment({body, author_id, asset_id, parent_id, status, username});
// comment.save().then(({id}) => {
// res.status(200).send({'id': id});
// }).catch(error => {
// next(error);
// });
});
router.post('/:comment_id', (req, res, next) => {
+5 -14
View File
@@ -128,19 +128,10 @@ describe('Comment: models', () => {
expect(result[0]).to.have.property('body', 'comment 30');
});
});
it('should find an array of new comments to moderate when pre-moderation in settings', () => {
return Comment.moderationQueue().then((result) => {
expect(result).to.not.be.null;
expect(result).to.have.lengthOf(2);
});
});
it('should find an array of new comments to moderate when post-moderation in settings', () => {
return Comment.moderationQueue('post').then((result) => {
expect(result).to.not.be.null;
expect(result).to.have.lengthOf(1);
expect(result[0]).to.have.property('body', 'comment 30');
});
});
it('should fail when the moderation is not pre or post');
// it('should fail when the moderation is not pre or post', () => {
// return Comment.moderationQueue('any').catch(function(error) {
// expect(error).to.not.be.null;
// });
// });
});
});