feat: refactoring casl permission checks for recursive nested operations (#778)
* feat: nested casl abilities * fix: remove unused packages * Fixes * Fix createMany broken * Fix lint * Fix lint * Fix lint * Fix lint * Fixes * Fix CommentThread * Fix bugs * Fix lint * Fix bugs * Fixed auto routing * Fixed app path --------- Co-authored-by: Charles Bochet <charles@twenty.com> Co-authored-by: Lucas Bordeau <bordeau.lucas@gmail.com>
This commit is contained in:
@ -0,0 +1,14 @@
|
||||
/*
|
||||
Warnings:
|
||||
|
||||
- Added the required column `workspaceId` to the `comment_thread_targets` table without a default value. This is not possible if the table is not empty.
|
||||
|
||||
*/
|
||||
-- AlterTable
|
||||
ALTER TABLE "comment_thread_targets" ADD COLUMN "workspaceId" TEXT NOT NULL;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "comment_thread_targets" ADD CONSTRAINT "comment_thread_targets_workspaceId_fkey" FOREIGN KEY ("workspaceId") REFERENCES "workspaces"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "attachments" ADD CONSTRAINT "attachments_workspaceId_fkey" FOREIGN KEY ("workspaceId") REFERENCES "workspaces"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
@ -5,24 +5,33 @@ import {
|
||||
OnModuleInit,
|
||||
} from '@nestjs/common';
|
||||
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import { Prisma, PrismaClient } from '@prisma/client';
|
||||
import { createPrismaQueryEventHandler } from 'prisma-query-log';
|
||||
|
||||
import { EnvironmentService } from 'src/integrations/environment/environment.service';
|
||||
|
||||
// TODO: Check if this is still needed
|
||||
if (!global.prisma) {
|
||||
global.prisma = new PrismaClient();
|
||||
}
|
||||
export default global.prisma;
|
||||
// Prepare Prisma extenstion ability
|
||||
const createPrismaClient = (options: Prisma.PrismaClientOptions) => {
|
||||
const client = new PrismaClient(options);
|
||||
|
||||
return client;
|
||||
};
|
||||
|
||||
type ExtendedPrismaClient = ReturnType<typeof createPrismaClient>;
|
||||
|
||||
@Injectable()
|
||||
export class PrismaService extends PrismaClient implements OnModuleInit {
|
||||
export class PrismaService implements OnModuleInit {
|
||||
private readonly logger = new Logger(PrismaService.name);
|
||||
private prismaClient!: ExtendedPrismaClient;
|
||||
|
||||
public get client(): ExtendedPrismaClient {
|
||||
return this.prismaClient;
|
||||
}
|
||||
|
||||
constructor(private readonly environmentService: EnvironmentService) {
|
||||
const debugMode = environmentService.isDebugMode();
|
||||
super({
|
||||
|
||||
this.prismaClient = createPrismaClient({
|
||||
errorFormat: 'minimal',
|
||||
log: debugMode
|
||||
? [
|
||||
@ -44,16 +53,16 @@ export class PrismaService extends PrismaClient implements OnModuleInit {
|
||||
colorParameter: '\u001B[90m',
|
||||
});
|
||||
|
||||
this.$on('query' as any, logHandler);
|
||||
this.prismaClient.$on('query' as any, logHandler);
|
||||
}
|
||||
}
|
||||
|
||||
async onModuleInit() {
|
||||
await this.$connect();
|
||||
async onModuleInit(): Promise<void> {
|
||||
await this.prismaClient.$connect();
|
||||
}
|
||||
|
||||
async enableShutdownHooks(app: INestApplication) {
|
||||
this.$on('beforeExit', async () => {
|
||||
this.prismaClient.$on('beforeExit', async () => {
|
||||
await app.close();
|
||||
});
|
||||
}
|
||||
|
||||
@ -173,8 +173,10 @@ model Workspace {
|
||||
/// @TypeGraphQL.omit(input: true, output: true)
|
||||
deletedAt DateTime?
|
||||
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
Attachment Attachment[]
|
||||
CommentThreadTarget CommentThreadTarget[]
|
||||
|
||||
@@map("workspaces")
|
||||
}
|
||||
@ -379,9 +381,12 @@ model CommentThreadTarget {
|
||||
/// @Validator.IsOptional()
|
||||
id String @id @default(uuid())
|
||||
|
||||
commentThread CommentThread @relation(fields: [commentThreadId], references: [id], onDelete: Cascade)
|
||||
commentThread CommentThread @relation(fields: [commentThreadId], references: [id], onDelete: Cascade)
|
||||
commentThreadId String
|
||||
|
||||
/// @TypeGraphQL.omit(input: true, output: false)
|
||||
workspace Workspace @relation(fields: [workspaceId], references: [id])
|
||||
/// @TypeGraphQL.omit(input: true, output: true)
|
||||
workspaceId String
|
||||
commentableType CommentableType
|
||||
commentableId String
|
||||
|
||||
@ -515,6 +520,8 @@ model Attachment {
|
||||
activityId String
|
||||
activity CommentThread @relation(fields: [activityId], references: [id])
|
||||
|
||||
/// @TypeGraphQL.omit(input: true, output: false)
|
||||
workspace Workspace @relation(fields: [workspaceId], references: [id])
|
||||
/// @TypeGraphQL.omit(input: true, output: true)
|
||||
workspaceId String
|
||||
|
||||
|
||||
@ -18,6 +18,7 @@ export const seedComments = async (prisma: PrismaClient) => {
|
||||
update: {},
|
||||
create: {
|
||||
id: 'twenty-fe256b39-3ec3-4fe3-8997-b76aa0bfb600',
|
||||
workspaceId: 'twenty-7ed9d212-1c25-4d02-bf25-6aeccf7ea419',
|
||||
commentableType: 'Company',
|
||||
commentableId: 'twenty-fe256b39-3ec3-4fe3-8997-b76aa0bfa408',
|
||||
commentThreadId: 'twenty-fe256b39-3ec3-4fe3-8997-b76aa0bfb400',
|
||||
@ -68,6 +69,7 @@ export const seedComments = async (prisma: PrismaClient) => {
|
||||
update: {},
|
||||
create: {
|
||||
id: 'twenty-fe256b39-3ec3-4fe3-8997-a76aa0bfb600',
|
||||
workspaceId: 'twenty-7ed9d212-1c25-4d02-bf25-6aeccf7ea419',
|
||||
commentableType: 'Person',
|
||||
commentableId: 'twenty-755035db-623d-41fe-92e7-dd45b7c568e1',
|
||||
commentThreadId: 'twenty-fe256b39-3ec3-4fe3-8997-b76aa0bfc408',
|
||||
@ -103,6 +105,7 @@ export const seedComments = async (prisma: PrismaClient) => {
|
||||
update: {},
|
||||
create: {
|
||||
id: 'twenty-dev-fe256b39-3ec3-4fe3-8997-a76aa0bfba00',
|
||||
workspaceId: 'twenty-dev-7ed9d212-1c25-4d02-bf25-6aeccf7ea420',
|
||||
commentableType: 'Company',
|
||||
commentableId: 'twenty-dev-a674fa6c-1455-4c57-afaf-dd5dc086361e',
|
||||
commentThreadId: 'twenty-dev-fe256b39-3ec3-4fe3-8997-b76aaabfb408',
|
||||
|
||||
Reference in New Issue
Block a user