Skip to content

The open alternative to Salesforce, designed for AI.

4.8 KB
Raw Blame
import {
  Controller,
  Get,
  Logger,
  Res,
  UseFilters,
  UseGuards,
} from '@nestjs/common';

import { pipeline } from 'stream/promises';

import { Response } from 'express';
import { ApiPath, FileFolder } from 'twenty-shared/types';

import { ApplicationRestApiExceptionFilter } from 'src/engine/core-modules/application/application-rest-api-exception.filter';
import {
  FileStorageException,
  FileStorageExceptionCode,
} from 'src/engine/core-modules/file-storage/interfaces/file-storage-exception';
import { PRESIGNED_URL_NO_STORE_CACHE_CONTROL } from 'src/engine/core-modules/file/interfaces/file-folder.interface';
import { setFileResponseHeaders } from 'src/engine/core-modules/file/utils/set-file-response-headers.util';

import { WorkspaceEntity } from 'src/engine/core-modules/workspace/workspace.entity';
import { ApplicationTargetParam } from 'src/engine/decorators/auth/application-target-param.decorator';
import { AuthWorkspace } from 'src/engine/decorators/auth/auth-workspace.decorator';
import { AllowSuspendedWorkspace } from 'src/engine/decorators/auth/allow-suspended-workspace.decorator';
import { FlatEntityMapsRestApiExceptionFilter } from 'src/engine/metadata-modules/flat-entity/filters/flat-entity-maps-rest-api-exception.filter';
import { FrontComponentRestApiExceptionFilter } from 'src/engine/metadata-modules/front-component/filters/front-component-rest-api-exception.filter';
import {
  FrontComponentException,
  FrontComponentExceptionCode,
} from 'src/engine/metadata-modules/front-component/front-component.exception';
import { FrontComponentService } from 'src/engine/metadata-modules/front-component/front-component.service';
import { PermissionsRestApiExceptionFilter } from 'src/engine/permissions/utils/permissions-rest-api-exception.filter';
import { WorkspaceMigrationRunnerRestApiExceptionFilter } from 'src/engine/workspace-manager/workspace-migration/filters/workspace-migration-runner-rest-api-exception.filter';
import { ApplicationTargetGuard } from 'src/engine/guards/application-target.guard';
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';

@Controller(`${ApiPath.Rest}/front-components`)
@AllowSuspendedWorkspace()
@UseGuards(AccessPolicyGuard(FRONT_COMPONENT_READ_ACCESS_POLICY))
@UseFilters(
  PermissionsRestApiExceptionFilter,
  ApplicationRestApiExceptionFilter,
  FrontComponentRestApiExceptionFilter,
  FlatEntityMapsRestApiExceptionFilter,
  WorkspaceMigrationRunnerRestApiExceptionFilter,
)
export class FrontComponentController {
  private readonly logger = new Logger(FrontComponentController.name);

  constructor(private readonly frontComponentService: FrontComponentService) {}

  @Get([':frontComponentId', ':frontComponentId/:cacheKey'])
  @UseGuards(ApplicationTargetGuard)
  async getBuiltJs(
    @Res() res: Response,
    @ApplicationTargetParam('frontComponentId', {
      kind: 'applicationOwnedEntity',
      metadataName: 'frontComponent',
      requireApplicationRegistrationOwnership: false,
    })
    frontComponentId: string,
    @AuthWorkspace() workspace: WorkspaceEntity,
  ) {
    const fileResponse = await this.frontComponentService
      .getBuiltComponentPresignedUrlOrStream({
        frontComponentId,
        workspaceId: workspace.id,
      })
      .catch((error) => {
        if (error instanceof FrontComponentException) {
          throw error;
        }

        if (
          error instanceof FileStorageException &&
          error.code === FileStorageExceptionCode.FILE_NOT_FOUND
        ) {
          throw new FrontComponentException(
            'Front component built file not found',
            FrontComponentExceptionCode.FRONT_COMPONENT_NOT_FOUND,
          );
        }

        this.logger.error(
          'getBuiltComponentPresignedUrlOrStream failed unexpectedly',
          { error },
        );

        throw new FrontComponentException(
          'Error retrieving front component built file',
          FrontComponentExceptionCode.FRONT_COMPONENT_NOT_READY,
        );
      });

    if (fileResponse.type === 'redirect') {
      res.setHeader('Cache-Control', PRESIGNED_URL_NO_STORE_CACHE_CONTROL);

      return res.json({ url: fileResponse.presignedUrl });
    }

    setFileResponseHeaders(
      res,
      fileResponse.mimeType,
      FileFolder.BuiltFrontComponent,
    );

    try {
      await pipeline(fileResponse.stream, res);
    } catch (error) {
      fileResponse.stream.destroy();
      this.logger.error('Front component stream failed mid-transfer', {
        error,
      });

      if (!res.headersSent) {
        throw new FrontComponentException(
          'Error streaming front component built file',
          FrontComponentExceptionCode.FRONT_COMPONENT_NOT_READY,
        );
      }

      res.destroy();
    }
  }
}