All files / app/codeCharta/ui/customConfigs/addCustomConfigButton/addCustomConfigDialog/downloadAndPurgeConfigs downloadAndCollectPurgeableConfigs.ts

100% Statements 17/17
85.71% Branches 6/7
100% Functions 1/1
100% Lines 17/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34  10x   10x   10x 3x 3x   3x   3x   3x 4x   1x       4x 4x 1x 1x       3x 1x     3x    
import { CustomConfig, ExportCustomConfig } from "../../../../../model/customConfig/customConfig.api.model"
import { CustomConfigHelper } from "../../../../../util/customConfigHelper"
 
const customConfigAgeLimitInMonths = 6
 
export function downloadAndCollectPurgeableConfigs() {
    const purgeableConfigs = new Set<CustomConfig>()
    const customConfigs = CustomConfigHelper.getCustomConfigs()
 
    const downloadableConfigs: Map<string, ExportCustomConfig> = new Map()
 
    const millisecondsPerMonth = 1000 * 60 * 60 * 24 * 30
 
    for (const [key, value] of customConfigs.entries()) {
        if (value?.creationTime === undefined) {
            // Fallback, if creationTime property is not present. This can happen because it was released later.
            value.creationTime = Date.now()
        }
 
        // Download e.g. 6 month old or older Configs.
        const ageInMonth = (Date.now() - value.creationTime) / millisecondsPerMonth
        if (ageInMonth >= customConfigAgeLimitInMonths) {
            downloadableConfigs.set(key, CustomConfigHelper.createExportCustomConfigFromConfig(value))
            purgeableConfigs.add(value)
        }
    }
 
    if (downloadableConfigs.size > 0) {
        CustomConfigHelper.downloadCustomConfigs(downloadableConfigs)
    }
 
    return purgeableConfigs
}