Browse Source

feat: include workflow inside the execute workflow block when exporting a workflow

Ahmad Kholid 3 years ago
parent
commit
70e34a1755
2 changed files with 49 additions and 3 deletions
  1. 1 1
      src/models/workflow.js
  2. 48 2
      src/utils/workflow-data.js

+ 1 - 1
src/models/workflow.js

@@ -16,7 +16,7 @@ class Workflow extends Model {
       name: this.string(''),
       icon: this.string('riGlobalLine'),
       data: this.attr(null),
-      drawflow: this.attr(''),
+      drawflow: this.attr('{ "drawflow": { "Home": { "data": {} } } }'),
       dataColumns: this.attr([]),
       description: this.string(''),
       version: this.string(''),

+ 48 - 2
src/utils/workflow-data.js

@@ -1,4 +1,4 @@
-import { fileSaver, openFilePicker } from './helper';
+import { parseJSON, fileSaver, openFilePicker } from './helper';
 import Workflow from '@/models/workflow';
 
 export function importWorkflow() {
@@ -9,6 +9,26 @@ export function importWorkflow() {
       reader.onload = ({ target }) => {
         const workflow = JSON.parse(target.result);
 
+        if (workflow.includedWorkflows) {
+          Object.keys(workflow.includedWorkflows).forEach((workflowId) => {
+            const isWorkflowExists = Workflow.query()
+              .where('id', workflowId)
+              .exists();
+
+            if (isWorkflowExists) return;
+
+            Workflow.insert({
+              data: {
+                ...workflow.includedWorkflows[workflowId],
+                id: workflowId,
+                createdAt: Date.now(),
+              },
+            });
+          });
+
+          delete workflow.includedWorkflows;
+        }
+
         Workflow.insert({ data: { ...workflow, createdAt: Date.now() } });
       };
 
@@ -19,7 +39,7 @@ export function importWorkflow() {
     });
 }
 
-export function exportWorkflow(workflow) {
+function convertWorkflow(workflow) {
   const keys = [
     'name',
     'icon',
@@ -38,6 +58,32 @@ export function exportWorkflow(workflow) {
     content[key] = workflow[key];
   });
 
+  return content;
+}
+function findIncludedWorkflows({ drawflow }, maxDepth = 3, workflows = {}) {
+  if (maxDepth === 0) return workflows;
+
+  const blocks = parseJSON(drawflow, null)?.drawflow.Home.data;
+
+  if (!blocks) return workflows;
+
+  Object.values(blocks).forEach(({ data, name }) => {
+    if (name !== 'execute-workflow' || workflows[data.workflowId]) return;
+
+    const workflow = Workflow.find(data.workflowId);
+    workflows[data.workflowId] = convertWorkflow(workflow);
+
+    findIncludedWorkflows(workflow, maxDepth - 1, workflows);
+  });
+
+  return workflows;
+}
+export function exportWorkflow(workflow) {
+  const includedWorkflows = findIncludedWorkflows(workflow);
+  const content = convertWorkflow(workflow);
+
+  content.includedWorkflows = includedWorkflows;
+
   const blob = new Blob([JSON.stringify(content)], {
     type: 'application/json',
   });