feat: add teams (#848)

## Description

Add support for teams which will allow users to collaborate on
documents.

Teams features allows users to:

- Create, manage and transfer teams
- Manage team members
- Manage team emails
- Manage a shared team inbox and documents

These changes do NOT include the following, which are planned for a
future release:

- Team templates
- Team API
- Search menu integration

## Testing Performed

- Added E2E tests for general team management
- Added E2E tests to validate document counts

## Checklist

- [X] I have tested these changes locally and they work as expected.
- [X] I have added/updated tests that prove the effectiveness of these
changes.
- [ ] I have updated the documentation to reflect these changes, if
applicable.
- [X] I have followed the project's coding style guidelines.
This commit is contained in:
David Nguyen
2024-02-06 16:16:10 +11:00
committed by GitHub
parent c6457e75e0
commit 0c339b78b6
200 changed files with 12916 additions and 968 deletions

View File

@ -0,0 +1,375 @@
import type { User } from '@prisma/client';
import { nanoid } from 'nanoid';
import fs from 'node:fs';
import path from 'node:path';
import { match } from 'ts-pattern';
import { prisma } from '..';
import {
DocumentDataType,
DocumentStatus,
FieldType,
Prisma,
ReadStatus,
SendStatus,
SigningStatus,
} from '../client';
import { seedTeam } from './teams';
import { seedUser } from './users';
const examplePdf = fs
.readFileSync(path.join(__dirname, '../../../assets/example.pdf'))
.toString('base64');
type DocumentToSeed = {
sender: User;
recipients: (User | string)[];
type: DocumentStatus;
documentOptions?: Partial<Prisma.DocumentUncheckedCreateInput>;
};
export const seedDocuments = async (documents: DocumentToSeed[]) => {
await Promise.all(
documents.map(async (document, i) =>
match(document.type)
.with(DocumentStatus.DRAFT, async () =>
createDraftDocument(document.sender, document.recipients, {
key: i,
createDocumentOptions: document.documentOptions,
}),
)
.with(DocumentStatus.PENDING, async () =>
createPendingDocument(document.sender, document.recipients, {
key: i,
createDocumentOptions: document.documentOptions,
}),
)
.with(DocumentStatus.COMPLETED, async () =>
createCompletedDocument(document.sender, document.recipients, {
key: i,
createDocumentOptions: document.documentOptions,
}),
)
.exhaustive(),
),
);
};
const createDraftDocument = async (
sender: User,
recipients: (User | string)[],
options: CreateDocumentOptions = {},
) => {
const { key, createDocumentOptions = {} } = options;
const documentData = await prisma.documentData.create({
data: {
type: DocumentDataType.BYTES_64,
data: examplePdf,
initialData: examplePdf,
},
});
const document = await prisma.document.create({
data: {
title: `[TEST] Document ${key} - Draft`,
status: DocumentStatus.DRAFT,
documentDataId: documentData.id,
userId: sender.id,
...createDocumentOptions,
},
});
for (const recipient of recipients) {
const email = typeof recipient === 'string' ? recipient : recipient.email;
const name = typeof recipient === 'string' ? recipient : recipient.name ?? '';
await prisma.recipient.create({
data: {
email,
name,
token: nanoid(),
readStatus: ReadStatus.NOT_OPENED,
sendStatus: SendStatus.NOT_SENT,
signingStatus: SigningStatus.NOT_SIGNED,
signedAt: new Date(),
Document: {
connect: {
id: document.id,
},
},
Field: {
create: {
page: 1,
type: FieldType.NAME,
inserted: true,
customText: name,
positionX: new Prisma.Decimal(1),
positionY: new Prisma.Decimal(1),
width: new Prisma.Decimal(1),
height: new Prisma.Decimal(1),
documentId: document.id,
},
},
},
});
}
};
type CreateDocumentOptions = {
key?: string | number;
createDocumentOptions?: Partial<Prisma.DocumentUncheckedCreateInput>;
};
const createPendingDocument = async (
sender: User,
recipients: (User | string)[],
options: CreateDocumentOptions = {},
) => {
const { key, createDocumentOptions = {} } = options;
const documentData = await prisma.documentData.create({
data: {
type: DocumentDataType.BYTES_64,
data: examplePdf,
initialData: examplePdf,
},
});
const document = await prisma.document.create({
data: {
title: `[TEST] Document ${key} - Pending`,
status: DocumentStatus.PENDING,
documentDataId: documentData.id,
userId: sender.id,
...createDocumentOptions,
},
});
for (const recipient of recipients) {
const email = typeof recipient === 'string' ? recipient : recipient.email;
const name = typeof recipient === 'string' ? recipient : recipient.name ?? '';
await prisma.recipient.create({
data: {
email,
name,
token: nanoid(),
readStatus: ReadStatus.OPENED,
sendStatus: SendStatus.SENT,
signingStatus: SigningStatus.NOT_SIGNED,
signedAt: new Date(),
Document: {
connect: {
id: document.id,
},
},
Field: {
create: {
page: 1,
type: FieldType.NAME,
inserted: true,
customText: name,
positionX: new Prisma.Decimal(1),
positionY: new Prisma.Decimal(1),
width: new Prisma.Decimal(1),
height: new Prisma.Decimal(1),
documentId: document.id,
},
},
},
});
}
};
const createCompletedDocument = async (
sender: User,
recipients: (User | string)[],
options: CreateDocumentOptions = {},
) => {
const { key, createDocumentOptions = {} } = options;
const documentData = await prisma.documentData.create({
data: {
type: DocumentDataType.BYTES_64,
data: examplePdf,
initialData: examplePdf,
},
});
const document = await prisma.document.create({
data: {
title: `[TEST] Document ${key} - Completed`,
status: DocumentStatus.COMPLETED,
documentDataId: documentData.id,
userId: sender.id,
...createDocumentOptions,
},
});
for (const recipient of recipients) {
const email = typeof recipient === 'string' ? recipient : recipient.email;
const name = typeof recipient === 'string' ? recipient : recipient.name ?? '';
await prisma.recipient.create({
data: {
email,
name,
token: nanoid(),
readStatus: ReadStatus.OPENED,
sendStatus: SendStatus.SENT,
signingStatus: SigningStatus.SIGNED,
signedAt: new Date(),
Document: {
connect: {
id: document.id,
},
},
Field: {
create: {
page: 1,
type: FieldType.NAME,
inserted: true,
customText: name,
positionX: new Prisma.Decimal(1),
positionY: new Prisma.Decimal(1),
width: new Prisma.Decimal(1),
height: new Prisma.Decimal(1),
documentId: document.id,
},
},
},
});
}
};
/**
* Create 5 team documents:
* - Completed document with 2 recipients.
* - Pending document with 1 recipient.
* - Pending document with 4 recipients.
* - Draft document with 3 recipients.
* - Draft document with 2 recipients.
*
* Create 3 non team documents where the user is a team member:
* - Completed document with 2 recipients.
* - Pending document with 1 recipient.
* - Draft document with 2 recipients.
*
* Create 3 non team documents where the user is not a team member, but the recipient is:
* - Completed document with 2 recipients.
* - Pending document with 1 recipient.
* - Draft document with 2 recipients.
*
* This should result in the following team document dashboard counts:
* - 0 Inbox
* - 2 Pending
* - 1 Completed
* - 2 Draft
* - 5 All
*/
export const seedTeamDocuments = async () => {
const team = await seedTeam({
createTeamMembers: 4,
});
const documentOptions = {
teamId: team.id,
};
const teamMember1 = team.members[1].user;
const teamMember2 = team.members[2].user;
const teamMember3 = team.members[3].user;
const teamMember4 = team.members[4].user;
const [testUser1, testUser2, testUser3, testUser4] = await Promise.all([
seedUser(),
seedUser(),
seedUser(),
seedUser(),
]);
await seedDocuments([
/**
* Team documents.
*/
{
sender: teamMember1,
recipients: [testUser1, testUser2],
type: DocumentStatus.COMPLETED,
documentOptions,
},
{
sender: teamMember2,
recipients: [testUser1],
type: DocumentStatus.PENDING,
documentOptions,
},
{
sender: teamMember2,
recipients: [testUser1, testUser2, testUser3, testUser4],
type: DocumentStatus.PENDING,
documentOptions,
},
{
sender: teamMember2,
recipients: [testUser1, testUser2, teamMember1],
type: DocumentStatus.DRAFT,
documentOptions,
},
{
sender: team.owner,
recipients: [testUser1, testUser2],
type: DocumentStatus.DRAFT,
documentOptions,
},
/**
* Non team documents where the sender is a team member and recipient is not.
*/
{
sender: teamMember1,
recipients: [testUser1, testUser2],
type: DocumentStatus.COMPLETED,
},
{
sender: teamMember2,
recipients: [testUser1],
type: DocumentStatus.PENDING,
},
{
sender: teamMember3,
recipients: [testUser1, testUser2],
type: DocumentStatus.DRAFT,
},
/**
* Non team documents where the sender is not a team member and recipient is.
*/
{
sender: testUser1,
recipients: [teamMember1, teamMember2],
type: DocumentStatus.COMPLETED,
},
{
sender: testUser2,
recipients: [teamMember1],
type: DocumentStatus.PENDING,
},
{
sender: testUser3,
recipients: [teamMember1, teamMember2],
type: DocumentStatus.DRAFT,
},
]);
return {
team,
teamMember1,
teamMember2,
teamMember3,
teamMember4,
testUser1,
testUser2,
testUser3,
testUser4,
};
};

View File

@ -0,0 +1,177 @@
import { prisma } from '..';
import { TeamMemberInviteStatus, TeamMemberRole } from '../client';
import { seedUser } from './users';
const EMAIL_DOMAIN = `test.documenso.com`;
type SeedTeamOptions = {
createTeamMembers?: number;
createTeamEmail?: true | string;
};
export const seedTeam = async ({
createTeamMembers = 0,
createTeamEmail,
}: SeedTeamOptions = {}) => {
const teamUrl = `team-${Date.now()}`;
const teamEmail = createTeamEmail === true ? `${teamUrl}@${EMAIL_DOMAIN}` : createTeamEmail;
const teamOwner = await seedUser({
name: `${teamUrl}-original-owner`,
email: `${teamUrl}-original-owner@${EMAIL_DOMAIN}`,
});
const teamMembers = await Promise.all(
Array.from({ length: createTeamMembers }).map(async (_, i) => {
return seedUser({
name: `${teamUrl}-member-${i + 1}`,
email: `${teamUrl}-member-${i + 1}@${EMAIL_DOMAIN}`,
});
}),
);
const team = await prisma.team.create({
data: {
name: teamUrl,
url: teamUrl,
ownerUserId: teamOwner.id,
members: {
createMany: {
data: [teamOwner, ...teamMembers].map((user) => ({
userId: user.id,
role: TeamMemberRole.ADMIN,
})),
},
},
teamEmail: teamEmail
? {
create: {
email: teamEmail,
name: teamEmail,
},
}
: undefined,
},
});
return await prisma.team.findFirstOrThrow({
where: {
id: team.id,
},
include: {
owner: true,
members: {
include: {
user: true,
},
},
teamEmail: true,
},
});
};
export const unseedTeam = async (teamUrl: string) => {
const team = await prisma.team.findUnique({
where: {
url: teamUrl,
},
include: {
members: true,
},
});
if (!team) {
return;
}
await prisma.team.delete({
where: {
url: teamUrl,
},
});
await prisma.user.deleteMany({
where: {
id: {
in: team.members.map((member) => member.userId),
},
},
});
};
export const seedTeamTransfer = async (options: { newOwnerUserId: number; teamId: number }) => {
return await prisma.teamTransferVerification.create({
data: {
teamId: options.teamId,
token: Date.now().toString(),
expiresAt: new Date(Date.now() + 1000 * 60 * 60 * 24),
userId: options.newOwnerUserId,
name: '',
email: '',
},
});
};
export const seedTeamEmail = async ({ email, teamId }: { email: string; teamId: number }) => {
return await prisma.teamEmail.create({
data: {
name: email,
email,
teamId,
},
});
};
export const unseedTeamEmail = async ({ teamId }: { teamId: number }) => {
return await prisma.teamEmail.delete({
where: {
teamId,
},
});
};
export const seedTeamInvite = async ({
email,
teamId,
role = TeamMemberRole.ADMIN,
}: {
email: string;
teamId: number;
role?: TeamMemberRole;
}) => {
return await prisma.teamMemberInvite.create({
data: {
email,
teamId,
role,
status: TeamMemberInviteStatus.PENDING,
token: Date.now().toString(),
},
});
};
export const seedTeamEmailVerification = async ({
email,
teamId,
}: {
email: string;
teamId: number;
}) => {
return await prisma.teamEmailVerification.create({
data: {
teamId,
email,
name: email,
expiresAt: new Date(Date.now() + 1000 * 60 * 60 * 24),
token: Date.now().toString(),
},
});
};
export const unseedTeamEmailVerification = async ({ teamId }: { teamId: number }) => {
return await prisma.teamEmailVerification.delete({
where: {
teamId,
},
});
};

View File

@ -0,0 +1,34 @@
import { hashSync } from '@documenso/lib/server-only/auth/hash';
import { prisma } from '..';
type SeedUserOptions = {
name?: string;
email?: string;
password?: string;
verified?: boolean;
};
export const seedUser = async ({
name = `user-${Date.now()}`,
email = `user-${Date.now()}@test.documenso.com`,
password = 'password',
verified = true,
}: SeedUserOptions = {}) => {
return await prisma.user.create({
data: {
name,
email,
password: hashSync(password),
emailVerified: verified ? new Date() : undefined,
},
});
};
export const unseedUser = async (userId: number) => {
await prisma.user.delete({
where: {
id: userId,
},
});
};