Skip to content

Commit

Permalink
✨ store patches in chart_revisions
Browse files Browse the repository at this point in the history
  • Loading branch information
sophiamersmann authored and danyx23 committed Aug 12, 2024
1 parent 36270cd commit 3b80217
Showing 1 changed file with 7 additions and 15 deletions.
22 changes: 7 additions & 15 deletions adminSiteServer/apiRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ const expectChartById = async (
const saveNewChart = async (
knex: db.KnexReadWriteTransaction,
{ config, user }: { config: GrapherInterface; user: DbPlainUser }
): Promise<{ patchConfig: GrapherInterface; fullConfig: GrapherInterface }> => {
): Promise<GrapherInterface> => {
// if the schema version is missing, assume it's the latest
if (!config["$schema"]) {
config["$schema"] = defaultGrapherConfig["$schema"]
Expand Down Expand Up @@ -324,7 +324,7 @@ const saveNewChart = async (
[chartId, chartId, chartId]
)

return { patchConfig, fullConfig }
return patchConfig
}

const updateExistingChart = async (
Expand All @@ -334,7 +334,7 @@ const updateExistingChart = async (
user,
chartId,
}: { config: GrapherInterface; user: DbPlainUser; chartId: number }
): Promise<{ patchConfig: GrapherInterface; fullConfig: GrapherInterface }> => {
): Promise<GrapherInterface> => {
// make sure that the id of the incoming config matches the chart id
config.id = chartId

Expand Down Expand Up @@ -373,10 +373,7 @@ const updateExistingChart = async (
[new Date(), user.id, chartId]
)

return {
patchConfig,
fullConfig,
}
return patchConfig
}

const saveGrapher = async (
Expand Down Expand Up @@ -460,31 +457,26 @@ const saveGrapher = async (

// Execute the actual database update or creation
let chartId: number
let newFullConfig: GrapherInterface
if (existingConfig) {
chartId = existingConfig.id!
const newConfigPair = await updateExistingChart(knex, {
newConfig = await updateExistingChart(knex, {
config: newConfig,
user,
chartId,
})
newConfig = newConfigPair.patchConfig
newFullConfig = newConfigPair.fullConfig
} else {
const newConfigPair = await saveNewChart(knex, {
newConfig = await saveNewChart(knex, {
config: newConfig,
user,
})
newConfig = newConfigPair.patchConfig
newFullConfig = newConfigPair.fullConfig
chartId = newConfig.id!
}

// Record this change in version history
const chartRevisionLog = {
chartId: chartId as number,
userId: user.id,
config: serializeChartConfig(newFullConfig),
config: serializeChartConfig(newConfig),
createdAt: new Date(),
updatedAt: new Date(),
} satisfies DbInsertChartRevision
Expand Down

0 comments on commit 3b80217

Please sign in to comment.