mirror of
https://github.com/documenso/documenso.git
synced 2025-11-18 02:32:00 +10:00
create recipient
This commit is contained in:
47
apps/web/pages/api/documents/[id]/recipients/index.ts
Normal file
47
apps/web/pages/api/documents/[id]/recipients/index.ts
Normal file
@ -0,0 +1,47 @@
|
||||
import {
|
||||
defaultHandler,
|
||||
defaultResponder,
|
||||
getUserFromToken,
|
||||
} from "@documenso/lib/server";
|
||||
import prisma from "@documenso/prisma";
|
||||
import { NextApiRequest, NextApiResponse } from "next";
|
||||
import short from "short-uuid";
|
||||
import { Document as PrismaDocument } from "@prisma/client";
|
||||
|
||||
async function postHandler(req: NextApiRequest, res: NextApiResponse) {
|
||||
const user = await getUserFromToken(req, res);
|
||||
const { id: documentId } = req.query;
|
||||
const body = req.body;
|
||||
|
||||
if (!user) return;
|
||||
|
||||
if (!documentId) {
|
||||
res.status(400).send("Missing parameter documentId.");
|
||||
return;
|
||||
}
|
||||
|
||||
const document: PrismaDocument = await prisma.document.findFirstOrThrow({
|
||||
where: {
|
||||
id: +documentId,
|
||||
},
|
||||
});
|
||||
|
||||
// todo encapsulate entity ownerships
|
||||
if (document.userId !== user.id) {
|
||||
return res.status(401).send("User does not have access to this document.");
|
||||
}
|
||||
|
||||
await prisma.recipient.create({
|
||||
data: {
|
||||
documentId: +documentId,
|
||||
email: body.email,
|
||||
token: short.generate(),
|
||||
},
|
||||
});
|
||||
|
||||
return res.status(201).end();
|
||||
}
|
||||
|
||||
export default defaultHandler({
|
||||
POST: Promise.resolve({ default: defaultResponder(postHandler) }),
|
||||
});
|
||||
@ -57,6 +57,7 @@ async function getHandler(req: NextApiRequest, res: NextApiResponse) {
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
Recipient: true,
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user