Skip to content

The Open Source DocuSign Alternative.

chore: revert find-documents change for now

Mythie5724e73d49fc
diff --git a/packages/lib/server-only/document/find-documents.ts b/packages/lib/server-only/document/find-documents.ts
index ed3e775c4..c8b06236b 100644
--- a/packages/lib/server-only/document/find-documents.ts
+++ b/packages/lib/server-only/document/find-documents.ts
@@ -1,5 +1,3 @@
-import { sql } from 'kysely';
-import { jsonArrayFrom, jsonObjectFrom } from 'kysely/helpers/postgres';
import { DateTime } from 'luxon';
import { P, match } from 'ts-pattern';
@@ -62,22 +60,6 @@ export const findDocuments = async ({
teamEmail: true,
},
});
-
- const teamQuery = await prisma.$kysely
- .selectFrom('Team')
- .selectAll('Team')
- .where('Team.id', '=', teamId)
- .select((eb) => [
- jsonObjectFrom(
- eb
- .selectFrom('TeamEmail')
- .selectAll('TeamEmail')
- .where('TeamEmail.teamId', '=', teamId),
- ).as('teamEmail'),
- ])
- .innerJoin('TeamMember', 'TeamMember.teamId', 'Team.id')
- .where('TeamMember.userId', '=', userId)
- .execute();
}
return {
@@ -187,329 +169,6 @@ export const findDocuments = async ({
};
}
- let dataQuery = prisma.$kysely
- .selectFrom('Document')
- .selectAll('Document')
- .select((eb) => [
- jsonObjectFrom(
- eb
- .selectFrom('User')
- .select(['id', 'name', 'email'])
- .whereRef('User.id', '=', 'Document.userId'),
- ).as('User'),
- jsonArrayFrom(
- eb
- .selectFrom('Recipient')
- .selectAll('Recipient')
- .whereRef('Recipient.documentId', '=', 'Document.id'),
- ).as('Recipient'),
- jsonObjectFrom(
- eb.selectFrom('Team').select(['id', 'url']).whereRef('Team.id', '=', 'Document.teamId'),
- ).as('team'),
- ]);
-
- if (term && term.length >= 1) {
- dataQuery = dataQuery.where('Document.title', 'ilike', `%${term}%`);
- }
-
- if (period) {
- const daysAgo = parseInt(period.replace(/d$/, ''), 10);
- const startOfPeriod = DateTime.now().minus({ days: daysAgo }).startOf('day');
- dataQuery = dataQuery.where('Document.createdAt', '>=', startOfPeriod.toJSDate());
- }
-
- if (senderIds && senderIds.length > 0) {
- dataQuery = dataQuery.where('Document.userId', 'in', senderIds);
- }
-
- if (team) {
- if (ExtendedDocumentStatus.ALL === status) {
- dataQuery = dataQuery.where((eb) => {
- const ors = [eb('Document.teamId', '=', team.id)];
-
- if (team.teamEmail) {
- ors.push(
- eb.and([
- eb.not(eb(sql`CAST("Document"."status" AS TEXT)`, '=', ExtendedDocumentStatus.DRAFT)),
- eb.exists(
- eb
- .selectFrom('Recipient')
- .selectAll('Recipient')
- .whereRef('Recipient.documentId', '=', 'Document.id')
- .where('Recipient.email', '=', team.teamEmail.email),
- ),
- ]),
- );
-
- ors.push(
- eb.exists(
- eb
- .selectFrom('User')
- .selectAll('User')
- .where('User.email', '=', team.teamEmail.email),
- ),
- );
- }
-
- return eb.or(ors);
- });
- } else if (ExtendedDocumentStatus.INBOX === status) {
- if (team.teamEmail) {
- dataQuery = dataQuery.where((eb) => {
- const ands = [];
-
- if (team.teamEmail) {
- ands.push(
- eb.and([
- eb.not(
- eb(sql`CAST("Document"."status" AS TEXT)`, '=', ExtendedDocumentStatus.DRAFT),
- ),
- eb.exists(
- eb
- .selectFrom('Recipient')
- .selectAll('Recipient')
- .whereRef('Recipient.documentId', '=', 'Document.id')
- .where('Recipient.email', '=', team.teamEmail.email)
- .where(
- sql`CAST("Recipient"."signingStatus" AS TEXT)`,
- '=',
- SigningStatus.NOT_SIGNED,
- )
- .where(sql`CAST("Recipient"."role" AS TEXT)`, '!=', RecipientRole.CC),
- ),
- ]),
- );
- }
-
- return eb.and(ands);
- });
- }
- } else if (ExtendedDocumentStatus.DRAFT === status) {
- dataQuery = dataQuery.where((eb) => {
- const ors = [
- eb.and([
- eb('Document.teamId', '=', team.id),
- eb(sql`CAST("Document"."status" AS TEXT)`, '=', ExtendedDocumentStatus.DRAFT),
- ]),
- ];
-
- if (team.teamEmail) {
- ors.push(
- eb.and([
- eb(sql`CAST("Document"."status" AS TEXT)`, '=', ExtendedDocumentStatus.DRAFT),
- eb.exists(
- eb
- .selectFrom('User')
- .selectAll('User')
- .whereRef('userId', '=', 'Document.id')
- .where('User.email', '=', team.teamEmail.email),
- ),
- ]),
- );
- }
-
- return eb.or(ors);
- });
- } else if (ExtendedDocumentStatus.PENDING === status) {
- dataQuery = dataQuery.where((eb) => {
- const ors = [
- eb.and([
- eb('Document.teamId', '=', team.id),
- eb(sql`CAST("Document"."status" AS TEXT)`, '=', ExtendedDocumentStatus.PENDING),
- ]),
- ];
-
- if (team.teamEmail) {
- ors.push(
- eb.or([
- eb.and([
- eb(sql`CAST("Document"."status" AS TEXT)`, '=', ExtendedDocumentStatus.PENDING),
- eb.and([
- eb.exists(
- eb
- .selectFrom('User')
- .selectAll('User')
- .whereRef('userId', '=', 'Document.id')
- .where('User.email', '=', team.teamEmail.email),
- ),
- eb.exists(
- eb
- .selectFrom('Recipient')
- .selectAll('Recipient')
- .where('Recipient.email', '=', team.teamEmail.email)
- .where(
- sql`CAST("Recipient"."signingStatus" AS TEXT)`,
- '=',
- SigningStatus.SIGNED,
- )
- .where(sql`CAST("Recipient"."role" AS TEXT)`, '!=', RecipientRole.CC),
- ),
- ]),
- ]),
- ]),
- );
- }
-
- return eb.or(ors);
- });
- } else if (ExtendedDocumentStatus.COMPLETED === status) {
- dataQuery = dataQuery.where((eb) => {
- const ors = [];
-
- if (team.teamEmail) {
- ors.push(
- eb(sql`CAST("Document"."status" AS TEXT)`, '=', ExtendedDocumentStatus.COMPLETED),
- eb.or([
- eb('Document.teamId', '=', team.id),
- eb.and([
- eb.exists(
- eb
- .selectFrom('User')
- .selectAll('User')
- .whereRef('userId', '=', 'Document.id')
- .where('User.email', '=', team.teamEmail.email),
- ),
- eb.exists(
- eb
- .selectFrom('Recipient')
- .selectAll('Recipient')
- .where('Recipient.email', '=', team.teamEmail.email)
- .where(
- sql`CAST("Recipient"."signingStatus" AS TEXT)`,
- '=',
- SigningStatus.SIGNED,
- )
- .where(sql`CAST("Recipient"."role" AS TEXT)`, '!=', RecipientRole.CC),
- ),
- ]),
- ]),
- );
- }
-
- return eb.and(ors);
- });
- }
- } else if (user) {
- if (ExtendedDocumentStatus.ALL === status) {
- dataQuery = dataQuery.where(({ eb, or, and, exists }) => {
- return or([
- and([eb('Document.userId', '=', user.id), eb('Document.teamId', 'is', null)]),
- and([
- eb(sql`CAST("Document"."status" AS TEXT)`, '=', ExtendedDocumentStatus.COMPLETED),
- exists(
- eb
- .selectFrom('Recipient')
- .selectAll('Recipient')
- .whereRef('Recipient.documentId', '=', 'Document.id')
- .where('Recipient.email', '=', user.email),
- ),
- ]),
- and([
- eb(sql`CAST("Document"."status" AS TEXT)`, '=', ExtendedDocumentStatus.PENDING),
- exists(
- eb
- .selectFrom('Recipient')
- .selectAll('Recipient')
- .whereRef('Recipient.documentId', '=', 'Document.id')
- .where('Recipient.email', '=', user.email),
- ),
- ]),
- ]);
- });
- } else if (ExtendedDocumentStatus.INBOX === status) {
- dataQuery = dataQuery.where(({ eb, and, not, exists }) => {
- return and([
- not(eb(sql`CAST("Document"."status" AS TEXT)`, '=', ExtendedDocumentStatus.DRAFT)),
- exists(
- eb
- .selectFrom('Recipient')
- .selectAll('Recipient')
- .whereRef('Recipient.documentId', '=', 'Document.id')
- .where('Recipient.email', '=', user.email)
- .where(sql`CAST("Recipient"."signingStatus" AS TEXT)`, '=', SigningStatus.NOT_SIGNED)
- .where(sql`CAST("Recipient"."role" AS TEXT)`, '!=', RecipientRole.CC),
- ),
- ]);
- });
- } else if (ExtendedDocumentStatus.DRAFT === status) {
- dataQuery = dataQuery.where(({ eb, and }) => {
- return and([
- eb('Document.userId', '=', user.id),
- eb('Document.teamId', 'is', null),
- eb(sql`CAST("Document"."status" AS TEXT)`, '=', ExtendedDocumentStatus.DRAFT),
- ]);
- });
- } else if (ExtendedDocumentStatus.PENDING === status) {
- dataQuery = dataQuery.where(({ eb, or, and, exists }) => {
- return or([
- and([
- eb('Document.userId', '=', user.id),
- eb('Document.teamId', 'is', null),
- eb(sql`CAST("Document"."status" AS TEXT)`, '=', ExtendedDocumentStatus.PENDING),
- ]),
- and([
- eb(sql`CAST("Document"."status" AS TEXT)`, '=', ExtendedDocumentStatus.PENDING),
- exists(
- eb
- .selectFrom('Recipient')
- .selectAll('Recipient')
- .whereRef('Recipient.documentId', '=', 'Document.id')
- .where('Recipient.email', '=', user.email)
- .where(sql`CAST("Recipient"."signingStatus" AS TEXT)`, '=', SigningStatus.SIGNED)
- .where(sql`CAST("Recipient"."role" AS TEXT)`, '!=', RecipientRole.CC),
- ),
- ]),
- ]);
- });
- } else if (ExtendedDocumentStatus.COMPLETED === status) {
- dataQuery = dataQuery.where(({ eb, or, exists, and }) => {
- return or([
- and([
- eb('Document.userId', '=', user.id),
- eb('Document.teamId', 'is', null),
- eb(sql`CAST("Document"."status" AS TEXT)`, '=', ExtendedDocumentStatus.COMPLETED),
- ]),
- and([
- eb(sql`CAST("Document"."status" AS TEXT)`, '=', ExtendedDocumentStatus.COMPLETED),
- exists(
- eb
- .selectFrom('Recipient')
- .selectAll('Recipient')
- .whereRef('Recipient.documentId', '=', 'Document.id')
- .where('Recipient.email', '=', user.email),
- ),
- ]),
- ]);
- });
- }
- } else {
- return {
- data: [],
- count: 0,
- currentPage: 1,
- perPage,
- totalPages: 0,
- };
- }
-
- dataQuery = dataQuery.where(({ eb, or, and, not }) => {
- return and([
- or([
- eb(sql`CAST("Document"."status" AS TEXT)`, '=', ExtendedDocumentStatus.COMPLETED),
- and([
- not(eb(sql`CAST("Document"."status" AS TEXT)`, '=', ExtendedDocumentStatus.COMPLETED)),
- eb('Document.deletedAt', 'is', null),
- ]),
- ]),
- ]);
- });
-
- const finalQuery = await dataQuery
- .offset(Math.max(page - 1, 0) * perPage)
- .limit(perPage)
- .orderBy(orderByColumn, orderByDirection)
- .execute();
-
const [data, count] = await Promise.all([
prisma.document.findMany({
where: whereClause,
@@ -540,56 +199,7 @@ export const findDocuments = async ({
}),
]);
- const formattedFinalQuery = finalQuery.map((item) => {
- return {
- id: item.id,
- userId: item.userId,
- title: item.title,
- templateId: item.templateId,
- status: item.status,
- documentDataId: item.documentDataId,
- createdAt: item.createdAt,
- updatedAt: item.updatedAt,
- completedAt: item.completedAt,
- deletedAt: item.deletedAt,
- teamId: item.teamId,
- team:
- item.team && 'id' in item.team && 'url' in item.team
- ? {
- id: item.team.id,
- url: item.team.url,
- }
- : null,
- User: {
- // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
- id: (item.User as { id: number }).id,
- // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
- name: (item.User as { name: string | null }).name,
- // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
- email: (item.User as { email: string }).email,
- },
- Recipient: Array.isArray(item.Recipient)
- ? item.Recipient.map((recipient) => ({
- id: recipient?.id,
- documentId: recipient?.documentId,
- templateId: recipient?.templateId,
- email: recipient?.email,
- name: recipient?.name,
- role: recipient?.role,
- signingStatus: recipient?.signingStatus,
- token: recipient?.token,
- expired: recipient?.expired,
- readStatus: recipient?.readStatus,
- sendStatus: recipient?.sendStatus,
- signedAt: recipient?.signedAt,
- createdAt: item.createdAt,
- updatedAt: item.updatedAt,
- }))
- : [],
- };
- });
-
- const maskedData = formattedFinalQuery.map((document) =>
+ const maskedData = data.map((document) =>
maskRecipientTokensForDocument({
document,
user,