fix: tidy code and update endpoints

This commit is contained in:
Mythie
2023-09-21 00:51:02 +00:00
parent bcb163693a
commit 4b13a42731
19 changed files with 344 additions and 151 deletions

View File

@ -0,0 +1,58 @@
import { P, match } from 'ts-pattern';
import { prisma } from '@documenso/prisma';
import { alphaid } from '../../universal/id';
export type CreateSharingIdOptions =
| {
documentId: number;
token: string;
}
| {
documentId: number;
userId: number;
};
export const createOrGetShareLink = async ({ documentId, ...options }: CreateSharingIdOptions) => {
const email = await match(options)
.with({ token: P.string }, async ({ token }) => {
return await prisma.recipient
.findFirst({
where: {
documentId,
token,
},
})
.then((recipient) => recipient?.email);
})
.with({ userId: P.number }, async ({ userId }) => {
return await prisma.user
.findFirst({
where: {
id: userId,
},
})
.then((user) => user?.email);
})
.exhaustive();
if (!email) {
throw new Error('Unable to create share link for document with the given email');
}
return await prisma.documentShareLink.upsert({
where: {
documentId_email: {
email,
documentId,
},
},
create: {
email,
documentId,
slug: alphaid(14),
},
update: {},
});
};

View File

@ -1,20 +0,0 @@
import { nanoid } from 'nanoid';
import { prisma } from '@documenso/prisma';
export interface CreateSharingIdOptions {
documentId: number;
recipientId: number;
}
export const createSharingId = async ({ documentId, recipientId }: CreateSharingIdOptions) => {
const result = await prisma.share.create({
data: {
recipientId,
documentId,
link: nanoid(),
},
});
return result;
};

View File

@ -0,0 +1,42 @@
import { prisma } from '@documenso/prisma';
export type GetRecipientOrSenderByShareLinkSlugOptions = {
slug: string;
};
export const getRecipientOrSenderByShareLinkSlug = async ({
slug,
}: GetRecipientOrSenderByShareLinkSlugOptions) => {
const { documentId, email } = await prisma.documentShareLink.findFirstOrThrow({
where: {
slug,
},
});
const recipient = await prisma.recipient.findFirst({
where: {
documentId,
email,
},
include: {
Signature: true,
},
});
if (recipient) {
return recipient;
}
const sender = await prisma.user.findFirst({
where: {
Document: { some: { id: documentId } },
email,
},
});
if (sender) {
return sender;
}
throw new Error('Recipient or sender not found');
};

View File

@ -1,18 +0,0 @@
import { prisma } from '@documenso/prisma';
export interface GetSharingIdOptions {
shareId: string;
}
export const getSharingId = async ({ shareId }: GetSharingIdOptions) => {
const result = await prisma.share.findUnique({
where: {
link: shareId,
},
include: {
recipent: true,
},
});
return result;
};

View File

@ -0,0 +1,13 @@
import { prisma } from '@documenso/prisma';
export type GetShareLinkBySlugOptions = {
slug: string;
};
export const getShareLinkBySlug = async ({ slug }: GetShareLinkBySlugOptions) => {
return await prisma.documentShareLink.findFirstOrThrow({
where: {
slug,
},
});
};

View File

