Skip to content

The open alternative to Salesforce, designed for AI.

1.6 KB
Raw Blame
import { HTTPMethod } from 'twenty-shared/types';

import { type LogicFunctionConfig } from '@/sdk/define/logic-functions/logic-function-config';
import { createValidationResult } from '@/sdk/define/common/utils/create-validation-result';
import type { DefineEntity } from '@/sdk/define/common/types/define-entity.type';

export const defineLogicFunction: DefineEntity<LogicFunctionConfig> = (
  config,
) => {
  const errors = [];

  if (!config.universalIdentifier) {
    errors.push('Logic function must have a universalIdentifier');
  }

  if (!config.handler) {
    errors.push('Logic function must have a handler');
  }

  if (typeof config.handler !== 'function') {
    errors.push('Logic function handler must be a function');
  }

  if (config.httpRouteTriggerSettings) {
    if (!config.httpRouteTriggerSettings.path) {
      errors.push('Route trigger must have a path');
    }
    if (!config.httpRouteTriggerSettings.httpMethod) {
      errors.push('Route trigger must have an httpMethod');
    }
  }

  if (
    config.serverRouteTriggerSettings?.httpMethods?.some(
      (httpMethod) =>
        httpMethod !== HTTPMethod.GET && httpMethod !== HTTPMethod.POST,
    )
  ) {
    errors.push(
      'Server route trigger only supports the GET and POST http methods',
    );
  }

  if (config.cronTriggerSettings) {
    if (!config.cronTriggerSettings.pattern) {
      errors.push('Cron trigger must have a pattern');
    }
  }

  if (config.databaseEventTriggerSettings) {
    if (!config.databaseEventTriggerSettings.eventName) {
      errors.push('Database event trigger must have an eventName');
    }
  }

  return createValidationResult({
    config,
    errors,
  });
};