refactor: extract files listing out of cleanOldFailedQRScans()

This commit is contained in:
siiky 2024-07-03 12:23:04 +01:00
parent 806a2716f0
commit a573419dd3

View file

@ -132,6 +132,18 @@ function updateCoinAtmRadar () {
.then(rates => coinAtmRadar.update(rates, settings()))
}
const readdir = dirpath =>
fs.readdir(dirpath, { withFileTypes: true })
.then(_.map(entry => _.set('path', path.join(dirpath, entry.name), entry)))
const readdirRec = rootPath =>
readdir(rootPath)
.then(entries => Promise.all(
entries.map(entry => entry.isDirectory() ? readdirRec(entry.path) : [entry])
))
.then(_.flatten)
// @see lib/machine-loader.js:updateFailedQRScans()
const cleanOldFailedQRScans = () => {
const old = new Date()
old.setDate(old.getDate() - 2) // 2 days ago
@ -139,19 +151,17 @@ const cleanOldFailedQRScans = () => {
const then = new Date(path.basename(filepath).replace(/-[0-9]+\.jpg$/, ''))
return then < old
}
const baseDirPath = path.join(OPERATOR_DATA_DIR, 'failedQRScans')
fs.readdir(baseDirPath)
.then(machines => Promise.all(
machines.map(
machine => {
const machineDirPath = path.join(baseDirPath, machine)
return fs.readdir(machineDirPath)
.then(fnames => fnames.map(fname => path.join(machineDirPath, fname)))
}
)
readdirRec(path.join(OPERATOR_DATA_DIR, 'failedQRScans'))
.then(entries => Promise.all(
entries
.filter(entry => entry.isFile())
.map(entry => entry.path)
.filter(isOld)
.map(fs.unlink)
))
.then(_.flatten)
.then(filepaths => Promise.all(filepaths.filter(isOld).map(fs.unlink)))
.catch(err => {
console.log("Error cleaning up failed QR scans:", err)
})
}
function initializeEachSchema (schemas = ['public']) {