All files / owid-grapher/explorer/urlMigrations LegacyCovidUrlMigration.ts

87.23% Statements 82/94
59.09% Branches 13/22
100% Functions 4/4
87.23% Lines 82/94

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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 1321x 1x 1x 1x 1x 1x 1x 1x 8x 3x 3x   5x   5x       5x   5x     5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 6x 1x 6x   5x   5x     6x 6x 1x 1x 1x 1x 5x 5x 5x 1x 1x 6x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 2x 3x                                                                            
import { omit } from "../../clientUtils/Util"
import { QueryParams } from "../../clientUtils/urls/UrlUtils"
import { ExplorerUrlMigrationSpec } from "./ExplorerUrlMigrations"
import { legacyToCurrentGrapherUrl } from "../../grapher/core/GrapherUrlMigrations"
import { Url } from "../../clientUtils/urls/Url"
import { EXPLORERS_ROUTE_FOLDER } from "../ExplorerConstants"
 
const covidMetricFromLegacyQueryParams = (queryParams: QueryParams) => {
    if (queryParams.casesMetric) {
        return "Confirmed cases"
    } else if (queryParams.deathsMetric) {
        return "Confirmed deaths"
    } else if (queryParams.cfrMetric) {
        return "Case fatality rate"
    } else if (queryParams.testsMetric) {
        return "Tests"
    } else if (queryParams.testsPerCaseMetric) {
        return "Tests per confirmed case"
    } else if (queryParams.positiveTestRate) {
        return "Share of positive tests"
    } else if (queryParams.vaccinationsMetric) {
        return "Vaccinations"
    }
    return undefined
}
 
const legacyIntervalToNewValue = {
    daily: "New per day",
    weekly: "Weekly",
    total: "Cumulative",
    smoothed: "7-day rolling average",
    biweekly: "Biweekly",
    weeklyChange: "Weekly change",
    biweeklyChange: "Biweekly change",
}
 
const covidIntervalFromLegacyQueryParams = (queryParams: QueryParams) => {
    let legacyInterval: string | undefined = undefined
 
    // Early on, the query string was a few booleans like dailyFreq=true.
    // Now it is a single 'interval'. This transformation is for backward compat.
    if (queryParams.interval) {
        legacyInterval = queryParams.interval
    } else if (queryParams.totalFreq) {
        legacyInterval = "total"
    } else if (queryParams.dailyFreq) {
        legacyInterval = "daily"
    } else if (queryParams.smoothing) {
        legacyInterval = "smoothed"
    }
 
    if (legacyInterval) {
        return legacyIntervalToNewValue[
            legacyInterval as keyof typeof legacyIntervalToNewValue
        ]
    }
 
    return undefined
}
 
const boolParamToString = (bool: boolean | string | undefined) =>
    bool ? "true" : "false"
 
const legacyToCurrentCovidQueryParams = (
    queryParams: QueryParams,
    baseQueryParams: QueryParams = {}
): QueryParams => {
    const restQueryParams = omit(
        {
            ...baseQueryParams,
            ...queryParams,
        },
        "casesMetric",
        "deathsMetric",
        "cfrMetric",
        "testsMetric",
        "testsPerCaseMetric",
        "positiveTestRate",
        "vaccinationsMetric",
        "interval",
        "smoothing",
        "totalFreq",
        "dailyFreq",
        "aligned",
        "perCapita"
    )
 
    const urlContainsMetric = !!covidMetricFromLegacyQueryParams(queryParams)
 
    const explorerQueryParams: QueryParams = {
        Metric:
            covidMetricFromLegacyQueryParams(queryParams) ??
            covidMetricFromLegacyQueryParams(baseQueryParams),
        Interval:
            covidIntervalFromLegacyQueryParams(queryParams) ??
            covidIntervalFromLegacyQueryParams(baseQueryParams),
        "Align outbreaks": urlContainsMetric
            ? boolParamToString(queryParams.aligned)
            : boolParamToString(baseQueryParams.aligned),
        "Relative to Population": urlContainsMetric
            ? boolParamToString(queryParams.perCapita)
            : boolParamToString(baseQueryParams.perCapita),
    }
 
    return {
        ...restQueryParams,
        ...explorerQueryParams,
    }
}
 
export const legacyCovidMigrationSpec: ExplorerUrlMigrationSpec = {
    explorerSlug: "coronavirus-data-explorer",
    migrateUrl: (url, baseQueryStr) => {
        // Migrate the Grapher query params in both URLs
        const [explorerUrl, baseUrl] = [
            url,
            Url.fromQueryStr(baseQueryStr),
        ].map(legacyToCurrentGrapherUrl)
 
        return explorerUrl
            .setQueryParams(
                legacyToCurrentCovidQueryParams(
                    explorerUrl.queryParams,
                    baseUrl.queryParams
                )
            )
            .update({
                pathname: `/${EXPLORERS_ROUTE_FOLDER}/coronavirus-data-explorer`,
            })
    },
}