Skip to content

Commit

Permalink
🔨 rename static method and move to GdocPost
Browse files Browse the repository at this point in the history
  • Loading branch information
ikesau committed Nov 29, 2023
1 parent e7f5b8a commit 0bc9e7c
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 48 deletions.
4 changes: 2 additions & 2 deletions adminSiteServer/apiRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2443,7 +2443,7 @@ apiRouter.get("/gdocs/:id", async (req, res) => {
const publishedExplorersBySlug =
await explorerAdminServer.getAllPublishedExplorersBySlugCached()

const gdoc = await GdocPost.getGdocFromContentSource(
const gdoc = await GdocPost.load(
id,
publishedExplorersBySlug,
contentSource
Expand Down Expand Up @@ -2476,7 +2476,7 @@ apiRouter.put("/gdocs/:id", async (req, res) => {
const publishedExplorersBySlug =
await explorerAdminServer.getAllPublishedExplorersBySlugCached()

const initData = await GdocPost.getGdocFromContentSource(
const initData = await GdocPost.load(
id,
publishedExplorersBySlug,
GdocsContentSource.Gdocs
Expand Down
2 changes: 1 addition & 1 deletion adminSiteServer/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export class OwidAdminApp {
const publishedExplorersBySlug =
await adminExplorerServer.getAllPublishedExplorersBySlugCached()
try {
const gdoc = await GdocPost.getGdocFromContentSource<GdocPost>(
const gdoc = await GdocPost.load(
req.params.id,
publishedExplorersBySlug,
GdocsContentSource.Gdocs
Expand Down
16 changes: 6 additions & 10 deletions baker/siteRenderers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,10 @@ export const renderGdocsPageBySlug = async (
const publishedExplorersBySlug =
await explorerAdminServer.getAllPublishedExplorersBySlug()

const gdocWithAttachments =
await GdocPost.getGdocFromContentSource<GdocPost>(
gdoc.id,
publishedExplorersBySlug
)
const gdocWithAttachments = await GdocPost.load(
gdoc.id,
publishedExplorersBySlug
)

return renderGdoc(gdocWithAttachments)
}
Expand Down Expand Up @@ -241,7 +240,7 @@ export const renderFrontPage = async () => {

let featuredWork: IndexPost[]
try {
const frontPageConfigGdoc = await GdocPost.getGdocFromContentSource(
const frontPageConfigGdoc = await GdocPost.load(
GDOCS_HOMEPAGE_CONFIG_DOCUMENT_ID,
{}
)
Expand Down Expand Up @@ -307,10 +306,7 @@ export const renderFrontPage = async () => {
}

export const renderDonatePage = async () => {
const faqsGdoc = await GdocPost.getGdocFromContentSource<GdocPost>(
GDOCS_DONATE_FAQS_DOCUMENT_ID,
{}
)
const faqsGdoc = await GdocPost.load(GDOCS_DONATE_FAQS_DOCUMENT_ID, {})

return renderToHtmlPage(
<DonatePage
Expand Down
2 changes: 1 addition & 1 deletion datapage/Datapage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export const getDatapageGdoc = async (
isPreviewing &&
publishedExplorersBySlug &&
OwidGoogleAuth.areGdocAuthKeysSet()
? await GdocPost.getGdocFromContentSource<GdocPost>(
? await GdocPost.load(
googleDocId,
publishedExplorersBySlug,
GdocsContentSource.Gdocs
Expand Down
32 changes: 0 additions & 32 deletions db/model/Gdoc/GdocBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import {
import { getUrlTarget } from "@ourworldindata/components"
import {
LinkedChart,
GdocsContentSource,
JsonError,
keyBy,
excludeNull,
ImageMetadata,
Expand Down Expand Up @@ -71,8 +69,6 @@ export class GdocBase extends BaseEntity implements OwidGdocBaseInterface {
() => new Promise(() => [])
_filenameProperties: string[] = []
_omittableFields: string[] = []
_loadSubclassAttachments: (gdoc: typeof this) => Promise<void> = () =>
new Promise(() => undefined)

get enrichedBlockSources(): OwidEnrichedGdocBlock[][] {
const enrichedBlockSources: OwidEnrichedGdocBlock[][] = [
Expand Down Expand Up @@ -607,34 +603,6 @@ export class GdocBase extends BaseEntity implements OwidGdocBaseInterface {
this.errors = [...filenameErrors, ...linkErrors, ...subclassErrors]
}

static async getGdocFromContentSource<T extends GdocBase>(
id: string,
publishedExplorersBySlug: Record<string, any>,
contentSource?: GdocsContentSource
): Promise<T> {
const gdoc = await GdocBase.findOne({
where: {
id,
},
relations: ["tags"],
})

if (!gdoc) throw new JsonError(`No Google Doc with id ${id} found`)

if (contentSource === GdocsContentSource.Gdocs) {
await gdoc.fetchAndEnrichGdoc()
}

await gdoc.loadLinkedDocuments()
await gdoc.loadImageMetadata()
await gdoc.loadLinkedCharts(publishedExplorersBySlug)
await gdoc._loadSubclassAttachments(gdoc)

await gdoc.validate(publishedExplorersBySlug)

return gdoc as T
}

toJSON(): Record<string, any> {
return omit(this, [
"_enrichSubclassContent",
Expand Down
30 changes: 28 additions & 2 deletions db/model/Gdoc/GdocPost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import {
OwidEnrichedGdocBlock,
BreadcrumbItem,
RawBlockText,
GdocsContentSource,
JsonError,
} from "@ourworldindata/utils"
import { GDOCS_DETAILS_ON_DEMAND_ID } from "../../../settings/serverSettings.js"
import {
Expand Down Expand Up @@ -178,8 +180,32 @@ export class GdocPost extends GdocBase implements OwidGdocInterface {
return errors
}

_loadSubclassAttachments: (gdoc: this) => Promise<void> = async () => {
await this.loadRelatedCharts()
static async load(
id: string,
publishedExplorersBySlug: Record<string, any>,
contentSource?: GdocsContentSource
): Promise<GdocPost> {
const gdoc = await GdocPost.findOne({
where: {
id,
},
relations: ["tags"],
})

if (!gdoc) throw new JsonError(`No Google Doc with id ${id} found`)

if (contentSource === GdocsContentSource.Gdocs) {
await gdoc.fetchAndEnrichGdoc()
}

await gdoc.loadLinkedDocuments()
await gdoc.loadImageMetadata()
await gdoc.loadLinkedCharts(publishedExplorersBySlug)
await gdoc.loadRelatedCharts()

await gdoc.validate(publishedExplorersBySlug)

return gdoc
}

static async getDetailsOnDemandGdoc(): Promise<{
Expand Down

0 comments on commit 0bc9e7c

Please sign in to comment.