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.
142 lines
4.1 KiB
TypeScript
142 lines
4.1 KiB
TypeScript
import type { Prisma } from "@prisma/client";
|
|
import { NextRequest, NextResponse } from "next/server";
|
|
import { prisma } from "../../../../lib/server/db";
|
|
import { isDatabaseConfigured } from "../../../../lib/server/env";
|
|
import {
|
|
dbUnavailable,
|
|
forbidden,
|
|
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";
|
|
import { deleteUploadsForRule } from "../../../../lib/server/uploads/deleteUploadedFiles";
|
|
import { linkUploadsToRule } from "../../../../lib/server/uploads/linkUploads";
|
|
import { publishRuleBodySchema } from "../../../../lib/server/validation/createFlowSchemas";
|
|
import { readLimitedJson } from "../../../../lib/server/validation/requestBody";
|
|
import { jsonFromZodError } from "../../../../lib/server/validation/zodHttp";
|
|
import { publishedRuleAnalyticsColumns } from "../../../../lib/server/publishedRulePersist";
|
|
|
|
type RouteContext = { params: Promise<{ id: string }> };
|
|
|
|
export const GET = apiRoute<RouteContext>(
|
|
"rules.byId",
|
|
async (request, context) => {
|
|
if (!isDatabaseConfigured()) {
|
|
return dbUnavailable();
|
|
}
|
|
|
|
const { id } = await context.params;
|
|
const user = await getSessionUser();
|
|
const resolved = await getReadablePublishedRule({
|
|
idOrSlug: id,
|
|
user,
|
|
viaShareSlug: false,
|
|
requestPath: request.nextUrl.pathname,
|
|
});
|
|
if (!resolved) {
|
|
return notFound();
|
|
}
|
|
|
|
return NextResponse.json({
|
|
rule: resolved.rule,
|
|
viewerIsOwner: resolved.viewerIsOwner,
|
|
});
|
|
},
|
|
);
|
|
|
|
export const PATCH = apiRoute<RouteContext>(
|
|
"rules.byId.patch",
|
|
async (request: NextRequest, context) => {
|
|
if (!isDatabaseConfigured()) {
|
|
return dbUnavailable();
|
|
}
|
|
|
|
const user = await getSessionUser();
|
|
const { id } = await context.params;
|
|
|
|
const row = await prisma.publishedRule.findUnique({
|
|
where: { id },
|
|
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 user ? notFound() : unauthorized();
|
|
}
|
|
if (!user && !claimUpdate) {
|
|
return unauthorized();
|
|
}
|
|
if (user && row.userId !== user.id && !claimUpdate) {
|
|
return forbidden("You do not have permission to update this rule");
|
|
}
|
|
|
|
const parsedBody = await readLimitedJson(request);
|
|
if (parsedBody.ok === false) {
|
|
return parsedBody.response;
|
|
}
|
|
|
|
const validated = publishRuleBodySchema.safeParse(parsedBody.value);
|
|
if (!validated.success) {
|
|
return jsonFromZodError(validated.error);
|
|
}
|
|
|
|
const { title, summary, document } = validated.data;
|
|
|
|
await prisma.publishedRule.update({
|
|
where: { id: row.id },
|
|
data: {
|
|
title,
|
|
summary,
|
|
document: document as Prisma.InputJsonValue,
|
|
...publishedRuleAnalyticsColumns(document),
|
|
},
|
|
});
|
|
await linkUploadsToRule({
|
|
ruleId: row.id,
|
|
document,
|
|
userId: user?.id ?? null,
|
|
});
|
|
|
|
return NextResponse.json({ ok: true });
|
|
},
|
|
);
|
|
|
|
export const DELETE = apiRoute<RouteContext>(
|
|
"rules.byId.delete",
|
|
async (_request, context) => {
|
|
if (!isDatabaseConfigured()) {
|
|
return dbUnavailable();
|
|
}
|
|
|
|
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 },
|
|
});
|
|
if (!row) {
|
|
return notFound();
|
|
}
|
|
if (row.userId !== user.id) {
|
|
return forbidden("You do not have permission to delete this rule");
|
|
}
|
|
|
|
await deleteUploadsForRule(row.id);
|
|
await prisma.publishedRule.delete({ where: { id: row.id } });
|
|
|
|
return NextResponse.json({ ok: true });
|
|
},
|
|
);
|