2018-04-29 20:33:27 +00:00
|
|
|
const map = require('lodash/map');
|
2018-04-28 13:00:30 +00:00
|
|
|
|
|
|
|
|
2018-05-13 07:33:10 +00:00
|
|
|
const NotificationType = {
|
|
|
|
UploadComplete: 'uploadComplete',
|
|
|
|
CodeMoved: 'codeMoved'
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-28 13:00:30 +00:00
|
|
|
class Notification
|
|
|
|
{
|
|
|
|
constructor(values)
|
|
|
|
{
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
self.id = values.id || values._id || null;
|
|
|
|
self.uploadId = values.uploadId || null;
|
2018-05-13 07:33:10 +00:00
|
|
|
self.codeId = values.codeId || null;
|
|
|
|
self.userId = values.userId || null;
|
|
|
|
self.type = values.type || NotificationType.UploadComplete;
|
2018-04-28 13:00:30 +00:00
|
|
|
self.attempt = values.attempt || 0;
|
2018-05-13 07:33:10 +00:00
|
|
|
self.metadata = values.metadata || null;
|
2018-04-28 13:00:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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,
|
2018-05-13 07:33:10 +00:00
|
|
|
codeId: notification.codeId,
|
2018-04-28 13:00:30 +00:00
|
|
|
uploadId: notification.uploadId,
|
2018-05-13 07:33:10 +00:00
|
|
|
attempt: notification.attempt,
|
|
|
|
type: notification.type,
|
|
|
|
metadata: notification.metadata
|
2018-04-28 13:00:30 +00:00
|
|
|
}, (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 = {
|
2018-05-13 07:33:10 +00:00
|
|
|
NotificationType,
|
2018-04-28 13:00:30 +00:00
|
|
|
Notification,
|
|
|
|
NotificationRepository
|
|
|
|
}
|