Save a guest edit on the published Rule, and publish a new one when that update is refused.

A leftover editing id was treated as a successful finish in this browser only, so the public Rule stayed stale and a later Finish could skip creating a new Rule.
This commit is contained in:
adilallo committed 2026-11-23 14:59:46 -07:00
1 parent 0535f6f167
commit 2644023b5a
6 files changed
+207 -44

No files matched your search

+14 -8
View File
@@ -8,6 +8,7 @@ import {
notFound,
unauthorized,
} from "../../../../lib/server/responses";
import { browserHoldsClaimForHash } from "../../../../lib/server/guestRuleClaim";
import { getReadablePublishedRule } from "../../../../lib/server/publishedRules";
import { getSessionUser } from "../../../../lib/server/session";
import { apiRoute } from "../../../../lib/server/apiRoute";
@@ -54,20 +55,25 @@ export const PATCH = apiRoute<RouteContext>(
}
const user = await getSessionUser();
if (!user) {
return unauthorized();
}
const { id } = await context.params;
const row = await prisma.publishedRule.findUnique({
where: { id },
select: { id: true, userId: true },
select: { id: true, userId: true, claimTokenHash: true },
});
// This browser published the Rule and nobody has claimed it yet.
const claimUpdate =
row != null &&
row.userId == null &&
(await browserHoldsClaimForHash(row.claimTokenHash));
if (!row) {
return notFound();
return user ? notFound() : unauthorized();
}
if (row.userId !== user.id) {
if (!user && !claimUpdate) {
return unauthorized();
}
if (user && row.userId !== user.id && !claimUpdate) {
return forbidden("You do not have permission to update this rule");
}
@@ -95,7 +101,7 @@ export const PATCH = apiRoute<RouteContext>(
await linkUploadsToRule({
ruleId: row.id,
document,
userId: user.id,
userId: user?.id ?? null,
});
return NextResponse.json({ ok: true });