store.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-License-Identifier: AGPL-3.0-or-later
  4. */
  5. import Vue from 'vue'
  6. import Vuex, { Store } from 'vuex'
  7. import axios from '@nextcloud/axios'
  8. import { confirmPassword } from '@nextcloud/password-confirmation'
  9. import { loadState } from '@nextcloud/initial-state'
  10. import { getApiUrl } from './helpers/api.js'
  11. import '@nextcloud/password-confirmation/dist/style.css'
  12. Vue.use(Vuex)
  13. const store = new Store({
  14. state: {
  15. rules: [],
  16. scope: loadState('workflowengine', 'scope'),
  17. appstoreEnabled: loadState('workflowengine', 'appstoreenabled'),
  18. operations: loadState('workflowengine', 'operators'),
  19. plugins: Vue.observable({
  20. checks: {},
  21. operators: {},
  22. }),
  23. entities: loadState('workflowengine', 'entities'),
  24. events: loadState('workflowengine', 'entities')
  25. .map((entity) => entity.events.map(event => {
  26. return {
  27. id: `${entity.id}::${event.eventName}`,
  28. entity,
  29. ...event,
  30. }
  31. })).flat(),
  32. checks: loadState('workflowengine', 'checks'),
  33. },
  34. mutations: {
  35. addRule(state, rule) {
  36. state.rules.push({ ...rule, valid: true })
  37. },
  38. updateRule(state, rule) {
  39. const index = state.rules.findIndex((item) => rule.id === item.id)
  40. const newRule = Object.assign({}, rule)
  41. Vue.set(state.rules, index, newRule)
  42. },
  43. removeRule(state, rule) {
  44. const index = state.rules.findIndex((item) => rule.id === item.id)
  45. state.rules.splice(index, 1)
  46. },
  47. addPluginCheck(state, plugin) {
  48. Vue.set(state.plugins.checks, plugin.class, plugin)
  49. },
  50. addPluginOperator(state, plugin) {
  51. plugin = Object.assign(
  52. { color: 'var(--color-primary-element)' },
  53. plugin, state.operations[plugin.id] || {})
  54. if (typeof state.operations[plugin.id] !== 'undefined') {
  55. Vue.set(state.operations, plugin.id, plugin)
  56. }
  57. },
  58. },
  59. actions: {
  60. async fetchRules(context) {
  61. const { data } = await axios.get(getApiUrl(''))
  62. Object.values(data.ocs.data).flat().forEach((rule) => {
  63. context.commit('addRule', rule)
  64. })
  65. },
  66. async createNewRule(context, rule) {
  67. await confirmPassword()
  68. let entity = null
  69. let events = []
  70. if (rule.isComplex === false && rule.fixedEntity === '') {
  71. entity = context.state.entities.find((item) => rule.entities && rule.entities[0] === item.id)
  72. entity = entity || Object.values(context.state.entities)[0]
  73. events = [entity.events[0].eventName]
  74. }
  75. context.commit('addRule', {
  76. id: -(new Date().getTime()),
  77. class: rule.id,
  78. entity: entity ? entity.id : rule.fixedEntity,
  79. events,
  80. name: '', // unused in the new ui, there for legacy reasons
  81. checks: [
  82. { class: null, operator: null, value: '' },
  83. ],
  84. operation: rule.operation || '',
  85. })
  86. },
  87. updateRule(context, rule) {
  88. context.commit('updateRule', {
  89. ...rule,
  90. events: typeof rule.events === 'string' ? JSON.parse(rule.events) : rule.events,
  91. })
  92. },
  93. removeRule(context, rule) {
  94. context.commit('removeRule', rule)
  95. },
  96. async pushUpdateRule(context, rule) {
  97. await confirmPassword()
  98. let result
  99. if (rule.id < 0) {
  100. result = await axios.post(getApiUrl(''), rule)
  101. } else {
  102. result = await axios.put(getApiUrl(`/${rule.id}`), rule)
  103. }
  104. Vue.set(rule, 'id', result.data.ocs.data.id)
  105. context.commit('updateRule', rule)
  106. },
  107. async deleteRule(context, rule) {
  108. await confirmPassword()
  109. await axios.delete(getApiUrl(`/${rule.id}`))
  110. context.commit('removeRule', rule)
  111. },
  112. setValid(context, { rule, valid }) {
  113. rule.valid = valid
  114. context.commit('updateRule', rule)
  115. },
  116. },
  117. getters: {
  118. getRules(state) {
  119. return state.rules.filter((rule) => typeof state.operations[rule.class] !== 'undefined').sort((rule1, rule2) => {
  120. return rule1.id - rule2.id || rule2.class - rule1.class
  121. })
  122. },
  123. getOperationForRule(state) {
  124. return (rule) => state.operations[rule.class]
  125. },
  126. getEntityForOperation(state) {
  127. return (operation) => state.entities.find((entity) => operation.fixedEntity === entity.id)
  128. },
  129. getEventsForOperation(state) {
  130. return (operation) => state.events
  131. },
  132. /**
  133. * Return all available checker plugins for a given entity class
  134. *
  135. * @param {object} state the store state
  136. * @return {Function} the available plugins
  137. */
  138. getChecksForEntity(state) {
  139. return (entity) => {
  140. return Object.values(state.checks)
  141. .filter((check) => check.supportedEntities.indexOf(entity) > -1 || check.supportedEntities.length === 0)
  142. .map((check) => state.plugins.checks[check.id])
  143. .reduce((obj, item) => {
  144. obj[item.class] = item
  145. return obj
  146. }, {})
  147. }
  148. },
  149. },
  150. })
  151. export default store