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

101 lines
2.5 KiB
TypeScript

import type {
ActionItem,
AutomationWorkflow,
RuleEditorState,
WorkflowEdgeDef,
WorkflowNodeDef,
} from './types'
let nodeCounter = 0
export function nextNodeId(prefix: string) {
nodeCounter += 1
return `${prefix}-${Date.now()}-${nodeCounter}`
}
export function createDefaultWorkflow(kind: 'rule' | 'function' = 'rule'): AutomationWorkflow {
const startId = nextNodeId('start')
const condId = nextNodeId('cond')
const actionsId = nextNodeId('actions')
const endId = nextNodeId('end')
const nodes: WorkflowNodeDef[] = [
{ id: startId, type: 'start', position: { x: 80, y: 200 }, data: {} },
{
id: condId,
type: 'condition',
position: { x: 280, y: 180 },
data: { field: 'subject', operator: 'contains', value: '' },
},
{
id: actionsId,
type: 'actions',
position: { x: 520, y: 160 },
data: { actions: [{ type: 'label', value: '' }] },
},
{ id: endId, type: 'end', position: { x: 760, y: 200 }, data: {} },
]
const edges: WorkflowEdgeDef[] = [
{ id: nextNodeId('e'), source: startId, target: condId },
{ id: nextNodeId('e'), source: condId, target: actionsId, sourceHandle: 'true' },
{ id: nextNodeId('e'), source: actionsId, target: endId },
{ id: nextNodeId('e'), source: condId, target: endId, sourceHandle: 'false' },
]
return {
version: 1,
kind,
triggers: {
operator: 'or',
groups: [
{
operator: 'and',
items: [{ type: 'message_received' }],
},
],
},
variables: [],
nodes,
edges,
}
}
export function createDefaultRuleEditorState(
kind: 'rule' | 'function' = 'rule'
): RuleEditorState {
return {
name: kind === 'function' ? 'Nouvelle fonction' : 'Nouvelle règle',
priority: 0,
is_active: true,
rule_kind: kind,
workflow: createDefaultWorkflow(kind),
}
}
export function createEmptyAction(): ActionItem {
return { type: 'label', value: '' }
}
export const DEFAULT_SIMULATION_MESSAGE = {
from: 'expediteur@example.com',
to: ['moi@example.com'],
subject: 'Exemple de sujet',
body_text: 'Contenu du message de test.',
has_attachments: false,
labels: [] as string[],
}
export function workflowToApiPayload(state: RuleEditorState) {
return {
name: state.name.trim(),
priority: state.priority,
is_active: state.is_active,
rule_kind: state.rule_kind,
account_id: state.account_id,
conditions: [],
actions: [],
workflow: state.workflow,
}
}