mirror of
https://github.com/documenso/documenso.git
synced 2025-11-22 04:31:39 +10:00
Merge branch 'main' into feat/get-documents-by-ids-endpoint
This commit is contained in:
@ -26,6 +26,7 @@ import { redistributeEnvelopeRoute } from './redistribute-envelope';
|
||||
import { setEnvelopeFieldsRoute } from './set-envelope-fields';
|
||||
import { setEnvelopeRecipientsRoute } from './set-envelope-recipients';
|
||||
import { signEnvelopeFieldRoute } from './sign-envelope-field';
|
||||
import { signingStatusEnvelopeRoute } from './signing-status-envelope';
|
||||
import { updateEnvelopeRoute } from './update-envelope';
|
||||
import { updateEnvelopeItemsRoute } from './update-envelope-items';
|
||||
import { useEnvelopeRoute } from './use-envelope';
|
||||
@ -74,4 +75,5 @@ export const envelopeRouter = router({
|
||||
duplicate: duplicateEnvelopeRoute,
|
||||
distribute: distributeEnvelopeRoute,
|
||||
redistribute: redistributeEnvelopeRoute,
|
||||
signingStatus: signingStatusEnvelopeRoute,
|
||||
});
|
||||
|
||||
@ -0,0 +1,82 @@
|
||||
import { DocumentStatus, EnvelopeType, RecipientRole, SigningStatus } from '@prisma/client';
|
||||
|
||||
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
|
||||
import { prisma } from '@documenso/prisma';
|
||||
|
||||
import { maybeAuthenticatedProcedure } from '../trpc';
|
||||
import {
|
||||
ZSigningStatusEnvelopeRequestSchema,
|
||||
ZSigningStatusEnvelopeResponseSchema,
|
||||
} from './signing-status-envelope.types';
|
||||
|
||||
// Internal route - not intended for public API usage
|
||||
export const signingStatusEnvelopeRoute = maybeAuthenticatedProcedure
|
||||
.input(ZSigningStatusEnvelopeRequestSchema)
|
||||
.output(ZSigningStatusEnvelopeResponseSchema)
|
||||
.query(async ({ input, ctx }) => {
|
||||
const { token } = input;
|
||||
|
||||
ctx.logger.info({
|
||||
input: {
|
||||
token,
|
||||
},
|
||||
});
|
||||
|
||||
const envelope = await prisma.envelope.findFirst({
|
||||
where: {
|
||||
type: EnvelopeType.DOCUMENT,
|
||||
recipients: {
|
||||
some: {
|
||||
token,
|
||||
},
|
||||
},
|
||||
},
|
||||
include: {
|
||||
recipients: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
email: true,
|
||||
signingStatus: true,
|
||||
role: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!envelope) {
|
||||
throw new AppError(AppErrorCode.NOT_FOUND, {
|
||||
message: 'Envelope not found',
|
||||
});
|
||||
}
|
||||
|
||||
// Check if envelope is rejected
|
||||
if (envelope.status === DocumentStatus.REJECTED) {
|
||||
return {
|
||||
status: 'REJECTED',
|
||||
};
|
||||
}
|
||||
|
||||
if (envelope.status === DocumentStatus.COMPLETED) {
|
||||
return {
|
||||
status: 'COMPLETED',
|
||||
};
|
||||
}
|
||||
|
||||
const isComplete =
|
||||
envelope.recipients.some((recipient) => recipient.signingStatus === SigningStatus.REJECTED) ||
|
||||
envelope.recipients.every(
|
||||
(recipient) =>
|
||||
recipient.role === RecipientRole.CC || recipient.signingStatus === SigningStatus.SIGNED,
|
||||
);
|
||||
|
||||
if (isComplete) {
|
||||
return {
|
||||
status: 'PROCESSING',
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
status: 'PENDING',
|
||||
};
|
||||
});
|
||||
@ -0,0 +1,14 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
export const EnvelopeSigningStatus = z.enum(['PENDING', 'PROCESSING', 'COMPLETED', 'REJECTED']);
|
||||
|
||||
export const ZSigningStatusEnvelopeRequestSchema = z.object({
|
||||
token: z.string().describe('The recipient token to check the signing status for'),
|
||||
});
|
||||
|
||||
export const ZSigningStatusEnvelopeResponseSchema = z.object({
|
||||
status: EnvelopeSigningStatus.describe('The current signing status of the envelope'),
|
||||
});
|
||||
|
||||
export type TSigningStatusEnvelopeRequest = z.infer<typeof ZSigningStatusEnvelopeRequestSchema>;
|
||||
export type TSigningStatusEnvelopeResponse = z.infer<typeof ZSigningStatusEnvelopeResponseSchema>;
|
||||
Reference in New Issue
Block a user