2022-07-25 06:25:10 +00:00
|
|
|
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;
|
|
|
|
|
2022-07-25 06:25:10 +00:00
|
|
|
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();
|
2022-07-25 06:25:10 +00:00
|
|
|
};
|
|
|
|
};
|