From 65d0161007d3ddb16b5ddf2051382da1e18d8587 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Sat, 30 May 2020 13:41:46 +0100 Subject: [PATCH] =?UTF-8?q?orchestrator=20refactor,=C2=A0user=20creation?= =?UTF-8?q?=20block?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../builderStore/store/workflow/Workflow.js | 4 +- .../builder/src/components/common/binding.js | 52 +++++++++---------- .../ModelDataTable/ModelDataTable.svelte | 7 --- .../userInterface/StateBindingControl.svelte | 5 +- .../SetupPanel/WorkflowBlockSetup.svelte | 16 +++++- .../WorkflowPanel/blockDefinitions.js | 5 +- .../client/src/api/workflow/orchestrator.js | 48 +++++------------ packages/client/src/render/screenRouter.js | 1 + .../workflow/actions/CREATE_USER.js | 26 ++++++++++ packages/server/src/utilities/accessLevels.js | 2 + .../src/utilities/initialiseClientDb.js | 0 11 files changed, 91 insertions(+), 75 deletions(-) create mode 100644 packages/server/src/api/controllers/workflow/actions/CREATE_USER.js delete mode 100644 packages/server/src/utilities/initialiseClientDb.js diff --git a/packages/builder/src/builderStore/store/workflow/Workflow.js b/packages/builder/src/builderStore/store/workflow/Workflow.js index 2b495ef49b..623eb07f48 100644 --- a/packages/builder/src/builderStore/store/workflow/Workflow.js +++ b/packages/builder/src/builderStore/store/workflow/Workflow.js @@ -1,6 +1,6 @@ import mustache from "mustache" // TODO: tidy up import -import blockDefinitions from "../../../pages/[application]/workflow/WorkflowPanel/blockDefinitions" +import blockDefinitions from "../../../components/workflow/WorkflowPanel/blockDefinitions" import { generate } from "shortid" /** @@ -79,7 +79,7 @@ export default class Workflow { type: block.type, params: block.params, args, - heading: definition.actionId, + heading: block.actionId, body: mustache.render(tagline, args), }) diff --git a/packages/builder/src/components/common/binding.js b/packages/builder/src/components/common/binding.js index af04397327..18c31e8040 100644 --- a/packages/builder/src/components/common/binding.js +++ b/packages/builder/src/components/common/binding.js @@ -1,31 +1,31 @@ -import { isString } from "lodash/fp" +// import { isString } from "lodash/fp" -import { - BB_STATE_BINDINGPATH, - BB_STATE_FALLBACK, - BB_STATE_BINDINGSOURCE, - isBound, - parseBinding, -} from "@budibase/client/src/state/parseBinding" +// import { +// BB_STATE_BINDINGPATH, +// BB_STATE_FALLBACK, +// BB_STATE_BINDINGSOURCE, +// isBound, +// parseBinding, +// } from "@budibase/client/src/state/parseBinding" -export const isBinding = isBound +// export const isBinding = isBound -export const setBinding = ({ path, fallback, source }, binding = {}) => { - if (isNonEmptyString(path)) binding[BB_STATE_BINDINGPATH] = path - if (isNonEmptyString(fallback)) binding[BB_STATE_FALLBACK] = fallback - binding[BB_STATE_BINDINGSOURCE] = source || "store" - return binding -} +// export const setBinding = ({ path, fallback, source }, binding = {}) => { +// if (isNonEmptyString(path)) binding[BB_STATE_BINDINGPATH] = path +// if (isNonEmptyString(fallback)) binding[BB_STATE_FALLBACK] = fallback +// binding[BB_STATE_BINDINGSOURCE] = source || "store" +// return binding +// } -export const getBinding = val => { - const binding = parseBinding(val) - return binding - ? binding - : { - path: "", - source: "store", - fallback: "", - } -} +// export const getBinding = val => { +// const binding = parseBinding(val) +// return binding +// ? binding +// : { +// path: "", +// source: "store", +// fallback: "", +// } +// } -const isNonEmptyString = s => isString(s) && s.length > 0 +// const isNonEmptyString = s => isString(s) && s.length > 0 diff --git a/packages/builder/src/components/database/ModelDataTable/ModelDataTable.svelte b/packages/builder/src/components/database/ModelDataTable/ModelDataTable.svelte index 9e6ddc5042..8a16d5d0b1 100644 --- a/packages/builder/src/components/database/ModelDataTable/ModelDataTable.svelte +++ b/packages/builder/src/components/database/ModelDataTable/ModelDataTable.svelte @@ -43,13 +43,6 @@ ) } - async function selectRecord(record) { - return await api.loadRecord(record.key, { - appname: $store.appname, - instanceId: $backendUiStore.selectedDatabase._id, - }) - } - const ITEMS_PER_PAGE = 10 // Internal headers we want to hide from the user const INTERNAL_HEADERS = ["_id", "_rev", "modelId", "type"] diff --git a/packages/builder/src/components/userInterface/StateBindingControl.svelte b/packages/builder/src/components/userInterface/StateBindingControl.svelte index 01adfeeac3..a89b805e5b 100644 --- a/packages/builder/src/components/userInterface/StateBindingControl.svelte +++ b/packages/builder/src/components/userInterface/StateBindingControl.svelte @@ -2,8 +2,7 @@ import { backendUiStore } from "builderStore" import IconButton from "../common/IconButton.svelte" import Input from "../common/Input.svelte" - import PropertyCascader from "./PropertyCascader" - import { isBinding, getBinding, setBinding } from "../common/binding" + // import PropertyCascader from "./PropertyCascader" import Colorpicker from "../common/Colorpicker.svelte" export let value = "" @@ -50,7 +49,7 @@ {/each} {:else} - + {/if} diff --git a/packages/builder/src/components/workflow/SetupPanel/WorkflowBlockSetup.svelte b/packages/builder/src/components/workflow/SetupPanel/WorkflowBlockSetup.svelte index fe72e348fc..2b35d0ab44 100644 --- a/packages/builder/src/components/workflow/SetupPanel/WorkflowBlockSetup.svelte +++ b/packages/builder/src/components/workflow/SetupPanel/WorkflowBlockSetup.svelte @@ -5,6 +5,8 @@ let params + console.log("wfblock", workflowBlock) + $: workflowParams = workflowBlock.params ? Object.entries(workflowBlock.params) : [] @@ -13,7 +15,7 @@ {#each workflowParams as [parameter, type]}
@@ -27,6 +29,18 @@ {/each} + {:else if type === 'accessLevel'} + + {:else if type === 'password'} + {:else if type === 'number'} {{username}}", icon: "ri-user-add-fill", name: "Create User", environment: "SERVER", params: { - name: "string", + username: "string", password: "password", - accessLevel: "accessLevel", + accessLevelId: "accessLevel", }, }, SEND_EMAIL: { diff --git a/packages/client/src/api/workflow/orchestrator.js b/packages/client/src/api/workflow/orchestrator.js index bd82daa9a9..7a26c1648c 100644 --- a/packages/client/src/api/workflow/orchestrator.js +++ b/packages/client/src/api/workflow/orchestrator.js @@ -1,5 +1,7 @@ -import get from "lodash/fp/get" +import { get } from "svelte/store"; +import { setState } from "../../state/setState"; import mustache from "mustache"; +import { appStore } from "../../state/store"; /** * The workflow orchestrator is a class responsible for executing workflows. @@ -15,25 +17,21 @@ export default class Orchestrator { } set strategy(strategy) { - this._strategy = strategy + this._strategy = strategy({ api: this.api, instanceId: this.instanceId }); } async execute(workflowId) { const EXECUTE_WORKFLOW_URL = `/api/${this.instanceId}/workflows/${workflowId}` const workflow = await this.api.get({ url: EXECUTE_WORKFLOW_URL }) - this._strategy.run({ - workflow: workflow.definition, - api: this.api, - instanceId: this.instanceId, - }) + this._strategy.run(workflow.definition) } } // Execute a workflow from a running budibase app -export const clientStrategy = { +export const clientStrategy = ({ api, instanceId }) => ({ delay: ms => new Promise(resolve => setTimeout(resolve, ms)), context: {}, - bindContextArgs: function(args, api) { + bindContextArgs: function(args) { const mappedArgs = { ...args } console.log("original args", args) @@ -42,35 +40,18 @@ export const clientStrategy = { for (let arg in args) { const argValue = args[arg] // Means that it's bound to state or workflow context + console.log(argValue, get(appStore)); mappedArgs[arg] = mustache.render(argValue, { context: this.context, - // TODO: map to the real state - state: {} + state: get(appStore) }); } - // if (argValue.startsWith("$")) { - // // if value is bound to workflow context. - // if (argValue.startsWith("$context")) { - // const path = argValue.replace("$context.", "") - // // pass in the value from context - // mappedArgs[arg] = get(path, this.context) - // } - - // // if the value is bound to state - // if (argValue.startsWith("$state")) { - // const path = argValue.replace("$state.", "") - // // pass in the value from state - // // TODO: not working - // mappedArgs[arg] = api.getState(path) - // } - // } - // } console.log(mappedArgs) - return Object.values(mappedArgs) + return mappedArgs }, - run: async function({ workflow, api, instanceId }) { + run: async function(workflow) { const block = workflow.next console.log("Executing workflow block", block) @@ -81,7 +62,7 @@ export const clientStrategy = { if (block.environment === "CLIENT") { if (block.actionId === "SET_STATE") { // get props from the workflow context if required - api.setState(...this.bindContextArgs(block.args)) + setState(...Object.values(this.bindContextArgs(block.args))) // update the context with the data this.context = { ...this.context, @@ -135,7 +116,6 @@ export const clientStrategy = { console.log("workflowContext", this.context) - // TODO: clean this up, don't pass all those args - await this.run({ workflow: workflow.next, instanceId, api }) + await this.run(workflow.next) }, -} +}) diff --git a/packages/client/src/render/screenRouter.js b/packages/client/src/render/screenRouter.js index 58a2277843..afcb3049cf 100644 --- a/packages/client/src/render/screenRouter.js +++ b/packages/client/src/render/screenRouter.js @@ -1,5 +1,6 @@ import regexparam from "regexparam" import { routerStore } from "../state/store"; +import { initRouteStore } from "../state/store" // TODO: refactor export const screenRouter = ({ screens, onScreenSelected, appRootPath }) => { diff --git a/packages/server/src/api/controllers/workflow/actions/CREATE_USER.js b/packages/server/src/api/controllers/workflow/actions/CREATE_USER.js new file mode 100644 index 0000000000..96b0fa2584 --- /dev/null +++ b/packages/server/src/api/controllers/workflow/actions/CREATE_USER.js @@ -0,0 +1,26 @@ +const userController = require("../../user") + +module.exports = async function createUser(user) { + console.log("SAVING this user", user) + + const ctx = { + params: { + instanceId: "inst_60dd510_700f7dc06735403e81d5af91072d7241", + }, + request: { + body: user + }, + } + + try { + const response = await userController.create(ctx) + return { + user: response + } + } catch (err) { + console.error(err); + return { + user: null + } + } +} diff --git a/packages/server/src/utilities/accessLevels.js b/packages/server/src/utilities/accessLevels.js index 56e1647c31..9fff76e531 100644 --- a/packages/server/src/utilities/accessLevels.js +++ b/packages/server/src/utilities/accessLevels.js @@ -2,9 +2,11 @@ const viewController = require("../api/controllers/view") const modelController = require("../api/controllers/model") const workflowController = require("../api/controllers/workflow") +// Access Level IDs const ADMIN_LEVEL_ID = "ADMIN" const POWERUSER_LEVEL_ID = "POWER_USER" +// Permissions const READ_MODEL = "read-model" const WRITE_MODEL = "write-model" const READ_VIEW = "read-view" diff --git a/packages/server/src/utilities/initialiseClientDb.js b/packages/server/src/utilities/initialiseClientDb.js deleted file mode 100644 index e69de29bb2..0000000000