ultisuite-client/lib/mail-automation/condition-helpers.ts
2026-05-25 13:52:40 +02:00

102 lines
3.4 KiB
TypeScript

import type { ActionType, ConditionField, ConditionOperator } from './types'
export const LABEL_CONDITION_OPERATORS: { value: ConditionOperator; label: string }[] = [
{ value: 'has', label: 'Possède le libellé' },
{ value: 'not_has', label: 'Ne possède pas le libellé' },
]
export const STRING_CONDITION_OPERATORS: { value: ConditionOperator; label: string }[] = [
{ value: 'contains', label: 'contient' },
{ value: 'not_contains', label: 'ne contient pas' },
{ value: 'equals', label: 'égal à' },
{ value: 'starts_with', label: 'commence par' },
{ value: 'ends_with', label: 'finit par' },
{ value: 'regex', label: 'correspond à la regex' },
{ value: 'not_regex', label: 'ne correspond pas à la regex' },
]
export const ATTACHMENT_CONDITION_OPERATORS: { value: ConditionOperator; label: string }[] = [
{ value: 'equals', label: 'égal à' },
]
export function isStringConditionField(field: ConditionField): boolean {
return field === 'from' || field === 'to' || field === 'subject' || field === 'body'
}
export function isLabelConditionField(field: ConditionField): boolean {
return field === 'label'
}
export function operatorsForConditionField(field: ConditionField): ConditionOperator[] {
if (field === 'label') return ['has', 'not_has']
if (field === 'has_attachment') return ['equals']
return STRING_CONDITION_OPERATORS.map((o) => o.value)
}
export function operatorOptionsForConditionField(field: ConditionField) {
if (field === 'label') return LABEL_CONDITION_OPERATORS
if (field === 'has_attachment') return ATTACHMENT_CONDITION_OPERATORS
return STRING_CONDITION_OPERATORS
}
export function defaultOperatorForField(field: ConditionField): ConditionOperator {
if (field === 'label') return 'has'
if (field === 'has_attachment') return 'equals'
return 'contains'
}
export function formatConditionSummary(data: {
field: ConditionField
operator: ConditionOperator
value: string
}, fieldLabel: (f: string) => string, opLabel: (o: string) => string): string {
if (data.field === 'label') {
const prefix = data.operator === 'not_has' ? 'Sans libellé' : 'Libellé'
return `${prefix} « ${data.value || '…'} »`
}
return `${fieldLabel(data.field)} ${opLabel(data.operator)} « ${data.value || '…'} »`
}
export type AutomationSuggestionKind = 'label' | 'folder' | 'folder_id' | 'email' | 'webhook' | 'none'
export function conditionValueSuggestionKind(
field: ConditionField,
operator: ConditionOperator
): AutomationSuggestionKind {
if (field === 'label') return 'label'
if (field === 'from' || field === 'to') return 'email'
return 'none'
}
export function switchFieldSuggestionKind(field: string): AutomationSuggestionKind {
if (field === 'label') return 'label'
if (field === 'from' || field === 'to') return 'email'
return 'none'
}
export function actionValueSuggestionKind(type: ActionType): AutomationSuggestionKind {
switch (type) {
case 'label':
case 'remove_label':
return 'label'
case 'move':
return 'folder'
case 'forward':
case 'send_mail':
return 'email'
case 'webhook':
return 'webhook'
default:
return 'none'
}
}
export function triggerValueSuggestionKind(
type: 'message_received' | 'label_added' | 'label_removed',
field: 'folder_id' | 'label'
): AutomationSuggestionKind {
if (field === 'label') return 'label'
if (field === 'folder_id') return 'folder_id'
return 'none'
}