Recv/lib/repository/notification.js

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
}