store.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**
  2. * @copyright Copyright (c) 2019 Julius Härtl <jus@bitgrid.net>
  3. *
  4. * @author Julius Härtl <jus@bitgrid.net>
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. import Vue from 'vue'
  23. import Vuex from 'vuex'
  24. import axios from '@nextcloud/axios'
  25. import { getApiUrl } from './helpers/api'
  26. import confirmPassword from 'nextcloud-password-confirmation'
  27. Vue.use(Vuex)
  28. const store = new Vuex.Store({
  29. state: {
  30. rules: [],
  31. scope: OCP.InitialState.loadState('workflowengine', 'scope'),
  32. operations: OCP.InitialState.loadState('workflowengine', 'operators'),
  33. plugins: Vue.observable({
  34. checks: {},
  35. operators: {}
  36. }),
  37. entities: OCP.InitialState.loadState('workflowengine', 'entities'),
  38. events: OCP.InitialState.loadState('workflowengine', 'entities')
  39. .map((entity) => entity.events.map(event => {
  40. return {
  41. id: `${entity.id}::${event.eventName}`,
  42. entity,
  43. ...event
  44. }
  45. })).flat(),
  46. checks: OCP.InitialState.loadState('workflowengine', 'checks')
  47. },
  48. mutations: {
  49. addRule(state, rule) {
  50. state.rules.push({ ...rule, valid: true })
  51. },
  52. updateRule(state, rule) {
  53. const index = state.rules.findIndex((item) => rule.id === item.id)
  54. const newRule = Object.assign({}, rule)
  55. Vue.set(state.rules, index, newRule)
  56. },
  57. removeRule(state, rule) {
  58. const index = state.rules.findIndex((item) => rule.id === item.id)
  59. state.rules.splice(index, 1)
  60. },
  61. addPluginCheck(state, plugin) {
  62. Vue.set(state.plugins.checks, plugin.class, plugin)
  63. },
  64. addPluginOperator(state, plugin) {
  65. plugin = Object.assign(
  66. { color: 'var(--color-primary-element)' },
  67. plugin, state.operations[plugin.id] || {})
  68. Vue.set(state.operations, plugin.id, plugin)
  69. }
  70. },
  71. actions: {
  72. async fetchRules(context) {
  73. const { data } = await axios.get(getApiUrl(''))
  74. Object.values(data.ocs.data).flat().forEach((rule) => {
  75. context.commit('addRule', rule)
  76. })
  77. },
  78. createNewRule(context, rule) {
  79. let entity = null
  80. let events = []
  81. if (rule.isComplex === false && rule.fixedEntity === '') {
  82. entity = context.state.entities.find((item) => rule.entities && rule.entities[0] === item.id)
  83. entity = entity || Object.values(context.state.entities)[0]
  84. events = [entity.events[0].eventName]
  85. }
  86. context.commit('addRule', {
  87. id: -(new Date().getTime()),
  88. class: rule.id,
  89. entity: entity ? entity.id : rule.fixedEntity,
  90. events,
  91. name: '', // unused in the new ui, there for legacy reasons
  92. checks: [],
  93. operation: rule.operation || ''
  94. })
  95. },
  96. updateRule(context, rule) {
  97. context.commit('updateRule', {
  98. ...rule,
  99. events: typeof rule.events === 'string' ? JSON.parse(rule.events) : rule.events
  100. })
  101. },
  102. removeRule(context, rule) {
  103. context.commit('removeRule', rule)
  104. },
  105. async pushUpdateRule(context, rule) {
  106. await confirmPassword()
  107. let result
  108. if (rule.id < 0) {
  109. result = await axios.post(getApiUrl(''), rule)
  110. } else {
  111. result = await axios.put(getApiUrl(`/${rule.id}`), rule)
  112. }
  113. Vue.set(rule, 'id', result.data.ocs.data.id)
  114. context.commit('updateRule', rule)
  115. },
  116. async deleteRule(context, rule) {
  117. await confirmPassword()
  118. await axios.delete(getApiUrl(`/${rule.id}`))
  119. context.commit('removeRule', rule)
  120. },
  121. setValid(context, { rule, valid }) {
  122. rule.valid = valid
  123. context.commit('updateRule', rule)
  124. }
  125. },
  126. getters: {
  127. getRules(state) {
  128. return state.rules.sort((rule1, rule2) => {
  129. return rule1.id - rule2.id || rule2.class - rule1.class
  130. })
  131. },
  132. getOperationForRule(state) {
  133. return (rule) => state.operations[rule.class]
  134. },
  135. getEntityForOperation(state) {
  136. return (operation) => state.entities.find((entity) => operation.fixedEntity === entity.id)
  137. },
  138. getEventsForOperation(state) {
  139. return (operation) => state.events
  140. },
  141. /**
  142. * Return all available checker plugins for a given entity class
  143. * @param {Object} state the store state
  144. * @param {Object} entity the entity class
  145. * @returns {Array} the available plugins
  146. */
  147. getChecksForEntity(state) {
  148. return (entity) => {
  149. return Object.values(state.checks)
  150. .filter((check) => check.supportedEntities.indexOf(entity) > -1 || check.supportedEntities.length === 0)
  151. .map((check) => state.plugins.checks[check.id])
  152. .reduce((obj, item) => {
  153. obj[item.class] = item
  154. return obj
  155. }, {})
  156. }
  157. }
  158. }
  159. })
  160. export default store