Skip to content

The open alternative to Salesforce, designed for AI.

1.9 KB
Raw Blame
import { trimAndRemoveDuplicatedWhitespacesFromObjectStringProperties } from 'twenty-shared/utils';
import { v4 } from 'uuid';

import { type FlatApplication } from 'src/engine/core-modules/application/types/flat-application.type';
import { type FlatFrontComponent } from 'src/engine/metadata-modules/flat-front-component/types/flat-front-component.type';
import { type CreateFrontComponentArgs } from 'src/engine/metadata-modules/front-component/types/create-front-component-args.type';

export const fromCreateFrontComponentInputToFlatFrontComponentToCreate = ({
  createFrontComponentInput,
  workspaceId,
  flatApplication,
}: {
  createFrontComponentInput: CreateFrontComponentArgs;
  workspaceId: string;
  flatApplication: FlatApplication;
}): FlatFrontComponent => {
  const now = new Date().toISOString();

  const { name } = trimAndRemoveDuplicatedWhitespacesFromObjectStringProperties(
    createFrontComponentInput,
    ['name'],
  );

  const id = createFrontComponentInput.id ?? v4();
  const universalIdentifier =
    createFrontComponentInput.universalIdentifier ?? v4();

  return {
    id,
    name: name ?? createFrontComponentInput.componentName,
    description: createFrontComponentInput.description ?? null,
    sourceComponentPath: createFrontComponentInput.sourceComponentPath,
    builtComponentPath: createFrontComponentInput.builtComponentPath,
    componentName: createFrontComponentInput.componentName,
    builtComponentChecksum: createFrontComponentInput.builtComponentChecksum,
    isHeadless: createFrontComponentInput.isHeadless ?? false,
    widgetAvailability: [],
    universalWidgetAvailability:
      createFrontComponentInput.universalWidgetAvailability ?? [],
    usesSdkClient: createFrontComponentInput.usesSdkClient ?? false,
    workspaceId,
    createdAt: now,
    updatedAt: now,
    universalIdentifier,
    applicationId: flatApplication.id,
    applicationUniversalIdentifier: flatApplication.universalIdentifier,
  };
};