2018-04-29 20:33:27 +00:00
|
|
|
const map = require('lodash/map');
|
2018-04-28 13:00:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Notification
|
|
|
|
{
|
|
|
|
constructor(values)
|
|
|
|
{
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
self.id = values.id || values._id || null;
|
|
|
|
self.userId = values.userId || null;
|
|
|
|
self.uploadId = values.uploadId || null;
|
|
|
|
self.attempt = values.attempt || 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class NotificationRepository
|
|
|
|
{
|
|
|
|
constructor(store)
|
|
|
|
{
|
|
|
|
var self = this;
|
|
|
|
self.store = store;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
list()
|
|
|
|
{
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) =>
|
|
|
|
{
|
|
|
|
self.store.find({}, (err, docs) =>
|
|
|
|
{
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-29 20:33:27 +00:00
|
|
|
resolve(map(docs, (doc) => new Notification(doc)));
|
2018-04-28 13:00:30 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
insert(notification)
|
|
|
|
{
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) =>
|
|
|
|
{
|
|
|
|
self.store.insert({
|
|
|
|
userId: notification.userId,
|
|
|
|
uploadId: notification.uploadId,
|
|
|
|
attempt: notification.attempt
|
|
|
|
}, (err, dbNotification) =>
|
|
|
|
{
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(dbNotification._id);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
update(notification)
|
|
|
|
{
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) =>
|
|
|
|
{
|
|
|
|
self.store.update({ _id: notification.id }, { $set: {
|
|
|
|
attempt: notification.attempt
|
|
|
|
}},
|
|
|
|
(err, numAffected) =>
|
|
|
|
{
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (numAffected == 0)
|
|
|
|
{
|
|
|
|
reject();
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
delete(notificationId)
|
|
|
|
{
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) =>
|
|
|
|
{
|
|
|
|
self.store.remove({ _id: notificationId }, (err, numRemoved) =>
|
|
|
|
{
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
Notification,
|
|
|
|
NotificationRepository
|
|
|
|
}
|