mirror of
https://github.com/documenso/documenso.git
synced 2025-11-21 12:11:29 +10:00
feat: implement get envelopes by IDs route with validation schemas
This commit is contained in:
93
packages/trpc/server/envelope-router/get-envelopes-by-ids.ts
Normal file
93
packages/trpc/server/envelope-router/get-envelopes-by-ids.ts
Normal 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,
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
});
|
||||||
@ -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>;
|
||||||
Reference in New Issue
Block a user