Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance: persist query params when saving narrative view #4260

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -3283,7 +3285,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 @@ -3308,8 +3310,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 @@ -3370,10 +3374,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 @@ -3388,7 +3393,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 @@ -3408,6 +3414,9 @@ getRouteWithROTransaction(
configFull: parseChartConfig(row.configFull),
configPatch: parseChartConfig(row.configPatch),
parentConfigFull: parseChartConfig(row.parentConfigFull),
queryParamsForParentChart: JSON.parse(
row.queryParamsForParentChart
),
}

return chartView
Expand All @@ -3424,8 +3433,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 @@ -3444,6 +3453,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 @@ -3473,8 +3483,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 @@ -3488,10 +3498,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
Loading