recipients ui and api basics

This commit is contained in:
Timur Ercan
2023-02-03 18:39:17 +01:00
parent e9db5acc85
commit b854600fb3
5 changed files with 59 additions and 2 deletions

View File

@ -0,0 +1,33 @@
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";
import { getDocument } from "@documenso/lib/query";
async function deleteHandler(req: NextApiRequest, res: NextApiResponse) {
const user = await getUserFromToken(req, res);
const { id: documentId, rid: recipientId } = req.query;
const body = req.body;
if (!recipientId) {
res.status(400).send("Missing parameter recipientId.");
return;
}
await prisma.recipient.delete({
where: {
id: +recipientId,
},
});
return res.status(200).send(documentId + "<- did rid ->" + recipientId);
}
export default defaultHandler({
DELETE: Promise.resolve({ default: defaultResponder(deleteHandler) }),
});

View File

@ -32,9 +32,10 @@ async function postHandler(req: NextApiRequest, res: NextApiResponse) {
return res.status(401).send("User does not have access to this document.");
}
const upsert = await prisma.recipient.upsert({
await prisma.recipient.upsert({
where: {
email: body.email,
// todo id
},
update: {
email: body.email,

View File

@ -11,7 +11,6 @@ export const config = {
},
};
// POST /documents
async function postHandler(req: NextApiRequest, res: NextApiResponse) {
const form = formidable();

View File

@ -180,6 +180,29 @@ const RecipientsPage: NextPageWithLayout = (props: any) => {
);
};
async delete(recipient:any){
toast.promise(
fetch("/api/documents/" + recipient.documentId + "/recipients", {
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(recipient),
}),
{
loading: "Deleting...",
success: "Deleted.",
error: "Could not delete :/",
},
{
id: "deleting",
style: {
minWidth: "200px",
},
}
);
}
async function upsertRecipient(recipient: any) {
toast.promise(
fetch("/api/documents/" + recipient.documentId + "/recipients", {

View File

@ -8,6 +8,7 @@ export const getDocumentsForUserFromToken = async (
const user = await getUserFromToken(context.req, context.res);
if (!user) return Promise.reject("Invalid user or token.");
// todo remove document base64 data
const documents: PrismaDocument[] = await prisma.document.findMany({
where: {
userId: user.id,