Browse Source

Fixed issues with creating reports

theflametrooper 8 years ago
parent
commit
f79369f707
2 changed files with 16 additions and 12 deletions
  1. 9 5
      backend/logic/actions/reports.js
  2. 7 7
      backend/logic/songs.js

+ 9 - 5
backend/logic/actions/reports.js

@@ -153,7 +153,12 @@ module.exports = {
 		async.waterfall([
 
 			(next) => {
-				songs.getSong(data.songId, next);
+				db.models.song.findOne({ songId: data.songId }).exec(next);
+			},
+
+			(song, next) => {
+				if (!song) return next('Song not found.');
+				songs.getSong(song._id, next);
 			},
 
 			(song, next) => {
@@ -172,7 +177,7 @@ module.exports = {
 
 				next();
 			},
-			
+
 			(next) => {
 				let issues = [];
 
@@ -196,8 +201,7 @@ module.exports = {
 				err = utils.getError(err);
 				logger.error("REPORTS_CREATE", `Creating report for "${data.songId}" failed by user "${userId}". "${err}"`);
 				return cb({ 'status': 'failure', 'message': err });
-			}
-			else {
+			} else {
 				cache.pub('report.create', report);
 				logger.success("REPORTS_CREATE", `User "${userId}" created report for "${data.songId}".`);
 				return cb({ 'status': 'success', 'message': 'Successfully created report' });
@@ -205,4 +209,4 @@ module.exports = {
 		});
 	})
 
-};
+};

+ 7 - 7
backend/logic/songs.js

@@ -52,25 +52,25 @@ module.exports = {
 	/**
 	 * Gets a song by id from the cache or Mongo, and if it isn't in the cache yet, adds it the cache
 	 *
-	 * @param {String} songId - the id of the song we are trying to get
+	 * @param {String} id - the id of the song we are trying to get
 	 * @param {Function} cb - gets called once we're done initializing
 	 */
-	getSong: function(songId, cb) {
+	getSong: function(id, cb) {
 		async.waterfall([
 
 			(next) => {
-				if (!mongoose.Types.ObjectId.isValid(songId)) return next('Id is not a valid ObjectId.');
-				cache.hget('songs', songId, next);
+				if (!mongoose.Types.ObjectId.isValid(id)) return next('Id is not a valid ObjectId.');
+				cache.hget('songs', id, next);
 			},
 
 			(song, next) => {
 				if (song) return next(true, song);
-				db.models.song.findOne({_id: songId}, next);
+				db.models.song.findOne({_id: id}, next);
 			},
 
 			(song, next) => {
 				if (song) {
-					cache.hset('songs', songId, song, next);
+					cache.hset('songs', id, song, next);
 				} else next('Song not found.');
 			},
 
@@ -153,4 +153,4 @@ module.exports = {
 			cb(null);
 		});
 	}
-};
+};