salix/back/methods/notification/clean.js

50 lines
1.2 KiB
JavaScript
Raw Normal View History

module.exports = Self => {
Self.remoteMethod('clean', {
description: 'clean notifications from queue',
accessType: 'WRITE',
returns: {
type: 'object',
root: true
},
http: {
path: `/clean`,
verb: 'POST'
}
});
Self.clean = async options => {
const models = Self.app.models;
const status = ['sent', 'error'];
const myOptions = {};
let tx;
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
const config = await models.NotificationConfig.findOne({}, myOptions);
2022-11-18 08:57:07 +00:00
if (!config.cleanDays) return;
const cleanDate = new Date();
cleanDate.setDate(cleanDate.getDate() - config.cleanDays);
await models.NotificationQueue.destroyAll({
where: {status: {inq: status}},
created: {lt: cleanDate}
}, myOptions);
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
2022-11-18 08:57:07 +00:00
if (tx) await tx.commit();
};
};