Skip to content

The open alternative to Salesforce, designed for AI.

12.2 KB
Raw Blame
import { Inject, UseGuards, UseInterceptors, UseFilters } from '@nestjs/common';
import {
  Args,
  Info,
  Mutation,
  Parent,
  Query,
  ResolveField,
} from '@nestjs/graphql';

import { type GraphQLResolveInfo } from 'graphql';
import graphqlFields from 'graphql-fields';
import { type ApplicationCapability } from 'twenty-shared/application';
import { PermissionFlagType } from 'twenty-shared/constants';
import { type IframeMediaPermission } from 'twenty-shared/types';
import { isDefined } from 'twenty-shared/utils';

import { MetadataResolver } from 'src/engine/api/graphql/graphql-config/decorators/metadata-resolver.decorator';
import { UUIDScalarType } from 'src/engine/api/graphql/workspace-schema-builder/graphql-types/scalars';
import { ApplicationExceptionFilter } from 'src/engine/core-modules/application/application-exception-filter';
import { ApplicationTokenPairDTO } from 'src/engine/core-modules/application/application-oauth/dtos/application-token-pair.dto';
import { ApplicationService } from 'src/engine/core-modules/application/application.service';
import { UserApplicationVariableValueService } from 'src/engine/core-modules/application/application-variable/user-application-variable-value.service';
import { ApplicationVariableEntityService } from 'src/engine/core-modules/application/application-variable/application-variable.service';
import { type FlatApplication } from 'src/engine/core-modules/application/types/flat-application.type';
import { canCallerReachApplication } from 'src/engine/core-modules/application/utils/can-caller-reach-application.util';
import { ApplicationTokenService } from 'src/engine/core-modules/auth/token/services/application-token.service';
import { type AuthContextUser } from 'src/engine/core-modules/auth/types/auth-context.type';
import { IframeMediaPermissionDTO } from 'src/engine/core-modules/workspace/dtos/iframe-media-permission.dto';
import { AuthGraphqlApiExceptionFilter } from 'src/engine/core-modules/auth/filters/auth-graphql-api-exception.filter';
import { type WorkspaceEntity } from 'src/engine/core-modules/workspace/workspace.entity';
import { AuthApplication } from 'src/engine/decorators/auth/auth-application.decorator';
import { AuthUserWorkspaceId } from 'src/engine/decorators/auth/auth-user-workspace-id.decorator';
import { AuthUser } from 'src/engine/decorators/auth/auth-user.decorator';
import { AuthWorkspace } from 'src/engine/decorators/auth/auth-workspace.decorator';
import { AllowSuspendedWorkspace } from 'src/engine/decorators/auth/allow-suspended-workspace.decorator';
import { fromFlatFrontComponentToFrontComponentDto } from 'src/engine/metadata-modules/flat-front-component/utils/from-flat-front-component-to-front-component-dto.util';
import { CreateFrontComponentInput } from 'src/engine/metadata-modules/front-component/dtos/create-front-component.input';
import { FrontComponentDTO } from 'src/engine/metadata-modules/front-component/dtos/front-component.dto';
import { UpdateFrontComponentInput } from 'src/engine/metadata-modules/front-component/dtos/update-front-component.input';
import { FrontComponentService } from 'src/engine/metadata-modules/front-component/front-component.service';
import { FrontComponentGraphqlApiExceptionInterceptor } from 'src/engine/metadata-modules/front-component/interceptors/front-component-graphql-api-exception.interceptor';
import { WorkspaceCacheService } from 'src/engine/workspace-cache/services/workspace-cache.service';
import { WorkspaceMigrationGraphqlApiExceptionInterceptor } from 'src/engine/workspace-manager/workspace-migration/interceptors/workspace-migration-graphql-api-exception.interceptor';
import { FRONT_COMPONENT_READ_ACCESS_POLICY } from 'src/engine/metadata-modules/front-component/constants/front-component-read-access-policy.constant';
import { AccessPolicyGuard } from 'src/engine/guards/access-policy.guard';
import { type AccessPolicy } from 'src/engine/guards/types/access-policy.type';

const FRONT_COMPONENT_USER_SESSION_ACCESS_POLICY: AccessPolicy = {
  authPrincipals: {
    userSession: {
      standard: true,
      impersonated: true,
      playground: false,
      workspaceAgnostic: false,
    },
    apiKey: false,
    oauthClient: false,
    application: false,
  },
  requiredPermissionFlags: [],
};

