Skip to content

Commit

Permalink
🔨 move loadRelatedCharts into GdocPost
Browse files Browse the repository at this point in the history
  • Loading branch information
ikesau committed Dec 15, 2023
1 parent 1153709 commit 2c267e6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
27 changes: 0 additions & 27 deletions db/model/Gdoc/GdocBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
ManyToMany,
JoinTable,
} from "typeorm"
import { getConnection } from "../../db.js"
import { getUrlTarget } from "@ourworldindata/components"
import {
LinkedChart,
Expand All @@ -23,7 +22,6 @@ import {
Span,
EnrichedBlockResearchAndWritingLink,
traverseEnrichedSpan,
RelatedChart,
uniq,
omit,
identity,
Expand Down Expand Up @@ -94,7 +92,6 @@ export class GdocBase extends BaseEntity implements OwidGdocBaseInterface {
imageMetadata: Record<string, ImageMetadata> = {}
linkedCharts: Record<string, LinkedChart> = {}
linkedDocuments: Record<string, OwidGdocBaseInterface> = {}
relatedCharts: RelatedChart[] = []
latestDataInsights: MinimalDataInsightInterface[] = []

_getSubclassEnrichedBlocks: (gdoc: typeof this) => OwidEnrichedGdocBlock[] =
Expand Down Expand Up @@ -571,29 +568,6 @@ export class GdocBase extends BaseEntity implements OwidGdocBaseInterface {
}
}

async loadRelatedCharts(): Promise<void> {
if (!this.tags.length || !this.hasAllChartsBlock) return

const connection = await getConnection()
const relatedCharts = await connection.query(
`
SELECT DISTINCT
charts.config->>"$.slug" AS slug,
charts.config->>"$.title" AS title,
charts.config->>"$.variantName" AS variantName,
chart_tags.keyChartLevel
FROM charts
INNER JOIN chart_tags ON charts.id=chart_tags.chartId
WHERE chart_tags.tagId IN (?)
AND charts.config->>"$.isPublished" = "true"
ORDER BY title ASC
`,
[this.tags.map((tag) => tag.id)]
)

this.relatedCharts = relatedCharts
}

async fetchAndEnrichGdoc(): Promise<void> {
const docsClient = google.docs({
version: "v1",
Expand Down Expand Up @@ -694,7 +668,6 @@ export class GdocBase extends BaseEntity implements OwidGdocBaseInterface {
await this.loadLinkedDocuments()
await this.loadImageMetadata()
await this.loadLinkedCharts(publishedExplorersBySlug)
await this.loadRelatedCharts()
await this._loadSubclassAttachments()
await this.validate(publishedExplorersBySlug)
}
Expand Down
30 changes: 30 additions & 0 deletions db/model/Gdoc/GdocPost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
OwidGdocType,
OwidEnrichedGdocBlock,
RawBlockText,
RelatedChart,
} from "@ourworldindata/utils"
import { GDOCS_DETAILS_ON_DEMAND_ID } from "../../../settings/serverSettings.js"
import {
Expand All @@ -22,6 +23,7 @@ import { ADMIN_BASE_URL } from "../../../settings/clientSettings.js"
import { parseDetails, parseFaqs } from "./rawToEnriched.js"
import { htmlToEnrichedTextBlock } from "./htmlToEnriched.js"
import { GdocBase } from "./GdocBase.js"
import { getConnection } from "../../db.js"

@Entity("posts_gdocs")
export class GdocPost extends GdocBase implements OwidGdocPostInterface {
Expand All @@ -35,6 +37,7 @@ export class GdocPost extends GdocBase implements OwidGdocPostInterface {
}

linkedDocuments: Record<string, OwidGdocPostInterface> = {}
relatedCharts: RelatedChart[] = []
_filenameProperties = ["cover-image", "featured-image"]

_getSubclassEnrichedBlocks = (gdoc: this): OwidEnrichedGdocBlock[] => {
Expand Down Expand Up @@ -144,6 +147,33 @@ export class GdocPost extends GdocBase implements OwidGdocPostInterface {
return errors
}

_loadSubclassAttachments: () => Promise<void> = async () => {
await this.loadRelatedCharts()
}

async loadRelatedCharts(): Promise<void> {
if (!this.tags.length || !this.hasAllChartsBlock) return

const connection = await getConnection()
const relatedCharts = await connection.query(
`
SELECT DISTINCT
charts.config->>"$.slug" AS slug,
charts.config->>"$.title" AS title,
charts.config->>"$.variantName" AS variantName,
chart_tags.keyChartLevel
FROM charts
INNER JOIN chart_tags ON charts.id=chart_tags.chartId
WHERE chart_tags.tagId IN (?)
AND charts.config->>"$.isPublished" = "true"
ORDER BY title ASC
`,
[this.tags.map((tag) => tag.id)]
)

this.relatedCharts = relatedCharts
}

static async getDetailsOnDemandGdoc(): Promise<{
details: DetailDictionary
parseErrors: ParseError[]
Expand Down

0 comments on commit 2c267e6

Please sign in to comment.