diff --git a/lib/plugin/components/state-resources/set-context-data/index.js b/lib/plugin/components/state-resources/set-context-data/index.js index 787870a8..e4bc1da1 100644 --- a/lib/plugin/components/state-resources/set-context-data/index.js +++ b/lib/plugin/components/state-resources/set-context-data/index.js @@ -1,6 +1,6 @@ 'use strict' -const _ = require('lodash') +const { isString, isPlainObject } = require('lodash') const dottie = require('dottie') const jp = require('jsonpath') @@ -8,10 +8,17 @@ module.exports = class SetContextData { init (resourceConfig, env) { this.resourceConfig = resourceConfig this.userInfoService = env.bootedServices.userInfo + this.cachesService = env.bootedServices.caches } async run (event, context) { - this.email = this.userInfoService ? await this.userInfoService.emailFromUserId(context.userId) : '' + this.user = this.userInfoService && this.userInfoService.allFromUserId + ? await this.userInfoService.allFromUserId(context.userId) + : null + + this.userCachedRoles = this.cachesService && this.cachesService.userMemberships + ? this.cachesService.userMemberships.get(context.userId) + : [] const FORM_DATA_STRING_LENGTH = 8 const config = {} @@ -20,7 +27,7 @@ module.exports = class SetContextData { for (const key of Object.keys(this.resourceConfig)) { let theKey - if (_.isString(key) && key.length > 0) { + if (isString(key) && key.length > 0) { let dottiePath = key if (dottiePath[0] === '$') { @@ -47,19 +54,23 @@ module.exports = class SetContextData { getValue (event, context, key, config, val) { let value = val || config - if (_.isString(config) && config.substring(0, 2) === '$.') { + if (isString(config) && config.substring(0, 2) === '$.') { value = jp.value(event, config) } else if (config === '$NOW') { value = new Date().toISOString() } else if (config === '$USERID') { value = context.userId } else if (config === '$EMAIL') { - value = this.email - } else if (_.isArray(config)) { + value = this.user ? this.user.email : null + } else if (config === '$NAME') { + value = this.user ? this.user.name : null + } else if (config === '$CACHED_ROLES') { + value = this.userCachedRoles + } else if (Array.isArray(config)) { value = config.map(c => { return this.getValue(event, context, key, c, value) }) - } else if (_.isPlainObject(config)) { + } else if (isPlainObject(config)) { value = {} Object.keys(config).forEach(c => { value[c] = this.getValue(event, context, key, config[c]) diff --git a/package.json b/package.json index 74928f5e..31ce7fc8 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,7 @@ "devDependencies": { "@semantic-release/changelog": "6.0.1", "@semantic-release/git": "10.0.1", - "@wmfs/tymly-test-helpers": "1.17.0", + "@wmfs/tymly-test-helpers": "1.17.1", "chai": "4.3.6", "chai-string": "1.5.0", "chai-subset": "1.6.0", diff --git a/test/fixtures/blueprints/context-blueprint/state-machines/set-context-data.json b/test/fixtures/blueprints/context-blueprint/state-machines/set-context-data.json index 68bd1ef2..aa6bf1a6 100644 --- a/test/fixtures/blueprints/context-blueprint/state-machines/set-context-data.json +++ b/test/fixtures/blueprints/context-blueprint/state-machines/set-context-data.json @@ -32,4 +32,4 @@ ] } ] -} \ No newline at end of file +}