Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

12.7 KB
Raw Blame
import {
  NUMBER_DECIMAL_PLACES,
  PROPERTY_OPTION_IDS,
  SYSTEM_PROPERTY_IDS,
} from '@property/constants';
import type { Property, PropertyOption, ValueType } from '@property/types';
import type { SoupProperty } from '@service-storage/generated/schemas/soupProperty';
import { timeRangeFromInstants } from '../../database/core/cell-date-range';
import {
  COMPANY_STAGE_OPTIONS,
  TASK_STATUS_OPTIONS,
} from '../utils/task-properties';

const EPOCH_ZERO = new Date(0);

/**
 * Sort order for key properties (status, priority, assignees)
 */
const PROPERTY_SORT_ORDER = [
  SYSTEM_PROPERTY_IDS.STATUS,
  SYSTEM_PROPERTY_IDS.PRIORITY,
  SYSTEM_PROPERTY_IDS.ASSIGNEES,
] as const;

/**
 * Static options for system properties (Status and Priority).
 * These match the backend seed data and allow tooltips to display
 * human-readable values without fetching options from the API.
 */
const SYSTEM_PROPERTY_OPTIONS: Record<string, PropertyOption[]> = {
  [SYSTEM_PROPERTY_IDS.STATUS]: TASK_STATUS_OPTIONS.map(
    ({ value, label }, displayOrder) => ({
      id: value,
      property_definition_id: SYSTEM_PROPERTY_IDS.STATUS,
      value: { type: 'string', value: label },
      display_order: displayOrder,
      color: null,
      // TODO: need to properly handle dates. does not seem like these are even used/upserted anywhere?
      created_at: EPOCH_ZERO.toISOString(),
      updated_at: EPOCH_ZERO.toISOString(),
    })
  ),
  [SYSTEM_PROPERTY_IDS.PRIORITY]: [
    {
      id: PROPERTY_OPTION_IDS.PRIORITY.LOW,
      property_definition_id: SYSTEM_PROPERTY_IDS.PRIORITY,
      value: { type: 'string', value: 'Low' },
      display_order: 0,
      color: null,
      created_at: EPOCH_ZERO.toISOString(),
      updated_at: EPOCH_ZERO.toISOString(),
    },
    {
      id: PROPERTY_OPTION_IDS.PRIORITY.MEDIUM,
      property_definition_id: SYSTEM_PROPERTY_IDS.PRIORITY,
      value: { type: 'string', value: 'Medium' },
      display_order: 1,
      color: null,
      created_at: EPOCH_ZERO.toISOString(),
      updated_at: EPOCH_ZERO.toISOString(),
    },
    {
      id: PROPERTY_OPTION_IDS.PRIORITY.HIGH,
      property_definition_id: SYSTEM_PROPERTY_IDS.PRIORITY,
      value: { type: 'string', value: 'High' },
      display_order: 2,
      color: null,
      created_at: EPOCH_ZERO.toISOString(),
      updated_at: EPOCH_ZERO.toISOString(),
    },
    {
      id: PROPERTY_OPTION_IDS.PRIORITY.URGENT,
      property_definition_id: SYSTEM_PROPERTY_IDS.PRIORITY,
      value: { type: 'string', value: 'Urgent' },
      display_order: 3,
      color: null,
      created_at: EPOCH_ZERO.toISOString(),
      updated_at: EPOCH_ZERO.toISOString(),
    },
  ],
  [SYSTEM_PROPERTY_IDS.STAGE]: COMPANY_STAGE_OPTIONS.map(
    ({ value, label }, displayOrder) => ({
      id: value,
      property_definition_id: SYSTEM_PROPERTY_IDS.STAGE,
      value: { type: 'string', value: label },
      display_order: displayOrder,
      color: null,
      created_at: EPOCH_ZERO.toISOString(),
      updated_at: EPOCH_ZERO.toISOString(),
    })
  ),
};

/**
 * Get static options for a system property definition ID.
 * Returns undefined if not a known system property with static options.
 */
function getSystemPropertyOptions(
  propertyDefinitionId: string
): PropertyOption[] | undefined {
  return SYSTEM_PROPERTY_OPTIONS[propertyDefinitionId];
}

/**
 * Type guard to check if PropertyValue has a specific type
 */
function hasPropertyValueType(
  value: unknown,
  type: string
): value is { type: string; value: unknown } {
  return (
    value !== null &&
    value !== undefined &&
    typeof value === 'object' &&
    'type' in value &&
    (value as { type: string }).type === type &&
    'value' in value
  );
}

/**
 * Type guard for string arrays
 */
function isStringArray(value: unknown): value is string[] {
  return Array.isArray(value) && value.every((v) => typeof v === 'string');
}

/**
 * Type guard for entity reference arrays
 */
