Skip to content

Commit

Permalink
enhance: persist query params when saving narrative view (#4260)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelgerber authored Dec 10, 2024
1 parent d47497f commit 4e3bf99
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 14 deletions.
42 changes: 28 additions & 14 deletions adminSiteServer/apiRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,15 @@ import {
DbInsertChartView,
CHART_VIEW_PROPS_TO_PERSIST,
CHART_VIEW_PROPS_TO_OMIT,
JsonString,
} from "@ourworldindata/types"
import { uuidv7 } from "uuidv7"
import {
migrateGrapherConfigToLatestVersion,
getVariableDataRoute,
getVariableMetadataRoute,
defaultGrapherConfig,
grapherConfigToQueryParams,
} from "@ourworldindata/grapher"
import { getDatasetById, setTagsForDataset } from "../db/model/Dataset.js"
import { getUserById, insertUser, updateUser } from "../db/model/User.js"
Expand Down Expand Up @@ -3291,7 +3293,7 @@ postRouteWithRWTransaction(apiRouter, "/tagGraph", async (req, res, trx) => {
res.send({ success: true })
})

const createPatchConfigAndFullConfigForChartView = async (
const createPatchConfigAndQueryParamsForChartView = async (
knex: db.KnexReadonlyTransaction,
parentChartId: number,
config: GrapherInterface
Expand All @@ -3316,8 +3318,10 @@ const createPatchConfigAndFullConfigForChartView = async (
...pick(fullConfigIncludingDefaults, CHART_VIEW_PROPS_TO_PERSIST),
}

const queryParams = grapherConfigToQueryParams(config)

const fullConfig = mergeGrapherConfigs(parentChartConfig, patchConfigToSave)
return { patchConfig: patchConfigToSave, fullConfig }
return { patchConfig: patchConfigToSave, fullConfig, queryParams }
}

getRouteWithROTransaction(apiRouter, "/chartViews", async (req, res, trx) => {
Expand Down Expand Up @@ -3378,10 +3382,11 @@ getRouteWithROTransaction(
> & {
lastEditedByUser: string
chartConfigId: string
configFull: string
configPatch: string
configFull: JsonString
configPatch: JsonString
parentChartId: number
parentConfigFull: string
parentConfigFull: JsonString
queryParamsForParentChart: JsonString
}

const row = await db.knexRawFirst<ChartViewRow>(
Expand All @@ -3396,7 +3401,8 @@ getRouteWithROTransaction(
cc.full as configFull,
cc.patch as configPatch,
cv.parentChartId,
pcc.full as parentConfigFull
pcc.full as parentConfigFull,
cv.queryParamsForParentChart
FROM chart_views cv
JOIN chart_configs cc ON cv.chartConfigId = cc.id
JOIN charts pc ON cv.parentChartId = pc.id
Expand All @@ -3416,6 +3422,9 @@ getRouteWithROTransaction(
configFull: parseChartConfig(row.configFull),
configPatch: parseChartConfig(row.configPatch),
parentConfigFull: parseChartConfig(row.parentConfigFull),
queryParamsForParentChart: JSON.parse(
row.queryParamsForParentChart
),
}

return chartView
Expand All @@ -3432,8 +3441,8 @@ postRouteWithRWTransaction(apiRouter, "/chartViews", async (req, res, trx) => {
throw new JsonError("Invalid request", 400)
}

const { patchConfig, fullConfig } =
await createPatchConfigAndFullConfigForChartView(
const { patchConfig, fullConfig, queryParams } =
await createPatchConfigAndQueryParamsForChartView(
trx,
parentChartId,
rawConfig
Expand All @@ -3452,6 +3461,7 @@ postRouteWithRWTransaction(apiRouter, "/chartViews", async (req, res, trx) => {
parentChartId,
lastEditedByUserId: res.locals.user.id,
chartConfigId: chartConfigId,
queryParamsForParentChart: JSON.stringify(queryParams),
}
const result = await trx.table(ChartViewsTableName).insert(insertRow)
const [resultId] = result
Expand Down Expand Up @@ -3481,8 +3491,8 @@ putRouteWithRWTransaction(
throw new JsonError(`No chart view found for id ${id}`, 404)
}

const { patchConfig, fullConfig } =
await createPatchConfigAndFullConfigForChartView(
const { patchConfig, fullConfig, queryParams } =
await createPatchConfigAndQueryParamsForChartView(
trx,
existingRow.parentChartId,
rawConfig
Expand All @@ -3496,10 +3506,14 @@ putRouteWithRWTransaction(
)

// update chart_views
await trx.table(ChartViewsTableName).where({ id }).update({
updatedAt: new Date(),
lastEditedByUserId: res.locals.user.id,
})
await trx
.table(ChartViewsTableName)
.where({ id })
.update({
updatedAt: new Date(),
lastEditedByUserId: res.locals.user.id,
queryParamsForParentChart: JSON.stringify(queryParams),
})

return { success: true }
}
Expand Down
17 changes: 17 additions & 0 deletions db/migration/1733151294656-ChartViewsAddQueryParam.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { MigrationInterface, QueryRunner } from "typeorm"

export class ChartViewsAddQueryParam1733151294656
implements MigrationInterface
{
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`-- sql
ALTER TABLE chart_views ADD COLUMN queryParamsForParentChart JSON NULL AFTER parentChartId;
`)
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`-- sql
ALTER TABLE chart_views DROP COLUMN queryParamsForParentChart;
`)
}
}
1 change: 1 addition & 0 deletions packages/@ourworldindata/grapher/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export {
getSelectedEntityNamesParam,
generateSelectedEntityNamesParam,
} from "./core/EntityUrlBuilder"
export { grapherConfigToQueryParams } from "./core/GrapherUrl.js"
export {
type SlideShowManager,
SlideShowController,
Expand Down
2 changes: 2 additions & 0 deletions packages/@ourworldindata/types/src/dbTypes/ChartViews.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { JsonString } from "../domainTypes/Various.js"
import { GrapherInterface } from "../grapherTypes/GrapherTypes.js"

export const ChartViewsTableName = "chart_views"
Expand All @@ -6,6 +7,7 @@ export interface DbInsertChartView {
name: string
chartConfigId: string
parentChartId: number
queryParamsForParentChart?: JsonString | null
createdAt?: Date | null
updatedAt?: Date | null
lastEditedByUserId: number
Expand Down

0 comments on commit 4e3bf99

Please sign in to comment.