Skip to content

Commit

Permalink
🐛 fix lightning updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ikesau committed Oct 12, 2023
1 parent 5d6a89c commit ecb6c3c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
42 changes: 31 additions & 11 deletions adminSiteClient/gdocsDeploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const checkIsLightningUpdate = (
"imageMetadata",
"linkedCharts",
"errors",
"revisionId",
]

const lightningContentProps: Array<keyof OwidGdocContent> = [
Expand All @@ -50,19 +51,26 @@ export const checkIsLightningUpdate = (
...lightningContentProps.map((prop) => `content.${prop}`),
]

// When this function is called from server-side code and a Gdoc object
// is passed in, the omit() call will surface Gdoc class members. The
// comparison will then fail if the other operand in the comparison is
// an OwidGdocInterface object. To avoid this, we spread into new objects
// in order to compare the same types. Tags are stringified to avoid a similar
// issue with Dates and date strings.
const prevOmitted = omit(
{ ...prevGdoc, tags: nextGdoc.tags?.map((tag) => JSON.stringify(tag)) },
lightningProps
)
const nextOmitted = omit(
{ ...nextGdoc, tags: prevGdoc.tags?.map((tag) => JSON.stringify(tag)) },
lightningProps
)

return (
hasChanges &&
prevGdoc.published &&
nextGdoc.published &&
// When this function is called from server-side code and a Gdoc object
// is passed in, the omit() call will surface Gdoc class members. The
// comparison will then fail if the other operand in the comparison is
// an OwidGdocInterface object. To avoid this, we spread into new objects
// in order to compare the same types.
isEqual(
omit({ ...prevGdoc }, lightningProps),
omit({ ...nextGdoc }, lightningProps)
)
isEqual(prevOmitted, nextOmitted)
)
}

Expand All @@ -71,6 +79,18 @@ export const checkHasChanges = (
nextGdoc: OwidGdocInterface
) =>
!isEqual(
omit(prevGdoc, GDOC_DIFF_OMITTABLE_PROPERTIES),
omit(nextGdoc, GDOC_DIFF_OMITTABLE_PROPERTIES)
omit(
{
...prevGdoc,
tags: prevGdoc.tags?.map((tag) => JSON.stringify(tag)),
},
GDOC_DIFF_OMITTABLE_PROPERTIES
),
omit(
{
...nextGdoc,
tags: nextGdoc.tags?.map((tag) => JSON.stringify(tag)),
},
GDOC_DIFF_OMITTABLE_PROPERTIES
)
)
7 changes: 6 additions & 1 deletion adminSiteServer/apiRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2491,7 +2491,12 @@ apiRouter.put("/gdocs/:id", async (req, res) => {
return updated
}

const prevGdoc = await Gdoc.findOneBy({ id })
const prevGdoc = await Gdoc.findOne({
where: {
id: id,
},
relations: ["tags"],
})
if (!prevGdoc) throw new JsonError(`No Google Doc with id ${id} found`)

const nextGdoc = dataSource
Expand Down

0 comments on commit ecb6c3c

Please sign in to comment.