50 lines
1.2 KiB
JavaScript
50 lines
1.2 KiB
JavaScript
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);
|
|
|
|
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;
|
|
}
|
|
|
|
if (tx) await tx.commit();
|
|
};
|
|
};
|