const FRONT_COMPONENT_WRITE_ACCESS_POLICY: AccessPolicy = {
  authPrincipals: {
    userSession: {
      standard: true,
      impersonated: true,
      playground: true,
      workspaceAgnostic: false,
    },
    apiKey: true,
    oauthClient: true,
    application: false,
  },
  requiredPermissionFlags: [PermissionFlagType.APPLICATIONS],
};

@UseInterceptors(
  WorkspaceMigrationGraphqlApiExceptionInterceptor,
  FrontComponentGraphqlApiExceptionInterceptor,
)
@MetadataResolver(() => FrontComponentDTO)
@UseFilters(AuthGraphqlApiExceptionFilter)
export class FrontComponentResolver {
  constructor(
    @Inject(FrontComponentService)
    private readonly frontComponentService: FrontComponentService,
    @Inject(ApplicationTokenService)
    private readonly applicationTokenService: ApplicationTokenService,
    private readonly applicationVariableService: ApplicationVariableEntityService,
    private readonly userApplicationVariableValueService: UserApplicationVariableValueService,
    private readonly workspaceCacheService: WorkspaceCacheService,
    private readonly applicationService: ApplicationService,
  ) {}

  @ResolveField(() => String, { nullable: true })
  async frontComponentSharedDependenciesChecksum(
    @Parent() frontComponent: FrontComponentDTO,
    @AuthWorkspace() workspace: WorkspaceEntity,
  ): Promise<string | null> {
    const { flatApplicationMaps } =
      await this.workspaceCacheService.getOrRecompute(workspace.id, [
        'flatApplicationMaps',
      ]);

    return (
      flatApplicationMaps.byId[frontComponent.applicationId]
        ?.frontComponentSharedDependenciesChecksum ?? null
    );
  }

  @ResolveField(() => String, { nullable: true })
  async applicationName(
    @Parent() frontComponent: FrontComponentDTO,
    @AuthWorkspace() workspace: WorkspaceEntity,
  ): Promise<string | undefined> {
    const { flatApplicationMaps } =
      await this.workspaceCacheService.getOrRecompute(workspace.id, [
        'flatApplicationMaps',
      ]);

    return flatApplicationMaps.byId[frontComponent.applicationId]?.name;
  }

  @ResolveField(() => [String], { nullable: true })
  async applicationGrantedCapabilities(
    @Parent() frontComponent: FrontComponentDTO,
    @AuthWorkspace() workspace: WorkspaceEntity,
  ): Promise<ApplicationCapability[]> {
    const { flatApplicationMaps } =
      await this.workspaceCacheService.getOrRecompute(workspace.id, [
        'flatApplicationMaps',
      ]);

    return (
      flatApplicationMaps.byId[frontComponent.applicationId]
        ?.grantedCapabilities ?? []
    );
  }

  @ResolveField(() => [IframeMediaPermissionDTO], { nullable: true })
  async applicationGrantedIframeMediaPermissions(
    @Parent() frontComponent: FrontComponentDTO,
    @AuthWorkspace()
    { id: workspaceId, workspaceCustomApplicationId }: WorkspaceEntity,
    @AuthApplication({ allowUndefined: true })
    callingApplication: FlatApplication | undefined,
  ): Promise<IframeMediaPermission[]> {
    const application =
      await this.applicationService.findReadableInstalledFlatApplication({
        applicationId: frontComponent.applicationId,
        workspaceId,
        workspaceCustomApplicationId,
        callingApplication,
      });

    return application?.grantedIframeMediaPermissions ?? [];
  }

  @Query(() => [FrontComponentDTO])
  @UseGuards(AccessPolicyGuard(FRONT_COMPONENT_READ_ACCESS_POLICY))
  @AllowSuspendedWorkspace()
  async frontComponents(
    @AuthWorkspace() workspace: WorkspaceEntity,
    @AuthApplication({ allowUndefined: true })
    callingApplication: FlatApplication | undefined,
  ): Promise<FrontComponentDTO[]> {
    const frontComponents = await this.frontComponentService.findAll(
      workspace.id,
    );

    return frontComponents.filter((frontComponent) =>
      canCallerReachApplication({
        callingApplication,
        applicationId: frontComponent.applicationId,
        operation: 'read',
        workspaceCustomApplicationId: workspace.workspaceCustomApplicationId,
      }),
    );
  }

