From 0bc9e7c2e097815d4bb5164ee23e733da74fa105 Mon Sep 17 00:00:00 2001 From: Ike Saunders Date: Wed, 29 Nov 2023 18:11:05 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20rename=20static=20method=20and?= =?UTF-8?q?=20move=20to=20GdocPost?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- adminSiteServer/apiRouter.ts | 4 ++-- adminSiteServer/app.tsx | 2 +- baker/siteRenderers.tsx | 16 ++++++---------- datapage/Datapage.ts | 2 +- db/model/Gdoc/GdocBase.ts | 32 -------------------------------- db/model/Gdoc/GdocPost.ts | 30 ++++++++++++++++++++++++++++-- 6 files changed, 38 insertions(+), 48 deletions(-) diff --git a/adminSiteServer/apiRouter.ts b/adminSiteServer/apiRouter.ts index 338585d7dea..5881c6ee5e0 100644 --- a/adminSiteServer/apiRouter.ts +++ b/adminSiteServer/apiRouter.ts @@ -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 @@ -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 diff --git a/adminSiteServer/app.tsx b/adminSiteServer/app.tsx index 8839d02f5a2..bce406cd939 100644 --- a/adminSiteServer/app.tsx +++ b/adminSiteServer/app.tsx @@ -130,7 +130,7 @@ export class OwidAdminApp { const publishedExplorersBySlug = await adminExplorerServer.getAllPublishedExplorersBySlugCached() try { - const gdoc = await GdocPost.getGdocFromContentSource( + const gdoc = await GdocPost.load( req.params.id, publishedExplorersBySlug, GdocsContentSource.Gdocs diff --git a/baker/siteRenderers.tsx b/baker/siteRenderers.tsx index 746968137de..bbab28c496c 100644 --- a/baker/siteRenderers.tsx +++ b/baker/siteRenderers.tsx @@ -155,11 +155,10 @@ export const renderGdocsPageBySlug = async ( const publishedExplorersBySlug = await explorerAdminServer.getAllPublishedExplorersBySlug() - const gdocWithAttachments = - await GdocPost.getGdocFromContentSource( - gdoc.id, - publishedExplorersBySlug - ) + const gdocWithAttachments = await GdocPost.load( + gdoc.id, + publishedExplorersBySlug + ) return renderGdoc(gdocWithAttachments) } @@ -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, {} ) @@ -307,10 +306,7 @@ export const renderFrontPage = async () => { } export const renderDonatePage = async () => { - const faqsGdoc = await GdocPost.getGdocFromContentSource( - GDOCS_DONATE_FAQS_DOCUMENT_ID, - {} - ) + const faqsGdoc = await GdocPost.load(GDOCS_DONATE_FAQS_DOCUMENT_ID, {}) return renderToHtmlPage( ( + ? await GdocPost.load( googleDocId, publishedExplorersBySlug, GdocsContentSource.Gdocs diff --git a/db/model/Gdoc/GdocBase.ts b/db/model/Gdoc/GdocBase.ts index 4a0f62f9c99..e5f24e4cc90 100644 --- a/db/model/Gdoc/GdocBase.ts +++ b/db/model/Gdoc/GdocBase.ts @@ -8,8 +8,6 @@ import { import { getUrlTarget } from "@ourworldindata/components" import { LinkedChart, - GdocsContentSource, - JsonError, keyBy, excludeNull, ImageMetadata, @@ -71,8 +69,6 @@ export class GdocBase extends BaseEntity implements OwidGdocBaseInterface { () => new Promise(() => []) _filenameProperties: string[] = [] _omittableFields: string[] = [] - _loadSubclassAttachments: (gdoc: typeof this) => Promise = () => - new Promise(() => undefined) get enrichedBlockSources(): OwidEnrichedGdocBlock[][] { const enrichedBlockSources: OwidEnrichedGdocBlock[][] = [ @@ -607,34 +603,6 @@ export class GdocBase extends BaseEntity implements OwidGdocBaseInterface { this.errors = [...filenameErrors, ...linkErrors, ...subclassErrors] } - static async getGdocFromContentSource( - id: string, - publishedExplorersBySlug: Record, - contentSource?: GdocsContentSource - ): Promise { - 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 { return omit(this, [ "_enrichSubclassContent", diff --git a/db/model/Gdoc/GdocPost.ts b/db/model/Gdoc/GdocPost.ts index f25144eb8a5..8e2ef63388e 100644 --- a/db/model/Gdoc/GdocPost.ts +++ b/db/model/Gdoc/GdocPost.ts @@ -22,6 +22,8 @@ import { OwidEnrichedGdocBlock, BreadcrumbItem, RawBlockText, + GdocsContentSource, + JsonError, } from "@ourworldindata/utils" import { GDOCS_DETAILS_ON_DEMAND_ID } from "../../../settings/serverSettings.js" import { @@ -178,8 +180,32 @@ export class GdocPost extends GdocBase implements OwidGdocInterface { return errors } - _loadSubclassAttachments: (gdoc: this) => Promise = async () => { - await this.loadRelatedCharts() + static async load( + id: string, + publishedExplorersBySlug: Record, + contentSource?: GdocsContentSource + ): Promise { + 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<{