mirror of
https://github.com/glenndehaan/unifi-voucher-site.git
synced 2026-04-05 08:54:17 -04:00
Updated info.js to show deprecation messages when utilizing the options.json. Added translation status to info.js. Implemented the TRANSLATION_HIDDEN_LANGUAGES, TASK_CLEANUP_EXPIRED and TASK_CLEANUP_UNUSED environment variables in variables.js. Updated language dropdown to hide themselves if the number of allowed languages is lower than 2. Added the created at field within details.ejs. Implemented cleanup.js for automatically cleaning-up expired and unused vouchers. Updated the languages.js util with global filters. Added country flags to languages.js. Updated docker-compose.yml. Dependency updates. Updated README.md. Lowered log intensity within server.js
This commit is contained in:
69
utils/cleanup.js
Normal file
69
utils/cleanup.js
Normal file
@@ -0,0 +1,69 @@
|
||||
/**
|
||||
* Import own modules
|
||||
*/
|
||||
const cache = require('../modules/cache');
|
||||
const log = require('../modules/log');
|
||||
const unifi = require('../modules/unifi');
|
||||
|
||||
/**
|
||||
* Import own utils
|
||||
*/
|
||||
const {updateCache} = require('./cache');
|
||||
|
||||
module.exports = {
|
||||
/**
|
||||
* Function to clean up expired vouchers
|
||||
*
|
||||
* @returns {Promise<unknown>}
|
||||
*/
|
||||
cleanupExpired: () => {
|
||||
return new Promise(async (resolve) => {
|
||||
// Filter vouchers in cache
|
||||
const vouchers = cache.vouchers.filter((voucher) => {
|
||||
return voucher.expired;
|
||||
});
|
||||
|
||||
log.debug(`[Cleanup] Removing ${vouchers.length} voucher(s)...`);
|
||||
|
||||
// Remove vouchers
|
||||
for(let item = 0; item < vouchers.length; item++) {
|
||||
log.debug(`[Cleanup] Removing voucher: ${vouchers[item].id}`);
|
||||
await unifi.remove(vouchers[item].id);
|
||||
}
|
||||
|
||||
// Update cache
|
||||
await updateCache();
|
||||
|
||||
resolve();
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Function to clean up unused voucher that are still active after a day
|
||||
*
|
||||
* @returns {Promise<unknown>}
|
||||
*/
|
||||
cleanupUnused: () => {
|
||||
return new Promise(async (resolve) => {
|
||||
const vouchers = cache.vouchers.filter((voucher) => {
|
||||
const today = new Date();
|
||||
const voucherDate = new Date(voucher.createdAt);
|
||||
voucherDate.setDate(voucherDate.getDate() + 1);
|
||||
|
||||
return voucherDate.getTime() < today.getTime();
|
||||
});
|
||||
|
||||
log.debug(`[Cleanup] Removing ${vouchers.length} voucher(s)...`);
|
||||
|
||||
for(let item = 0; item < vouchers.length; item++) {
|
||||
log.debug(`[Cleanup] Removing voucher: ${vouchers[item].id}...`);
|
||||
await unifi.remove(vouchers[item].id);
|
||||
}
|
||||
|
||||
// Update cache
|
||||
await updateCache();
|
||||
|
||||
resolve();
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user