Skip to content

Commit dd1aed1

Browse files
authored
Revert "chore: add check for immediate children (#1300)" (#1314)
This reverts commit 21a3c21.
1 parent 3d21c1a commit dd1aed1

File tree

2 files changed

+39
-82
lines changed

2 files changed

+39
-82
lines changed

apps/studio/src/server/modules/resource/resource.service.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import type {
1717
import type { SearchResultResource } from "./resource.types"
1818
import type { ResourceItemContent } from "~/schemas/resource"
1919
import { INDEX_PAGE_PERMALINK } from "~/constants/sitemap"
20-
import { CHILD_PAGE_RESOURCE_TYPES, getSitemapTree } from "~/utils/sitemap"
20+
import { getSitemapTree } from "~/utils/sitemap"
2121
import { logPublishEvent } from "../audit/audit.service"
2222
import { publishSite } from "../aws/codebuild.service"
2323
import { db, jsonb, ResourceType, sql } from "../database"
@@ -362,7 +362,7 @@ export const getLocalisedSitemap = async (
362362
.leftJoin("Blob as published", "Version.blobId", "published.id")
363363
.select(() => [headerSql, thumbnailSql, ...defaultResourceSelect]),
364364
)
365-
.with("childCousinPages", (eb) => {
365+
.with("childCousinIndexPages", (eb) => {
366366
return eb
367367
.selectFrom("Resource")
368368
.where("parentId", "in", (qb) =>
@@ -374,7 +374,7 @@ export const getLocalisedSitemap = async (
374374
ResourceType.Folder,
375375
]),
376376
)
377-
.where("type", "in", CHILD_PAGE_RESOURCE_TYPES)
377+
.where("type", "=", ResourceType.IndexPage)
378378
.where("state", "=", "Published")
379379
.leftJoin("Version", "Version.id", "publishedVersionId")
380380
.leftJoin("Blob as published", "Version.blobId", "published.id")
@@ -390,7 +390,7 @@ export const getLocalisedSitemap = async (
390390
)
391391
.union((eb) =>
392392
eb
393-
.selectFrom("childCousinPages as Resource")
393+
.selectFrom("childCousinIndexPages as Resource")
394394
.select(["summary", "thumbnail", ...defaultResourceSelect]),
395395
)
396396
.orderBy("title asc")

apps/studio/src/utils/sitemap.ts

Lines changed: 35 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,8 @@ import type { IsomerSitemap } from "@opengovsg/isomer-components"
22
import { ResourceType } from "~prisma/generated/generatedEnums"
33

44
import type { Resource } from "~prisma/generated/selectableTypes"
5+
import { INDEX_PAGE_PERMALINK } from "~/constants/sitemap"
56

6-
export const CHILD_PAGE_RESOURCE_TYPES = [
7-
ResourceType.Page,
8-
ResourceType.CollectionPage,
9-
ResourceType.CollectionLink,
10-
ResourceType.IndexPage,
11-
] as const
127
type ResourceDto = Omit<
138
Resource,
149
"id" | "parentId" | "publishedVersionId" | "draftBlobId"
@@ -19,63 +14,35 @@ type ResourceDto = Omit<
1914
thumbnail?: string
2015
}
2116

22-
const getResourcesWithFullPermalink = (
23-
resources: ResourceDto[],
24-
parent: ResourceDto,
25-
): ResourceDto[] => {
26-
const children = resources
27-
.filter((resources) => {
28-
return parent.type === ResourceType.RootPage
29-
? resources.parentId === null &&
30-
resources.type !== ResourceType.RootPage
31-
: resources.parentId === parent.id
32-
})
33-
.map((child) => {
34-
return {
35-
...child,
36-
permalink: `${parent.type === ResourceType.RootPage ? "" : parent.permalink}/${child.permalink}`,
37-
}
38-
})
39-
40-
return [
41-
...children,
42-
...children.flatMap((child) =>
43-
getResourcesWithFullPermalink(resources, child),
44-
),
45-
]
46-
}
47-
4817
const getSitemapTreeFromArray = (
4918
resources: ResourceDto[],
5019
parentId: string | null,
20+
path: string,
5121
): IsomerSitemap[] | undefined => {
5222
if (resources.length === 0) {
5323
return undefined
5424
}
5525

5626
// Get the immediate children of the resource with the given parent ID
57-
const children = resources
58-
.filter((resource) => {
59-
const hasPageDescendants = resources.some((possibleChild) => {
60-
return (
61-
possibleChild.permalink.startsWith(resource.permalink) &&
62-
CHILD_PAGE_RESOURCE_TYPES.find((t) => t === possibleChild.type)
63-
)
64-
})
27+
const children = resources.filter((resource) => {
28+
if (parentId === null) {
6529
return (
66-
CHILD_PAGE_RESOURCE_TYPES.find((t) => t === resource.type) ||
67-
hasPageDescendants
30+
resource.parentId === null &&
31+
resource.type !== ResourceType.RootPage &&
32+
resource.type !== ResourceType.FolderMeta &&
33+
resource.type !== ResourceType.CollectionMeta
6834
)
69-
})
70-
.filter((resource) => {
71-
return (
72-
resource.parentId === parentId &&
73-
resource.type !== ResourceType.IndexPage
74-
)
75-
})
35+
}
36+
return (
37+
resource.parentId === parentId &&
38+
resource.permalink !== INDEX_PAGE_PERMALINK
39+
)
40+
})
7641

7742
// TODO: Sort the children by the page ordering if the FolderMeta resource exists
7843
return children.map((resource) => {
44+
const permalink = `${path}${resource.permalink}`
45+
7946
if (
8047
resource.type === ResourceType.Page ||
8148
resource.type === ResourceType.CollectionPage
@@ -85,8 +52,9 @@ const getSitemapTreeFromArray = (
8552
layout: "content", // Note: We are not using the layout field in our sitemap for preview
8653
title: resource.title,
8754
summary: resource.summary ?? "",
88-
lastModified: resource.updatedAt.toISOString(),
89-
permalink: resource.permalink,
55+
lastModified: new Date() // TODO: Update this to the updated_at field in DB
56+
.toISOString(),
57+
permalink,
9058
image: {
9159
src: resource.thumbnail ?? "",
9260
alt: "",
@@ -97,36 +65,30 @@ const getSitemapTreeFromArray = (
9765
const indexPage = resources.find(
9866
(child) =>
9967
// NOTE: This child is the index page of this resource
100-
child.type === ResourceType.IndexPage && child.parentId === resource.id,
68+
child.permalink === INDEX_PAGE_PERMALINK &&
69+
child.parentId === resource.id,
10170
)
10271

103-
if (!!indexPage) {
104-
return {
105-
id: String(resource.id),
106-
layout: "content", // Note: We are not using the layout field in our previews
107-
title: indexPage.title,
108-
summary: indexPage.summary ?? `Pages in ${resource.title}`,
109-
lastModified: new Date() // TODO: Update this to the updated_at field in DB
110-
.toISOString(),
111-
// NOTE: This permalink is unused in the preview
112-
permalink: resource.permalink,
113-
image: !!indexPage.thumbnail
114-
? { src: indexPage.thumbnail, alt: "" }
115-
: undefined,
116-
children: getSitemapTreeFromArray(resources, resource.id),
117-
}
118-
}
72+
const titleOfPage = indexPage?.title
73+
const summaryOfPage = indexPage?.summary
11974

12075
return {
12176
id: String(resource.id),
12277
layout: "content", // Note: We are not using the layout field in our previews
123-
title: resource.title,
124-
summary: `Pages in ${resource.title}`,
78+
title: titleOfPage || resource.title,
79+
summary: summaryOfPage ?? `Pages in ${resource.title}`,
12580
lastModified: new Date() // TODO: Update this to the updated_at field in DB
12681
.toISOString(),
12782
// NOTE: This permalink is unused in the preview
128-
permalink: resource.permalink,
129-
children: getSitemapTreeFromArray(resources, resource.id),
83+
permalink,
84+
image: !!indexPage?.thumbnail
85+
? { src: indexPage.thumbnail, alt: "" }
86+
: undefined,
87+
children: getSitemapTreeFromArray(
88+
resources,
89+
resource.id,
90+
`${permalink}/`,
91+
),
13092
}
13193
})
13294
}
@@ -136,11 +98,6 @@ export const getSitemapTree = (
13698
rootResource: ResourceDto,
13799
resources: ResourceDto[],
138100
): IsomerSitemap => {
139-
const resourcesWithFullPermalink = getResourcesWithFullPermalink(
140-
resources,
141-
rootResource,
142-
)
143-
144101
return {
145102
id: String(rootResource.id),
146103
layout: "homepage", // Note: We are not using the layout field in our previews
@@ -150,6 +107,6 @@ export const getSitemapTree = (
150107
.toISOString(),
151108
// NOTE: This permalink is unused in the preview
152109
permalink: "/",
153-
children: getSitemapTreeFromArray(resourcesWithFullPermalink, null),
110+
children: getSitemapTreeFromArray(resources, null, "/"),
154111
}
155112
}

0 commit comments

Comments
 (0)