2060 create a new api key (#2206)
* Add folder for api settings * Init create api key page * Update create api key page * Implement api call to create apiKey * Add create api key mutation * Get id when creating apiKey * Display created Api Key * Add delete api key button * Remove button from InputText * Update stuff * Add test for ApiDetail * Fix type * Use recoil instead of router state * Remane route paths * Remove online return * Move and test date util * Remove useless Component * Rename ApiKeys paths * Rename ApiKeys files * Add input text info testing * Rename hooks to webhooks * Remove console error * Add tests to reach minimum coverage
This commit is contained in:
@ -7,7 +7,6 @@ import {
|
||||
ActivityTarget,
|
||||
Attachment,
|
||||
ApiKey,
|
||||
Hook,
|
||||
Comment,
|
||||
Company,
|
||||
Favorite,
|
||||
@ -22,6 +21,7 @@ import {
|
||||
ViewField,
|
||||
ViewFilter,
|
||||
ViewSort,
|
||||
WebHook,
|
||||
Workspace,
|
||||
WorkspaceMember,
|
||||
} from '@prisma/client';
|
||||
@ -36,7 +36,7 @@ type SubjectsAbility = Subjects<{
|
||||
Comment: Comment;
|
||||
Company: Company;
|
||||
Favorite: Favorite;
|
||||
Hook: Hook;
|
||||
WebHook: WebHook;
|
||||
Person: Person;
|
||||
Pipeline: Pipeline;
|
||||
PipelineProgress: PipelineProgress;
|
||||
@ -83,10 +83,10 @@ 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 });
|
||||
// WebHook
|
||||
can(AbilityAction.Read, 'WebHook', { workspaceId: workspace.id });
|
||||
can(AbilityAction.Create, 'WebHook');
|
||||
can(AbilityAction.Delete, 'WebHook', { workspaceId: workspace.id });
|
||||
|
||||
// Workspace
|
||||
can(AbilityAction.Read, 'Workspace');
|
||||
|
||||
@ -2,6 +2,11 @@ import { Module } from '@nestjs/common';
|
||||
|
||||
import { AbilityFactory } from 'src/ability/ability.factory';
|
||||
import { PrismaService } from 'src/database/prisma.service';
|
||||
import {
|
||||
CreateWebHookAbilityHandler,
|
||||
DeleteWebHookAbilityHandler,
|
||||
ReadWebHookAbilityHandler,
|
||||
} from 'src/ability/handlers/web-hook.ability-handler';
|
||||
|
||||
import {
|
||||
CreateUserAbilityHandler,
|
||||
@ -129,11 +134,6 @@ import {
|
||||
ManageApiKeyAbilityHandler,
|
||||
ReadApiKeyAbilityHandler,
|
||||
} from './handlers/api-key.ability-handler';
|
||||
import {
|
||||
CreateHookAbilityHandler,
|
||||
DeleteHookAbilityHandler,
|
||||
ReadHookAbilityHandler,
|
||||
} from './handlers/hook.ability-handler';
|
||||
|
||||
@Module({
|
||||
providers: [
|
||||
@ -247,9 +247,9 @@ import {
|
||||
CreateApiKeyAbilityHandler,
|
||||
UpdateApiKeyAbilityHandler,
|
||||
// Hook
|
||||
CreateHookAbilityHandler,
|
||||
DeleteHookAbilityHandler,
|
||||
ReadHookAbilityHandler,
|
||||
CreateWebHookAbilityHandler,
|
||||
DeleteWebHookAbilityHandler,
|
||||
ReadWebHookAbilityHandler,
|
||||
],
|
||||
exports: [
|
||||
AbilityFactory,
|
||||
@ -360,9 +360,9 @@ import {
|
||||
CreateApiKeyAbilityHandler,
|
||||
UpdateApiKeyAbilityHandler,
|
||||
// Hook
|
||||
CreateHookAbilityHandler,
|
||||
DeleteHookAbilityHandler,
|
||||
ReadHookAbilityHandler,
|
||||
CreateWebHookAbilityHandler,
|
||||
DeleteWebHookAbilityHandler,
|
||||
ReadWebHookAbilityHandler,
|
||||
],
|
||||
})
|
||||
export class AbilityModule {}
|
||||
|
||||
@ -16,14 +16,14 @@ import { AbilityAction } from 'src/ability/ability.action';
|
||||
import { assert } from 'src/utils/assert';
|
||||
|
||||
@Injectable()
|
||||
export class CreateHookAbilityHandler implements IAbilityHandler {
|
||||
export class CreateWebHookAbilityHandler 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',
|
||||
'WebHook',
|
||||
ability,
|
||||
this.prismaService.client,
|
||||
args,
|
||||
@ -31,27 +31,27 @@ export class CreateHookAbilityHandler implements IAbilityHandler {
|
||||
if (!allowed) {
|
||||
return false;
|
||||
}
|
||||
return ability.can(AbilityAction.Create, 'Hook');
|
||||
return ability.can(AbilityAction.Create, 'WebHook');
|
||||
}
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class DeleteHookAbilityHandler implements IAbilityHandler {
|
||||
export class DeleteWebHookAbilityHandler 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({
|
||||
const hook = await this.prismaService.client.webHook.findFirst({
|
||||
where: args.where,
|
||||
});
|
||||
assert(hook, '', NotFoundException);
|
||||
return ability.can(AbilityAction.Delete, subject('Hook', hook));
|
||||
return ability.can(AbilityAction.Delete, subject('WebHook', hook));
|
||||
}
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class ReadHookAbilityHandler implements IAbilityHandler {
|
||||
export class ReadWebHookAbilityHandler implements IAbilityHandler {
|
||||
async handle(ability: AppAbility) {
|
||||
return ability.can(AbilityAction.Read, 'Hook');
|
||||
return ability.can(AbilityAction.Read, 'WebHook');
|
||||
}
|
||||
}
|
||||
@ -19,7 +19,7 @@ import {
|
||||
import { JwtAuthGuard } from 'src/guards/jwt.auth.guard';
|
||||
import { UserAbility } from 'src/decorators/user-ability.decorator';
|
||||
import { AppAbility } from 'src/ability/ability.factory';
|
||||
import { AuthToken } from 'src/core/auth/dto/token.entity';
|
||||
import { ApiKeyToken } from 'src/core/auth/dto/token.entity';
|
||||
|
||||
import { ApiKeyService } from './api-key.service';
|
||||
|
||||
@ -28,13 +28,13 @@ import { ApiKeyService } from './api-key.service';
|
||||
export class ApiKeyResolver {
|
||||
constructor(private readonly apiKeyService: ApiKeyService) {}
|
||||
|
||||
@Mutation(() => AuthToken)
|
||||
@Mutation(() => ApiKeyToken)
|
||||
@UseGuards(AbilityGuard)
|
||||
@CheckAbilities(CreateApiKeyAbilityHandler)
|
||||
async createOneApiKey(
|
||||
@Args() args: CreateOneApiKeyArgs,
|
||||
@AuthWorkspace() { id: workspaceId }: Workspace,
|
||||
): Promise<AuthToken> {
|
||||
): Promise<ApiKeyToken> {
|
||||
return await this.apiKeyService.generateApiKeyToken(
|
||||
workspaceId,
|
||||
args.data.name,
|
||||
|
||||
@ -5,7 +5,7 @@ import { addMilliseconds, addSeconds } from 'date-fns';
|
||||
import ms from 'ms';
|
||||
|
||||
import { PrismaService } from 'src/database/prisma.service';
|
||||
import { AuthToken } from 'src/core/auth/dto/token.entity';
|
||||
import { ApiKeyToken } from 'src/core/auth/dto/token.entity';
|
||||
import { assert } from 'src/utils/assert';
|
||||
import { EnvironmentService } from 'src/integrations/environment/environment.service';
|
||||
|
||||
@ -28,7 +28,7 @@ export class ApiKeyService {
|
||||
workspaceId: string,
|
||||
name: string,
|
||||
expiresAt?: Date | string,
|
||||
): Promise<AuthToken> {
|
||||
): Promise<ApiKeyToken> {
|
||||
const secret = this.environmentService.getAccessTokenSecret();
|
||||
let expiresIn: string | number;
|
||||
let expirationDate: Date;
|
||||
@ -52,6 +52,7 @@ export class ApiKeyService {
|
||||
},
|
||||
});
|
||||
return {
|
||||
id,
|
||||
token: this.jwtService.sign(jwtPayload, {
|
||||
secret,
|
||||
expiresIn,
|
||||
|
||||
@ -9,6 +9,18 @@ export class AuthToken {
|
||||
expiresAt: Date;
|
||||
}
|
||||
|
||||
@ObjectType()
|
||||
export class ApiKeyToken {
|
||||
@Field(() => String)
|
||||
id: string;
|
||||
|
||||
@Field(() => String)
|
||||
token: string;
|
||||
|
||||
@Field(() => Date)
|
||||
expiresAt: Date;
|
||||
}
|
||||
|
||||
@ObjectType()
|
||||
export class AuthTokenPair {
|
||||
@Field(() => AuthToken)
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
|
||||
import { WebHookModule } from 'src/core/web-hook/web-hook.module';
|
||||
|
||||
import { UserModule } from './user/user.module';
|
||||
import { CommentModule } from './comment/comment.module';
|
||||
import { CompanyModule } from './company/company.module';
|
||||
@ -15,7 +17,6 @@ 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: [
|
||||
@ -34,7 +35,7 @@ import { HookModule } from './hook/hook.module';
|
||||
ViewModule,
|
||||
FavoriteModule,
|
||||
ApiKeyModule,
|
||||
HookModule,
|
||||
WebHookModule,
|
||||
],
|
||||
exports: [
|
||||
AuthModule,
|
||||
@ -48,7 +49,7 @@ import { HookModule } from './hook/hook.module';
|
||||
AttachmentModule,
|
||||
FavoriteModule,
|
||||
ApiKeyModule,
|
||||
HookModule,
|
||||
WebHookModule,
|
||||
],
|
||||
})
|
||||
export class CoreModule {}
|
||||
|
||||
@ -2,11 +2,10 @@ import { Module } from '@nestjs/common';
|
||||
|
||||
import { PrismaModule } from 'src/database/prisma.module';
|
||||
import { AbilityModule } from 'src/ability/ability.module';
|
||||
|
||||
import { HookResolver } from './hook.resolver';
|
||||
import { WebHookResolver } from 'src/core/web-hook/web-hook.resolver';
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule, AbilityModule],
|
||||
providers: [HookResolver],
|
||||
providers: [WebHookResolver],
|
||||
})
|
||||
export class HookModule {}
|
||||
export class WebHookModule {}
|
||||
@ -4,35 +4,35 @@ 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';
|
||||
CreateWebHookAbilityHandler,
|
||||
DeleteWebHookAbilityHandler,
|
||||
ReadWebHookAbilityHandler,
|
||||
} from 'src/ability/handlers/web-hook.ability-handler';
|
||||
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';
|
||||
import { CreateOneWebHookArgs } from 'src/core/@generated/web-hook/create-one-web-hook.args';
|
||||
import { DeleteOneWebHookArgs } from 'src/core/@generated/web-hook/delete-one-web-hook.args';
|
||||
import { FindManyWebHookArgs } from 'src/core/@generated/web-hook/find-many-web-hook.args';
|
||||
import { WebHook } from 'src/core/@generated/web-hook/web-hook.model';
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Resolver(() => Hook)
|
||||
export class HookResolver {
|
||||
@Resolver(() => WebHook)
|
||||
export class WebHookResolver {
|
||||
constructor(private readonly prismaService: PrismaService) {}
|
||||
@Mutation(() => Hook)
|
||||
@Mutation(() => WebHook)
|
||||
@UseGuards(AbilityGuard)
|
||||
@CheckAbilities(CreateHookAbilityHandler)
|
||||
async createOneHook(
|
||||
@Args() args: CreateOneHookArgs,
|
||||
@CheckAbilities(CreateWebHookAbilityHandler)
|
||||
async createOneWebHook(
|
||||
@Args() args: CreateOneWebHookArgs,
|
||||
@AuthWorkspace() { id: workspaceId }: Workspace,
|
||||
): Promise<Hook> {
|
||||
return this.prismaService.client.hook.create({
|
||||
): Promise<WebHook> {
|
||||
return this.prismaService.client.webHook.create({
|
||||
data: {
|
||||
...args.data,
|
||||
...{ workspace: { connect: { id: workspaceId } } },
|
||||
@ -40,31 +40,31 @@ export class HookResolver {
|
||||
});
|
||||
}
|
||||
|
||||
@Mutation(() => Hook, { nullable: false })
|
||||
@Mutation(() => WebHook, { nullable: false })
|
||||
@UseGuards(AbilityGuard)
|
||||
@CheckAbilities(DeleteHookAbilityHandler)
|
||||
async deleteOneHook(@Args() args: DeleteOneHookArgs): Promise<Hook> {
|
||||
const hookToDelete = this.prismaService.client.hook.findUnique({
|
||||
@CheckAbilities(DeleteWebHookAbilityHandler)
|
||||
async deleteOneWebHook(@Args() args: DeleteOneWebHookArgs): Promise<WebHook> {
|
||||
const hookToDelete = this.prismaService.client.webHook.findUnique({
|
||||
where: args.where,
|
||||
});
|
||||
if (!hookToDelete) {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
return await this.prismaService.client.hook.delete({
|
||||
return await this.prismaService.client.webHook.delete({
|
||||
where: args.where,
|
||||
});
|
||||
}
|
||||
|
||||
@Query(() => [Hook])
|
||||
@Query(() => [WebHook])
|
||||
@UseGuards(AbilityGuard)
|
||||
@CheckAbilities(ReadHookAbilityHandler)
|
||||
async findManyHook(
|
||||
@Args() args: FindManyHookArgs,
|
||||
@CheckAbilities(ReadWebHookAbilityHandler)
|
||||
async findManyWebHook(
|
||||
@Args() args: FindManyWebHookArgs,
|
||||
@UserAbility() ability: AppAbility,
|
||||
) {
|
||||
const filterOptions = [accessibleBy(ability).WorkspaceMember];
|
||||
if (args.where) filterOptions.push(args.where);
|
||||
return this.prismaService.client.hook.findMany({
|
||||
return this.prismaService.client.webHook.findMany({
|
||||
...args,
|
||||
where: { AND: filterOptions },
|
||||
});
|
||||
@ -0,0 +1,27 @@
|
||||
/*
|
||||
Warnings:
|
||||
|
||||
- You are about to drop the `hooks` table. If the table is not empty, all the data it contains will be lost.
|
||||
|
||||
*/
|
||||
-- DropForeignKey
|
||||
ALTER TABLE "hooks" DROP CONSTRAINT "hooks_workspaceId_fkey";
|
||||
|
||||
-- DropTable
|
||||
DROP TABLE "hooks";
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "web_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 "web_hooks_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "web_hooks" ADD CONSTRAINT "web_hooks_workspaceId_fkey" FOREIGN KEY ("workspaceId") REFERENCES "workspaces"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
@ -179,7 +179,7 @@ model Workspace {
|
||||
views View[]
|
||||
viewSorts ViewSort[]
|
||||
apiKeys ApiKey[]
|
||||
hooks Hook[]
|
||||
webHooks WebHook[]
|
||||
|
||||
/// @TypeGraphQL.omit(input: true, output: true)
|
||||
deletedAt DateTime?
|
||||
@ -910,7 +910,7 @@ model ApiKey {
|
||||
@@map("api_keys")
|
||||
}
|
||||
|
||||
model Hook {
|
||||
model WebHook {
|
||||
/// @Validator.IsString()
|
||||
/// @Validator.IsOptional()
|
||||
id String @id @default(uuid())
|
||||
@ -925,5 +925,5 @@ model Hook {
|
||||
/// @TypeGraphQL.omit(input: true, output: true)
|
||||
deletedAt DateTime?
|
||||
|
||||
@@map("hooks")
|
||||
@@map("web_hooks")
|
||||
}
|
||||
|
||||
@ -22,5 +22,5 @@ export type ModelSelectMap = {
|
||||
ViewSort: Prisma.ViewSortSelect;
|
||||
ViewField: Prisma.ViewFieldSelect;
|
||||
ApiKey: Prisma.ApiKeySelect;
|
||||
Hook: Prisma.HookSelect;
|
||||
WebHook: Prisma.WebHookSelect;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user