2049 timebox 1j zapier integration 4 define and implement a first trigger for zapier app (#2132)
* Add create company trigger * Refactor * Add operation in subscribe * Add create hook api endpoint * Add import of hook module * Add a test for hook subscribe * Add delete hook api endpoint * Add delete hook test * Add findMany hook route --------- Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
@ -7,6 +7,7 @@ import {
|
||||
ActivityTarget,
|
||||
Attachment,
|
||||
ApiKey,
|
||||
Hook,
|
||||
Comment,
|
||||
Company,
|
||||
Favorite,
|
||||
@ -35,6 +36,7 @@ type SubjectsAbility = Subjects<{
|
||||
Comment: Comment;
|
||||
Company: Company;
|
||||
Favorite: Favorite;
|
||||
Hook: Hook;
|
||||
Person: Person;
|
||||
Pipeline: Pipeline;
|
||||
PipelineProgress: PipelineProgress;
|
||||
@ -81,6 +83,11 @@ export class AbilityFactory {
|
||||
can(AbilityAction.Create, 'ApiKey');
|
||||
can(AbilityAction.Update, 'ApiKey', { workspaceId: workspace.id });
|
||||
|
||||
// Hook
|
||||
can(AbilityAction.Read, 'Hook', { workspaceId: workspace.id });
|
||||
can(AbilityAction.Create, 'Hook');
|
||||
can(AbilityAction.Delete, 'Hook', { workspaceId: workspace.id });
|
||||
|
||||
// Workspace
|
||||
can(AbilityAction.Read, 'Workspace');
|
||||
can(AbilityAction.Update, 'Workspace');
|
||||
|
||||
@ -128,6 +128,11 @@ import {
|
||||
ManageApiKeyAbilityHandler,
|
||||
ReadApiKeyAbilityHandler,
|
||||
} from './handlers/api-key.ability-handler';
|
||||
import {
|
||||
CreateHookAbilityHandler,
|
||||
DeleteHookAbilityHandler,
|
||||
ReadHookAbilityHandler,
|
||||
} from './handlers/hook.ability-handler';
|
||||
|
||||
@Module({
|
||||
providers: [
|
||||
@ -239,6 +244,10 @@ import {
|
||||
ManageApiKeyAbilityHandler,
|
||||
CreateApiKeyAbilityHandler,
|
||||
UpdateApiKeyAbilityHandler,
|
||||
// Hook
|
||||
CreateHookAbilityHandler,
|
||||
DeleteHookAbilityHandler,
|
||||
ReadHookAbilityHandler,
|
||||
],
|
||||
exports: [
|
||||
AbilityFactory,
|
||||
@ -348,6 +357,10 @@ import {
|
||||
ManageApiKeyAbilityHandler,
|
||||
CreateApiKeyAbilityHandler,
|
||||
UpdateApiKeyAbilityHandler,
|
||||
// Hook
|
||||
CreateHookAbilityHandler,
|
||||
DeleteHookAbilityHandler,
|
||||
ReadHookAbilityHandler,
|
||||
],
|
||||
})
|
||||
export class AbilityModule {}
|
||||
|
||||
57
server/src/ability/handlers/hook.ability-handler.ts
Normal file
57
server/src/ability/handlers/hook.ability-handler.ts
Normal file
@ -0,0 +1,57 @@
|
||||
import {
|
||||
Injectable,
|
||||
ExecutionContext,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { GqlExecutionContext } from '@nestjs/graphql';
|
||||
|
||||
import { subject } from '@casl/ability';
|
||||
|
||||
import { IAbilityHandler } from 'src/ability/interfaces/ability-handler.interface';
|
||||
|
||||
import { AppAbility } from 'src/ability/ability.factory';
|
||||
import { relationAbilityChecker } from 'src/ability/ability.util';
|
||||
import { PrismaService } from 'src/database/prisma.service';
|
||||
import { AbilityAction } from 'src/ability/ability.action';
|
||||
import { assert } from 'src/utils/assert';
|
||||
|
||||
@Injectable()
|
||||
export class CreateHookAbilityHandler implements IAbilityHandler {
|
||||
constructor(private readonly prismaService: PrismaService) {}
|
||||
|
||||
async handle(ability: AppAbility, context: ExecutionContext) {
|
||||
const gqlContext = GqlExecutionContext.create(context);
|
||||
const args = gqlContext.getArgs();
|
||||
const allowed = await relationAbilityChecker(
|
||||
'Hook',
|
||||
ability,
|
||||
this.prismaService.client,
|
||||
args,
|
||||
);
|
||||
if (!allowed) {
|
||||
return false;
|
||||
}
|
||||
return ability.can(AbilityAction.Create, 'Hook');
|
||||
}
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class DeleteHookAbilityHandler implements IAbilityHandler {
|
||||
constructor(private readonly prismaService: PrismaService) {}
|
||||
async handle(ability: AppAbility, context: ExecutionContext) {
|
||||
const gqlContext = GqlExecutionContext.create(context);
|
||||
const args = gqlContext.getArgs();
|
||||
const hook = await this.prismaService.client.hook.findFirst({
|
||||
where: args.where,
|
||||
});
|
||||
assert(hook, '', NotFoundException);
|
||||
return ability.can(AbilityAction.Delete, subject('Hook', hook));
|
||||
}
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class ReadHookAbilityHandler implements IAbilityHandler {
|
||||
async handle(ability: AppAbility) {
|
||||
return ability.can(AbilityAction.Read, 'Hook');
|
||||
}
|
||||
}
|
||||
@ -15,6 +15,7 @@ import { ActivityModule } from './activity/activity.module';
|
||||
import { ViewModule } from './view/view.module';
|
||||
import { FavoriteModule } from './favorite/favorite.module';
|
||||
import { ApiKeyModule } from './api-key/api-key.module';
|
||||
import { HookModule } from './hook/hook.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@ -33,6 +34,7 @@ import { ApiKeyModule } from './api-key/api-key.module';
|
||||
ViewModule,
|
||||
FavoriteModule,
|
||||
ApiKeyModule,
|
||||
HookModule,
|
||||
],
|
||||
exports: [
|
||||
AuthModule,
|
||||
@ -46,6 +48,7 @@ import { ApiKeyModule } from './api-key/api-key.module';
|
||||
AttachmentModule,
|
||||
FavoriteModule,
|
||||
ApiKeyModule,
|
||||
HookModule,
|
||||
],
|
||||
})
|
||||
export class CoreModule {}
|
||||
|
||||
12
server/src/core/hook/hook.module.ts
Normal file
12
server/src/core/hook/hook.module.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
|
||||
import { PrismaModule } from 'src/database/prisma.module';
|
||||
import { AbilityModule } from 'src/ability/ability.module';
|
||||
|
||||
import { HookResolver } from './hook.resolver';
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule, AbilityModule],
|
||||
providers: [HookResolver],
|
||||
})
|
||||
export class HookModule {}
|
||||
72
server/src/core/hook/hook.resolver.ts
Normal file
72
server/src/core/hook/hook.resolver.ts
Normal file
@ -0,0 +1,72 @@
|
||||
import { NotFoundException, UseGuards } from '@nestjs/common';
|
||||
import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
|
||||
|
||||
import { accessibleBy } from '@casl/prisma';
|
||||
|
||||
import { JwtAuthGuard } from 'src/guards/jwt.auth.guard';
|
||||
import { Hook } from 'src/core/@generated/hook/hook.model';
|
||||
import { AbilityGuard } from 'src/guards/ability.guard';
|
||||
import { CheckAbilities } from 'src/decorators/check-abilities.decorator';
|
||||
import {
|
||||
CreateHookAbilityHandler,
|
||||
DeleteHookAbilityHandler,
|
||||
ReadHookAbilityHandler,
|
||||
} from 'src/ability/handlers/hook.ability-handler';
|
||||
import { CreateOneHookArgs } from 'src/core/@generated/hook/create-one-hook.args';
|
||||
import { PrismaService } from 'src/database/prisma.service';
|
||||
import { AuthWorkspace } from 'src/decorators/auth-workspace.decorator';
|
||||
import { Workspace } from 'src/core/@generated/workspace/workspace.model';
|
||||
import { DeleteOneHookArgs } from 'src/core/@generated/hook/delete-one-hook.args';
|
||||
import { FindManyHookArgs } from 'src/core/@generated/hook/find-many-hook.args';
|
||||
import { UserAbility } from 'src/decorators/user-ability.decorator';
|
||||
import { AppAbility } from 'src/ability/ability.factory';
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Resolver(() => Hook)
|
||||
export class HookResolver {
|
||||
constructor(private readonly prismaService: PrismaService) {}
|
||||
@Mutation(() => Hook)
|
||||
@UseGuards(AbilityGuard)
|
||||
@CheckAbilities(CreateHookAbilityHandler)
|
||||
async createOneHook(
|
||||
@Args() args: CreateOneHookArgs,
|
||||
@AuthWorkspace() { id: workspaceId }: Workspace,
|
||||
): Promise<Hook> {
|
||||
return this.prismaService.client.hook.create({
|
||||
data: {
|
||||
...args.data,
|
||||
...{ workspace: { connect: { id: workspaceId } } },
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@Mutation(() => Hook, { nullable: false })
|
||||
@UseGuards(AbilityGuard)
|
||||
@CheckAbilities(DeleteHookAbilityHandler)
|
||||
async deleteOneHook(@Args() args: DeleteOneHookArgs): Promise<Hook> {
|
||||
const hookToDelete = this.prismaService.client.hook.findUnique({
|
||||
where: args.where,
|
||||
});
|
||||
if (!hookToDelete) {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
return await this.prismaService.client.hook.delete({
|
||||
where: args.where,
|
||||
});
|
||||
}
|
||||
|
||||
@Query(() => [Hook])
|
||||
@UseGuards(AbilityGuard)
|
||||
@CheckAbilities(ReadHookAbilityHandler)
|
||||
async findManyHook(
|
||||
@Args() args: FindManyHookArgs,
|
||||
@UserAbility() ability: AppAbility,
|
||||
) {
|
||||
const filterOptions = [accessibleBy(ability).WorkspaceMember];
|
||||
if (args.where) filterOptions.push(args.where);
|
||||
return this.prismaService.client.hook.findMany({
|
||||
...args,
|
||||
where: { AND: filterOptions },
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
-- CreateTable
|
||||
CREATE TABLE "hooks" (
|
||||
"id" TEXT NOT NULL,
|
||||
"workspaceId" TEXT NOT NULL,
|
||||
"targetUrl" TEXT NOT NULL,
|
||||
"operation" TEXT NOT NULL,
|
||||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" TIMESTAMP(3) NOT NULL,
|
||||
"deletedAt" TIMESTAMP(3),
|
||||
|
||||
CONSTRAINT "hooks_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "hooks" ADD CONSTRAINT "hooks_workspaceId_fkey" FOREIGN KEY ("workspaceId") REFERENCES "workspaces"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
@ -179,6 +179,7 @@ model Workspace {
|
||||
views View[]
|
||||
viewSorts ViewSort[]
|
||||
apiKeys ApiKey[]
|
||||
hooks Hook[]
|
||||
|
||||
/// @TypeGraphQL.omit(input: true, output: true)
|
||||
deletedAt DateTime?
|
||||
@ -907,3 +908,21 @@ model ApiKey {
|
||||
|
||||
@@map("api_keys")
|
||||
}
|
||||
|
||||
model Hook {
|
||||
/// @Validator.IsString()
|
||||
/// @Validator.IsOptional()
|
||||
id String @id @default(uuid())
|
||||
/// @TypeGraphQL.omit(input: true, output: true)
|
||||
workspace Workspace @relation(fields: [workspaceId], references: [id])
|
||||
/// @TypeGraphQL.omit(input: true, output: true)
|
||||
workspaceId String
|
||||
targetUrl String
|
||||
operation String
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
/// @TypeGraphQL.omit(input: true, output: true)
|
||||
deletedAt DateTime?
|
||||
|
||||
@@map("hooks")
|
||||
}
|
||||
|
||||
@ -22,4 +22,5 @@ export type ModelSelectMap = {
|
||||
ViewSort: Prisma.ViewSortSelect;
|
||||
ViewField: Prisma.ViewFieldSelect;
|
||||
ApiKey: Prisma.ApiKeySelect;
|
||||
Hook: Prisma.HookSelect;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user