workflow.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { Model } from '@vuex-orm/core';
  2. import { nanoid } from 'nanoid';
  3. import browser from 'webextension-polyfill';
  4. import Log from './log';
  5. import { cleanWorkflowTriggers } from '@/utils/workflow-trigger';
  6. import { fetchApi } from '@/utils/api';
  7. import decryptFlow, { getWorkflowPass } from '@/utils/decrypt-flow';
  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. onError: 'stop-workflow',
  34. executedBlockOnWeb: false,
  35. }),
  36. logs: this.hasMany(Log, 'workflowId'),
  37. globalData: this.string('[{ "key": "value" }]'),
  38. };
  39. }
  40. static beforeCreate(model) {
  41. if (model.dataColumns.length > 0) {
  42. model.table = model.dataColumns;
  43. model.dataColumns = [];
  44. }
  45. if (model.isProtected) {
  46. const pass = getWorkflowPass(model.pass);
  47. model.drawflow = decryptFlow(model, pass);
  48. model.isProtected = false;
  49. }
  50. if (model.table && !model.table[0]?.id) {
  51. model.table = model.table.map((column) => {
  52. if (!column.id) column.id = column.name;
  53. return column;
  54. });
  55. }
  56. return model;
  57. }
  58. static async insert(payload) {
  59. const res = await super.insert(payload);
  60. await this.store().dispatch('saveToStorage', 'workflows');
  61. return res;
  62. }
  63. static async afterDelete({ id }) {
  64. try {
  65. await cleanWorkflowTriggers(id);
  66. const hostedWorkflow = this.store().state.hostWorkflows[id];
  67. const { backupIds } = await browser.storage.local.get('backupIds');
  68. const isBackup = (backupIds || []).includes(id);
  69. if (hostedWorkflow || isBackup) {
  70. const response = await fetchApi(`/me/workflows?id=${id}`, {
  71. method: 'DELETE',
  72. });
  73. if (!response.ok) {
  74. throw new Error(response.statusText);
  75. }
  76. if (isBackup) {
  77. backupIds.splice(backupIds.indexOf(id), 1);
  78. await browser.storage.local.set({ backupIds });
  79. }
  80. await browser.storage.local.set({ clearCache: true });
  81. }
  82. } catch (error) {
  83. console.error(error);
  84. }
  85. }
  86. }
  87. export default Workflow;