aboutsummaryrefslogtreecommitdiffstats
path: root/apps/workflowengine/src/store.js
blob: a07b0989357d8f4c093a2d1869716ace32da52cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
 * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

import Vue from 'vue'
import Vuex, { Store } from 'vuex'
import axios from '@nextcloud/axios'
import { confirmPassword } from '@nextcloud/password-confirmation'
import { loadState } from '@nextcloud/initial-state'
import { getApiUrl } from './helpers/api.js'

import '@nextcloud/password-confirmation/dist/style.css'

Vue.use(Vuex)

const store = new Store({
	state: {
		rules: [],
		scope: loadState('workflowengine', 'scope'),
		appstoreEnabled: loadState('workflowengine', 'appstoreenabled'),
		operations: loadState('workflowengine', 'operators'),

		plugins: Vue.observable({
			checks: {},
			operators: {},
		}),

		entities: loadState('workflowengine', 'entities'),
		events: loadState('workflowengine', 'entities')
			.map((entity) => entity.events.map(event => {
				return {
					id: `${entity.id}::${event.eventName}`,
					entity,
					...event,
				}
			})).flat(),
		checks: loadState('workflowengine', 'checks'),
	},
	mutations: {
		addRule(state, rule) {
			state.rules.push({ ...rule, valid: true })
		},
		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)
		},
		addPluginCheck(state, plugin) {
			Vue.set(state.plugins.checks, plugin.class, plugin)
		},
		addPluginOperator(state, plugin) {
			plugin = Object.assign(
				{ color: 'var(--color-primary-element)' },
				plugin, state.operations[plugin.id] || {})
			if (typeof state.operations[plugin.id] !== 'undefined') {
				Vue.set(state.operations, plugin.id, plugin)
			}
		},
	},
	actions: {
		async fetchRules(context) {
			const { data } = await axios.get(getApiUrl(''))
			Object.values(data.ocs.data).flat().forEach((rule) => {
				context.commit('addRule', rule)
			})
		},
		async createNewRule(context, rule) {
			await confirmPassword()
			let entity = null
			let events = []
			if (rule.isComplex === false && rule.fixedEntity === '') {
				entity = context.state.entities.find((item) => rule.entities && rule.entities[0] === item.id)
				entity = entity || Object.values(context.state.entities)[0]
				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: [
					{ class: null, operator: null, value: '' },
				],
				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)
		},
		setValid(context, { rule, valid }) {
			rule.valid = valid
			context.commit('updateRule', rule)
		},
	},
	getters: {
		getRules(state) {
			return state.rules.filter((rule) => typeof state.operations[rule.class] !== 'undefined').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
		},

		/**
		 * Return all available checker plugins for a given entity class
		 *
		 * @param {object} state the store state
		 * @return {Function} the available plugins
		 */
		getChecksForEntity(state) {
			return (entity) => {
				return Object.values(state.checks)
					.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
					}, {})
			}
		},
	},
})

export default store