From 6c858fce102ba80624177ed23a8db7178d1f1d08 Mon Sep 17 00:00:00 2001 From: Harish V Date: Sat, 10 Aug 2024 00:29:44 +0800 Subject: [PATCH] fix comments --- apps/studio/src/server/modules/page/page.router.ts | 4 ++-- .../src/server/modules/version/version.service.ts | 10 ++++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/apps/studio/src/server/modules/page/page.router.ts b/apps/studio/src/server/modules/page/page.router.ts index fc26cd36f6..57e3a3e33f 100644 --- a/apps/studio/src/server/modules/page/page.router.ts +++ b/apps/studio/src/server/modules/page/page.router.ts @@ -26,7 +26,7 @@ import { updatePageById, } from "../resource/resource.service" import { getSiteConfig } from "../site/site.service" -import { addNewVersion } from "../version/version.service" +import { incrementVersion } from "../version/version.service" import { createDefaultPage } from "./page.service" const ajv = new Ajv({ allErrors: true, strict: false, logger: false }) @@ -250,7 +250,7 @@ export const pageRouter = router({ .mutation(async ({ ctx, input: { siteId, pageId } }) => { /* Step 1: Update DB table to latest state */ // Create a new version - const addedVersionResult = await addNewVersion({ + const addedVersionResult = await incrementVersion({ siteId, pageId, userId: ctx.user.id, diff --git a/apps/studio/src/server/modules/version/version.service.ts b/apps/studio/src/server/modules/version/version.service.ts index daa7475ed7..9958bda6a5 100644 --- a/apps/studio/src/server/modules/version/version.service.ts +++ b/apps/studio/src/server/modules/version/version.service.ts @@ -1,4 +1,5 @@ import type { SelectExpression } from "kysely" +import { TRPCError } from "@trpc/server" import { type DB } from "~prisma/generated/generatedTypes" import type { SafeKysely } from "../database" @@ -45,7 +46,7 @@ const createVersion = async ( return { versionId: addedVersion.id } } -export const addNewVersion = async ({ +export const incrementVersion = async ({ siteId, pageId, userId, @@ -56,12 +57,17 @@ export const addNewVersion = async ({ }) => { return await db .transaction() + // serialize to prevent discrepancies from + // concurrent publishes from other users .setIsolationLevel("serializable") .execute(async (tx) => { const page = await getPageById(tx, { siteId, resourceId: pageId }) if (!page.draftBlobId) { - return { error: "No drafts to publish for this page" } + throw new TRPCError({ + code: "BAD_REQUEST", + message: "No drafts to publish for this page", + }) } let newVersionNum = 1