mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 00:03:33 +10:00
fix: tidy code and update endpoints
This commit is contained in:
58
packages/lib/server-only/share/create-or-get-share-link.ts
Normal file
58
packages/lib/server-only/share/create-or-get-share-link.ts
Normal 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: {},
|
||||
});
|
||||
};
|
||||
@ -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;
|
||||
};
|
||||
@ -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');
|
||||
};
|
||||
@ -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;
|
||||
};
|
||||
13
packages/lib/server-only/share/get-share-link-by-slug.ts
Normal file
13
packages/lib/server-only/share/get-share-link-by-slug.ts
Normal 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,
|
||||
},
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user