New Rules stay off lists until the person who holds the pen turns on a share link, or the team features one. Uploaded files are readable only by those readers, stripped of location metadata, and deleted with the Rule, draft, or account.
106 lines
2.9 KiB
TypeScript
106 lines
2.9 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,
|
|
unauthorized,
|
|
} from "../../../../lib/server/responses";
|
|
import { getSessionUser } from "../../../../lib/server/session";
|
|
import { apiRoute } from "../../../../lib/server/apiRoute";
|
|
import { putDraftBodySchema } from "../../../../lib/server/validation/createFlowSchemas";
|
|
import { readLimitedJson } from "../../../../lib/server/validation/requestBody";
|
|
import { jsonFromZodError } from "../../../../lib/server/validation/zodHttp";
|
|
import { deleteUploadsForDraft } from "../../../../lib/server/uploads/deleteUploadedFiles";
|
|
import { linkUploadsToDraft } from "../../../../lib/server/uploads/linkUploads";
|
|
|
|
export const GET = apiRoute("drafts.me.get", async () => {
|
|
if (!isDatabaseConfigured()) {
|
|
return dbUnavailable();
|
|
}
|
|
|
|
const user = await getSessionUser();
|
|
if (!user) {
|
|
return unauthorized();
|
|
}
|
|
|
|
const draft = await prisma.ruleDraft.findUnique({
|
|
where: { userId: user.id },
|
|
});
|
|
|
|
return NextResponse.json({
|
|
draft: draft
|
|
? { payload: draft.payload, updatedAt: draft.updatedAt }
|
|
: null,
|
|
});
|
|
});
|
|
|
|
export const PUT = apiRoute("drafts.me.put", async (request: NextRequest) => {
|
|
if (!isDatabaseConfigured()) {
|
|
return dbUnavailable();
|
|
}
|
|
|
|
const user = await getSessionUser();
|
|
if (!user) {
|
|
return unauthorized();
|
|
}
|
|
|
|
const parsedBody = await readLimitedJson(request);
|
|
if (parsedBody.ok === false) {
|
|
return parsedBody.response;
|
|
}
|
|
|
|
const validated = putDraftBodySchema.safeParse(parsedBody.value);
|
|
if (!validated.success) {
|
|
return jsonFromZodError(validated.error);
|
|
}
|
|
|
|
const { payload } = validated.data;
|
|
|
|
const jsonPayload = payload as Prisma.InputJsonValue;
|
|
|
|
const draft = await prisma.ruleDraft.upsert({
|
|
where: { userId: user.id },
|
|
create: {
|
|
userId: user.id,
|
|
payload: jsonPayload,
|
|
},
|
|
update: {
|
|
payload: jsonPayload,
|
|
},
|
|
});
|
|
await linkUploadsToDraft({
|
|
draftId: draft.id,
|
|
payload,
|
|
userId: user.id,
|
|
});
|
|
|
|
return NextResponse.json({
|
|
draft: { payload: draft.payload, updatedAt: draft.updatedAt },
|
|
});
|
|
});
|
|
|
|
export const DELETE = apiRoute("drafts.me.delete", async () => {
|
|
if (!isDatabaseConfigured()) {
|
|
return dbUnavailable();
|
|
}
|
|
|
|
const user = await getSessionUser();
|
|
if (!user) {
|
|
return unauthorized();
|
|
}
|
|
|
|
// Idempotent: missing draft is a no-op so callers can fire-and-forget after
|
|
// publish / exit without worrying about prior state.
|
|
const draft = await prisma.ruleDraft.findUnique({
|
|
where: { userId: user.id },
|
|
select: { id: true },
|
|
});
|
|
if (draft) {
|
|
await deleteUploadsForDraft(draft.id);
|
|
}
|
|
await prisma.ruleDraft.deleteMany({ where: { userId: user.id } });
|
|
|
|
return NextResponse.json({ ok: true });
|
|
});
|