Skip to content

Commit

Permalink
[notifications] Fixed notifications data references #1074
Browse files Browse the repository at this point in the history
  • Loading branch information
jfresco committed Dec 9, 2015
1 parent e16cdb7 commit 1de8a1c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
15 changes: 15 additions & 0 deletions lib/db-api/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
33 changes: 25 additions & 8 deletions lib/db-api/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
});
Expand All @@ -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
}))
Expand All @@ -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
}))
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 1de8a1c

Please sign in to comment.