@ -0,0 +1,35 @@
/*
Warnings:
- You are about to drop the `Share` table. If the table is not empty, all the data it contains will be lost.
*/
-- DropForeignKey
ALTER TABLE "Share" DROP CONSTRAINT "Share_documentId_fkey";
-- DropForeignKey
ALTER TABLE "Share" DROP CONSTRAINT "Share_recipientId_fkey";
-- DropTable
DROP TABLE "Share";
-- CreateTable
CREATE TABLE "DocumentShareLink" (
"id" SERIAL NOT NULL,
"email" TEXT NOT NULL,
"slug" TEXT NOT NULL,
"documentId" INTEGER NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "DocumentShareLink_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE UNIQUE INDEX "DocumentShareLink_slug_key" ON "DocumentShareLink"("slug");
-- CreateIndex
CREATE UNIQUE INDEX "DocumentShareLink_documentId_email_key" ON "DocumentShareLink"("documentId", "email");
-- AddForeignKey
ALTER TABLE "DocumentShareLink" ADD CONSTRAINT "DocumentShareLink_documentId_fkey" FOREIGN KEY ("documentId") REFERENCES "Document"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

View File

@ -101,18 +101,18 @@ enum DocumentStatus {
}
model Document {
id Int @id @default(autoincrement())
id Int @id @default(autoincrement())
userId Int
User User @relation(fields: [userId], references: [id], onDelete: Cascade)
User User @relation(fields: [userId], references: [id], onDelete: Cascade)
title String
status DocumentStatus @default(DRAFT)
status DocumentStatus @default(DRAFT)
Recipient Recipient[]
Field Field[]
Share Share[]
ShareLink DocumentShareLink[]
documentDataId String
documentData DocumentData @relation(fields: [documentDataId], references: [id], onDelete: Cascade)
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
documentData DocumentData @relation(fields: [documentDataId], references: [id], onDelete: Cascade)
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
@@unique([documentDataId])
}
@ -160,7 +160,6 @@ model Recipient {
Document Document @relation(fields: [documentId], references: [id], onDelete: Cascade)
Field Field[]
Signature Signature[]
Share Share[]
@@unique([documentId, email])
}
@ -203,13 +202,15 @@ model Signature {
Field Field @relation(fields: [fieldId], references: [id], onDelete: Restrict)
}
model Share {
id Int @id @default(autoincrement())
recipientId Int
recipent Recipient @relation(fields: [recipientId], references: [id])
link String @unique
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
document Document? @relation(fields: [documentId], references: [id])
documentId Int?
model DocumentShareLink {
id Int @id @default(autoincrement())
email String
slug String @unique
documentId Int
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
document Document @relation(fields: [documentId], references: [id])
@@unique([documentId, email])
}

View File

@ -2,7 +2,7 @@ import { authRouter } from './auth-router/router';
import { documentRouter } from './document-router/router';
import { fieldRouter } from './field-router/router';
import { profileRouter } from './profile-router/router';
import { shareRouter } from './share-router/router';
import { shareLinkRouter } from './share-link-router/router';
import { procedure, router } from './trpc';
export const appRouter = router({
@ -11,7 +11,7 @@ export const appRouter = router({
profile: profileRouter,
document: documentRouter,
field: fieldRouter,
share: shareRouter,
shareLink: shareLinkRouter,
});
export type AppRouter = typeof appRouter;

View File

@ -0,0 +1,35 @@
import { TRPCError } from '@trpc/server';
import { createOrGetShareLink } from '@documenso/lib/server-only/share/create-or-get-share-link';
import { procedure, router } from '../trpc';
import { ZCreateOrGetShareLinkMutationSchema } from './schema';
export const shareLinkRouter = router({
createOrGetShareLink: procedure
.input(ZCreateOrGetShareLinkMutationSchema)
.mutation(async ({ ctx, input }) => {
try {
const { documentId, token } = input;
if (token) {
return await createOrGetShareLink({ documentId, token });
}
if (!ctx.user?.id) {
throw new Error(
'You must either provide a token or be logged in to create a sharing link.',
);
}
return await createOrGetShareLink({ documentId, userId: ctx.user.id });
} catch (err) {
console.error(err);
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'We were unable to create a sharing link.',
});
}
}),
});

View File

@ -0,0 +1,10 @@
import { z } from 'zod';
export const ZCreateOrGetShareLinkMutationSchema = z.object({
documentId: z.number(),
token: z.string().optional(),
});
export type TCreateOrGetShareLinkMutationSchema = z.infer<
typeof ZCreateOrGetShareLinkMutationSchema
>;

View File

@ -1,38 +0,0 @@
import { TRPCError } from '@trpc/server';
import { createSharingId } from '@documenso/lib/server-only/share/create-share-id';
import { getSharingId } from '@documenso/lib/server-only/share/get-share-id';
import { procedure, router } from '../trpc';
import { ZShareLinkCreateSchema, ZShareLinkGetSchema } from './schema';
export const shareRouter = router({
create: procedure.input(ZShareLinkCreateSchema).mutation(async ({ input }) => {
try {
const { documentId, recipientId } = input;
return await createSharingId({ documentId, recipientId });
} catch (err) {
console.error(err);
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'We were unable to create a sharing link.',
});
}
}),
get: procedure.input(ZShareLinkGetSchema).query(async ({ input }) => {
try {
const { shareId } = input;
return await getSharingId({ shareId });
} catch (err) {
console.error(err);
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'We were unable to create a sharing link.',
});
}
}),
});

View File

@ -1,13 +0,0 @@
import { z } from 'zod';
export const ZShareLinkCreateSchema = z.object({
documentId: z.number(),
recipientId: z.number(),
});
export const ZShareLinkGetSchema = z.object({
shareId: z.string(),
});
export type ZShareLinkCreateSchema = z.infer<typeof ZShareLinkCreateSchema>;
export type ZShareLinkGetSchema = z.infer<typeof ZShareLinkGetSchema>;