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";
|
} from "@documenso/lib/server";
|
||||||
import prisma from "@documenso/prisma";
|
import prisma from "@documenso/prisma";
|
||||||
import { NextApiRequest, NextApiResponse } from "next";
|
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 { Document as PrismaDocument } from "@prisma/client";
|
||||||
|
import { getDocument } from "@documenso/lib/query";
|
||||||
|
|
||||||
async function getHandler(req: NextApiRequest, res: NextApiResponse) {
|
async function getHandler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
const user = await getUserFromToken(req, res);
|
const user = await getUserFromToken(req, res);
|
||||||
@ -21,10 +19,9 @@ async function getHandler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const document: PrismaDocument = await prisma.document.findFirstOrThrow({
|
const document: PrismaDocument = await getDocument(+documentId, {
|
||||||
where: {
|
res: res,
|
||||||
id: +documentId,
|
req: req,
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!document)
|
if (!document)
|
||||||
|
|||||||
@ -7,6 +7,7 @@ import prisma from "@documenso/prisma";
|
|||||||
import { NextApiRequest, NextApiResponse } from "next";
|
import { NextApiRequest, NextApiResponse } from "next";
|
||||||
import short from "short-uuid";
|
import short from "short-uuid";
|
||||||
import { Document as PrismaDocument } from "@prisma/client";
|
import { Document as PrismaDocument } from "@prisma/client";
|
||||||
|
import { getDocument } from "@documenso/lib/query";
|
||||||
|
|
||||||
async function postHandler(req: NextApiRequest, res: NextApiResponse) {
|
async function postHandler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
const user = await getUserFromToken(req, res);
|
const user = await getUserFromToken(req, res);
|
||||||
@ -20,10 +21,9 @@ async function postHandler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const document: PrismaDocument = await prisma.document.findFirstOrThrow({
|
const document: PrismaDocument = await getDocument(+documentId, {
|
||||||
where: {
|
res: res,
|
||||||
id: +documentId,
|
req: req,
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// todo encapsulate entity ownerships
|
// todo encapsulate entity ownerships
|
||||||
|
|||||||
@ -5,8 +5,9 @@ import {
|
|||||||
} from "@documenso/lib/server";
|
} from "@documenso/lib/server";
|
||||||
import prisma from "@documenso/prisma";
|
import prisma from "@documenso/prisma";
|
||||||
import { NextApiRequest, NextApiResponse } from "next";
|
import { NextApiRequest, NextApiResponse } from "next";
|
||||||
import { sendSignedMail, sendSigningRequest } from "@documenso/lib/mail";
|
import { sendSigningRequest } from "@documenso/lib/mail";
|
||||||
import { SendStatus } from "@prisma/client";
|
import { getDocument } from "@documenso/lib/query";
|
||||||
|
import { Document as PrismaDocument } from "@prisma/client";
|
||||||
|
|
||||||
async function postHandler(req: NextApiRequest, res: NextApiResponse) {
|
async function postHandler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
const user = await getUserFromToken(req, res);
|
const user = await getUserFromToken(req, res);
|
||||||
@ -19,19 +20,9 @@ async function postHandler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const document = await prisma.document.findFirstOrThrow({
|
const document: PrismaDocument = await getDocument(+documentId, {
|
||||||
where: {
|
res: res,
|
||||||
id: +documentId,
|
req: req,
|
||||||
},
|
|
||||||
include: {
|
|
||||||
User: {
|
|
||||||
select: {
|
|
||||||
name: true,
|
|
||||||
email: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Recipient: true,
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!document)
|
if (!document)
|
||||||
|
|||||||
@ -6,6 +6,8 @@ import {
|
|||||||
import prisma from "@documenso/prisma";
|
import prisma from "@documenso/prisma";
|
||||||
import { NextApiRequest, NextApiResponse } from "next";
|
import { NextApiRequest, NextApiResponse } from "next";
|
||||||
import { SigningStatus, DocumentStatus } from "@prisma/client";
|
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) {
|
async function postHandler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
const existingUser = await getUserFromToken(req, res);
|
const existingUser = await getUserFromToken(req, res);
|
||||||
@ -25,10 +27,9 @@ async function postHandler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let document = await prisma.document.findFirstOrThrow({
|
const document: PrismaDocument = await getDocument(recipient.documentId, {
|
||||||
where: {
|
res: res,
|
||||||
id: recipient.documentId,
|
req: req,
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!document) res.status(404).end(`No document found.`);
|
if (!document) res.status(404).end(`No document found.`);
|
||||||
|
|||||||
@ -4,7 +4,6 @@ import { NextPageWithLayout } from "../../_app";
|
|||||||
import dynamic from "next/dynamic";
|
import dynamic from "next/dynamic";
|
||||||
import { useRouter } from "next/router";
|
import { useRouter } from "next/router";
|
||||||
import { NEXT_PUBLIC_WEBAPP_URL } from "@documenso/lib";
|
import { NEXT_PUBLIC_WEBAPP_URL } from "@documenso/lib";
|
||||||
import prisma from "@documenso/prisma";
|
|
||||||
import { getUserFromToken } from "@documenso/lib/server";
|
import { getUserFromToken } from "@documenso/lib/server";
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import { DocumentStatus } from "@prisma/client";
|
import { DocumentStatus } from "@prisma/client";
|
||||||
@ -16,6 +15,8 @@ import {
|
|||||||
UserPlusIcon,
|
UserPlusIcon,
|
||||||
UsersIcon,
|
UsersIcon,
|
||||||
} from "@heroicons/react/24/outline";
|
} 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"), {
|
const PDFViewer = dynamic(() => import("../../../components/pdf-viewer"), {
|
||||||
ssr: false,
|
ssr: false,
|
||||||
@ -154,13 +155,10 @@ export async function getServerSideProps(context: any) {
|
|||||||
if (!user) return;
|
if (!user) return;
|
||||||
|
|
||||||
const { id: documentId } = context.query;
|
const { id: documentId } = context.query;
|
||||||
const document = await prisma.document.findFirstOrThrow({
|
|
||||||
where: {
|
const document: PrismaDocument = await getDocument(+documentId, {
|
||||||
id: +documentId,
|
res: context.res,
|
||||||
},
|
req: context.req,
|
||||||
include: {
|
|
||||||
Recipient: true,
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// todo optimize querys
|
// todo optimize querys
|
||||||
|
|||||||
@ -1,32 +1,15 @@
|
|||||||
import prisma from "@documenso/prisma";
|
|
||||||
import Head from "next/head";
|
import Head from "next/head";
|
||||||
import { ReactElement } from "react";
|
import { ReactElement } from "react";
|
||||||
import Layout from "../../../components/layout";
|
import Layout from "../../../components/layout";
|
||||||
import { NextPageWithLayout } from "../../_app";
|
import { NextPageWithLayout } from "../../_app";
|
||||||
import { Fragment } from "react";
|
import { NEXT_PUBLIC_WEBAPP_URL } from "@documenso/lib";
|
||||||
import { Menu, Transition } from "@headlessui/react";
|
import { PaperAirplaneIcon, UserCircleIcon } from "@heroicons/react/24/outline";
|
||||||
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 { ChevronLeftIcon, ChevronRightIcon } from "@heroicons/react/20/solid";
|
import { ChevronLeftIcon, ChevronRightIcon } from "@heroicons/react/20/solid";
|
||||||
import { getUserFromToken } from "@documenso/lib/server";
|
import { getUserFromToken } from "@documenso/lib/server";
|
||||||
import { useRouter } from "next/router";
|
import { useRouter } from "next/router";
|
||||||
import { toast } from "react-hot-toast";
|
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 RecipientsPage: NextPageWithLayout = (props: any) => {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
@ -172,13 +155,9 @@ export async function getServerSideProps(context: any) {
|
|||||||
if (!user) return;
|
if (!user) return;
|
||||||
|
|
||||||
const { id: documentId } = context.query;
|
const { id: documentId } = context.query;
|
||||||
const document = await prisma.document.findFirstOrThrow({
|
const document: PrismaDocument = await getDocument(+documentId, {
|
||||||
where: {
|
res: context.res,
|
||||||
id: +documentId,
|
req: context.req,
|
||||||
},
|
|
||||||
include: {
|
|
||||||
Recipient: true,
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
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";
|
import { Document as PrismaDocument } from "@prisma/client";
|
||||||
|
|
||||||
export const getDocumentsForUserFromToken = async (
|
export const getDocumentsForUserFromToken = async (
|
||||||
ssrContext: any
|
context: any
|
||||||
): Promise<PrismaDocument[]> => {
|
): 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");
|
if (!user) return Promise.reject("Invalid user or token.s");
|
||||||
|
|
||||||
const documents: PrismaDocument[] = await prisma.document.findMany({
|
const documents: PrismaDocument[] = await prisma.document.findMany({
|
||||||
|
|||||||
@ -1 +1,2 @@
|
|||||||
export { getDocumentsForUserFromToken } from "./getDocumentsForUserFromToken";
|
export { getDocumentsForUserFromToken } from "./getDocumentsForUserFromToken";
|
||||||
|
export { getDocument } from "./getDocument";
|
||||||
|
|||||||
Reference in New Issue
Block a user