Skip to content

Commit

Permalink
[notifications] Refactor #1074
Browse files Browse the repository at this point in the history
  • Loading branch information
jfresco committed Dec 11, 2015
1 parent f621876 commit 2b02386
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
25 changes: 14 additions & 11 deletions lib/db-api/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,22 @@ exports.all = function all(user) {
*/

exports.create = function create(data) {
return map(data)
.then(saveNotification)
.catch(err => Promise.reject(err));
}
return map(data)
.then(saveNotification)
.catch(err => Promise.reject(err));
};

function saveNotification (event) {
log('Attempting to push new notification: %j', event);
return new Promise((resolve, reject) => {
var notification = new Notification(event);
notification.save(function (err, doc) {
if (err) {
log('Error pushing notification: %s', err);
return reject(err);
}

log('Notification pushed succesfully');
return resolve(doc);
});
});
Expand Down Expand Up @@ -123,30 +126,30 @@ const mappers = {
])
.then(data => resolve({
type: 'topic',
user: data.topic.author.id,
relatedUser: data.user.id,
topic: data.topic.id
user: data[1].author.id,
relatedUser: data[0].id,
topic: data[1].id
}))
.catch(err => reject(err));
});
}
}
};

function getUser (email) {
return new Promise ((resolve, reject) => {
return new Promise((resolve, reject) => {
User.getByEmail(email, function (err, user) {
if (err) {
return reject(err);
}

return resolve(user);
})
});
});
}

function getTopic (id) {
return new Promise((resolve, reject) => {
Topic.searchOne(eventData.topic.id, function (err, topic) {
Topic.searchOne(id, function (err, topic) {
if (err) {
return reject(err);
}
Expand Down
12 changes: 7 additions & 5 deletions lib/notifications/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,21 @@ if (embedded()) {
})
}

var send = exports.notifier.send.bind(exports.notifier);
var send = exports.notifier.send.bind(exports.notifier)

var notify = event => {
return new Promise((resolve, reject) => {
send(event, (err, data) => {
if (err) {
return reject(err);
// May fail for jobs not defined in notifier.
// Don't reject. If fails, the `then` function must be executed.
log('notifier failed to send notification: %s', err)
}

return resolve(data);
});
});
};
})
})
}

exports.notifier.send = function (cb) {
Promise.all([
Expand Down

0 comments on commit 2b02386

Please sign in to comment.