function isEntityReferenceArray(
  value: unknown
): value is Array<{ entity_type: string; entity_id: string }> {
  return (
    Array.isArray(value) &&
    value.every(
      (v) =>
        typeof v === 'object' &&
        v !== null &&
        'entity_type' in v &&
        'entity_id' in v
    )
  );
}

/**
 * Convert SoupProperty from DSS format to domain Property type
 *
 * SoupProperty is a simplified representation from the document storage service,
 * while Property is the full domain model used by UI components.
 */
export function soupPropertyToProperty(soupProperty: SoupProperty): Property {
  const definition = soupProperty.definition;
  const propertyValue = soupProperty.value;

  // Get static options for system properties (Status, Priority)
  // This allows tooltips to display human-readable values
  const options = getSystemPropertyOptions(definition.id);

  const baseProperty = {
    propertyId: soupProperty.id,
    propertyDefinitionId: definition.id,
    displayName: definition.display_name,
    isMultiSelect: definition.is_multi_select,
    isMetadata: definition.is_metadata,
    isSystemProperty: definition.is_system,
    isRequired: definition.id === SYSTEM_PROPERTY_IDS.STATUS,
    options,
    owner: definition.owner,
    specificEntityType: definition.specific_entity_type,
    createdAt: definition.created_at,
    updatedAt: definition.updated_at,
  };

  const valueType = (
    definition.data_type === 'TAG' ? 'SELECT_STRING' : definition.data_type
  ) as ValueType;

  // Handle each value type with proper type checking
  switch (valueType) {
    case 'STRING': {
      if (hasPropertyValueType(propertyValue, 'String')) {
        const stringVal = propertyValue.value;
        if (typeof stringVal === 'string' && stringVal) {
          return { ...baseProperty, valueType: 'STRING', value: stringVal };
        }
      }
      return { ...baseProperty, valueType: 'STRING', value: null };
    }

    case 'NUMBER': {
      if (hasPropertyValueType(propertyValue, 'Number')) {
        const numVal = propertyValue.value;
        if (
          typeof numVal === 'number' &&
          numVal !== undefined &&
          numVal !== null
        ) {
          return {
            ...baseProperty,
            valueType: 'NUMBER',
            value: parseFloat(numVal.toFixed(NUMBER_DECIMAL_PLACES)),
          };
        }
      }
      return { ...baseProperty, valueType: 'NUMBER', value: null };
    }

    case 'BOOLEAN': {
      if (hasPropertyValueType(propertyValue, 'Boolean')) {
        const boolVal = propertyValue.value;
        if (typeof boolVal === 'boolean') {
          return { ...baseProperty, valueType: 'BOOLEAN', value: boolVal };
        }
      }
      return { ...baseProperty, valueType: 'BOOLEAN', value: null };
    }

    case 'DATE': {
      if (hasPropertyValueType(propertyValue, 'Date')) {
        const dateVal = propertyValue.value;
        if (
          dateVal &&
          (typeof dateVal === 'string' || typeof dateVal === 'number')
        ) {
          return {
            ...baseProperty,
            valueType: 'DATE',
            value: new Date(dateVal),
          };
        }
      }
      return { ...baseProperty, valueType: 'DATE', value: null };
    }

    case 'DATE_RANGE': {
      return {
        ...baseProperty,
        valueType: 'DATE_RANGE',
        value:
          (propertyValue?.type === 'DateRange' &&
            timeRangeFromInstants(propertyValue.value)) ||
          null,
      };
    }

    case 'SELECT_STRING':
    case 'SELECT_NUMBER': {
      if (hasPropertyValueType(propertyValue, 'SelectOption')) {
        const selectVal = propertyValue.value;
        if (isStringArray(selectVal)) {
          return {
            ...baseProperty,
            valueType,
            value: selectVal,
          };
        }
      }
      return { ...baseProperty, valueType, value: null };
    }

    case 'ENTITY': {
      if (hasPropertyValueType(propertyValue, 'EntityReference')) {
        const entityVal = propertyValue.value;
        if (isEntityReferenceArray(entityVal)) {
          return { ...baseProperty, valueType: 'ENTITY', value: entityVal };
        }
      }
      return { ...baseProperty, valueType: 'ENTITY', value: null };
    }

    case 'LINK': {
      if (hasPropertyValueType(propertyValue, 'Link')) {
        const linkVal = propertyValue.value;
        if (isStringArray(linkVal)) {
          return {
            ...baseProperty,
            valueType: 'LINK',
            value: linkVal,
          };
        }
      }
      return { ...baseProperty, valueType: 'LINK', value: null };
    }

    default: {
      const exhaustiveCheck: never = valueType;
      throw new Error(`Unsupported value type: ${String(exhaustiveCheck)}`);
    }
  }
}

