feat: implement get envelopes by IDs route with validation schemas

This commit is contained in:
Catalin Pit
2025-11-20 12:07:15 +02:00
parent b3b8a35446
commit 1fe7f78479
2 changed files with 117 additions and 0 deletions

View File

@ -0,0 +1,93 @@
import { getEnvelopeWhereInput } from '@documenso/lib/server-only/envelope/get-envelope-by-id';
import { prisma } from '@documenso/prisma';
import { authenticatedProcedure } from '../trpc';
import {
ZGetEnvelopesByIdsRequestSchema,
ZGetEnvelopesByIdsResponseSchema,
getEnvelopesByIdsMeta,
} from './get-envelopes-by-ids.types';
export const getEnvelopesByIdsRoute = authenticatedProcedure
.meta(getEnvelopesByIdsMeta)
.input(ZGetEnvelopesByIdsRequestSchema)
.output(ZGetEnvelopesByIdsResponseSchema)
.mutation(async ({ input, ctx }) => {
const { teamId, user } = ctx;
const { envelopeIds } = input;
ctx.logger.info({
input: {
envelopeIds,
},
});
const { envelopeWhereInput } = await getEnvelopeWhereInput({
id: {
type: 'envelopeId',
id: envelopeIds[0],
},
userId: user.id,
teamId,
type: null,
});
const envelopeOrInput = envelopeWhereInput.OR!;
const envelopes = await prisma.envelope.findMany({
where: {
id: {
in: envelopeIds,
},
OR: envelopeOrInput,
},
include: {
envelopeItems: {
include: {
documentData: true,
},
orderBy: {
order: 'asc',
},
},
folder: true,
documentMeta: true,
user: {
select: {
id: true,
name: true,
email: true,
},
},
recipients: {
orderBy: {
id: 'asc',
},
},
fields: true,
team: {
select: {
id: true,
url: true,
},
},
directLink: {
select: {
directTemplateRecipientId: true,
enabled: true,
id: true,
token: true,
},
},
},
});
return envelopes.map((envelope) => ({
...envelope,
user: {
id: envelope.user.id,
name: envelope.user.name || '',
email: envelope.user.email,
},
}));
});

View File

@ -0,0 +1,24 @@
import { z } from 'zod';
import { ZEnvelopeSchema } from '@documenso/lib/types/envelope';
import type { TrpcRouteMeta } from '../trpc';
export const getEnvelopesByIdsMeta: TrpcRouteMeta = {
openapi: {
method: 'POST',
path: '/envelope/get-many',
summary: 'Get multiple envelopes',
description: 'Retrieve multiple envelopes by their IDs',
tags: ['Envelope'],
},
};
export const ZGetEnvelopesByIdsRequestSchema = z.object({
envelopeIds: z.array(z.string()).min(1),
});
export const ZGetEnvelopesByIdsResponseSchema = z.array(ZEnvelopeSchema);
export type TGetEnvelopesByIdsRequest = z.infer<typeof ZGetEnvelopesByIdsRequestSchema>;
export type TGetEnvelopesByIdsResponse = z.infer<typeof ZGetEnvelopesByIdsResponseSchema>;