Browse Source

Added logger and cleaned up reports and news actions.

KrisVos130 8 years ago
parent
commit
e83e0f182d
2 changed files with 111 additions and 76 deletions
  1. 29 11
      backend/logic/actions/news.js
  2. 82 65
      backend/logic/actions/reports.js

+ 29 - 11
backend/logic/actions/news.js

@@ -5,6 +5,7 @@ const async = require('async');
 const db = require('../db');
 const cache = require('../cache');
 const utils = require('../utils');
+const logger = require('../logger');
 const hooks = require('./hooks');
 
 cache.sub('news.create', news => {
@@ -18,35 +19,52 @@ cache.sub('news.create', news => {
 module.exports = {
 
 	index: (session, cb) => {
-		db.models.news.find({}).sort({ createdAt: 'desc' }).exec((err, news) => {
-			if (err) throw err;
-			else cb({ status: 'success', data: news });
+		async.waterfall([
+			(next) => {
+				db.models.news.find({}).sort({ createdAt: 'desc' }).exec(next);
+			}
+		], (err, news) => {
+			if (err) {
+				logger.log("NEWS_INDEX", "ERROR", `Indexing news failed. "${err.message}"`);
+				return cb({status: 'failure', message: 'Something went wrong.'});
+			}
+			logger.log("NEWS_INDEX", "SUCCESS", `Indexing news successful.`);
+			return cb({ status: 'success', data: news });
 		});
 	},
 
 	create: hooks.adminRequired((session, data, cb, userId) => {
 		async.waterfall([
-
 			(next) => {
 				data.createdBy = userId;
 				data.createdAt = Date.now();
 				db.models.news.create(data, next);
 			}
-
 		], (err, news) => {
-			console.log(err);
-			if (err) return cb({ 'status': 'failure', 'message': 'Something went wrong' });
-			else {
+			if (err) {
+				logger.log("NEWS_CREATE", "ERROR", `Creating news failed. "${err.message}"`);
+				return cb({ 'status': 'failure', 'message': 'Something went wrong' });
+			} else {
 				cache.pub('news.create', news);
+				logger.log("NEWS_CREATE", "SUCCESS", `Creating news successful.`);
 				return cb({ 'status': 'success', 'message': 'Successfully created News' });
 			}
 		});
 	}),
 
 	newest: (session, cb) => {
-		db.models.news.findOne({}).sort({ createdAt: 'desc' }).exec((err, news) => {
-			if (err) throw err;
-			else cb({ status: 'success', data: news });
+		async.waterfall([
+			(next) => {
+				db.models.news.findOne({}).sort({ createdAt: 'desc' }).exec(next);
+			}
+		], (err, news) => {
+			if (err) {
+				logger.log("NEWS_NEWEST", "ERROR", `Getting the latest news failed. "${err.message}"`);
+				return cb({ 'status': 'failure', 'message': 'Something went wrong' });
+			} else {
+				logger.log("NEWS_NEWEST", "SUCCESS", `Successfully got the latest news.`);
+				return cb({ status: 'success', data: news });
+			}
 		});
 	}
 

+ 82 - 65
backend/logic/actions/reports.js

@@ -5,8 +5,50 @@ const async = require('async');
 const db = require('../db');
 const cache = require('../cache');
 const utils = require('../utils');
+const logger = require('../logger');
 const hooks = require('./hooks');
 const songs = require('../songs');
+const reportableIssues = [
+	{
+		name: 'Video',
+		reasons: [
+			'Doesn\'t exist',
+			'It\'s private',
+			'It\'s not available in my country'
+		]
+	},
+	{
+		name: 'Title',
+		reasons: [
+			'Incorrect',
+			'Inappropriate'
+		]
+	},
+	{
+		name: 'Duration',
+		reasons: [
+			'Skips too soon',
+			'Skips too late',
+			'Starts too soon',
+			'Skips too late'
+		]
+	},
+	{
+		name: 'Artists',
+		reasons: [
+			'Incorrect',
+			'Inappropriate'
+		]
+	},
+	{
+		name: 'Thumbnail',
+		reasons: [
+			'Incorrect',
+			'Inappropriate',
+			'Doesn\'t exist'
+		]
+	}
+];
 
 cache.sub('report.resolve', reportId => {
 	utils.emitToRoom('admin.reports', 'event:admin.report.resolved', reportId);
@@ -19,29 +61,40 @@ cache.sub('report.create', report => {
 module.exports = {
 
 	index: hooks.adminRequired((session, cb) => {
-		db.models.report.find({ resolved: false }).sort({ released: 'desc' }).exec((err, reports) => {
+
+		async.waterfall([
+			(next) => {
+				db.models.report.find({ resolved: false }).sort({ released: 'desc' }).exec(next);
+			}
+		], (err, reports) => {
 			if (err) {
-				console.error(err);
-				cb({ 'status': 'failure', 'message': 'Something went wrong'});
+				logger.log("REPORTS_INDEX", "ERROR", `Indexing reports failed. "${err.message}"`);
+				return cb({ 'status': 'failure', 'message': 'Something went wrong'});
 			}
+			logger.log("REPORTS_INDEX", "SUCCESS", "Indexing reports successful.");
 			cb({ status: 'success', data: reports });
 		});
 	}),
 
-	resolve: hooks.adminRequired((session, _id, cb) => {
-		db.models.report.findOne({ _id }).sort({ released: 'desc' }).exec((err, report) => {
+	resolve: hooks.adminRequired((session, reportId, cb, userId) => {
+		async.waterfall([
+			(next) => {
+				db.models.report.findOne({ _id: reportId }).sort({ released: 'desc' }).exec(next);
+			},
+
+			(report, next) => {
+				if (!report) return next('Report not found.');
+				db.models.update({ _id: reportId }, next);
+			}
+		], (err) => {
 			if (err) {
-				console.error(err);
-				cb({ 'status': 'failure', 'message': 'Something went wrong'});
+				logger.log("REPORTS_RESOLVE", "ERROR", `Resolving report "${reportId}" failed. Mongo error. "${err.message}"`);
+				return cb({ 'status': 'failure', 'message': 'Something went wrong'});
+			} else {
+				cache.pub('report.resolve', reportId);
+				logger.log("REPORTS_RESOLVE", "SUCCESS", `"${userId}" resolved report "${reportId}".`);
+				cb({ status: 'success', message: 'Successfully resolved Report' });
 			}
-			report.resolved = true;
-			report.save(err => {
-				if (err) console.error(err);
-				else {
-					cache.pub('report.resolve', _id);
-					cb({ status: 'success', message: 'Successfully resolved Report' });
-				}
-			});
 		});
 	}),
 
@@ -49,60 +102,17 @@ module.exports = {
 		async.waterfall([
 
 			(next) => {
-				songs.getSong(data.songId, (err, song) => {
-					if (err) return next(err);
-					if (!song) return next('Song does not exist in our Database');
-					next();
-				});
+				songs.getSong(data.songId, next);
 			},
 
-			(next) => {
-				let issues = [
-					{
-						name: 'Video',
-						reasons: [
-							'Doesn\'t exist',
-							'It\'s private',
-							'It\'s not available in my country'
-						]
-					},
-					{
-						name: 'Title',
-						reasons: [
-							'Incorrect',
-							'Inappropriate'
-						]
-					},
-					{
-						name: 'Duration',
-						reasons: [
-							'Skips too soon',
-							'Skips too late',
-							'Starts too soon',
-							'Skips too late'
-						]
-					},
-					{
-						name: 'Artists',
-						reasons: [
-							'Incorrect',
-							'Inappropriate'
-						]
-					},
-					{
-						name: 'Thumbnail',
-						reasons: [
-							'Incorrect',
-							'Inappropriate',
-							'Doesn\'t exist'
-						]
-					}
-				];
+			(song, next) => {
+				if (!song) return next('Song not found.');
+
 
 				for (let z = 0; z < data.issues.length; z++) {
-					if (issues.filter(issue => { return issue.name == data.issues[z].name; }).length > 0) {
+					if (reportableIssues.filter(issue => { return issue.name == data.issues[z].name; }).length > 0) {
 						for (let r = 0; r < issues.length; r++) {
-							if (issues[r].reasons.every(reason => data.issues[z].reasons.indexOf(reason) < -1)) {
+							if (reportableIssues[r].reasons.every(reason => data.issues[z].reasons.indexOf(reason) < -1)) {
 								return cb({ 'status': 'failure', 'message': 'Invalid data' });
 							}
 						}
@@ -131,9 +141,16 @@ module.exports = {
 			}
 
 		], (err, report) => {
-			if (err) return cb({ 'status': 'failure', 'message': 'Something went wrong' });
+			if (err) {
+				let error = 'An error occurred.';
+				if (typeof err === "string") error = err;
+				else if (err.message) error = err.message;
+				logger.log("REPORTS_CREATE", "ERROR", `Creating report for "${data.songId}" failed. "${error}"`);
+				return cb({ 'status': 'failure', 'message': 'Something went wrong' });
+			}
 			else {
 				cache.pub('report.create', report);
+				logger.log("REPORTS_CREATE", "SUCCESS", `"${userId}" created report for "${data.songId}".`);
 				return cb({ 'status': 'success', 'message': 'Successfully created report' });
 			}
 		});