-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdateEntity.js
69 lines (56 loc) · 1.92 KB
/
updateEntity.js
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
var getCompiler = require('cerebral-url-scheme-compiler/get')
var getKeys = require('./helpers/getKeys.js')
function updateEntity (entityType, uuidPath, dataPath) {
var getUuidValue
if (typeof uuidPath === 'string') {
getUuidValue = getCompiler(uuidPath)
} else {
getUuidValue = function () {
return uuidPath
}
}
var getDataValue
if (typeof dataPath === 'string') {
getDataValue = getCompiler(dataPath)
} else {
getDataValue = Object.keys(dataPath).reduce(function (newDataPath, key) {
newDataPath[key] = getCompiler(dataPath[key])
return newDataPath
}, {})
}
function action (args) {
var state = args.state
var output = args.output
var modules = args.modules
var module = modules['cerebral-module-entities']
var meta = module.meta || {}
var keys = getKeys(entityType, meta)
var uuid = getUuidValue(args)
var ensuredUuid = uuid
var uuidById = state.get(module.path.concat(['ids', entityType, uuid]))
if (uuidById) {
ensuredUuid = uuidById
}
var data = typeof getDataValue === 'function' ? getDataValue(args) : Object.keys(getDataValue).reduce(function (data, key) {
data[key] = getDataValue[key](args)
return data
}, {})
var ids = state.get(module.path.concat(['ids']))
if (!ids) {
ids = {}
state.set(module.path.concat([entityType], ids))
}
if (data[keys.id] && !ids[entityType]) {
state.set(module.path.concat(['ids', entityType], {}))
}
ids = state.get(module.path.concat(['ids']))
if (data[keys.id] && !ids[entityType][data[keys.id]]) {
state.set(module.path.concat(['ids', entityType, data[keys.id]]), ensuredUuid)
}
state.merge(module.path.concat([entityType, ensuredUuid]), data)
output({uuid: ensuredUuid})
}
action.displayName = 'entities.updateEntity (' + ([].slice.call(arguments).join(', ')) + ')'
return action
}
module.exports = updateEntity