migrate organizations from clerk to in-house

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-06-16 19:32:53 +02:00
parent 84ac68fe63
commit c7dbc2f7c4
27 changed files with 523 additions and 294 deletions

View File

@@ -0,0 +1,53 @@
-- AlterTable
ALTER TABLE "projects" ADD COLUMN "organizationId" TEXT;
-- CreateTable
CREATE TABLE "organizations" (
"id" TEXT NOT NULL DEFAULT gen_random_uuid(),
"name" TEXT NOT NULL,
"createdByUserId" TEXT,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "organizations_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "users" (
"id" TEXT NOT NULL DEFAULT gen_random_uuid(),
"email" TEXT NOT NULL,
"first_name" TEXT,
"last_name" TEXT,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "users_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "members" (
"id" UUID NOT NULL DEFAULT gen_random_uuid(),
"role" TEXT NOT NULL,
"email" TEXT NOT NULL,
"userId" TEXT,
"organizationId" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "members_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE UNIQUE INDEX "users_email_key" ON "users"("email");
-- AddForeignKey
ALTER TABLE "organizations" ADD CONSTRAINT "organizations_createdByUserId_fkey" FOREIGN KEY ("createdByUserId") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "members" ADD CONSTRAINT "members_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "members" ADD CONSTRAINT "members_organizationId_fkey" FOREIGN KEY ("organizationId") REFERENCES "organizations"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "projects" ADD CONSTRAINT "projects_organizationId_fkey" FOREIGN KEY ("organizationId") REFERENCES "organizations"("id") ON DELETE SET NULL ON UPDATE CASCADE;

View File

@@ -0,0 +1,12 @@
/*
Warnings:
- You are about to drop the column `first_name` on the `users` table. All the data in the column will be lost.
- You are about to drop the column `last_name` on the `users` table. All the data in the column will be lost.
*/
-- AlterTable
ALTER TABLE "users" DROP COLUMN "first_name",
DROP COLUMN "last_name",
ADD COLUMN "firstName" TEXT,
ADD COLUMN "lastName" TEXT;

View File

@@ -0,0 +1,6 @@
-- AlterTable
ALTER TABLE "members" ADD COLUMN "invitedById" TEXT,
ADD COLUMN "meta" JSONB;
-- AddForeignKey
ALTER TABLE "members" ADD CONSTRAINT "members_invitedById_fkey" FOREIGN KEY ("invitedById") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

View File

@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "users" ADD COLUMN "deletedAt" TIMESTAMP(3);

View File

@@ -0,0 +1,23 @@
-- AlterTable
ALTER TABLE "clients" ADD COLUMN "organizationId" TEXT;
-- AlterTable
ALTER TABLE "dashboards" ADD COLUMN "organizationId" TEXT;
-- AlterTable
ALTER TABLE "project_access" ADD COLUMN "organizationId" TEXT;
-- AlterTable
ALTER TABLE "shares" ADD COLUMN "organizationId" TEXT;
-- AddForeignKey
ALTER TABLE "project_access" ADD CONSTRAINT "project_access_organizationId_fkey" FOREIGN KEY ("organizationId") REFERENCES "organizations"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "clients" ADD CONSTRAINT "clients_organizationId_fkey" FOREIGN KEY ("organizationId") REFERENCES "organizations"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "dashboards" ADD CONSTRAINT "dashboards_organizationId_fkey" FOREIGN KEY ("organizationId") REFERENCES "organizations"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "shares" ADD CONSTRAINT "shares_organizationId_fkey" FOREIGN KEY ("organizationId") REFERENCES "organizations"("id") ON DELETE SET NULL ON UPDATE CASCADE;

View File

@@ -0,0 +1,2 @@
-- AddForeignKey
ALTER TABLE "project_access" ADD CONSTRAINT "project_access_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

View File

@@ -17,10 +17,63 @@ enum ProjectType {
backend
}
model Organization {
id String @id @default(dbgenerated("gen_random_uuid()"))
name String
projects Project[]
members Member[]
createdByUserId String?
createdBy User? @relation(fields: [createdByUserId], references: [id])
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
ProjectAccess ProjectAccess[]
Client Client[]
Dashboard Dashboard[]
ShareOverview ShareOverview[]
@@map("organizations")
}
model User {
id String @id @default(dbgenerated("gen_random_uuid()"))
email String @unique
firstName String?
lastName String?
createdOrganizations Organization[]
membership Member[]
sentInvites Member[] @relation("invitedBy")
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
deletedAt DateTime?
ProjectAccess ProjectAccess[]
@@map("users")
}
model Member {
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
role String
email String
// userId is nullable because we want to allow invites to be sent to emails that are not registered
userId String?
user User? @relation(fields: [userId], references: [id])
invitedById String?
invitedBy User? @relation("invitedBy", fields: [invitedById], references: [id])
organizationId String
organization Organization @relation(fields: [organizationId], references: [id])
meta Json?
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
@@map("members")
}
model Project {
id String @id @default(dbgenerated("gen_random_uuid()"))
name String
organizationSlug String
organization Organization? @relation(fields: [organizationId], references: [id])
organizationId String?
eventsCount Int @default(0)
types ProjectType[] @default([])
@@ -47,14 +100,17 @@ enum AccessLevel {
}
model ProjectAccess {
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
projectId String
project Project @relation(fields: [projectId], references: [id])
project Project @relation(fields: [projectId], references: [id])
organizationSlug String
organization Organization? @relation(fields: [organizationId], references: [id])
organizationId String?
userId String
user User @relation(fields: [userId], references: [id])
level AccessLevel
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
@@map("project_access")
}
@@ -105,13 +161,15 @@ enum ClientType {
}
model Client {
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
name String
secret String?
type ClientType @default(write)
type ClientType @default(write)
projectId String?
project Project? @relation(fields: [projectId], references: [id])
project Project? @relation(fields: [projectId], references: [id])
organizationSlug String
organization Organization? @relation(fields: [organizationId], references: [id])
organizationId String?
cors String?
createdAt DateTime @default(now())
@@ -139,11 +197,13 @@ enum ChartType {
}
model Dashboard {
id String @id @default(dbgenerated("gen_random_uuid()"))
id String @id @default(dbgenerated("gen_random_uuid()"))
name String
organizationSlug String
organization Organization? @relation(fields: [organizationId], references: [id])
organizationId String?
projectId String
project Project @relation(fields: [projectId], references: [id])
project Project @relation(fields: [projectId], references: [id])
reports Report[]
createdAt DateTime @default(now())
@@ -195,14 +255,16 @@ model Waitlist {
}
model ShareOverview {
id String @unique
projectId String @unique
project Project @relation(fields: [projectId], references: [id])
id String @unique
projectId String @unique
project Project @relation(fields: [projectId], references: [id])
organizationSlug String
public Boolean @default(false)
organization Organization? @relation(fields: [organizationId], references: [id])
organizationId String?
public Boolean @default(false)
password String?
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
@@map("shares")
}

View File

@@ -1,24 +1,22 @@
import type {
Organization,
OrganizationInvitation,
OrganizationMembership,
} from '@clerk/nextjs/dist/types/server';
import { auth, clerkClient } from '@clerk/nextjs/server';
import { sort } from 'ramda';
import { auth } from '@clerk/nextjs/server';
import type { ProjectAccess } from '../prisma-client';
import type { Organization, Prisma, ProjectAccess } from '../prisma-client';
import { db } from '../prisma-client';
export type IServiceOrganization = ReturnType<typeof transformOrganization>;
export type IServiceInvite = ReturnType<typeof transformInvite>;
export type IServiceMember = ReturnType<typeof transformMember>;
export type IServiceInvite = Prisma.MemberGetPayload<{
include: { user: true };
}>;
export type IServiceMember = Prisma.MemberGetPayload<{
include: { user: true };
}> & { access: ProjectAccess[] };
export type IServiceProjectAccess = ProjectAccess;
export function transformOrganization(org: Organization) {
return {
id: org.id,
slug: org.id,
name: org.name,
slug: org.slug!,
createdAt: org.createdAt,
};
}
@@ -26,20 +24,28 @@ export function transformOrganization(org: Organization) {
export async function getCurrentOrganizations() {
const session = auth();
if (!session.userId) return [];
const organizations = await clerkClient.users.getOrganizationMembershipList({
userId: session.userId,
const organizations = await db.organization.findMany({
where: {
members: {
some: {
userId: session.userId,
},
},
},
orderBy: {
createdAt: 'desc',
},
});
return sort(
(a, b) => a.createdAt - b.createdAt,
organizations.data.map((item) => transformOrganization(item.organization))
);
return organizations.map(transformOrganization);
}
export function getOrganizationBySlug(slug: string) {
return clerkClient.organizations
.getOrganization({ slug })
.then(transformOrganization)
.catch(() => null);
return db.organization.findUnique({
where: {
id: slug,
},
});
}
export async function getOrganizationByProjectId(projectId: string) {
@@ -47,63 +53,42 @@ export async function getOrganizationByProjectId(projectId: string) {
where: {
id: projectId,
},
include: {
organization: true,
},
});
return clerkClient.organizations.getOrganization({
slug: project.organizationSlug,
});
}
if (!project.organization) {
return null;
}
export function transformInvite(invite: OrganizationInvitation) {
return {
id: invite.id,
organizationId: invite.organizationId,
email: invite.emailAddress,
role: invite.role,
status: invite.status,
createdAt: invite.createdAt,
updatedAt: invite.updatedAt,
publicMetadata: invite.publicMetadata,
};
return transformOrganization(project.organization);
}
export async function getInvites(organizationSlug: string) {
const org = await getOrganizationBySlug(organizationSlug);
if (!org) return [];
return await clerkClient.organizations
.getOrganizationInvitationList({
organizationId: org.id,
})
.then((invites) => invites.data.map(transformInvite));
}
export function transformMember(
item: OrganizationMembership & {
access: IServiceProjectAccess[];
}
) {
return {
memberId: item.id,
id: item.publicUserData?.userId,
name:
[item.publicUserData?.firstName, item.publicUserData?.lastName]
.filter(Boolean)
.join(' ') || 'Unknown',
role: item.role,
createdAt: item.createdAt,
updatedAt: item.updatedAt,
publicMetadata: item.publicMetadata,
organization: transformOrganization(item.organization),
access: item.access,
};
return db.member.findMany({
where: {
organizationId: organizationSlug,
userId: null,
},
include: {
user: true,
},
});
}
export async function getMembers(organizationSlug: string) {
const org = await getOrganizationBySlug(organizationSlug);
if (!org) return [];
const [members, access] = await Promise.all([
clerkClient.organizations.getOrganizationMembershipList({
organizationId: org.id,
db.member.findMany({
where: {
organizationId: organizationSlug,
userId: {
not: null,
},
},
include: {
user: true,
},
}),
db.projectAccess.findMany({
where: {
@@ -112,22 +97,17 @@ export async function getMembers(organizationSlug: string) {
}),
]);
return members.data
.map((member) => {
const projectAccess = access.filter(
(item) => item.userId === member.publicUserData?.userId
);
return {
...member,
access: projectAccess,
};
})
.map(transformMember);
return members.map((member) => ({
...member,
access: access.filter((a) => a.userId === member.userId),
}));
}
export async function getMember(organizationSlug: string, userId: string) {
const org = await getOrganizationBySlug(organizationSlug);
if (!org) return null;
const members = await getMembers(org.id);
return members.find((member) => member.id === userId) ?? null;
return db.member.findFirst({
where: {
organizationId: organizationSlug,
userId,
},
});
}

View File

@@ -58,15 +58,34 @@ export async function getCurrentProjects(organizationSlug: string) {
return [];
}
return db.project.findMany({
where: {
organizationSlug,
},
include: {
access: true,
},
orderBy: {
eventsCount: 'desc',
},
});
const [projects, members, access] = await Promise.all([
db.project.findMany({
where: {
organizationSlug,
},
}),
db.member.findMany({
where: {
userId: session.userId,
organizationId: organizationSlug,
},
}),
db.projectAccess.findMany({
where: {
userId: session.userId,
},
}),
]);
if (members.length === 0) {
return [];
}
if (access.length > 0) {
return projects.filter((project) =>
access.some((a) => a.projectId === project.id)
);
}
return projects;
}

View File

@@ -1,18 +1,7 @@
import type { User } from '@clerk/nextjs/dist/types/server';
import { auth, clerkClient } from '@clerk/nextjs/server';
import { auth } from '@clerk/nextjs/server';
import { db } from '../prisma-client';
export function transformUser(user: User) {
return {
name: `${user.firstName} ${user.lastName}`,
email: user.emailAddresses[0]?.emailAddress ?? '',
id: user.id,
lastName: user.lastName ?? '',
firstName: user.firstName ?? '',
};
}
export async function getCurrentUser() {
const session = auth();
if (!session.userId) {
@@ -22,20 +11,9 @@ export async function getCurrentUser() {
}
export async function getUserById(id: string) {
return clerkClient.users.getUser(id).then(transformUser);
}
export async function isWaitlistUserAccepted() {
const user = await getCurrentUser();
const waitlist = await db.waitlist.findFirst({
return db.user.findUnique({
where: {
email: user?.email,
id,
},
});
if (!waitlist) {
return false;
}
return waitlist.accepted;
}