Skip to content

The open alternative to Salesforce, designed for AI.

1.1 KB
Raw Blame
import { isDefined } from 'twenty-shared/utils';

import { type UsageLimitFormValues } from '@/settings/billing/types/UsageLimitFormValues';
import { buildUsageQuotaScopeInput } from '@/settings/billing/utils/buildUsageQuotaScopeInput';
import { getUsageLimitInputScale } from '@/settings/billing/utils/getUsageLimitInputScale';
import { hasUsageLimitValue } from '@/settings/billing/utils/hasUsageLimitValue';
import { type CreateUsageLimitInput } from '~/generated-metadata/graphql';

export const buildCreateUsageLimitInput = (
  values: UsageLimitFormValues,
): CreateUsageLimitInput | null => {
  const scope = buildUsageQuotaScopeInput(values);

  if (!isDefined(scope) || !hasUsageLimitValue(values)) {
    return null;
  }

  const inputValue = Number(values.limitValue);
  const limitValue = Math.round(
    inputValue * getUsageLimitInputScale(scope.unit),
  );

  if (
    !Number.isSafeInteger(limitValue) ||
    limitValue < 0 ||
    (limitValue === 0 && inputValue !== 0)
  ) {
    return null;
  }

  return {
    ...scope,
    limitKind: 'quota',
    periodCount: 1,
    limitValue,
    burstValue: null,
  };
};