mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 00:03:33 +10:00
getDocument refactor
This commit is contained in:
@ -5,10 +5,8 @@ import {
|
||||
} from "@documenso/lib/server";
|
||||
import prisma from "@documenso/prisma";
|
||||
import { NextApiRequest, NextApiResponse } from "next";
|
||||
import { useRouter } from "next/router";
|
||||
import fs from "fs";
|
||||
import { buffer } from "stream/consumers";
|
||||
import { Document as PrismaDocument } from "@prisma/client";
|
||||
import { getDocument } from "@documenso/lib/query";
|
||||
|
||||
async function getHandler(req: NextApiRequest, res: NextApiResponse) {
|
||||
const user = await getUserFromToken(req, res);
|
||||
@ -21,10 +19,9 @@ async function getHandler(req: NextApiRequest, res: NextApiResponse) {
|
||||
return;
|
||||
}
|
||||
|
||||
const document: PrismaDocument = await prisma.document.findFirstOrThrow({
|
||||
where: {
|
||||
id: +documentId,
|
||||
},
|
||||
const document: PrismaDocument = await getDocument(+documentId, {
|
||||
res: res,
|
||||
req: req,
|
||||
});
|
||||
|
||||
if (!document)
|
||||
|
||||
@ -7,6 +7,7 @@ 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 postHandler(req: NextApiRequest, res: NextApiResponse) {
|
||||
const user = await getUserFromToken(req, res);
|
||||
@ -20,10 +21,9 @@ async function postHandler(req: NextApiRequest, res: NextApiResponse) {
|
||||
return;
|
||||
}
|
||||
|
||||
const document: PrismaDocument = await prisma.document.findFirstOrThrow({
|
||||
where: {
|
||||
id: +documentId,
|
||||
},
|
||||
const document: PrismaDocument = await getDocument(+documentId, {
|
||||
res: res,
|
||||
req: req,
|
||||
});
|
||||
|
||||
// todo encapsulate entity ownerships
|
||||
|
||||
@ -5,8 +5,9 @@ import {
|
||||
} from "@documenso/lib/server";
|
||||
import prisma from "@documenso/prisma";
|
||||
import { NextApiRequest, NextApiResponse } from "next";
|
||||
import { sendSignedMail, sendSigningRequest } from "@documenso/lib/mail";
|
||||
import { SendStatus } from "@prisma/client";
|
||||
import { sendSigningRequest } from "@documenso/lib/mail";
|
||||
import { getDocument } from "@documenso/lib/query";
|
||||
import { Document as PrismaDocument } from "@prisma/client";
|
||||
|
||||
async function postHandler(req: NextApiRequest, res: NextApiResponse) {
|
||||
const user = await getUserFromToken(req, res);
|
||||
@ -19,19 +20,9 @@ async function postHandler(req: NextApiRequest, res: NextApiResponse) {
|
||||
return;
|
||||
}
|
||||
|
||||
const document = await prisma.document.findFirstOrThrow({
|
||||
where: {
|
||||
id: +documentId,
|
||||
},
|
||||
include: {
|
||||
User: {
|
||||
select: {
|
||||
name: true,
|
||||
email: true,
|
||||
},
|
||||
},
|
||||
Recipient: true,
|
||||
},
|
||||
const document: PrismaDocument = await getDocument(+documentId, {
|
||||
res: res,
|
||||
req: req,
|
||||
});
|
||||
|
||||
if (!document)
|
||||
|
||||
@ -6,6 +6,8 @@ import {
|
||||
import prisma from "@documenso/prisma";
|
||||
import { NextApiRequest, NextApiResponse } from "next";
|
||||
import { SigningStatus, DocumentStatus } from "@prisma/client";
|
||||
import { getDocument } from "@documenso/lib/query";
|
||||
import { Document as PrismaDocument } from "@prisma/client";
|
||||
|
||||
async function postHandler(req: NextApiRequest, res: NextApiResponse) {
|
||||
const existingUser = await getUserFromToken(req, res);
|
||||
@ -25,10 +27,9 @@ async function postHandler(req: NextApiRequest, res: NextApiResponse) {
|
||||
return;
|
||||
}
|
||||
|
||||
let document = await prisma.document.findFirstOrThrow({
|
||||
where: {
|
||||
id: recipient.documentId,
|
||||
},
|
||||
const document: PrismaDocument = await getDocument(recipient.documentId, {
|
||||
res: res,
|
||||
req: req,
|
||||
});
|
||||
|
||||
if (!document) res.status(404).end(`No document found.`);
|
||||
|
||||
@ -4,7 +4,6 @@ import { NextPageWithLayout } from "../../_app";
|
||||
import dynamic from "next/dynamic";
|
||||
import { useRouter } from "next/router";
|
||||
import { NEXT_PUBLIC_WEBAPP_URL } from "@documenso/lib";
|
||||
import prisma from "@documenso/prisma";
|
||||
import { getUserFromToken } from "@documenso/lib/server";
|
||||
import Link from "next/link";
|
||||
import { DocumentStatus } from "@prisma/client";
|
||||
@ -16,6 +15,8 @@ import {
|
||||
UserPlusIcon,
|
||||
UsersIcon,
|
||||
} from "@heroicons/react/24/outline";
|
||||
import { getDocument } from "@documenso/lib/query";
|
||||
import { Document as PrismaDocument } from "@prisma/client";
|
||||
|
||||
const PDFViewer = dynamic(() => import("../../../components/pdf-viewer"), {
|
||||
ssr: false,
|
||||
@ -154,13 +155,10 @@ export async function getServerSideProps(context: any) {
|
||||
if (!user) return;
|
||||
|
||||
const { id: documentId } = context.query;
|
||||
const document = await prisma.document.findFirstOrThrow({
|
||||
where: {
|
||||
id: +documentId,
|
||||
},
|
||||
include: {
|
||||
Recipient: true,
|
||||
},
|
||||
|
||||
const document: PrismaDocument = await getDocument(+documentId, {
|
||||
res: context.res,
|
||||
req: context.req,
|
||||
});
|
||||
|
||||
// todo optimize querys
|
||||
|
||||
@ -1,32 +1,15 @@
|
||||
import prisma from "@documenso/prisma";
|
||||
import Head from "next/head";
|
||||
import { ReactElement } from "react";
|
||||
import Layout from "../../../components/layout";
|
||||
import { NextPageWithLayout } from "../../_app";
|
||||
import { Fragment } from "react";
|
||||
import { Menu, Transition } from "@headlessui/react";
|
||||
import {
|
||||
ArchiveBoxIcon,
|
||||
ArrowRightCircleIcon,
|
||||
ChevronDownIcon,
|
||||
DocumentDuplicateIcon,
|
||||
HeartIcon,
|
||||
PencilSquareIcon,
|
||||
TrashIcon,
|
||||
UserPlusIcon,
|
||||
} from "@heroicons/react/20/solid";
|
||||
import { classNames, NEXT_PUBLIC_WEBAPP_URL } from "@documenso/lib";
|
||||
import {
|
||||
PaperAirplaneIcon,
|
||||
UserCircleIcon,
|
||||
UserGroupIcon,
|
||||
UserIcon,
|
||||
UsersIcon,
|
||||
} from "@heroicons/react/24/outline";
|
||||
import { NEXT_PUBLIC_WEBAPP_URL } from "@documenso/lib";
|
||||
import { PaperAirplaneIcon, UserCircleIcon } from "@heroicons/react/24/outline";
|
||||
import { ChevronLeftIcon, ChevronRightIcon } from "@heroicons/react/20/solid";
|
||||
import { getUserFromToken } from "@documenso/lib/server";
|
||||
import { useRouter } from "next/router";
|
||||
import { toast } from "react-hot-toast";
|
||||
import { getDocument } from "@documenso/lib/query";
|
||||
import { Document as PrismaDocument } from "@prisma/client";
|
||||
|
||||
const RecipientsPage: NextPageWithLayout = (props: any) => {
|
||||
const router = useRouter();
|
||||
@ -172,13 +155,9 @@ export async function getServerSideProps(context: any) {
|
||||
if (!user) return;
|
||||
|
||||
const { id: documentId } = context.query;
|
||||
const document = await prisma.document.findFirstOrThrow({
|
||||
where: {
|
||||
id: +documentId,
|
||||
},
|
||||
include: {
|
||||
Recipient: true,
|
||||
},
|
||||
const document: PrismaDocument = await getDocument(+documentId, {
|
||||
res: context.res,
|
||||
req: context.req,
|
||||
});
|
||||
|
||||
return {
|
||||
|
||||
25
packages/lib/query/getDocument.ts
Normal file
25
packages/lib/query/getDocument.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import { getUserFromToken } from "@documenso/lib/server";
|
||||
import prisma from "@documenso/prisma";
|
||||
import { Document as PrismaDocument } from "@prisma/client";
|
||||
|
||||
export const getDocument = async (
|
||||
documentId: number,
|
||||
context: any
|
||||
): Promise<PrismaDocument> => {
|
||||
const user = await getUserFromToken(context.req, context.res);
|
||||
if (!user) return Promise.reject("Invalid user or token.");
|
||||
if (!documentId) Promise.reject("No documentId");
|
||||
if (!context) Promise.reject("No context");
|
||||
|
||||
const document: PrismaDocument = await prisma.document.findFirstOrThrow({
|
||||
where: {
|
||||
id: documentId,
|
||||
userId: user.id,
|
||||
},
|
||||
include: {
|
||||
Recipient: true,
|
||||
},
|
||||
});
|
||||
|
||||
return document;
|
||||
};
|
||||
@ -3,9 +3,9 @@ import prisma from "@documenso/prisma";
|
||||
import { Document as PrismaDocument } from "@prisma/client";
|
||||
|
||||
export const getDocumentsForUserFromToken = async (
|
||||
ssrContext: any
|
||||
context: any
|
||||
): Promise<PrismaDocument[]> => {
|
||||
const user = await getUserFromToken(ssrContext.req, ssrContext.res);
|
||||
const user = await getUserFromToken(context.req, context.res);
|
||||
if (!user) return Promise.reject("Invalid user or token.s");
|
||||
|
||||
const documents: PrismaDocument[] = await prisma.document.findMany({
|
||||
|
||||
@ -1 +1,2 @@
|
||||
export { getDocumentsForUserFromToken } from "./getDocumentsForUserFromToken";
|
||||
export { getDocument } from "./getDocument";
|
||||
|
||||
Reference in New Issue
Block a user