Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
import { PROPERTY_OPTION_IDS, SYSTEM_PROPERTY_IDS } from '@property/constants';
import type { TaskEntityWithProperties } from '../types/entity';
/** The stored due-date instant; absent and cleared DATE values are undated. */
export function getTaskDueDate(
entity: TaskEntityWithProperties
): string | undefined {
const value = entity.properties?.find(
(property) => property.definition.id === SYSTEM_PROPERTY_IDS.DUE_DATE
)?.value;
return value?.type === 'Date' && typeof value.value === 'string'
? value.value
: undefined;
}
export const TASK_STATUS_OPTIONS = [
{ value: PROPERTY_OPTION_IDS.STATUS.NOT_STARTED, label: 'Not Started' },
{ value: PROPERTY_OPTION_IDS.STATUS.IN_PROGRESS, label: 'In Progress' },
{ value: PROPERTY_OPTION_IDS.STATUS.IN_REVIEW, label: 'In Review' },
{ value: PROPERTY_OPTION_IDS.STATUS.COMPLETED, label: 'Completed' },
{ value: PROPERTY_OPTION_IDS.STATUS.CANCELED, label: 'Canceled' },
] as const;
export const TASK_PRIORITY_OPTIONS = [
{ value: PROPERTY_OPTION_IDS.PRIORITY.URGENT, label: 'Urgent' },
{ value: PROPERTY_OPTION_IDS.PRIORITY.HIGH, label: 'High' },
{ value: PROPERTY_OPTION_IDS.PRIORITY.MEDIUM, label: 'Medium' },
{ value: PROPERTY_OPTION_IDS.PRIORITY.LOW, label: 'Low' },
] as const;
// Retired from the default stage set, but companies may still carry these
// values — labels stay resolvable and stage filters still offer them.
const LEGACY_STAGE_IDS: ReadonlySet<string> = new Set([
PROPERTY_OPTION_IDS.STAGE.QUALIFIED,
PROPERTY_OPTION_IDS.STAGE.TRIAL,
PROPERTY_OPTION_IDS.STAGE.NEGOTIATION,
]);
/**
* Every system stage option in canonical pipeline order — fixes the
* display order of stage filters/columns when legacy stages are shown.
*/
export const ALL_COMPANY_STAGE_OPTIONS = [
{ value: PROPERTY_OPTION_IDS.STAGE.LEAD, label: 'Lead' },
{ value: PROPERTY_OPTION_IDS.STAGE.QUALIFIED, label: 'Qualified' },
{ value: PROPERTY_OPTION_IDS.STAGE.DEMO, label: 'Demo' },
{ value: PROPERTY_OPTION_IDS.STAGE.TRIAL, label: 'Trial' },
{ value: PROPERTY_OPTION_IDS.STAGE.NEGOTIATION, label: 'Negotiation' },
{ value: PROPERTY_OPTION_IDS.STAGE.CUSTOMER, label: 'Customer' },
{ value: PROPERTY_OPTION_IDS.STAGE.CHURNED, label: 'Churned' },
] as const;
/** The default (non-legacy) stage set, in canonical order. */
export const COMPANY_STAGE_OPTIONS = ALL_COMPANY_STAGE_OPTIONS.filter(
(option) => !LEGACY_STAGE_IDS.has(option.value)
);
const PROPERTY_OPTION_LABELS: Record<string, string> = {
...Object.fromEntries(TASK_STATUS_OPTIONS.map((o) => [o.value, o.label])),
...Object.fromEntries(TASK_PRIORITY_OPTIONS.map((o) => [o.value, o.label])),
...Object.fromEntries(
ALL_COMPANY_STAGE_OPTIONS.map((o) => [o.value, o.label])
),
};
export const getPropertyOptionLabel = (
optionId: string
): string | undefined => {
return PROPERTY_OPTION_LABELS[optionId];
};
const getTaskPropertyByDefinitionId = (
entity: TaskEntityWithProperties,
definitionId: string
) => {
return entity.properties?.find((property) => {
return property.definition.id === definitionId;
});
};
const isStringArray = (value: unknown): value is string[] => {
return (
Array.isArray(value) && value.every((item) => typeof item === 'string')
);
};
const isEntityReferenceArray = (
value: unknown
): value is Array<{ entity_type: string; entity_id: string }> => {
return (
Array.isArray(value) &&
value.every((item) => {
return (
typeof item === 'object' &&
item !== null &&
'entity_type' in item &&
'entity_id' in item &&
typeof item.entity_type === 'string' &&
typeof item.entity_id === 'string'
);
})
);
};
/**
* Extracts assignee user ids from task properties.
*/
export const getTaskAssigneeIds = (
entity: TaskEntityWithProperties
): string[] => {
const assigneesProperty = getTaskPropertyByDefinitionId(
entity,
SYSTEM_PROPERTY_IDS.ASSIGNEES
);
if (!assigneesProperty?.value) {
return [];
}
const value = assigneesProperty.value;
if (
value.type !== 'EntityReference' ||
!isEntityReferenceArray(value.value)
) {
return [];
}
return value.value
.filter((reference) => reference.entity_type === 'USER')
.map((reference) => reference.entity_id);
};
/**
* Ids of the entities a task's entity-reference property points at, e.g.
* the CRM companies in its Companies property.
*/
export const getTaskReferencedEntityIds = (
entity: TaskEntityWithProperties,
propertyDefinitionId: string
): string[] => {
const value = getTaskPropertyByDefinitionId(
entity,
propertyDefinitionId
)?.value;
if (
value?.type !== 'EntityReference' ||
!isEntityReferenceArray(value.value)
) {
return [];
}
return value.value.map((reference) => reference.entity_id);
};
/**
* Gets the status option id from task properties.
*/
export const getTaskStatusOptionId = (
entity: TaskEntityWithProperties
): string | undefined => {
const statusProperty = getTaskPropertyByDefinitionId(
entity,
SYSTEM_PROPERTY_IDS.STATUS
);
if (!statusProperty?.value) {
return undefined;
}
const value = statusProperty.value;
if (value.type !== 'SelectOption' || !isStringArray(value.value)) {
return undefined;
}
return value.value[0];
};
/**
* Gets the priority option id from task properties.
*/
export const getTaskPriorityOptionId = (
entity: TaskEntityWithProperties
): string | undefined => {
const priorityProperty = getTaskPropertyByDefinitionId(
entity,
SYSTEM_PROPERTY_IDS.PRIORITY
);
if (!priorityProperty?.value) {
return undefined;
}
const value = priorityProperty.value;
if (value.type !== 'SelectOption' || !isStringArray(value.value)) {
return undefined;
}
return value.value[0];
};
/**
* Checks if a task is in a closed state.
*/
export const isTaskClosed = (entity: TaskEntityWithProperties): boolean => {
if (entity.subType?.is_completed === true) {
return true;
}
const statusOptionId = getTaskStatusOptionId(entity);
return (
statusOptionId === PROPERTY_OPTION_IDS.STATUS.COMPLETED ||
statusOptionId === PROPERTY_OPTION_IDS.STATUS.CANCELED
);
};
/**
* Checks if the current user is assigned to the task.
* If a task has no assignees, it is treated as visible to everyone.
*/
export const isCurrentUserAssigned = (
entity: TaskEntityWithProperties,
currentUserId: string | undefined
): boolean => {
if (!currentUserId) {
return false;
}
const assigneeIds = getTaskAssigneeIds(entity);
if (assigneeIds.length === 0) {
return true;
}
return assigneeIds.includes(currentUserId);
};