Files
sign/packages/lib/server-only/recipient/get-recipient-by-id.ts

61 lines
1.3 KiB
TypeScript
Raw Normal View History

2024-12-26 17:25:14 +11:00
import type { z } from 'zod';
import { prisma } from '@documenso/prisma';
2024-12-26 17:25:14 +11:00
import { FieldSchema, RecipientSchema } from '@documenso/prisma/generated/zod';
import { AppError, AppErrorCode } from '../../errors/app-error';
export type GetRecipientByIdOptions = {
2024-12-26 17:25:14 +11:00
recipientId: number;
userId: number;
teamId?: number;
};
2024-12-26 17:25:14 +11:00
export const ZGetRecipientByIdResponseSchema = RecipientSchema.extend({
Field: FieldSchema.array(),
});
export type TGetRecipientByIdResponse = z.infer<typeof ZGetRecipientByIdResponseSchema>;
/**
* Get a recipient by ID. This will also return the recipient signing token so
* be careful when using this.
*/
export const getRecipientById = async ({
recipientId,
userId,
teamId,
}: GetRecipientByIdOptions): Promise<TGetRecipientByIdResponse> => {
const recipient = await prisma.recipient.findFirst({
where: {
2024-12-26 17:25:14 +11:00
id: recipientId,
Document: teamId
? {
team: {
id: teamId,
members: {
some: {
userId,
2024-12-26 17:25:14 +11:00
},
},
},
}
: {
userId,
teamId: null,
},
2024-12-26 17:25:14 +11:00
},
include: {
Field: true,
},
});
if (!recipient) {
2024-12-26 17:25:14 +11:00
throw new AppError(AppErrorCode.NOT_FOUND, {
message: 'Recipient not found',
});
}
return recipient;
};