feat: add field and recipient endpoints

This commit is contained in:
Mythie
2024-01-29 22:53:15 +11:00
parent 5a28eaa4ff
commit 98df273ebc
11 changed files with 720 additions and 8 deletions

View File

@ -0,0 +1,41 @@
import { prisma } from '@documenso/prisma';
import type { FieldType } from '@documenso/prisma/client';
export type CreateFieldOptions = {
documentId: number;
recipientId: number;
type: FieldType;
pageNumber: number;
pageX: number;
pageY: number;
pageWidth: number;
pageHeight: number;
};
export const createField = async ({
documentId,
recipientId,
type,
pageNumber,
pageX,
pageY,
pageWidth,
pageHeight,
}: CreateFieldOptions) => {
const field = await prisma.field.create({
data: {
documentId,
recipientId,
type,
page: pageNumber,
positionX: pageX,
positionY: pageY,
width: pageWidth,
height: pageHeight,
customText: '',
inserted: false,
},
});
return field;
};

View File

@ -0,0 +1,17 @@
import { prisma } from '@documenso/prisma';
export type DeleteFieldOptions = {
fieldId: number;
documentId: number;
};
export const deleteField = async ({ fieldId, documentId }: DeleteFieldOptions) => {
const field = await prisma.field.delete({
where: {
id: fieldId,
documentId,
},
});
return field;
};

View File

@ -0,0 +1,17 @@
import { prisma } from '@documenso/prisma';
export type GetFieldByIdOptions = {
fieldId: number;
documentId: number;
};
export const getFieldById = async ({ fieldId, documentId }: GetFieldByIdOptions) => {
const field = await prisma.field.findFirst({
where: {
id: fieldId,
documentId,
},
});
return field;
};

View File

@ -0,0 +1,44 @@
import { prisma } from '@documenso/prisma';
import type { FieldType } from '@documenso/prisma/client';
export type UpdateFieldOptions = {
fieldId: number;
documentId: number;
recipientId?: number;
type?: FieldType;
pageNumber?: number;
pageX?: number;
pageY?: number;
pageWidth?: number;
pageHeight?: number;
};
export const updateField = async ({
fieldId,
documentId,
recipientId,
type,
pageNumber,
pageX,
pageY,
pageWidth,
pageHeight,
}: UpdateFieldOptions) => {
const field = await prisma.field.update({
where: {
id: fieldId,
documentId,
},
data: {
recipientId,
type,
page: pageNumber,
positionX: pageX,
positionY: pageY,
width: pageWidth,
height: pageHeight,
},
});
return field;
};

View File

@ -0,0 +1,32 @@
import { prisma } from '@documenso/prisma';
import { SendStatus } from '@documenso/prisma/client';
export type DeleteRecipientOptions = {
documentId: number;
recipientId: number;
};
export const deleteRecipient = async ({ documentId, recipientId }: DeleteRecipientOptions) => {
const recipient = await prisma.recipient.findFirst({
where: {
id: recipientId,
documentId,
},
});
if (!recipient) {
throw new Error('Recipient not found');
}
if (recipient.sendStatus !== SendStatus.NOT_SENT) {
throw new Error('Can not delete a recipient that has already been sent a document');
}
const deletedRecipient = await prisma.recipient.delete({
where: {
id: recipient.id,
},
});
return deletedRecipient;
};

View File

@ -0,0 +1,21 @@
import { prisma } from '@documenso/prisma';
export type GetRecipientByEmailOptions = {
documentId: number;
email: string;
};
export const getRecipientByEmail = async ({ documentId, email }: GetRecipientByEmailOptions) => {
const recipient = await prisma.recipient.findFirst({
where: {
documentId,
email: email.toLowerCase(),
},
});
if (!recipient) {
throw new Error('Recipient not found');
}
return recipient;
};

View File

@ -0,0 +1,21 @@
import { prisma } from '@documenso/prisma';
export type GetRecipientByIdOptions = {
id: number;
documentId: number;
};
export const getRecipientById = async ({ documentId, id }: GetRecipientByIdOptions) => {
const recipient = await prisma.recipient.findFirst({
where: {
documentId,
id,
},
});
if (!recipient) {
throw new Error('Recipient not found');
}
return recipient;
};

View File

@ -0,0 +1,38 @@
import { prisma } from '@documenso/prisma';
export type UpdateRecipientOptions = {
documentId: number;
recipientId: number;
email?: string;
name?: string;
};
export const updateRecipient = async ({
documentId,
recipientId,
email,
name,
}: UpdateRecipientOptions) => {
const recipient = await prisma.recipient.findFirst({
where: {
id: recipientId,
documentId,
},
});
if (!recipient) {
throw new Error('Recipient not found');
}
const updatedRecipient = await prisma.recipient.update({
where: {
id: recipient.id,
},
data: {
email: email?.toLowerCase() ?? recipient.email,
name: name ?? recipient.name,
},
});
return updatedRecipient;
};