2024-02-06 16:16:10 +11:00
|
|
|
import Link from 'next/link';
|
|
|
|
|
import { redirect } from 'next/navigation';
|
|
|
|
|
|
2024-08-27 20:34:39 +09:00
|
|
|
import { Plural, Trans } from '@lingui/macro';
|
|
|
|
|
import { useLingui } from '@lingui/react';
|
2024-02-15 18:20:10 +11:00
|
|
|
import { ChevronLeft, Clock9, Users2 } from 'lucide-react';
|
2024-02-12 17:30:23 +11:00
|
|
|
import { match } from 'ts-pattern';
|
2024-02-06 16:16:10 +11:00
|
|
|
|
|
|
|
|
import { DOCUMENSO_ENCRYPTION_KEY } from '@documenso/lib/constants/crypto';
|
|
|
|
|
import { getRequiredServerComponentSession } from '@documenso/lib/next-auth/get-server-component-session';
|
|
|
|
|
import { getDocumentById } from '@documenso/lib/server-only/document/get-document-by-id';
|
2024-02-19 14:31:26 +11:00
|
|
|
import { getServerComponentFlag } from '@documenso/lib/server-only/feature-flags/get-server-component-feature-flag';
|
2024-06-24 06:08:06 +00:00
|
|
|
import { getFieldsForDocument } from '@documenso/lib/server-only/field/get-fields-for-document';
|
2024-02-06 16:16:10 +11:00
|
|
|
import { getRecipientsForDocument } from '@documenso/lib/server-only/recipient/get-recipients-for-document';
|
2024-09-16 17:14:16 +03:00
|
|
|
import { DocumentVisibility } from '@documenso/lib/types/document-visibility';
|
2024-02-06 16:16:10 +11:00
|
|
|
import { symmetricDecrypt } from '@documenso/lib/universal/crypto';
|
|
|
|
|
import { formatDocumentsPath } from '@documenso/lib/utils/teams';
|
2024-02-15 18:20:10 +11:00
|
|
|
import { DocumentStatus } from '@documenso/prisma/client';
|
2024-04-19 17:37:38 +07:00
|
|
|
import type { Team, TeamEmail } from '@documenso/prisma/client';
|
2024-09-16 17:14:16 +03:00
|
|
|
import { TeamMemberRole } from '@documenso/prisma/client';
|
2024-04-19 17:37:38 +07:00
|
|
|
import { Badge } from '@documenso/ui/primitives/badge';
|
2024-02-15 18:20:10 +11:00
|
|
|
import { Button } from '@documenso/ui/primitives/button';
|
2024-02-12 17:30:23 +11:00
|
|
|
import { Card, CardContent } from '@documenso/ui/primitives/card';
|
2024-02-06 16:16:10 +11:00
|
|
|
import { LazyPDFViewer } from '@documenso/ui/primitives/lazy-pdf-viewer';
|
|
|
|
|
|
|
|
|
|
import { StackAvatarsWithTooltip } from '~/components/(dashboard)/avatar/stack-avatars-with-tooltip';
|
2024-02-15 18:20:10 +11:00
|
|
|
import { DocumentHistorySheet } from '~/components/document/document-history-sheet';
|
2024-04-18 21:56:31 +07:00
|
|
|
import { DocumentReadOnlyFields } from '~/components/document/document-read-only-fields';
|
2024-11-06 21:34:06 +09:00
|
|
|
import { DocumentRecipientLinkCopyDialog } from '~/components/document/document-recipient-link-copy-dialog';
|
2024-02-12 17:30:23 +11:00
|
|
|
import {
|
|
|
|
|
DocumentStatus as DocumentStatusComponent,
|
|
|
|
|
FRIENDLY_STATUS_MAP,
|
|
|
|
|
} from '~/components/formatter/document-status';
|
|
|
|
|
|
|
|
|
|
import { DocumentPageViewButton } from './document-page-view-button';
|
|
|
|
|
import { DocumentPageViewDropdown } from './document-page-view-dropdown';
|
|
|
|
|
import { DocumentPageViewInformation } from './document-page-view-information';
|
2024-02-15 18:20:10 +11:00
|
|
|
import { DocumentPageViewRecentActivity } from './document-page-view-recent-activity';
|
|
|
|
|
import { DocumentPageViewRecipients } from './document-page-view-recipients';
|
2024-02-06 16:16:10 +11:00
|
|
|
|
|
|
|
|
export type DocumentPageViewProps = {
|
|
|
|
|
params: {
|
|
|
|
|
id: string;
|
|
|
|
|
};
|
2024-09-16 17:14:16 +03:00
|
|
|
team?: Team & { teamEmail: TeamEmail | null } & { currentTeamMember: { role: TeamMemberRole } };
|
2024-02-06 16:16:10 +11:00
|
|
|
};
|
|
|
|
|
|
2024-02-08 12:33:20 +11:00
|
|
|
export const DocumentPageView = async ({ params, team }: DocumentPageViewProps) => {
|
2024-02-06 16:16:10 +11:00
|
|
|
const { id } = params;
|
2024-08-27 20:34:39 +09:00
|
|
|
const { _ } = useLingui();
|
2024-02-06 16:16:10 +11:00
|
|
|
|
|
|
|
|
const documentId = Number(id);
|
|
|
|
|
|
|
|
|
|
const documentRootPath = formatDocumentsPath(team?.url);
|
|
|
|
|
|
|
|
|
|
if (!documentId || Number.isNaN(documentId)) {
|
|
|
|
|
redirect(documentRootPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { user } = await getRequiredServerComponentSession();
|
|
|
|
|
|
|
|
|
|
const document = await getDocumentById({
|
|
|
|
|
id: documentId,
|
|
|
|
|
userId: user.id,
|
|
|
|
|
teamId: team?.id,
|
|
|
|
|
}).catch(() => null);
|
|
|
|
|
|
2024-09-16 17:14:16 +03:00
|
|
|
if (document?.teamId && !team?.url) {
|
|
|
|
|
redirect(documentRootPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const documentVisibility = document?.visibility;
|
|
|
|
|
const currentTeamMemberRole = team?.currentTeamMember?.role;
|
|
|
|
|
const isRecipient = document?.Recipient.find((recipient) => recipient.email === user.email);
|
|
|
|
|
let canAccessDocument = true;
|
|
|
|
|
|
feat: add global settings for teams (#1391)
## Description
This PR introduces global settings for teams. At the moment, it allows
team admins to configure the following:
* The default visibility of the documents uploaded to the team account
* Whether to include the document owner (sender) details when sending
emails to the recipients.
### Include Sender Details
If the Sender Details setting is enabled, the emails sent by the team
will include the sender's name:
> "Example User" on behalf of "Example Team" has invited you to sign
"document.pdf"
Otherwise, the email will say:
> "Example Team" has invited you to sign "document.pdf"
### Default Document Visibility
This new option allows users to set the default visibility for the
documents uploaded to the team account. It can have the following
values:
* Everyone
* Manager and above
* Admins only
If the default document visibility isn't set, the document will be set
to the role of the user who created the document:
* If a user with the "User" role creates a document, the document's
visibility is set to "Everyone".
* Manager role -> "Manager and above"
* Admin role -> "Admins only"
Otherwise, if there is a default document visibility value, it uses that
value.
#### Gotcha
To avoid issues, the `document owner` and the `recipient` can access the
document irrespective of their role. For example:
* If a team member with the role "Member" uploads a document and the
default document visibility is "Admins", only the document owner and
admins can access the document.
* Similar to the other scenarios.
* If an admin uploads a document and the default document visibility is
"Admins", the recipient can access the document.
* The admins have access to all the documents.
* Managers have access to documents with the visibility set to
"Everyone" and "Manager and above"
* Members have access only to the documents with the visibility set to
"Everyone".
## Testing Performed
Tested it locally.
2024-11-08 13:50:49 +02:00
|
|
|
if (team && !isRecipient && document?.userId !== user.id) {
|
2024-09-16 17:14:16 +03:00
|
|
|
canAccessDocument = match([documentVisibility, currentTeamMemberRole])
|
|
|
|
|
.with([DocumentVisibility.EVERYONE, TeamMemberRole.ADMIN], () => true)
|
|
|
|
|
.with([DocumentVisibility.EVERYONE, TeamMemberRole.MANAGER], () => true)
|
|
|
|
|
.with([DocumentVisibility.EVERYONE, TeamMemberRole.MEMBER], () => true)
|
|
|
|
|
.with([DocumentVisibility.MANAGER_AND_ABOVE, TeamMemberRole.ADMIN], () => true)
|
|
|
|
|
.with([DocumentVisibility.MANAGER_AND_ABOVE, TeamMemberRole.MANAGER], () => true)
|
|
|
|
|
.with([DocumentVisibility.ADMIN, TeamMemberRole.ADMIN], () => true)
|
|
|
|
|
.otherwise(() => false);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 14:31:26 +11:00
|
|
|
const isDocumentHistoryEnabled = await getServerComponentFlag(
|
|
|
|
|
'app_document_page_view_history_sheet',
|
|
|
|
|
);
|
|
|
|
|
|
2024-09-16 17:14:16 +03:00
|
|
|
if (!document || !document.documentData || (team && !canAccessDocument)) {
|
|
|
|
|
redirect(documentRootPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (team && !canAccessDocument) {
|
2024-02-06 16:16:10 +11:00
|
|
|
redirect(documentRootPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { documentData, documentMeta } = document;
|
|
|
|
|
|
|
|
|
|
if (documentMeta?.password) {
|
|
|
|
|
const key = DOCUMENSO_ENCRYPTION_KEY;
|
|
|
|
|
|
|
|
|
|
if (!key) {
|
|
|
|
|
throw new Error('Missing DOCUMENSO_ENCRYPTION_KEY');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const securePassword = Buffer.from(
|
|
|
|
|
symmetricDecrypt({
|
|
|
|
|
key,
|
|
|
|
|
data: documentMeta.password,
|
|
|
|
|
}),
|
|
|
|
|
).toString('utf-8');
|
|
|
|
|
|
|
|
|
|
documentMeta.password = securePassword;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-24 06:08:06 +00:00
|
|
|
const [recipients, fields] = await Promise.all([
|
2024-04-18 21:56:31 +07:00
|
|
|
getRecipientsForDocument({
|
|
|
|
|
documentId,
|
|
|
|
|
teamId: team?.id,
|
|
|
|
|
userId: user.id,
|
|
|
|
|
}),
|
2024-06-24 06:08:06 +00:00
|
|
|
getFieldsForDocument({
|
2024-04-18 21:56:31 +07:00
|
|
|
documentId,
|
2024-06-24 06:08:06 +00:00
|
|
|
userId: user.id,
|
2024-04-18 21:56:31 +07:00
|
|
|
}),
|
|
|
|
|
]);
|
2024-02-12 17:30:23 +11:00
|
|
|
|
|
|
|
|
const documentWithRecipients = {
|
|
|
|
|
...document,
|
|
|
|
|
Recipient: recipients,
|
|
|
|
|
};
|
2024-02-06 16:16:10 +11:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="mx-auto -mt-4 w-full max-w-screen-xl px-4 md:px-8">
|
2024-11-06 21:34:06 +09:00
|
|
|
{document.status === DocumentStatus.PENDING && (
|
|
|
|
|
<DocumentRecipientLinkCopyDialog recipients={recipients} />
|
|
|
|
|
)}
|
|
|
|
|
|
2024-02-26 10:50:46 +11:00
|
|
|
<Link href={documentRootPath} className="flex items-center text-[#7AC455] hover:opacity-80">
|
2024-02-06 16:16:10 +11:00
|
|
|
<ChevronLeft className="mr-2 inline-block h-5 w-5" />
|
2024-08-27 20:34:39 +09:00
|
|
|
<Trans>Documents</Trans>
|
2024-02-06 16:16:10 +11:00
|
|
|
</Link>
|
|
|
|
|
|
2024-07-28 23:13:35 -04:00
|
|
|
<div className="flex flex-row justify-between truncate">
|
2024-02-15 18:20:10 +11:00
|
|
|
<div>
|
|
|
|
|
<h1 className="mt-4 truncate text-2xl font-semibold md:text-3xl" title={document.title}>
|
|
|
|
|
{document.title}
|
|
|
|
|
</h1>
|
|
|
|
|
|
|
|
|
|
<div className="mt-2.5 flex items-center gap-x-6">
|
|
|
|
|
<DocumentStatusComponent
|
|
|
|
|
inheritColor
|
|
|
|
|
status={document.status}
|
|
|
|
|
className="text-muted-foreground"
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
{recipients.length > 0 && (
|
|
|
|
|
<div className="text-muted-foreground flex items-center">
|
|
|
|
|
<Users2 className="mr-2 h-5 w-5" />
|
|
|
|
|
|
2024-04-19 16:17:32 +07:00
|
|
|
<StackAvatarsWithTooltip
|
|
|
|
|
recipients={recipients}
|
|
|
|
|
documentStatus={document.status}
|
|
|
|
|
position="bottom"
|
|
|
|
|
>
|
2024-08-27 20:34:39 +09:00
|
|
|
<span>
|
|
|
|
|
<Trans>{recipients.length} Recipient(s)</Trans>
|
|
|
|
|
</span>
|
2024-02-15 18:20:10 +11:00
|
|
|
</StackAvatarsWithTooltip>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2024-04-19 17:37:38 +07:00
|
|
|
|
2024-08-27 20:34:39 +09:00
|
|
|
{document.deletedAt && (
|
|
|
|
|
<Badge variant="destructive">
|
|
|
|
|
<Trans>Document deleted</Trans>
|
|
|
|
|
</Badge>
|
|
|
|
|
)}
|
2024-02-15 18:20:10 +11:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
2024-02-19 14:31:26 +11:00
|
|
|
{isDocumentHistoryEnabled && (
|
|
|
|
|
<div className="self-end">
|
|
|
|
|
<DocumentHistorySheet documentId={document.id} userId={user.id}>
|
|
|
|
|
<Button variant="outline">
|
|
|
|
|
<Clock9 className="mr-1.5 h-4 w-4" />
|
2024-08-27 20:34:39 +09:00
|
|
|
<Trans>Document history</Trans>
|
2024-02-19 14:31:26 +11:00
|
|
|
</Button>
|
|
|
|
|
</DocumentHistorySheet>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2024-02-06 16:16:10 +11:00
|
|
|
</div>
|
|
|
|
|
|
2024-02-12 17:30:23 +11:00
|
|
|
<div className="mt-6 grid w-full grid-cols-12 gap-8">
|
|
|
|
|
<Card
|
|
|
|
|
className="relative col-span-12 rounded-xl before:rounded-xl lg:col-span-6 xl:col-span-7"
|
|
|
|
|
gradient
|
|
|
|
|
>
|
|
|
|
|
<CardContent className="p-2">
|
|
|
|
|
<LazyPDFViewer document={document} key={documentData.id} documentData={documentData} />
|
|
|
|
|
</CardContent>
|
|
|
|
|
</Card>
|
|
|
|
|
|
2024-04-18 21:56:31 +07:00
|
|
|
{document.status === DocumentStatus.PENDING && (
|
2024-06-24 06:08:06 +00:00
|
|
|
<DocumentReadOnlyFields fields={fields} documentMeta={documentMeta || undefined} />
|
2024-04-18 21:56:31 +07:00
|
|
|
)}
|
|
|
|
|
|
2024-02-12 17:30:23 +11:00
|
|
|
<div className="col-span-12 lg:col-span-6 xl:col-span-5">
|
2024-02-15 18:20:10 +11:00
|
|
|
<div className="space-y-6">
|
|
|
|
|
<section className="border-border bg-widget flex flex-col rounded-xl border pb-4 pt-6">
|
2024-02-12 17:30:23 +11:00
|
|
|
<div className="flex flex-row items-center justify-between px-4">
|
|
|
|
|
<h3 className="text-foreground text-2xl font-semibold">
|
2024-08-27 20:34:39 +09:00
|
|
|
{_(FRIENDLY_STATUS_MAP[document.status].labelExtended)}
|
2024-02-12 17:30:23 +11:00
|
|
|
</h3>
|
|
|
|
|
|
|
|
|
|
<DocumentPageViewDropdown document={documentWithRecipients} team={team} />
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<p className="text-muted-foreground mt-2 px-4 text-sm ">
|
|
|
|
|
{match(document.status)
|
2024-08-27 20:34:39 +09:00
|
|
|
.with(DocumentStatus.COMPLETED, () => (
|
|
|
|
|
<Trans>This document has been signed by all recipients</Trans>
|
|
|
|
|
))
|
|
|
|
|
.with(DocumentStatus.DRAFT, () => (
|
|
|
|
|
<Trans>This document is currently a draft and has not been sent</Trans>
|
|
|
|
|
))
|
2024-02-12 17:30:23 +11:00
|
|
|
.with(DocumentStatus.PENDING, () => {
|
|
|
|
|
const pendingRecipients = recipients.filter(
|
|
|
|
|
(recipient) => recipient.signingStatus === 'NOT_SIGNED',
|
|
|
|
|
);
|
|
|
|
|
|
2024-08-27 20:34:39 +09:00
|
|
|
return (
|
|
|
|
|
<Plural
|
|
|
|
|
value={pendingRecipients.length}
|
|
|
|
|
one="Waiting on 1 recipient"
|
|
|
|
|
other="Waiting on # recipients"
|
|
|
|
|
/>
|
|
|
|
|
);
|
2024-02-12 17:30:23 +11:00
|
|
|
})
|
|
|
|
|
.exhaustive()}
|
|
|
|
|
</p>
|
|
|
|
|
|
|
|
|
|
<div className="mt-4 border-t px-4 pt-4">
|
|
|
|
|
<DocumentPageViewButton document={documentWithRecipients} team={team} />
|
|
|
|
|
</div>
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
{/* Document information section. */}
|
|
|
|
|
<DocumentPageViewInformation document={documentWithRecipients} userId={user.id} />
|
|
|
|
|
|
|
|
|
|
{/* Recipients section. */}
|
2024-02-15 18:20:10 +11:00
|
|
|
<DocumentPageViewRecipients
|
|
|
|
|
document={documentWithRecipients}
|
|
|
|
|
documentRootPath={documentRootPath}
|
|
|
|
|
/>
|
2024-02-12 17:30:23 +11:00
|
|
|
|
2024-02-15 18:20:10 +11:00
|
|
|
{/* Recent activity section. */}
|
|
|
|
|
<DocumentPageViewRecentActivity documentId={document.id} userId={user.id} />
|
2024-02-12 17:30:23 +11:00
|
|
|
</div>
|
2024-02-06 16:16:10 +11:00
|
|
|
</div>
|
2024-02-12 17:30:23 +11:00
|
|
|
</div>
|
2024-02-06 16:16:10 +11:00
|
|
|
</div>
|
|
|
|
|
);
|
2024-02-08 12:33:20 +11:00
|
|
|
};
|