diff --git a/api/services/NotificationService.js b/api/services/NotificationService.js index fc8919aea..5d40f5bb7 100644 --- a/api/services/NotificationService.js +++ b/api/services/NotificationService.js @@ -1,4 +1,5 @@ const NameService = require('./NameService'); +const CommonService = require('./CommonService'); const NOTIFICATION_ENTITIES = { CAVE: 'cave', @@ -22,6 +23,11 @@ const NOTIFICATION_TYPES = { RESTORE: 'RESTORE', }; +async function removeOlderNotifications() { + const query = `DELETE FROM t_notification WHERE date_inscription < current_timestamp - interval '2 month';`; + await CommonService.query(query); +} + const safeGetPropId = (prop, data) => { if (data && data[prop]) { if (data[prop] instanceof Object) { @@ -470,6 +476,11 @@ module.exports = { return true; }) ); + + // 5% chance to also remove older notifications + if (process.env.NODE_ENV !== 'test' && Math.random() < 0.05) + removeOlderNotifications(); + return res; } catch (error) { // Fail silently to avoid sending an error to the user