From aa21926eee6e65da73a114cef79ecd35aa367456 Mon Sep 17 00:00:00 2001 From: Meena Brend Date: Fri, 6 May 2022 07:29:49 +0100 Subject: [PATCH 1/7] fix: change set context data to allow more user info --- .../state-resources/set-context-data/index.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) 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..9cbf3069 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') @@ -11,7 +11,7 @@ module.exports = class SetContextData { } async run (event, context) { - this.email = this.userInfoService ? await this.userInfoService.emailFromUserId(context.userId) : '' + this.user = this.userInfoService ? await this.userInfoService.allFromUserId(context.userId) : null const FORM_DATA_STRING_LENGTH = 8 const config = {} @@ -20,7 +20,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 +47,21 @@ 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)) { + } else if (config === '$USER_EMAIL') { + value = this.user ? this.user.email : null + } else if (config === '$USER_NAME') { + value = this.user ? this.user.name : null + } 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]) From a655b9fce1f350539e613b632f020c881ee63c0f Mon Sep 17 00:00:00 2001 From: Meena Brend Date: Fri, 6 May 2022 07:31:16 +0100 Subject: [PATCH 2/7] test: update state machine to match context data changes --- .../context-blueprint/state-machines/set-context-data.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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..d37c73e6 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 @@ -9,7 +9,7 @@ "$.formData.dogName": "$.dog", "$.formData.catOwnerId": "$USERID", "$.formData.catBirthday": "$NOW", - "$.formData.email": "$EMAIL", + "$.formData.email": "$USER_EMAIL", "$.formData.faveColours": "$.faveColours", "$.formData.measurements": [ { @@ -32,4 +32,4 @@ ] } ] -} \ No newline at end of file +} From 11d5b89caaf740069d9f5a4b1b972cf98592d65b Mon Sep 17 00:00:00 2001 From: Meena Brend Date: Fri, 6 May 2022 07:39:50 +0100 Subject: [PATCH 3/7] build(deps-dev): update @wmfs/tymly-test-helpers to 1.17.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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", From f1dc8e02e4f51bdab5cf99c44ef00dc6c4e5dd8e Mon Sep 17 00:00:00 2001 From: Meena Brend Date: Tue, 10 May 2022 08:38:45 +0100 Subject: [PATCH 4/7] test: revert to older naming --- .../context-blueprint/state-machines/set-context-data.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 d37c73e6..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 @@ -9,7 +9,7 @@ "$.formData.dogName": "$.dog", "$.formData.catOwnerId": "$USERID", "$.formData.catBirthday": "$NOW", - "$.formData.email": "$USER_EMAIL", + "$.formData.email": "$EMAIL", "$.formData.faveColours": "$.faveColours", "$.formData.measurements": [ { From 6dfe4bed2e9bc89d3160ccab782c03f7bed126ce Mon Sep 17 00:00:00 2001 From: Meena Brend Date: Tue, 10 May 2022 08:39:01 +0100 Subject: [PATCH 5/7] fix: revert to older naming --- .../components/state-resources/set-context-data/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 9cbf3069..4be6c119 100644 --- a/lib/plugin/components/state-resources/set-context-data/index.js +++ b/lib/plugin/components/state-resources/set-context-data/index.js @@ -53,9 +53,9 @@ module.exports = class SetContextData { value = new Date().toISOString() } else if (config === '$USERID') { value = context.userId - } else if (config === '$USER_EMAIL') { + } else if (config === '$EMAIL') { value = this.user ? this.user.email : null - } else if (config === '$USER_NAME') { + } else if (config === '$NAME') { value = this.user ? this.user.name : null } else if (Array.isArray(config)) { value = config.map(c => { From 0dcf5ed1f4bfba6be81d2db403eebc331bc30e10 Mon Sep 17 00:00:00 2001 From: Meena Brend Date: Tue, 10 May 2022 08:41:03 +0100 Subject: [PATCH 6/7] fix: ensure allFromUserId is a function --- .../components/state-resources/set-context-data/index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 4be6c119..e8e407ad 100644 --- a/lib/plugin/components/state-resources/set-context-data/index.js +++ b/lib/plugin/components/state-resources/set-context-data/index.js @@ -11,7 +11,9 @@ module.exports = class SetContextData { } async run (event, context) { - this.user = this.userInfoService ? await this.userInfoService.allFromUserId(context.userId) : null + this.user = this.userInfoService && this.userInfoService.allFromUserId + ? await this.userInfoService.allFromUserId(context.userId) + : null const FORM_DATA_STRING_LENGTH = 8 const config = {} From ecaa0709761c7673c126fc4fb414d5f858748b05 Mon Sep 17 00:00:00 2001 From: meenahoda Date: Tue, 10 May 2022 09:26:40 +0100 Subject: [PATCH 7/7] feat: ability to get cached user roles --- .../state-resources/set-context-data/index.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) 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 e8e407ad..e4bc1da1 100644 --- a/lib/plugin/components/state-resources/set-context-data/index.js +++ b/lib/plugin/components/state-resources/set-context-data/index.js @@ -8,13 +8,18 @@ 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.user = this.userInfoService && this.userInfoService.allFromUserId - ? await this.userInfoService.allFromUserId(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 = {} const data = {} @@ -59,6 +64,8 @@ module.exports = class SetContextData { 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)