refactor: improve find envelopes endpoint implementation

This commit is contained in:
Ephraim Atta-Duncan
2025-11-25 21:13:01 +00:00
parent 4d29a66ba1
commit c5b7522ea0
4 changed files with 47 additions and 30 deletions

View File

@@ -1,4 +1,4 @@
import { expect, test } from '@playwright/test';
import { type APIRequestContext, expect, test } from '@playwright/test';
import type { Team, User } from '@prisma/client';
import fs from 'node:fs';
import path from 'node:path';
@@ -165,6 +165,9 @@ test.describe('API V2 Envelopes', () => {
positionY: 0,
width: 0,
height: 0,
fieldMeta: {
type: 'signature',
},
},
{
type: FieldType.SIGNATURE,
@@ -174,6 +177,9 @@ test.describe('API V2 Envelopes', () => {
positionY: 0,
width: 0,
height: 0,
fieldMeta: {
type: 'signature',
},
},
],
},
@@ -561,7 +567,7 @@ test.describe('API V2 Envelopes', () => {
test.describe('Envelope find endpoint', () => {
const createEnvelope = async (
request: ReturnType<typeof test.extend>['request'] extends Promise<infer R> ? R : never,
request: APIRequestContext,
token: string,
payload: TCreateEnvelopePayload,
) => {
@@ -747,10 +753,7 @@ test.describe('API V2 Envelopes', () => {
expect(envelope?.id).toBeDefined();
expect(envelope?.type).toBe(EnvelopeType.DOCUMENT);
expect(envelope?.status).toBe(DocumentStatus.DRAFT);
expect(envelope?.documentMeta).toBeDefined();
expect(envelope?.recipients).toBeDefined();
expect(envelope?.fields).toBeDefined();
expect(envelope?.envelopeItems).toBeDefined();
expect(envelope?.user).toBeDefined();
expect(envelope?.team).toBeDefined();
});

View File

@@ -150,18 +150,6 @@ export const findEnvelopes = async ({
[orderByColumn]: orderByDirection,
},
include: {
envelopeItems: {
select: {
envelopeId: true,
id: true,
title: true,
order: true,
},
orderBy: {
order: 'asc',
},
},
documentMeta: true,
user: {
select: {
id: true,
@@ -174,21 +162,12 @@ export const findEnvelopes = async ({
id: 'asc',
},
},
fields: true,
team: {
select: {
id: true,
url: true,
},
},
directLink: {
select: {
directTemplateRecipientId: true,
enabled: true,
id: true,
token: true,
},
},
},
}),
prisma.envelope.count({

View File

@@ -115,5 +115,40 @@ export type TEnvelopeLite = z.infer<typeof ZEnvelopeLiteSchema>;
/**
* A version of the envelope response schema when returning multiple envelopes at once from a single API endpoint.
*/
// export const ZEnvelopeManySchema = X
// export type TEnvelopeMany = z.infer<typeof ZEnvelopeManySchema>;
export const ZEnvelopeManySchema = EnvelopeSchema.pick({
internalVersion: true,
type: true,
status: true,
source: true,
visibility: true,
templateType: true,
id: true,
secondaryId: true,
externalId: true,
createdAt: true,
updatedAt: true,
completedAt: true,
deletedAt: true,
title: true,
authOptions: true,
formValues: true,
publicTitle: true,
publicDescription: true,
userId: true,
teamId: true,
folderId: true,
templateId: true,
}).extend({
user: z.object({
id: z.number(),
name: z.string(),
email: z.string(),
}),
recipients: ZEnvelopeRecipientLiteSchema.array(),
team: TeamSchema.pick({
id: true,
url: true,
}).nullable(),
});
export type TEnvelopeMany = z.infer<typeof ZEnvelopeManySchema>;

View File

@@ -1,7 +1,7 @@
import { DocumentSource, DocumentStatus, EnvelopeType } from '@prisma/client';
import { z } from 'zod';
import { ZEnvelopeSchema } from '@documenso/lib/types/envelope';
import { ZEnvelopeManySchema } from '@documenso/lib/types/envelope';
import { ZFindResultResponse, ZFindSearchParamsSchema } from '@documenso/lib/types/search-params';
import type { TrpcRouteMeta } from '../trpc';
@@ -39,7 +39,7 @@ export const ZFindEnvelopesRequestSchema = ZFindSearchParamsSchema.extend({
});
export const ZFindEnvelopesResponseSchema = ZFindResultResponse.extend({
data: ZEnvelopeSchema.array(),
data: ZEnvelopeManySchema.array(),
});
export type TFindEnvelopesRequest = z.infer<typeof ZFindEnvelopesRequestSchema>;