107 lines
3.5 KiB
JavaScript
107 lines
3.5 KiB
JavaScript
import { isSuccess, signedGet, signedPost } from '@/api/client';
|
|
|
|
const workflows = {
|
|
service: 'workflow',
|
|
module: 'workflow_definition',
|
|
title: 'Workflows',
|
|
subtitle: 'Automation',
|
|
baseUrl: '/workflow_definition',
|
|
listPath: '/workflows',
|
|
listFields: [
|
|
{
|
|
key: 'actions',
|
|
title: 'Actions',
|
|
buttons: [
|
|
'edit',
|
|
'delete',
|
|
{ name: 'editor', to: (row) => `/workflow/editor?id=${row.id}`, title: 'Editor' },
|
|
{ name: 'monitor', to: (row) => `/workflow/monitor?definition_id=${row.id}`, title: 'Runs' },
|
|
'duplicate',
|
|
],
|
|
},
|
|
{ key: 'name', title: 'Name', type: 'text' },
|
|
{ key: 'entity', title: 'Entity', type: 'text' },
|
|
{ key: 'run_on', title: 'Run on', type: 'text' },
|
|
{ key: 'published_version', title: 'Published', type: 'text' },
|
|
{ key: 'status', title: 'Status', type: 'text' },
|
|
],
|
|
formFields: async () => {
|
|
const formsRes = await signedGet('form', 'formentity/get_all');
|
|
const forms = {};
|
|
const formItems = isSuccess(formsRes) ? formsRes.data?.items || [] : [];
|
|
formItems.forEach((form) => {
|
|
if (!form?.id) return;
|
|
forms[form.id] = form.title || form.id;
|
|
});
|
|
return [
|
|
{ key: 'name', title: 'Name', type: 'text', required: true },
|
|
{ key: 'description', title: 'Description', type: 'textarea' },
|
|
{
|
|
key: 'entity',
|
|
title: 'Entity',
|
|
type: 'select',
|
|
required: true,
|
|
values: {
|
|
manual: 'Manual',
|
|
form_submission: 'Form submission',
|
|
contact_approval: 'Contact approval',
|
|
},
|
|
default: 'manual',
|
|
},
|
|
{
|
|
key: 'form_id',
|
|
title: 'Form',
|
|
type: 'select',
|
|
required: true,
|
|
values: forms,
|
|
showIf: { entity: 'form_submission' },
|
|
},
|
|
{
|
|
key: 'run_on',
|
|
title: 'Run on',
|
|
type: 'select',
|
|
required: true,
|
|
values: { insert: 'Insert', update: 'Update', both: 'Both' },
|
|
default: 'insert',
|
|
showIf: { entity: 'form_submission' },
|
|
},
|
|
];
|
|
},
|
|
beforeForm: (item) => ({
|
|
...item,
|
|
entity: item.entity || 'manual',
|
|
description: item.description || '',
|
|
form_id: item.form_id || '',
|
|
run_on: item.id ? (item.run_on || '') : (item.run_on || 'insert'),
|
|
}),
|
|
duplicateItem: async (row) => {
|
|
const response = await signedGet('workflow', 'workflow_definition/get_one', { id: row.id });
|
|
if (!isSuccess(response)) return { ok: false, message: response?.message || 'Unable to load workflow' };
|
|
const item = response.data?.item || {};
|
|
const created = await signedPost('workflow', 'workflow_definition/create', {
|
|
name: `${item.name || 'Workflow'} (Copy)`,
|
|
description: item.description || '',
|
|
entity: item.entity || 'manual',
|
|
form_id: item.entity === 'form_submission' ? item.form_id || '' : '',
|
|
...(item.entity === 'form_submission' ? { run_on: item.run_on || '' } : {}),
|
|
nodes: item.nodes || [],
|
|
edges: item.edges || [],
|
|
layout: item.layout || {},
|
|
});
|
|
if (!isSuccess(created)) return { ok: false, message: created?.message || 'Unable to duplicate workflow' };
|
|
return { ok: true, message: 'Workflow duplicated' };
|
|
},
|
|
beforeSave: (data) => {
|
|
const payload = { ...data };
|
|
if (payload.entity !== 'form_submission') {
|
|
payload.form_id = '';
|
|
delete payload.run_on;
|
|
} else if (!payload.run_on) {
|
|
delete payload.run_on;
|
|
}
|
|
return payload;
|
|
},
|
|
};
|
|
|
|
export default workflows;
|