/**
 * Convert array of SoupProperty to Property array
 */
function _soupPropertiesToProperties(
  soupProperties: SoupProperty[]
): Property[] {
  return soupProperties.map(soupPropertyToProperty);
}

/** Definition stubs for the builtin CRM company properties. */
const COMPANY_DEFAULT_PROPERTY_DEFS = [
  {
    id: SYSTEM_PROPERTY_IDS.STAGE,
    displayName: 'Stage',
    dataType: 'SELECT_STRING',
    specificEntityType: undefined,
  },
  {
    id: SYSTEM_PROPERTY_IDS.COMPANY_OWNER,
    displayName: 'Owner',
    dataType: 'ENTITY',
    specificEntityType: 'USER',
  },
  {
    id: SYSTEM_PROPERTY_IDS.REVENUE,
    displayName: 'Revenue',
    dataType: 'NUMBER',
    specificEntityType: undefined,
  },
] as const;

/**
 * Build placeholder `Property` values for the builtin CRM company
 * properties (Stage / Owner / Revenue). Companies are auto-created from
 * synced email, so most have no `entity_properties` rows yet — these
 * stubs let the panel and list render (and edit) the defaults before a
 * value ever gets saved. Setting a value upserts the real row.
 */
export function buildCompanyDefaultProperties(): Property[] {
  return COMPANY_DEFAULT_PROPERTY_DEFS.map((def) =>
    soupPropertyToProperty({
      id: def.id,
      definition: {
        id: def.id,
        display_name: def.displayName,
        data_type: def.dataType,
        is_metadata: false,
        is_multi_select: false,
        is_system: true,
        owner: { scope: 'system' },
        specific_entity_type: def.specificEntityType ?? null,
        created_at: EPOCH_ZERO.toISOString(),
        updated_at: EPOCH_ZERO.toISOString(),
      },
    })
  );
}

/**
 * Placeholder for a task's Project property, which a task only carries once it
 * joins a project. Lets the standard property row and editor show and set it;
 * setting a value upserts the real row.
 */
export function buildTaskProjectDefaultProperty(): Property {
  return soupPropertyToProperty({
    id: `pending:${SYSTEM_PROPERTY_IDS.PROJECT}`,
    definition: {
      id: SYSTEM_PROPERTY_IDS.PROJECT,
      display_name: 'Project',
      data_type: 'ENTITY',
      is_metadata: false,
      is_multi_select: false,
      is_system: true,
      owner: { scope: 'system' },
      specific_entity_type: 'INITIATIVE',
      created_at: EPOCH_ZERO.toISOString(),
      updated_at: EPOCH_ZERO.toISOString(),
    },
  });
}

/**
 * A task's key properties with Project before Assignees, using the Project
 * placeholder when the task isn't in a project.
 */
export function withTaskProject(
  keyProperties: Property[],
  properties: Property[]
): Property[] {
  const project =
    properties.find(
      (property) =>
        property.propertyDefinitionId === SYSTEM_PROPERTY_IDS.PROJECT
    ) ?? buildTaskProjectDefaultProperty();
  const assigneesIndex = keyProperties.findIndex(
    (property) =>
      property.propertyDefinitionId === SYSTEM_PROPERTY_IDS.ASSIGNEES
  );
  const projectIndex =
    assigneesIndex < 0 ? keyProperties.length : assigneesIndex;
  return [
    ...keyProperties.slice(0, projectIndex),
    project,
    ...keyProperties.slice(projectIndex),
  ];
}

/**
 * Sort properties by the defined sort order (status, priority, assignees first)
 */
function sortProperties(properties: Property[]): Property[] {
  return [...properties].sort((a, b) => {
    const aIndex = PROPERTY_SORT_ORDER.indexOf(
      a.propertyDefinitionId as (typeof PROPERTY_SORT_ORDER)[number]
    );
    const bIndex = PROPERTY_SORT_ORDER.indexOf(
      b.propertyDefinitionId as (typeof PROPERTY_SORT_ORDER)[number]
    );

    if (aIndex !== -1 && bIndex !== -1) {
      return aIndex - bIndex;
    }
    if (aIndex !== -1) {
      return -1;
    }
    if (bIndex !== -1) {
      return 1;
    }
    return 0;
  });
}

/**
 * Filter properties to only include key properties (status, priority, assignees)
 */
function filterKeyProperties(properties: Property[]): Property[] {
  return properties.filter((prop) =>
    PROPERTY_SORT_ORDER.includes(
      prop.propertyDefinitionId as (typeof PROPERTY_SORT_ORDER)[number]
    )
  );
}

/**
 * Get sorted key properties from a property array
 */
export function getSortedKeyProperties(properties: Property[]): Property[] {
  return sortProperties(filterKeyProperties(properties));
}