mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 08:13:56 +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: {},
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user