From 1de8a1cc069954580d3538ba87c87930f2fa5d45 Mon Sep 17 00:00:00 2001 From: Jose Date: Fri, 4 Dec 2015 15:39:44 -0300 Subject: [PATCH] [notifications] Fixed notifications data references #1074 --- lib/db-api/comment.js | 15 +++++++++++++++ lib/db-api/notification.js | 33 +++++++++++++++++++++++++-------- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/lib/db-api/comment.js b/lib/db-api/comment.js index f0121e42c7..3c7356dfd0 100644 --- a/lib/db-api/comment.js +++ b/lib/db-api/comment.js @@ -50,6 +50,21 @@ exports.all = function all(fn) { return this; }; +/** + * Get all comments + * + * @param {ObjectId} id comment id + * @param {Function} fn callback function + * - 'err' error found while process or `null` + * - 'comments' list items found or `undefined` + * @return {Module} `comment` module + * @api public + */ + +exports.getById = function getById(id, fn) { + return Comment.findById(id, fn); +}; + /** * Create comment for `proposal` by `author` * with `text` diff --git a/lib/db-api/notification.js b/lib/db-api/notification.js index d08af7367d..805598f008 100644 --- a/lib/db-api/notification.js +++ b/lib/db-api/notification.js @@ -6,6 +6,7 @@ var mongoose = require('mongoose'); var Notification = mongoose.model('Notification'); var User = require('./user'); var Topic = require('./topic'); +var Comment = require('./comment'); var log = require('debug')('democracyos:db-api:notification'); var utils = require('lib/utils'); var pluck = utils.pluck; @@ -79,12 +80,14 @@ function map (eventData) { const mappers = { reply: function mapReply (eventData) { return new Promise((resolve, reject) => { - getUser(eventData.to) - .then(user => resolve({ + getComment(eventData.comment.id) + .then(comment => getTopic(comment.reference)) + .then(topic => resolve({ type: 'reply', relatedUser: eventData.reply.author.id, - user: user, - comment: eventData.reply.id + user: eventData.comment.author.id, + comment: eventData.comment.id, + topic: topic.id })) .catch(err => reject(err)); }); @@ -95,8 +98,8 @@ const mappers = { getTopic(eventData.comment.reference) .then(topic => resolve({ type: 'upvote', - user: topic.author.id, - relatedUser: eventData.user.id, + user: eventData.comment.author._id, + relatedUser: eventData.user._id, topic: topic.id, comment: eventData.comment.id })) @@ -109,8 +112,8 @@ const mappers = { getTopic(eventData.comment.reference) .then(topic => resolve({ type: 'downvote', - user: topic.author.id, - relatedUser: eventData.user.id, + user: eventData.comment.author._id, + relatedUser: eventData.user._id, topic: topic.id, comment: eventData.comment.id })) @@ -147,6 +150,20 @@ function getUser (email) { }); } +function getComment (id) { + return new Promise((resolve, reject) => { + Comment.getById(id, (err, doc) => { + if (err) { + return reject(err); + } else if (!doc) { + return reject(new Error('comment not found')) + } + + return resolve(doc); + }) + }); +} + function getTopic (id) { return new Promise((resolve, reject) => { Topic.searchOne(id, function (err, topic) {