Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions lib/plugin/components/state-resources/set-context-data/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
'use strict'

const _ = require('lodash')
const { isString, isPlainObject } = require('lodash')
const dottie = require('dottie')
const jp = require('jsonpath')

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 = {}
Expand All @@ -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] === '$') {
Expand All @@ -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])
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@
]
}
]
}
}