diff --git a/apps/workflowengine/src/helpers/api.js b/apps/workflowengine/src/helpers/api.js new file mode 100644 index 0000000000..c2c8d9b6b4 --- /dev/null +++ b/apps/workflowengine/src/helpers/api.js @@ -0,0 +1,30 @@ +/* + * @copyright Copyright (c) 2019 Julius Härtl + * + * @author Julius Härtl + * + * @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 . + * + */ + +const getApiUrl = (url) => { + const scopeValue = OCP.InitialState.loadState('workflowengine', 'scope') === 0 ? 'global' : 'user' + return OC.linkToOCS('apps/workflowengine/api/v1/workflows', 2) + scopeValue + url + '?format=json' +} + +export { + getApiUrl +} diff --git a/apps/workflowengine/src/store.js b/apps/workflowengine/src/store.js new file mode 100644 index 0000000000..ec9d736dd0 --- /dev/null +++ b/apps/workflowengine/src/store.js @@ -0,0 +1,137 @@ +/* + * @copyright Copyright (c) 2019 Julius Härtl + * + * @author Julius Härtl + * + * @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 . + * + */ + +import Vue from 'vue' +import Vuex from 'vuex' +import axios from 'nextcloud-axios' +import { getApiUrl } from './helpers/api' +import { ALL_CHECKS, Operators } from './services/Operation' +import confirmPassword from 'nextcloud-password-confirmation' + +Vue.use(Vuex) + +const store = new Vuex.Store({ + state: { + rules: [], + scope: OCP.InitialState.loadState('workflowengine', 'scope'), + // TODO: move to backend data + operations: Operators, + entities: OCP.InitialState.loadState('workflowengine', 'entities').map(entity => { + return { + ...entity, + // TODO: move to backend data once checks are provided + checks: [...ALL_CHECKS] + } + }), + events: OCP.InitialState.loadState('workflowengine', 'entities') + .map((entity) => entity.events.map(event => { + return { + id: `${entity.id}::${event.eventName}`, + entity, + ...event + } + })).flat() + }, + mutations: { + addRule(state, rule) { + state.rules.push(rule) + }, + 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) + } + }, + 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 === '') { + entity = context.state.entities.find((item) => rule.entities[0] === item.id) + 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 + checks: [], + operation: rule.operation || '' + }) + }, + updateRule(context, rule) { + context.commit('updateRule', { + ...rule, + events: typeof rule.events === 'string' ? JSON.parse(rule.events) : rule.events + }) + }, + removeRule(context, rule) { + context.commit('removeRule', rule) + }, + async pushUpdateRule(context, rule) { + await confirmPassword() + 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) + } + }, + getters: { + getRules(state) { + return state.rules.sort((rule1, rule2) => { + 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 + } + } +}) + +export default store