mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 00:03:33 +10:00
feat: add organisations (#1820)
This commit is contained in:
@ -1,12 +1,11 @@
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
|
||||
import { hashSync } from '@documenso/lib/server-only/auth/hash';
|
||||
|
||||
import { prisma } from '..';
|
||||
import { DocumentDataType, DocumentSource, Role, TeamMemberRole } from '../client';
|
||||
import { DocumentDataType, DocumentSource } from '../client';
|
||||
import { seedPendingDocument } from './documents';
|
||||
import { seedDirectTemplate, seedTemplate } from './templates';
|
||||
import { seedUser } from './users';
|
||||
|
||||
const createDocumentData = async ({ documentData }: { documentData: string }) => {
|
||||
return prisma.documentData.create({
|
||||
@ -23,32 +22,31 @@ export const seedDatabase = async () => {
|
||||
.readFileSync(path.join(__dirname, '../../../assets/example.pdf'))
|
||||
.toString('base64');
|
||||
|
||||
const exampleUser = await prisma.user.upsert({
|
||||
const exampleUserExists = await prisma.user.findFirst({
|
||||
where: {
|
||||
email: 'example@documenso.com',
|
||||
},
|
||||
create: {
|
||||
name: 'Example User',
|
||||
email: 'example@documenso.com',
|
||||
emailVerified: new Date(),
|
||||
password: hashSync('password'),
|
||||
roles: [Role.USER],
|
||||
},
|
||||
update: {},
|
||||
});
|
||||
|
||||
const adminUser = await prisma.user.upsert({
|
||||
const adminUserExists = await prisma.user.findFirst({
|
||||
where: {
|
||||
email: 'admin@documenso.com',
|
||||
},
|
||||
create: {
|
||||
name: 'Admin User',
|
||||
email: 'admin@documenso.com',
|
||||
emailVerified: new Date(),
|
||||
password: hashSync('password'),
|
||||
roles: [Role.USER, Role.ADMIN],
|
||||
},
|
||||
update: {},
|
||||
});
|
||||
|
||||
if (exampleUserExists || adminUserExists) {
|
||||
return;
|
||||
}
|
||||
|
||||
const exampleUser = await seedUser({
|
||||
name: 'Example User',
|
||||
email: 'example@documenso.com',
|
||||
});
|
||||
|
||||
const adminUser = await seedUser({
|
||||
name: 'Admin User',
|
||||
email: 'admin@documenso.com',
|
||||
isAdmin: true,
|
||||
});
|
||||
|
||||
for (let i = 1; i <= 4; i++) {
|
||||
@ -59,11 +57,12 @@ export const seedDatabase = async () => {
|
||||
source: DocumentSource.DOCUMENT,
|
||||
title: `Example Document ${i}`,
|
||||
documentDataId: documentData.id,
|
||||
userId: exampleUser.id,
|
||||
userId: exampleUser.user.id,
|
||||
teamId: exampleUser.team.id,
|
||||
recipients: {
|
||||
create: {
|
||||
name: String(adminUser.name),
|
||||
email: adminUser.email,
|
||||
name: String(adminUser.user.name),
|
||||
email: adminUser.user.email,
|
||||
token: Math.random().toString(36).slice(2, 9),
|
||||
},
|
||||
},
|
||||
@ -79,11 +78,12 @@ export const seedDatabase = async () => {
|
||||
source: DocumentSource.DOCUMENT,
|
||||
title: `Document ${i}`,
|
||||
documentDataId: documentData.id,
|
||||
userId: adminUser.id,
|
||||
userId: adminUser.user.id,
|
||||
teamId: adminUser.team.id,
|
||||
recipients: {
|
||||
create: {
|
||||
name: String(exampleUser.name),
|
||||
email: exampleUser.email,
|
||||
name: String(exampleUser.user.name),
|
||||
email: exampleUser.user.email,
|
||||
token: Math.random().toString(36).slice(2, 9),
|
||||
},
|
||||
},
|
||||
@ -91,14 +91,14 @@ export const seedDatabase = async () => {
|
||||
});
|
||||
}
|
||||
|
||||
await seedPendingDocument(exampleUser, [adminUser], {
|
||||
await seedPendingDocument(exampleUser.user, exampleUser.team.id, [adminUser.user], {
|
||||
key: 'example-pending',
|
||||
createDocumentOptions: {
|
||||
title: 'Pending Document',
|
||||
},
|
||||
});
|
||||
|
||||
await seedPendingDocument(adminUser, [exampleUser], {
|
||||
await seedPendingDocument(adminUser.user, adminUser.team.id, [exampleUser.user], {
|
||||
key: 'admin-pending',
|
||||
createDocumentOptions: {
|
||||
title: 'Pending Document',
|
||||
@ -108,80 +108,24 @@ export const seedDatabase = async () => {
|
||||
await Promise.all([
|
||||
seedTemplate({
|
||||
title: 'Template 1',
|
||||
userId: exampleUser.id,
|
||||
userId: exampleUser.user.id,
|
||||
teamId: exampleUser.team.id,
|
||||
}),
|
||||
seedDirectTemplate({
|
||||
title: 'Direct Template 1',
|
||||
userId: exampleUser.id,
|
||||
userId: exampleUser.user.id,
|
||||
teamId: exampleUser.team.id,
|
||||
}),
|
||||
|
||||
seedTemplate({
|
||||
title: 'Template 1',
|
||||
userId: adminUser.id,
|
||||
userId: adminUser.user.id,
|
||||
teamId: adminUser.team.id,
|
||||
}),
|
||||
seedDirectTemplate({
|
||||
title: 'Direct Template 1',
|
||||
userId: adminUser.id,
|
||||
userId: adminUser.user.id,
|
||||
teamId: adminUser.team.id,
|
||||
}),
|
||||
]);
|
||||
|
||||
const testUsers = [
|
||||
'test@documenso.com',
|
||||
'test2@documenso.com',
|
||||
'test3@documenso.com',
|
||||
'test4@documenso.com',
|
||||
];
|
||||
|
||||
const createdUsers = [];
|
||||
|
||||
for (const email of testUsers) {
|
||||
const testUser = await prisma.user.upsert({
|
||||
where: {
|
||||
email: email,
|
||||
},
|
||||
create: {
|
||||
name: 'Test User',
|
||||
email: email,
|
||||
emailVerified: new Date(),
|
||||
password: hashSync('password'),
|
||||
roles: [Role.USER],
|
||||
},
|
||||
update: {},
|
||||
});
|
||||
|
||||
createdUsers.push(testUser);
|
||||
}
|
||||
|
||||
const team1 = await prisma.team.create({
|
||||
data: {
|
||||
name: 'Team 1',
|
||||
url: 'team1',
|
||||
ownerUserId: createdUsers[0].id,
|
||||
},
|
||||
});
|
||||
|
||||
const team2 = await prisma.team.create({
|
||||
data: {
|
||||
name: 'Team 2',
|
||||
url: 'team2',
|
||||
ownerUserId: createdUsers[1].id,
|
||||
},
|
||||
});
|
||||
|
||||
for (const team of [team1, team2]) {
|
||||
await prisma.teamMember.createMany({
|
||||
data: [
|
||||
{
|
||||
teamId: team.id,
|
||||
userId: createdUsers[1].id,
|
||||
role: TeamMemberRole.ADMIN,
|
||||
},
|
||||
{
|
||||
teamId: team.id,
|
||||
userId: createdUsers[2].id,
|
||||
role: TeamMemberRole.MEMBER,
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user