workflow.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { Model } from '@vuex-orm/core';
  2. import { nanoid } from 'nanoid';
  3. import browser from 'webextension-polyfill';
  4. import { cleanWorkflowTriggers } from '@/utils/workflow-trigger';
  5. import { fetchApi } from '@/utils/api';
  6. import decryptFlow, { getWorkflowPass } from '@/utils/decrypt-flow';
  7. import Log from './log';
  8. class Workflow extends Model {
  9. static entity = 'workflows';
  10. static primaryKey = 'id';
  11. static autoSave = true;
  12. static fields() {
  13. return {
  14. __id: this.attr(null),
  15. id: this.uid(() => nanoid()),
  16. name: this.string(''),
  17. icon: this.string('riGlobalLine'),
  18. data: this.attr(null),
  19. drawflow: this.attr(''),
  20. table: this.attr([]),
  21. dataColumns: this.attr([]),
  22. description: this.string(''),
  23. pass: this.string(''),
  24. trigger: this.attr(null),
  25. version: this.string(''),
  26. createdAt: this.number(Date.now()),
  27. isDisabled: this.boolean(false),
  28. isProtected: this.boolean(false),
  29. settings: this.attr({
  30. blockDelay: 0,
  31. saveLog: true,
  32. debugMode: false,
  33. restartTimes: 3,
  34. reuseLastState: false,
  35. onError: 'stop-workflow',
  36. executedBlockOnWeb: false,
  37. }),
  38. logs: this.hasMany(Log, 'workflowId'),
  39. globalData: this.string('[{ "key": "value" }]'),
  40. };
  41. }
  42. static beforeCreate(model) {
  43. if (model.dataColumns.length > 0) {
  44. model.table = model.dataColumns;
  45. model.dataColumns = [];
  46. }
  47. if (model.isProtected) {
  48. const pass = getWorkflowPass(model.pass);
  49. model.drawflow = decryptFlow(model, pass);
  50. model.isProtected = false;
  51. }
  52. if (model.table && !model.table[0]?.id) {
  53. model.table = model.table.map((column) => {
  54. if (!column.id) column.id = column.name;
  55. return column;
  56. });
  57. }
  58. return model;
  59. }
  60. static async insert(payload) {
  61. const res = await super.insert(payload);
  62. await this.store().dispatch('saveToStorage', 'workflows');
  63. return res;
  64. }
  65. static async afterDelete({ id }) {
  66. try {
  67. await cleanWorkflowTriggers(id);
  68. const hostedWorkflow = this.store().state.hostWorkflows[id];
  69. const { backupIds } = await browser.storage.local.get('backupIds');
  70. const isBackup = (backupIds || []).includes(id);
  71. if (hostedWorkflow || isBackup) {
  72. const response = await fetchApi(`/me/workflows?id=${id}`, {
  73. method: 'DELETE',
  74. });
  75. if (!response.ok) {
  76. throw new Error(response.statusText);
  77. }
  78. if (isBackup) {
  79. backupIds.splice(backupIds.indexOf(id), 1);
  80. await browser.storage.local.set({ backupIds });
  81. }
  82. await browser.storage.local.set({ clearCache: true });
  83. }
  84. } catch (error) {
  85. console.error(error);
  86. }
  87. }
  88. }
  89. export default Workflow;