feat: add tenant table/logic

This commit is contained in:
piopi 2025-01-13 09:13:31 -05:00
parent a37a57b437
commit 81b4027737
No known key found for this signature in database
GPG key ID: E305BD1ADD33F590
3 changed files with 53 additions and 3 deletions

View file

@ -41,6 +41,15 @@ export default ts.config(
caughtErrorsIgnorePattern: '^_', caughtErrorsIgnorePattern: '^_',
}, },
], ],
'no-restricted-syntax': [
'error',
{
selector:
'CallExpression:matches([callee.object.object.name="prisma"], [callee.object.object.name="prismaTransactionClient"], [callee.object.object.name="transactionClient"]):matches([callee.property.name="findFirst"], [callee.property.name="findMany"], [callee.property.name="updateMany"], [callee.property.name="deleteMany"], [callee.property.name="count"], [callee.property.name="aggregate"], [callee.property.name="groupBy"]):not(:has(ObjectExpression > Property[key.name="where"] > ObjectExpression > Property[key.name="tenantId"]))',
message:
'Please filter on the current tenant when using findFirst, findMany, updateMany, deleteMany, count, aggregate or groupBy.',
},
],
}, },
} }
); );

View file

@ -15,12 +15,29 @@ datasource db {
} }
model User { model User {
id String @id @default(uuid()) id String @default(uuid())
clerkId String @unique clerkId String
tenant Tenant @relation(fields: [tenantId], references: [id])
tenantId String
email String? email String?
name String name String
createdAt DateTime @default(now()) createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt updatedAt DateTime @default(now()) @updatedAt
@@id([id, tenantId])
@@unique([clerkId, tenantId])
}
model Tenant {
id String @id @default(uuid())
name String
slug String @unique
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
clerkId String @unique
User User[]
@@map("tenant")
} }

View file

@ -14,7 +14,7 @@ export async function validateSession({ locals }: ServerLoadEvent) {
return redirect(307, '/login'); return redirect(307, '/login');
} }
if (!locals.auth.orgId && locals.auth.sessionId) { if ((!locals.auth.orgId && locals.auth.sessionId) || !locals.auth.orgId) {
// Sign out the user if they are not associated with an organization // Sign out the user if they are not associated with an organization
await clerkSessionClient.sessions.revokeSession(locals.auth.sessionId); await clerkSessionClient.sessions.revokeSession(locals.auth.sessionId);
return redirect(307, '/login'); return redirect(307, '/login');
@ -22,9 +22,32 @@ export async function validateSession({ locals }: ServerLoadEvent) {
const clerkUser = await clerkClient.users.getUser(locals.auth.userId); const clerkUser = await clerkClient.users.getUser(locals.auth.userId);
const tenantClerkId = locals.auth.orgId;
let tenant = await prisma.tenant.findUnique({
where: {
clerkId: tenantClerkId,
},
});
if (!tenant) {
const organization = await clerkClient.organizations.getOrganization({
organizationId: tenantClerkId,
});
tenant = await prisma.tenant.create({
data: {
clerkId: tenantClerkId,
name: organization.name,
slug: organization.slug ?? `tenant-${tenantClerkId}`,
},
});
}
let user = await prisma.user.findFirst({ let user = await prisma.user.findFirst({
where: { where: {
clerkId: clerkUser.id, clerkId: clerkUser.id,
tenantId: tenant.id,
}, },
}); });
@ -41,6 +64,7 @@ export async function validateSession({ locals }: ServerLoadEvent) {
clerkId: clerkUser.id, clerkId: clerkUser.id,
email: clerkUser.emailAddresses[0].emailAddress, email: clerkUser.emailAddresses[0].emailAddress,
name: clerkUser.fullName ?? '', name: clerkUser.fullName ?? '',
tenantId: tenant.id,
}, },
}); });