mirror of
https://github.com/hanzoai/sign.git
synced 2026-10-21 01:21:51 +00:00
The product is branded Hanzo eSign (esign.hanzo.ai, repo hanzoai/esign). Rename all workspace packages @hanzo/sign-* → @hanzo/esign-* (1052 files) and align docker/Dockerfile turbo --scope/--filter to the actual remix package name @hanzo/esign (was a pre-existing mismatch with -remix that broke the build). Protected deps (@hanzo/insights, @hanzo/logo) untouched.
104 lines
2.4 KiB
TypeScript
104 lines
2.4 KiB
TypeScript
import type { Envelope, Prisma } from '@prisma/client';
|
|
import { DocumentStatus, EnvelopeType, RecipientRole } from '@prisma/client';
|
|
|
|
import type { FindResultResponse } from '@hanzo/esign-lib/types/search-params';
|
|
import { mapEnvelopesToDocumentMany } from '@hanzo/esign-lib/utils/document';
|
|
import { maskRecipientTokensForDocument } from '@hanzo/esign-lib/utils/mask-recipient-tokens-for-document';
|
|
import { prisma } from '@hanzo/esign-prisma';
|
|
|
|
import { ZFindInboxRequestSchema, ZFindInboxResponseSchema } from './find-inbox.types';
|
|
|
|
export type FindInboxOptions = {
|
|
userId: number;
|
|
page?: number;
|
|
perPage?: number;
|
|
orderBy?: {
|
|
column: keyof Omit<Envelope, 'envelope'>;
|
|
direction: 'asc' | 'desc';
|
|
};
|
|
};
|
|
|
|
export const findInbox = async ({ userId, page = 1, perPage = 10, orderBy }: FindInboxOptions) => {
|
|
const user = await prisma.user.findFirstOrThrow({
|
|
where: {
|
|
id: userId,
|
|
},
|
|
select: {
|
|
id: true,
|
|
email: true,
|
|
},
|
|
});
|
|
|
|
const orderByColumn = orderBy?.column ?? 'createdAt';
|
|
const orderByDirection = orderBy?.direction ?? 'desc';
|
|
|
|
const whereClause: Prisma.EnvelopeWhereInput = {
|
|
type: EnvelopeType.DOCUMENT,
|
|
status: {
|
|
not: DocumentStatus.DRAFT,
|
|
},
|
|
deletedAt: null,
|
|
recipients: {
|
|
some: {
|
|
email: user.email,
|
|
role: {
|
|
not: RecipientRole.CC,
|
|
},
|
|
},
|
|
},
|
|
};
|
|
|
|
const [data, count] = await Promise.all([
|
|
prisma.envelope.findMany({
|
|
where: whereClause,
|
|
skip: Math.max(page - 1, 0) * perPage,
|
|
take: perPage,
|
|
orderBy: {
|
|
[orderByColumn]: orderByDirection,
|
|
},
|
|
include: {
|
|
user: {
|
|
select: {
|
|
id: true,
|
|
name: true,
|
|
email: true,
|
|
},
|
|
},
|
|
recipients: true,
|
|
team: {
|
|
select: {
|
|
id: true,
|
|
url: true,
|
|
},
|
|
},
|
|
envelopeItems: {
|
|
select: {
|
|
id: true,
|
|
envelopeId: true,
|
|
title: true,
|
|
order: true,
|
|
},
|
|
},
|
|
},
|
|
}),
|
|
prisma.envelope.count({
|
|
where: whereClause,
|
|
}),
|
|
]);
|
|
|
|
const maskedData = data.map((document) =>
|
|
maskRecipientTokensForDocument({
|
|
document,
|
|
user,
|
|
}),
|
|
);
|
|
|
|
return {
|
|
data: maskedData,
|
|
count,
|
|
currentPage: Math.max(page, 1),
|
|
perPage,
|
|
totalPages: Math.ceil(count / perPage),
|
|
} satisfies FindResultResponse<typeof data>;
|
|
};
|