Skip to content

Commit

Permalink
🔨 add chart view map to public admin router
Browse files Browse the repository at this point in the history
  • Loading branch information
sophiamersmann committed Jan 30, 2025
1 parent df7af51 commit 7f646d9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions adminSiteServer/functionalRouterHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FunctionalRouter } from "./FunctionalRouter.js"
import { Request, Response } from "express"
import * as db from "../db/db.js"

export function getRouteWithROTransaction<T>(
router: FunctionalRouter,
targetPath: string,
Expand Down
16 changes: 16 additions & 0 deletions adminSiteServer/publicApiRouter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import e from "express"
import { FunctionalRouter } from "./FunctionalRouter.js"
import { Request, Response } from "./authentication.js"
import * as db from "../db/db.js"
import { getChartViewNameConfigMap } from "../db/model/ChartView.js"
import { getRouteWithROTransaction } from "./functionalRouterHelpers.js"

export const publicApiRouter = new FunctionalRouter()

Expand All @@ -22,3 +25,16 @@ publicApiRouter.router.get("/health", async (req: Request, res: Response) => {
console.error("Error at health endpoint", e)
}
})

getRouteWithROTransaction(
publicApiRouter,
"/chartViewMap",
async (
_req: Request,
_res: e.Response<any, Record<string, any>>,
trx: db.KnexReadonlyTransaction
) => {
const chartViewMap = await getChartViewNameConfigMap(trx)
return chartViewMap
}
)
17 changes: 16 additions & 1 deletion db/model/ChartView.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { ChartViewInfo, JsonString } from "@ourworldindata/types"
import {
ChartViewInfo,
DbPlainChartView,
JsonString,
} from "@ourworldindata/types"
import * as db from "../db.js"

export const getChartViewsInfo = async (
Expand Down Expand Up @@ -32,3 +36,14 @@ JOIN chart_configs pcc on pc.configId = pcc.id
queryParamsForParentChart: JSON.parse(row.queryParamsForParentChart),
}))
}

export const getChartViewNameConfigMap = async (
knex: db.KnexReadonlyTransaction
): Promise<
Record<DbPlainChartView["name"], DbPlainChartView["chartConfigId"]>
> => {
const rows = await db.knexRaw<
Pick<DbPlainChartView, "name" | "chartConfigId">
>(knex, `SELECT name, chartConfigId FROM chart_views`)
return Object.fromEntries(rows.map((row) => [row.name, row.chartConfigId]))
}

0 comments on commit 7f646d9

Please sign in to comment.