Recv/lib/repository/notification.js
Mark van Renswoude 4db1b2f23d Production bundle optimizations
Fixed production check in webpack config, removed moment locales and reduced lodash and fontawesome inclusion
2018-04-29 22:33:27 +02:00

123 lines
1.9 KiB
JavaScript

const map = require('lodash/map');
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;
}
resolve(map(docs, (doc) => new Notification(doc)));
});
});
}
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
}