From 7fd998aa910fb4d213089bcab0fb75cf094bee03 Mon Sep 17 00:00:00 2001 From: sophiamersmann Date: Wed, 21 Aug 2024 14:24:18 +0200 Subject: [PATCH] Clean up --- adminSiteServer/apiRouter.ts | 24 ++++--------------- db/model/ChartConfigs.ts | 2 +- db/model/Variable.ts | 16 +++++++++---- .../grapher/src/core/Grapher.tsx | 3 +++ .../src/schema/grapher-schema.004.yaml | 2 +- .../{ChartXParents.ts => ChartsXParents.ts} | 0 6 files changed, 20 insertions(+), 27 deletions(-) rename packages/@ourworldindata/types/src/dbTypes/{ChartXParents.ts => ChartsXParents.ts} (100%) diff --git a/adminSiteServer/apiRouter.ts b/adminSiteServer/apiRouter.ts index fb671245ac0..d1d64ce5ea0 100644 --- a/adminSiteServer/apiRouter.ts +++ b/adminSiteServer/apiRouter.ts @@ -300,16 +300,6 @@ const saveNewChart = async ( shouldInherit = false, }: { config: GrapherInterface; user: DbPlainUser; shouldInherit?: boolean } ): Promise => { - // if the schema version is missing, assume it's the latest - if (!config.$schema) { - config.$schema = defaultGrapherConfig.$schema - } - - // if isPublished is missing, add it - if (!config.isPublished) { - config.isPublished = false - } - // grab the parent of the chart if inheritance should be enabled const parent = shouldInherit ? await getParentByChartConfig(knex, config) @@ -380,16 +370,6 @@ const updateExistingChart = async ( // make sure that the id of the incoming config matches the chart id config.id = chartId - // if the schema version is missing, assume it's the latest - if (!config.$schema) { - config.$schema = defaultGrapherConfig.$schema - } - - // if isPublished is missing, add it - if (!config.isPublished) { - config.isPublished = false - } - // if inheritance is enabled, grab the parent from its config const shouldInherit = params.shouldInherit ?? @@ -531,6 +511,10 @@ const saveGrapher = async ( existingConfig?.$schema ?? defaultGrapherConfig.$schema + // add the isPublished field if is missing + newConfig.isPublished = + newConfig.isPublished ?? existingConfig?.isPublished ?? false + // Execute the actual database update or creation let chartId: number if (existingConfig) { diff --git a/db/model/ChartConfigs.ts b/db/model/ChartConfigs.ts index f66d3ff7eef..921edc545d6 100644 --- a/db/model/ChartConfigs.ts +++ b/db/model/ChartConfigs.ts @@ -46,7 +46,7 @@ export async function updateExistingFullConfig( await updateExistingConfig(knex, { ...params, column: "full" }) } -export async function updateExistingConfig( +async function updateExistingConfig( knex: db.KnexReadWriteTransaction, { column, diff --git a/db/model/Variable.ts b/db/model/Variable.ts index f3a86eea182..309c8f9c96b 100644 --- a/db/model/Variable.ts +++ b/db/model/Variable.ts @@ -319,7 +319,7 @@ export async function updateGrapherConfigAdminOfVariable( trx: db.KnexReadWriteTransaction, variable: VariableWithGrapherConfigs, config: GrapherInterface -): Promise { +): Promise<{ chartId: number; isPublished: boolean }[]> { const { variableId } = variable const patchConfigAdmin = makeConfigValidForIndicator({ @@ -347,10 +347,16 @@ export async function updateGrapherConfigAdminOfVariable( }) } - await updateAllChartsThatInheritFromIndicator(trx, variableId, { - patchConfigETL: variable.etl?.patchConfig ?? {}, - patchConfigAdmin: patchConfigAdmin, - }) + const updatedCharts = await updateAllChartsThatInheritFromIndicator( + trx, + variableId, + { + patchConfigETL: variable.etl?.patchConfig ?? {}, + patchConfigAdmin: patchConfigAdmin, + } + ) + + return updatedCharts } // TODO: these are domain functions and should live somewhere else diff --git a/packages/@ourworldindata/grapher/src/core/Grapher.tsx b/packages/@ourworldindata/grapher/src/core/Grapher.tsx index 295c882add3..e985c3df065 100644 --- a/packages/@ourworldindata/grapher/src/core/Grapher.tsx +++ b/packages/@ourworldindata/grapher/src/core/Grapher.tsx @@ -555,6 +555,9 @@ export class Grapher updatePersistables(this, obj) + // Regression fix: some legacies have this set to Null. Todo: clean DB. + if (obj.originUrl === null) this.originUrl = "" + // update selection if (obj.selectedEntityNames) this.selection.setSelectedEntities(obj.selectedEntityNames) diff --git a/packages/@ourworldindata/grapher/src/schema/grapher-schema.004.yaml b/packages/@ourworldindata/grapher/src/schema/grapher-schema.004.yaml index e8e27537cb8..625bff5fafd 100644 --- a/packages/@ourworldindata/grapher/src/schema/grapher-schema.004.yaml +++ b/packages/@ourworldindata/grapher/src/schema/grapher-schema.004.yaml @@ -15,7 +15,7 @@ properties: description: Url of the concrete schema version to use to validate this document format: uri default: "https://files.ourworldindata.org/schemas/grapher-schema.004.json" - # for now, we only use the schema to validate configs in our database. + # for now, we only validate configs in our database using this schema. # since we expect all configs in our database to be valid against the latest schema, # we restrict the $schema field to a single value, the latest schema version. # if we ever need to validate configs against multiple schema versions, diff --git a/packages/@ourworldindata/types/src/dbTypes/ChartXParents.ts b/packages/@ourworldindata/types/src/dbTypes/ChartsXParents.ts similarity index 100% rename from packages/@ourworldindata/types/src/dbTypes/ChartXParents.ts rename to packages/@ourworldindata/types/src/dbTypes/ChartsXParents.ts