2
0

feat(editor): Team workspaces

This commit is contained in:
Baptiste Arnaud
2022-05-13 15:22:44 -07:00
parent 6c2986590b
commit f0fdf08b00
132 changed files with 3354 additions and 1228 deletions

View File

@ -1 +1,2 @@
DATABASE_URL=postgresql://postgres:@localhost:5432/typebot
DATABASE_URL=postgresql://postgres:typebot@localhost:5432/typebot
ENCRYPTION_SECRET=

View File

@ -1,5 +1,5 @@
import { PrismaClient } from 'db'
import path from 'path'
import { migrateWorkspace } from './workspaceMigration'
require('dotenv').config({
path: path.join(
@ -8,7 +8,8 @@ require('dotenv').config({
),
})
const prisma = new PrismaClient()
const main = async () => {}
const main = async () => {
await migrateWorkspace()
}
main().then()

View File

@ -6,7 +6,8 @@
"private": true,
"scripts": {
"start:local": "ts-node index.ts",
"start:prod": "NODE_ENV=production ts-node index.ts"
"start:prod": "NODE_ENV=production ts-node index.ts",
"start:workspaces:migration": "ts-node workspaceMigration.ts"
},
"devDependencies": {
"db": "*",

View File

@ -0,0 +1,85 @@
import { Plan, PrismaClient, WorkspaceRole } from 'db'
import path from 'path'
const prisma = new PrismaClient()
export const migrateWorkspace = async () => {
const users = await prisma.user.findMany({
where: { workspaces: { none: {} } },
include: {
folders: true,
typebots: true,
credentials: true,
customDomains: true,
CollaboratorsOnTypebots: {
include: { typebot: { select: { workspaceId: true } } },
},
},
})
let i = 1
for (const user of users) {
console.log('Updating', user.email, `(${i}/${users.length})`)
i += 1
const newWorkspace = await prisma.workspace.create({
data: {
name: user.name ? `${user.name}'s workspace` : 'My workspace',
members: { create: { userId: user.id, role: WorkspaceRole.ADMIN } },
stripeId: user.stripeId,
plan: user.plan ?? Plan.FREE,
},
})
await prisma.credentials.updateMany({
where: { id: { in: user.credentials.map((c) => c.id) } },
data: { workspaceId: newWorkspace.id, ownerId: null },
})
await prisma.customDomain.updateMany({
where: {
name: { in: user.customDomains.map((c) => c.name) },
ownerId: user.id,
},
data: { workspaceId: newWorkspace.id, ownerId: null },
})
await prisma.dashboardFolder.updateMany({
where: {
id: { in: user.folders.map((c) => c.id) },
},
data: { workspaceId: newWorkspace.id, ownerId: null },
})
await prisma.typebot.updateMany({
where: {
id: { in: user.typebots.map((c) => c.id) },
},
data: { workspaceId: newWorkspace.id, ownerId: null },
})
for (const collab of user.CollaboratorsOnTypebots) {
if (!collab.typebot.workspaceId) continue
await prisma.memberInWorkspace.upsert({
where: {
userId_workspaceId: {
userId: user.id,
workspaceId: collab.typebot.workspaceId,
},
},
create: {
role: WorkspaceRole.GUEST,
userId: user.id,
workspaceId: collab.typebot.workspaceId,
},
update: {},
})
}
}
}
require('dotenv').config({
path: path.join(
__dirname,
process.env.NODE_ENV === 'production' ? '.env.production' : '.env.local'
),
})
const main = async () => {
await migrateWorkspace()
}
main().then()