- Introduced new routes for Automation Rules and Automation Tasks in the frontend. - Updated the AppSidebar to include sub-items for Automation navigation. - Enhanced HeaderMetricsContext to manage credit and page metrics more effectively, ensuring proper merging and clearing of metrics. - Adjusted AppLayout and TablePageTemplate to maintain credit balance while managing page metrics.
177 lines
4.6 KiB
TypeScript
177 lines
4.6 KiB
TypeScript
import { fetchAPI } from '../services/api';
|
|
|
|
/**
|
|
* Automation API Client
|
|
* Functions for automation rules and scheduled tasks
|
|
*/
|
|
|
|
export interface AutomationRule {
|
|
id: number;
|
|
name: string;
|
|
description?: string;
|
|
trigger: 'schedule' | 'event' | 'manual';
|
|
schedule?: string; // Cron-like string
|
|
conditions: Array<{
|
|
field: string;
|
|
operator: string;
|
|
value: any;
|
|
}>;
|
|
actions: Array<{
|
|
type: string;
|
|
params: Record<string, any>;
|
|
}>;
|
|
is_active: boolean;
|
|
status: 'active' | 'inactive' | 'paused';
|
|
execution_count: number;
|
|
last_executed_at?: string;
|
|
created_at: string;
|
|
updated_at: string;
|
|
}
|
|
|
|
export interface ScheduledTask {
|
|
id: number;
|
|
rule_id?: number;
|
|
rule_name?: string;
|
|
task_type: string;
|
|
status: 'pending' | 'running' | 'completed' | 'failed' | 'cancelled';
|
|
scheduled_at: string;
|
|
started_at?: string;
|
|
completed_at?: string;
|
|
result?: Record<string, any>;
|
|
error?: string;
|
|
retry_count: number;
|
|
created_at: string;
|
|
}
|
|
|
|
export interface AutomationRuleCreateData {
|
|
name: string;
|
|
description?: string;
|
|
trigger: 'schedule' | 'event' | 'manual';
|
|
schedule?: string;
|
|
conditions?: Array<{
|
|
field: string;
|
|
operator: string;
|
|
value: any;
|
|
}>;
|
|
actions: Array<{
|
|
type: string;
|
|
params: Record<string, any>;
|
|
}>;
|
|
is_active?: boolean;
|
|
}
|
|
|
|
export interface AutomationRuleUpdateData extends Partial<AutomationRuleCreateData> {}
|
|
|
|
export const automationApi = {
|
|
/**
|
|
* List automation rules
|
|
*/
|
|
listRules: async (filters?: {
|
|
search?: string;
|
|
trigger?: string;
|
|
is_active?: boolean;
|
|
status?: string;
|
|
ordering?: string;
|
|
page?: number;
|
|
page_size?: number;
|
|
}) => {
|
|
const params = new URLSearchParams();
|
|
if (filters?.search) params.append('search', filters.search);
|
|
if (filters?.trigger) params.append('trigger', filters.trigger);
|
|
if (filters?.is_active !== undefined) params.append('is_active', String(filters.is_active));
|
|
if (filters?.status) params.append('status', filters.status);
|
|
if (filters?.ordering) params.append('ordering', filters.ordering);
|
|
if (filters?.page) params.append('page', String(filters.page));
|
|
if (filters?.page_size) params.append('page_size', String(filters.page_size));
|
|
|
|
const query = params.toString();
|
|
return await fetchAPI(`/v1/automation/rules/${query ? `?${query}` : ''}`);
|
|
},
|
|
|
|
/**
|
|
* Get a single automation rule
|
|
*/
|
|
getRule: async (id: number) => {
|
|
return await fetchAPI(`/v1/automation/rules/${id}/`) as AutomationRule;
|
|
},
|
|
|
|
/**
|
|
* Create a new automation rule
|
|
*/
|
|
createRule: async (data: AutomationRuleCreateData) => {
|
|
return await fetchAPI('/v1/automation/rules/', {
|
|
method: 'POST',
|
|
body: JSON.stringify(data),
|
|
}) as AutomationRule;
|
|
},
|
|
|
|
/**
|
|
* Update an automation rule
|
|
*/
|
|
updateRule: async (id: number, data: AutomationRuleUpdateData) => {
|
|
return await fetchAPI(`/v1/automation/rules/${id}/`, {
|
|
method: 'PATCH',
|
|
body: JSON.stringify(data),
|
|
}) as AutomationRule;
|
|
},
|
|
|
|
/**
|
|
* Delete an automation rule
|
|
*/
|
|
deleteRule: async (id: number) => {
|
|
return await fetchAPI(`/v1/automation/rules/${id}/`, {
|
|
method: 'DELETE',
|
|
});
|
|
},
|
|
|
|
/**
|
|
* Execute an automation rule manually
|
|
*/
|
|
executeRule: async (id: number, context?: Record<string, any>) => {
|
|
return await fetchAPI(`/v1/automation/rules/${id}/execute/`, {
|
|
method: 'POST',
|
|
body: JSON.stringify({ context: context || {} }),
|
|
});
|
|
},
|
|
|
|
/**
|
|
* List scheduled tasks
|
|
*/
|
|
listTasks: async (filters?: {
|
|
rule_id?: number;
|
|
status?: string;
|
|
task_type?: string;
|
|
ordering?: string;
|
|
page?: number;
|
|
page_size?: number;
|
|
}) => {
|
|
const params = new URLSearchParams();
|
|
if (filters?.rule_id) params.append('rule_id', String(filters.rule_id));
|
|
if (filters?.status) params.append('status', filters.status);
|
|
if (filters?.task_type) params.append('task_type', filters.task_type);
|
|
if (filters?.ordering) params.append('ordering', filters.ordering);
|
|
if (filters?.page) params.append('page', String(filters.page));
|
|
if (filters?.page_size) params.append('page_size', String(filters.page_size));
|
|
|
|
const query = params.toString();
|
|
return await fetchAPI(`/v1/automation/scheduled-tasks/${query ? `?${query}` : ''}`);
|
|
},
|
|
|
|
/**
|
|
* Get a single scheduled task
|
|
*/
|
|
getTask: async (id: number) => {
|
|
return await fetchAPI(`/v1/automation/scheduled-tasks/${id}/`) as ScheduledTask;
|
|
},
|
|
|
|
/**
|
|
* Retry a failed scheduled task
|
|
*/
|
|
retryTask: async (id: number) => {
|
|
return await fetchAPI(`/v1/automation/scheduled-tasks/${id}/retry/`, {
|
|
method: 'POST',
|
|
});
|
|
},
|
|
};
|
|
|