2021-01-14 06:19:15 +03:00
|
|
|
const {notifications} = require('../../services/notifications');
|
2019-08-09 17:11:24 +03:00
|
|
|
const api = require('./index');
|
|
|
|
const internalContext = {context: {internal: true}};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
docName: 'notifications',
|
|
|
|
|
|
|
|
browse: {
|
|
|
|
permissions: true,
|
|
|
|
query(frame) {
|
2021-01-14 05:55:55 +03:00
|
|
|
return notifications.browse({
|
|
|
|
user: {
|
2021-01-14 07:49:55 +03:00
|
|
|
id: frame.user && frame.user.id
|
2019-08-09 17:11:24 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
add: {
|
|
|
|
statusCode(result) {
|
|
|
|
if (result.notifications.length) {
|
|
|
|
return 201;
|
|
|
|
} else {
|
|
|
|
return 200;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: true,
|
|
|
|
query(frame) {
|
2021-01-14 05:55:55 +03:00
|
|
|
const {allNotifications, notificationsToAdd} = notifications.add({
|
|
|
|
notifications: frame.data.notifications
|
2019-08-09 17:11:24 +03:00
|
|
|
});
|
|
|
|
|
2021-01-14 08:30:09 +03:00
|
|
|
if (notificationsToAdd.length){
|
|
|
|
return api.settings.edit({
|
|
|
|
settings: [{
|
|
|
|
key: 'notifications',
|
|
|
|
// @NOTE: We always need to store all notifications!
|
|
|
|
value: allNotifications.concat(notificationsToAdd)
|
|
|
|
}]
|
|
|
|
}, internalContext).then(() => {
|
|
|
|
return notificationsToAdd;
|
|
|
|
});
|
|
|
|
}
|
2019-08-09 17:11:24 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
destroy: {
|
|
|
|
statusCode: 204,
|
|
|
|
options: ['notification_id'],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
notification_id: {
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: true,
|
2021-06-03 11:54:09 +03:00
|
|
|
async query(frame) {
|
|
|
|
const allNotifications = await notifications.destroy({
|
2021-01-14 05:55:55 +03:00
|
|
|
notificationId: frame.options.notification_id,
|
|
|
|
user: {
|
2021-01-14 07:49:55 +03:00
|
|
|
id: frame.user && frame.user.id
|
2021-01-14 05:55:55 +03:00
|
|
|
}
|
2020-04-29 18:44:27 +03:00
|
|
|
});
|
2019-08-09 17:11:24 +03:00
|
|
|
|
|
|
|
return api.settings.edit({
|
|
|
|
settings: [{
|
|
|
|
key: 'notifications',
|
|
|
|
value: allNotifications
|
|
|
|
}]
|
|
|
|
}, internalContext).return();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears all notifications. Method used in tests only
|
|
|
|
*
|
|
|
|
* @private Not exposed over HTTP
|
|
|
|
*/
|
|
|
|
destroyAll: {
|
|
|
|
statusCode: 204,
|
|
|
|
permissions: {
|
|
|
|
method: 'destroy'
|
|
|
|
},
|
|
|
|
query() {
|
2021-01-14 05:55:55 +03:00
|
|
|
const allNotifications = notifications.destroyAll();
|
2019-08-09 17:11:24 +03:00
|
|
|
|
|
|
|
return api.settings.edit({
|
|
|
|
settings: [{
|
|
|
|
key: 'notifications',
|
|
|
|
value: allNotifications
|
|
|
|
}]
|
|
|
|
}, internalContext).return();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|