2019-09-25 19:19:42 +03:00
|
|
|
/**
|
2019-08-30 12:35:18 +03:00
|
|
|
* @copyright Copyright (c) 2019 Julius Härtl <jus@bitgrid.net>
|
|
|
|
*
|
|
|
|
* @author Julius Härtl <jus@bitgrid.net>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Vue from 'vue'
|
|
|
|
import Vuex from 'vuex'
|
2019-10-04 13:17:09 +03:00
|
|
|
import axios from '@nextcloud/axios'
|
2019-08-30 12:35:18 +03:00
|
|
|
import { getApiUrl } from './helpers/api'
|
2020-03-22 12:46:04 +03:00
|
|
|
import confirmPassword from '@nextcloud/password-confirmation'
|
2019-10-03 18:08:39 +03:00
|
|
|
import { loadState } from '@nextcloud/initial-state'
|
2019-08-30 12:35:18 +03:00
|
|
|
|
|
|
|
Vue.use(Vuex)
|
|
|
|
|
|
|
|
const store = new Vuex.Store({
|
|
|
|
state: {
|
|
|
|
rules: [],
|
2019-10-03 18:08:39 +03:00
|
|
|
scope: loadState('workflowengine', 'scope'),
|
2020-03-15 19:32:50 +03:00
|
|
|
appstoreEnabled: loadState('workflowengine', 'appstoreenabled'),
|
2019-10-03 18:08:39 +03:00
|
|
|
operations: loadState('workflowengine', 'operators'),
|
2019-08-30 15:14:12 +03:00
|
|
|
|
|
|
|
plugins: Vue.observable({
|
|
|
|
checks: {},
|
2019-11-13 15:05:10 +03:00
|
|
|
operators: {},
|
2019-08-30 15:14:12 +03:00
|
|
|
}),
|
|
|
|
|
2019-10-03 18:08:39 +03:00
|
|
|
entities: loadState('workflowengine', 'entities'),
|
|
|
|
events: loadState('workflowengine', 'entities')
|
2019-08-30 12:35:18 +03:00
|
|
|
.map((entity) => entity.events.map(event => {
|
|
|
|
return {
|
|
|
|
id: `${entity.id}::${event.eventName}`,
|
|
|
|
entity,
|
2019-11-13 15:05:10 +03:00
|
|
|
...event,
|
2019-08-30 12:35:18 +03:00
|
|
|
}
|
2019-09-06 14:47:11 +03:00
|
|
|
})).flat(),
|
2019-11-13 15:05:10 +03:00
|
|
|
checks: loadState('workflowengine', 'checks'),
|
2019-08-30 12:35:18 +03:00
|
|
|
},
|
|
|
|
mutations: {
|
|
|
|
addRule(state, rule) {
|
2019-09-02 12:35:33 +03:00
|
|
|
state.rules.push({ ...rule, valid: true })
|
2019-08-30 12:35:18 +03:00
|
|
|
},
|
|
|
|
updateRule(state, rule) {
|
|
|
|
const index = state.rules.findIndex((item) => rule.id === item.id)
|
|
|
|
const newRule = Object.assign({}, rule)
|
|
|
|
Vue.set(state.rules, index, newRule)
|
|
|
|
},
|
|
|
|
removeRule(state, rule) {
|
|
|
|
const index = state.rules.findIndex((item) => rule.id === item.id)
|
|
|
|
state.rules.splice(index, 1)
|
2019-08-30 15:14:12 +03:00
|
|
|
},
|
|
|
|
addPluginCheck(state, plugin) {
|
|
|
|
Vue.set(state.plugins.checks, plugin.class, plugin)
|
|
|
|
},
|
|
|
|
addPluginOperator(state, plugin) {
|
2019-08-31 12:53:15 +03:00
|
|
|
plugin = Object.assign(
|
|
|
|
{ color: 'var(--color-primary-element)' },
|
|
|
|
plugin, state.operations[plugin.id] || {})
|
2019-10-11 16:05:58 +03:00
|
|
|
if (typeof state.operations[plugin.id] !== 'undefined') {
|
|
|
|
Vue.set(state.operations, plugin.id, plugin)
|
|
|
|
}
|
2019-11-13 15:05:10 +03:00
|
|
|
},
|
2019-08-30 12:35:18 +03:00
|
|
|
},
|
|
|
|
actions: {
|
|
|
|
async fetchRules(context) {
|
|
|
|
const { data } = await axios.get(getApiUrl(''))
|
|
|
|
Object.values(data.ocs.data).flat().forEach((rule) => {
|
|
|
|
context.commit('addRule', rule)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
createNewRule(context, rule) {
|
|
|
|
let entity = null
|
|
|
|
let events = []
|
|
|
|
if (rule.isComplex === false && rule.fixedEntity === '') {
|
2019-08-31 12:53:15 +03:00
|
|
|
entity = context.state.entities.find((item) => rule.entities && rule.entities[0] === item.id)
|
2019-09-09 16:04:51 +03:00
|
|
|
entity = entity || Object.values(context.state.entities)[0]
|
2019-08-30 12:35:18 +03:00
|
|
|
events = [entity.events[0].eventName]
|
|
|
|
}
|
|
|
|
|
|
|
|
context.commit('addRule', {
|
|
|
|
id: -(new Date().getTime()),
|
|
|
|
class: rule.id,
|
|
|
|
entity: entity ? entity.id : rule.fixedEntity,
|
|
|
|
events,
|
|
|
|
name: '', // unused in the new ui, there for legacy reasons
|
2019-12-23 14:22:22 +03:00
|
|
|
checks: [
|
2019-12-23 14:35:45 +03:00
|
|
|
{ class: null, operator: null, value: '' },
|
2019-12-23 14:22:22 +03:00
|
|
|
],
|
2019-11-13 15:05:10 +03:00
|
|
|
operation: rule.operation || '',
|
2019-08-30 12:35:18 +03:00
|
|
|
})
|
|
|
|
},
|
|
|
|
updateRule(context, rule) {
|
|
|
|
context.commit('updateRule', {
|
|
|
|
...rule,
|
2019-11-13 15:05:10 +03:00
|
|
|
events: typeof rule.events === 'string' ? JSON.parse(rule.events) : rule.events,
|
2019-08-30 12:35:18 +03:00
|
|
|
})
|
|
|
|
},
|
|
|
|
removeRule(context, rule) {
|
|
|
|
context.commit('removeRule', rule)
|
|
|
|
},
|
|
|
|
async pushUpdateRule(context, rule) {
|
2019-12-30 15:25:59 +03:00
|
|
|
if (context.state.scope === 0) {
|
|
|
|
await confirmPassword()
|
|
|
|
}
|
2019-08-30 12:35:18 +03:00
|
|
|
let result
|
|
|
|
if (rule.id < 0) {
|
|
|
|
result = await axios.post(getApiUrl(''), rule)
|
|
|
|
} else {
|
|
|
|
result = await axios.put(getApiUrl(`/${rule.id}`), rule)
|
|
|
|
}
|
|
|
|
Vue.set(rule, 'id', result.data.ocs.data.id)
|
|
|
|
context.commit('updateRule', rule)
|
|
|
|
},
|
|
|
|
async deleteRule(context, rule) {
|
|
|
|
await confirmPassword()
|
|
|
|
await axios.delete(getApiUrl(`/${rule.id}`))
|
|
|
|
context.commit('removeRule', rule)
|
2019-09-02 12:35:33 +03:00
|
|
|
},
|
2019-09-09 16:04:51 +03:00
|
|
|
setValid(context, { rule, valid }) {
|
2019-09-02 12:35:33 +03:00
|
|
|
rule.valid = valid
|
|
|
|
context.commit('updateRule', rule)
|
2019-11-13 15:05:10 +03:00
|
|
|
},
|
2019-08-30 12:35:18 +03:00
|
|
|
},
|
|
|
|
getters: {
|
|
|
|
getRules(state) {
|
2020-01-09 10:25:37 +03:00
|
|
|
return state.rules.filter((rule) => typeof state.operations[rule.class] !== 'undefined').sort((rule1, rule2) => {
|
2019-08-30 12:35:18 +03:00
|
|
|
return rule1.id - rule2.id || rule2.class - rule1.class
|
|
|
|
})
|
|
|
|
},
|
|
|
|
getOperationForRule(state) {
|
|
|
|
return (rule) => state.operations[rule.class]
|
|
|
|
},
|
|
|
|
getEntityForOperation(state) {
|
|
|
|
return (operation) => state.entities.find((entity) => operation.fixedEntity === entity.id)
|
|
|
|
},
|
|
|
|
getEventsForOperation(state) {
|
|
|
|
return (operation) => state.events
|
2019-09-06 14:47:11 +03:00
|
|
|
},
|
2019-09-25 19:19:42 +03:00
|
|
|
|
2019-09-06 14:47:11 +03:00
|
|
|
/**
|
|
|
|
* Return all available checker plugins for a given entity class
|
2019-09-25 19:19:42 +03:00
|
|
|
* @param {Object} state the store state
|
|
|
|
* @param {Object} entity the entity class
|
|
|
|
* @returns {Array} the available plugins
|
2019-09-06 14:47:11 +03:00
|
|
|
*/
|
|
|
|
getChecksForEntity(state) {
|
|
|
|
return (entity) => {
|
2019-09-12 18:15:46 +03:00
|
|
|
return Object.values(state.checks)
|
2019-09-06 14:47:11 +03:00
|
|
|
.filter((check) => check.supportedEntities.indexOf(entity) > -1 || check.supportedEntities.length === 0)
|
|
|
|
.map((check) => state.plugins.checks[check.id])
|
|
|
|
.reduce((obj, item) => {
|
|
|
|
obj[item.class] = item
|
|
|
|
return obj
|
|
|
|
}, {})
|
|
|
|
}
|
2019-11-13 15:05:10 +03:00
|
|
|
},
|
|
|
|
},
|
2019-08-30 12:35:18 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
export default store
|