  @Query(() => FrontComponentDTO, { nullable: true })
  @UseGuards(AccessPolicyGuard(FRONT_COMPONENT_USER_SESSION_ACCESS_POLICY))
  async frontComponent(
    @Args('id', { type: () => UUIDScalarType }) id: string,
    @AuthWorkspace() workspace: WorkspaceEntity,
    @AuthUser() user: AuthContextUser,
    @AuthUserWorkspaceId() userWorkspaceId: string,
    @Info() info: GraphQLResolveInfo,
  ): Promise<FrontComponentDTO | null> {
    const dto = await this.frontComponentService.findById(id, workspace.id);

    if (!dto) {
      return null;
    }

    const selectedFields = graphqlFields(info);

    const [applicationTokenPair, applicationVariables] = await Promise.all([
      isDefined(selectedFields.applicationTokenPair)
        ? this.applicationTokenService.generateApplicationTokenPair({
            applicationId: dto.applicationId,
            workspaceId: workspace.id,
            userWorkspaceId,
            userId: user.id,
          })
        : undefined,
      isDefined(selectedFields.applicationVariables)
        ? this.getPublicApplicationVariables({
            workspaceId: workspace.id,
            applicationId: dto.applicationId,
            userWorkspaceId,
          })
        : undefined,
    ]);

    return {
      ...dto,
      applicationTokenPair,
      applicationVariables,
    };
  }

  @Mutation(() => ApplicationTokenPairDTO)
  @UseGuards(AccessPolicyGuard(FRONT_COMPONENT_USER_SESSION_ACCESS_POLICY))
  @UseFilters(ApplicationExceptionFilter)
  async generateFrontComponentApplicationTokenPair(
    @Args('applicationId', { type: () => UUIDScalarType })
    applicationId: string,
    @AuthWorkspace() workspace: WorkspaceEntity,
    @AuthUser() user: AuthContextUser,
    @AuthUserWorkspaceId() userWorkspaceId: string,
  ): Promise<ApplicationTokenPairDTO> {
    return this.applicationTokenService.generateApplicationTokenPair({
      applicationId,
      workspaceId: workspace.id,
      userWorkspaceId,
      userId: user.id,
    });
  }

  @Mutation(() => FrontComponentDTO)
  @UseGuards(AccessPolicyGuard(FRONT_COMPONENT_WRITE_ACCESS_POLICY))
  async createFrontComponent(
    @Args('input') input: CreateFrontComponentInput,
    @AuthWorkspace() workspace: WorkspaceEntity,
  ): Promise<FrontComponentDTO> {
    const flatFrontComponent = await this.frontComponentService.createOne({
      input,
      workspaceId: workspace.id,
    });

    return fromFlatFrontComponentToFrontComponentDto(flatFrontComponent);
  }

  @Mutation(() => FrontComponentDTO)
  @UseGuards(AccessPolicyGuard(FRONT_COMPONENT_WRITE_ACCESS_POLICY))
  async updateFrontComponent(
    @Args('input') input: UpdateFrontComponentInput,
    @AuthWorkspace() workspace: WorkspaceEntity,
  ): Promise<FrontComponentDTO> {
    const flatFrontComponent = await this.frontComponentService.updateOne({
      id: input.id,
      update: input.update,
      workspaceId: workspace.id,
    });

    return fromFlatFrontComponentToFrontComponentDto(flatFrontComponent);
  }

  @Mutation(() => FrontComponentDTO)
  @UseGuards(AccessPolicyGuard(FRONT_COMPONENT_WRITE_ACCESS_POLICY))
  async deleteFrontComponent(
    @Args('id', { type: () => UUIDScalarType }) id: string,
    @AuthWorkspace() workspace: WorkspaceEntity,
  ): Promise<FrontComponentDTO> {
    const flatFrontComponent = await this.frontComponentService.destroyOne({
      id,
      workspaceId: workspace.id,
    });

    return fromFlatFrontComponentToFrontComponentDto(flatFrontComponent);
  }

  private async getPublicApplicationVariables({
    workspaceId,
    applicationId,
    userWorkspaceId,
  }: {
    workspaceId: string;
    applicationId: string;
    userWorkspaceId: string;
  }): Promise<Record<string, string>> {
    const [workspaceVariables, userVariables] = await Promise.all([
      this.applicationVariableService.getPublicEnvVariables({
        workspaceId,
        applicationId,
      }),
      this.userApplicationVariableValueService.getPublicEnvVariables({
        workspaceId,
        applicationId,
        userWorkspaceId,
      }),
    ]);

    return { ...workspaceVariables, ...userVariables };
  }
}