Refactored unifi.js to separate logic. Updated application header. Fixed incorrect screen-reader text. Updated sync button text. Implemented last sync date/time. Implemented cache util. Fixed missing vouchers check to prevent 'headers are already send' error. Fixed missing cache sync on api voucher creation. Implemented auto sync every 15 minutes

This commit is contained in:
Glenn de Haan
2024-04-03 23:15:10 +02:00
parent 65c79b1f3d
commit 4404fabafa
4 changed files with 93 additions and 33 deletions

37
utils/cache.js Normal file
View File

@@ -0,0 +1,37 @@
/**
* Import own modules
*/
const log = require('../modules/log');
const unifi = require('../modules/unifi');
const cache = require('../modules/cache');
/**
* Exports all cache utils
*
* @type {{updateCache: (function(): Promise<*>)}}
*/
module.exports = {
/**
* Update the cache
*
* @return {Promise<*>}
*/
updateCache: () => {
return new Promise(async (resolve) => {
log.info('[Cache] Requesting UniFi Vouchers...');
const vouchers = await unifi.list().catch((e) => {
log.error('[Cache] Error requesting vouchers!');
log.error(e);
});
if(vouchers) {
cache.vouchers = vouchers;
cache.updated = new Date().getTime();
log.info(`[Cache] Saved ${vouchers.length} voucher(s)`);
}
resolve();
});
}
};