mirror of
https://github.com/documenso/documenso.git
synced 2025-11-15 09:12:02 +10:00
feat: update db on template placeholder recipient removal
This commit is contained in:
@ -0,0 +1,63 @@
|
||||
import { prisma } from '@documenso/prisma';
|
||||
import { SendStatus } from '@documenso/prisma/client';
|
||||
|
||||
export type DeleteRecipientForTemplateOptions = {
|
||||
templateId: number;
|
||||
recipientId: number;
|
||||
userId: number;
|
||||
teamId?: number;
|
||||
};
|
||||
|
||||
export const deleteRecipientFromTemplate = async ({
|
||||
templateId,
|
||||
recipientId,
|
||||
userId,
|
||||
teamId,
|
||||
}: DeleteRecipientForTemplateOptions) => {
|
||||
const recipient = await prisma.recipient.findFirst({
|
||||
where: {
|
||||
id: recipientId,
|
||||
Template: {
|
||||
id: templateId,
|
||||
...(teamId
|
||||
? {
|
||||
team: {
|
||||
id: teamId,
|
||||
members: {
|
||||
some: {
|
||||
userId,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
: {
|
||||
userId,
|
||||
teamId: null,
|
||||
}),
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
console.log('deleteRecipientFromTemplate function pure', recipient);
|
||||
|
||||
console.log('deleteRecipientFromTemplate function pure', {
|
||||
templateId,
|
||||
recipientId,
|
||||
userId,
|
||||
teamId,
|
||||
});
|
||||
|
||||
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');
|
||||
}
|
||||
|
||||
return await prisma.recipient.delete({
|
||||
where: {
|
||||
id: recipient.id,
|
||||
},
|
||||
});
|
||||
};
|
||||
@ -2,6 +2,7 @@ import { TRPCError } from '@trpc/server';
|
||||
|
||||
import { completeDocumentWithToken } from '@documenso/lib/server-only/document/complete-document-with-token';
|
||||
import { deleteRecipient } from '@documenso/lib/server-only/recipient/delete-recipient';
|
||||
import { deleteRecipientFromTemplate } from '@documenso/lib/server-only/recipient/delete-recipient-from-template';
|
||||
import { setRecipientsForDocument } from '@documenso/lib/server-only/recipient/set-recipients-for-document';
|
||||
import { setRecipientsForTemplate } from '@documenso/lib/server-only/recipient/set-recipients-for-template';
|
||||
import { extractNextApiRequestMetadata } from '@documenso/lib/universal/extract-request-metadata';
|
||||
@ -76,7 +77,23 @@ export const recipientRouter = router({
|
||||
removeTemplateSigner: authenticatedProcedure
|
||||
.input(ZRemoveTemplateSignerMutationSchema)
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
// TODO: Implement
|
||||
try {
|
||||
const { templateId, recipientId, teamId } = input;
|
||||
const userId = ctx.user.id;
|
||||
|
||||
return await deleteRecipientFromTemplate({
|
||||
userId,
|
||||
templateId,
|
||||
teamId,
|
||||
recipientId,
|
||||
});
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
throw new TRPCError({
|
||||
code: 'BAD_REQUEST',
|
||||
message: 'We were unable to remove the recipient. Please try again later.',
|
||||
});
|
||||
}
|
||||
}),
|
||||
|
||||
removeSigner: authenticatedProcedure
|
||||
|
||||
@ -94,7 +94,6 @@ export const AddTemplatePlaceholderRecipientsFormPartial = ({
|
||||
const { mutateAsync: removeTemplateSigner } = trpc.recipient.removeTemplateSigner.useMutation({
|
||||
...DO_NOT_INVALIDATE_QUERY_ON_MUTATION,
|
||||
onSuccess: (deletedRecipient) => {
|
||||
console.log('deletedRecipient', deletedRecipient);
|
||||
utils.template.getTemplateWithDetailsById.setData(
|
||||
{
|
||||
id: template.id,
|
||||
@ -103,7 +102,7 @@ export const AddTemplatePlaceholderRecipientsFormPartial = ({
|
||||
if (!oldData) return template;
|
||||
return {
|
||||
...oldData,
|
||||
recipients: oldData.recipients.filter((r) => r.id !== deletedRecipient.id),
|
||||
recipients: oldData.Recipient.filter((r) => r.id !== deletedRecipient.id),
|
||||
};
|
||||
},
|
||||
);
|
||||
@ -214,8 +213,6 @@ export const AddTemplatePlaceholderRecipientsFormPartial = ({
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('Updating signer', currentSigner);
|
||||
|
||||
await addTemplateSigners({
|
||||
templateId: template.id,
|
||||
teamId: template.teamId ?? undefined,
|
||||
@ -236,8 +233,6 @@ export const AddTemplatePlaceholderRecipientsFormPartial = ({
|
||||
const handleRemoveSigner = async (index: number) => {
|
||||
const signer = signers[index];
|
||||
|
||||
console.log('signer', signer);
|
||||
|
||||
if (!signer) {
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user