Skip to content

Commit c82df51

Browse files
authored
chore: shift over index page creation to collection (#1552)
* chore: shift over index page creation to collection * chore: fix duplicate import error * chore: swap from 2 -> 3 audit logs for tests * chore: rename to indexPage * chore: update wording for default subtitle
1 parent 97dec74 commit c82df51

File tree

3 files changed

+72
-7
lines changed

3 files changed

+72
-7
lines changed

apps/studio/src/server/modules/collection/__tests__/collection.router.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ describe("collection.router", async () => {
218218
})
219219
expect(result).toMatchObject({ id: actualCollection.id })
220220
expect(auditSpy).toHaveBeenCalled()
221-
await assertAuditLogRows(2)
221+
await assertAuditLogRows(3)
222222
const auditEntry = await db
223223
.selectFrom("AuditLog")
224224
.where("eventType", "=", "ResourceCreate")
@@ -252,7 +252,7 @@ describe("collection.router", async () => {
252252
})
253253
expect(result).toMatchObject({ id: actualCollection.id })
254254
expect(auditSpy).toHaveBeenCalled()
255-
await assertAuditLogRows(2)
255+
await assertAuditLogRows(3)
256256
const auditEntry = await db
257257
.selectFrom("AuditLog")
258258
.where("eventType", "=", "ResourceCreate")
@@ -286,7 +286,7 @@ describe("collection.router", async () => {
286286
})
287287
expect(actualCollection.parentId).toEqual(parent.id)
288288
expect(result).toMatchObject({ id: actualCollection.id })
289-
await assertAuditLogRows(2)
289+
await assertAuditLogRows(3)
290290
expect(auditSpy).toHaveBeenCalled()
291291
const auditEntry = await db
292292
.selectFrom("AuditLog")
@@ -297,7 +297,7 @@ describe("collection.router", async () => {
297297
expect(auditEntry.userId).toBe(session.userId)
298298
})
299299

300-
it("should create a nested collection if `parentFolderId` is provided and hte user is not an admin", async () => {
300+
it("should create a nested collection if `parentFolderId` is provided and the user is not an admin", async () => {
301301
// Arrange
302302
const permalinkToUse = "test-collection-777"
303303
const { folder: parent, site } = await setupFolder()
@@ -321,7 +321,7 @@ describe("collection.router", async () => {
321321
})
322322
expect(actualCollection.parentId).toEqual(parent.id)
323323
expect(result).toMatchObject({ id: actualCollection.id })
324-
await assertAuditLogRows(2)
324+
await assertAuditLogRows(3)
325325
expect(auditSpy).toHaveBeenCalled()
326326
const auditEntry = await db
327327
.selectFrom("AuditLog")

apps/studio/src/server/modules/collection/collection.router.ts

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { UnwrapTagged } from "type-fest"
22
import { TRPCError } from "@trpc/server"
3-
import { AuditLogEvent } from "~prisma/generated/generatedEnums"
43
import { get, pick } from "lodash"
54

5+
import { INDEX_PAGE_PERMALINK } from "~/constants/sitemap"
66
import {
77
createCollectionSchema,
88
editLinkSchema,
@@ -13,7 +13,13 @@ import { readFolderSchema } from "~/schemas/folder"
1313
import { createCollectionPageSchema } from "~/schemas/page"
1414
import { protectedProcedure, router } from "~/server/trpc"
1515
import { logResourceEvent } from "../audit/audit.service"
16-
import { db, jsonb, ResourceState, ResourceType } from "../database"
16+
import {
17+
AuditLogEvent,
18+
db,
19+
jsonb,
20+
ResourceState,
21+
ResourceType,
22+
} from "../database"
1723
import { PG_ERROR_CODES } from "../database/constants"
1824
import { bulkValidateUserPermissionsForResources } from "../permissions/permissions.service"
1925
import {
@@ -26,6 +32,7 @@ import {
2632
import { validateUserPermissionsForSite } from "../site/site.service"
2733
import { defaultCollectionSelect } from "./collection.select"
2834
import {
35+
createCollectionIndexJson,
2936
createCollectionLinkJson,
3037
createCollectionPageJson,
3138
} from "./collection.service"
@@ -127,6 +134,44 @@ export const collectionRouter = router({
127134
by: user,
128135
})
129136

137+
const indexJson = createCollectionIndexJson(collection.title)
138+
139+
const blob = await tx
140+
.insertInto("Blob")
141+
.values({ content: jsonb(indexJson) })
142+
.returning("Blob.id")
143+
.executeTakeFirstOrThrow()
144+
145+
const indexPage = await tx
146+
.insertInto("Resource")
147+
.values({
148+
title: collection.title,
149+
permalink: INDEX_PAGE_PERMALINK,
150+
siteId,
151+
parentId: collection.id,
152+
draftBlobId: blob.id,
153+
type: ResourceType.IndexPage,
154+
state: ResourceState.Draft,
155+
})
156+
.returningAll()
157+
.executeTakeFirstOrThrow()
158+
.catch((err) => {
159+
if (get(err, "code") === PG_ERROR_CODES.uniqueViolation) {
160+
throw new TRPCError({
161+
code: "CONFLICT",
162+
message: "A resource with the same permalink already exists",
163+
})
164+
}
165+
throw err
166+
})
167+
168+
await logResourceEvent(tx, {
169+
siteId,
170+
by: user,
171+
delta: { before: null, after: indexPage },
172+
eventType: AuditLogEvent.ResourceCreate,
173+
})
174+
130175
return collection
131176
})
132177

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
import type { UnwrapTagged } from "type-fest"
2+
import {
3+
COLLECTION_PAGE_DEFAULT_SORT_BY,
4+
COLLECTION_PAGE_DEFAULT_SORT_DIRECTION,
5+
CollectionPagePageProps,
6+
ISOMER_USABLE_PAGE_LAYOUTS,
7+
} from "@opengovsg/isomer-components"
28
import { format } from "date-fns"
39

410
import type { ResourceType } from "../database"
@@ -38,3 +44,17 @@ export const createCollectionLinkJson = ({}: {
3844
version: "0.1.0",
3945
} satisfies UnwrapTagged<PrismaJson.BlobJsonContent>
4046
}
47+
48+
export const createCollectionIndexJson = (title: string) => {
49+
return {
50+
layout: ISOMER_USABLE_PAGE_LAYOUTS.Collection,
51+
page: {
52+
title,
53+
subtitle: `Read up-to-date news articles, speeches, and press releases here.`,
54+
defaultSortBy: COLLECTION_PAGE_DEFAULT_SORT_BY,
55+
defaultSortDirection: COLLECTION_PAGE_DEFAULT_SORT_DIRECTION,
56+
} as CollectionPagePageProps,
57+
content: [],
58+
version: "0.1.0",
59+
}
60+
}

0 commit comments

Comments
 (0)