From 09695fabd6983dcbab5f39bd1e8ab0d7383172c3 Mon Sep 17 00:00:00 2001 From: Peter Clement Date: Tue, 22 Oct 2024 10:52:52 +0100 Subject: [PATCH] extract necessary user bindings and add types --- .../server/src/api/controllers/automation.ts | 7 +++--- .../server/src/api/controllers/row/index.ts | 22 +++++++++++++++---- packages/server/src/automations/triggers.ts | 6 ++--- .../server/src/definitions/automations.ts | 4 ++-- packages/server/src/sdk/users/utils.ts | 3 ++- packages/server/src/threads/automation.ts | 4 ++-- packages/types/src/documents/global/user.ts | 10 +++++++++ packages/types/src/sdk/automations/index.ts | 9 ++++++-- 8 files changed, 46 insertions(+), 19 deletions(-) diff --git a/packages/server/src/api/controllers/automation.ts b/packages/server/src/api/controllers/automation.ts index df23f9f3b7..b19218647b 100644 --- a/packages/server/src/api/controllers/automation.ts +++ b/packages/server/src/api/controllers/automation.ts @@ -13,7 +13,6 @@ import { UserCtx, DeleteAutomationResponse, FetchAutomationResponse, - User, } from "@budibase/types" import { getActionDefinitions as actionDefs } from "../../automations/actions" import sdk from "../../sdk" @@ -160,7 +159,7 @@ export async function trigger(ctx: UserCtx) { automation, { fields: ctx.request.body.fields, - user: ctx.user as User, + user: sdk.users.getUserContextBindings(ctx.user), timeout: ctx.request.body.timeout * 1000 || env.AUTOMATION_THREAD_TIMEOUT, }, @@ -185,7 +184,7 @@ export async function trigger(ctx: UserCtx) { await triggers.externalTrigger(automation, { ...ctx.request.body, appId: ctx.appId, - user: ctx.user as User, + user: sdk.users.getUserContextBindings(ctx.user), }) ctx.body = { message: `Automation ${automation._id} has been triggered.`, @@ -215,7 +214,7 @@ export async function test(ctx: UserCtx) { { ...testInput, appId: ctx.appId, - user: ctx.user, + user: sdk.users.getUserContextBindings(ctx.user), }, { getResponses: true } ) diff --git a/packages/server/src/api/controllers/row/index.ts b/packages/server/src/api/controllers/row/index.ts index 67502f8b36..33599a0119 100644 --- a/packages/server/src/api/controllers/row/index.ts +++ b/packages/server/src/api/controllers/row/index.ts @@ -71,7 +71,7 @@ export async function patch( row, table, oldRow, - ctx.user + sdk.users.getUserContextBindings(ctx.user) ) ctx.message = `${table.name} updated successfully.` ctx.body = row @@ -104,7 +104,14 @@ export const save = async (ctx: UserCtx) => { ) ctx.status = 200 ctx.eventEmitter && - ctx.eventEmitter.emitRow(`row:save`, appId, row, table, null, ctx.user) + ctx.eventEmitter.emitRow( + `row:save`, + appId, + row, + table, + null, + sdk.users.getUserContextBindings(ctx.user) + ) ctx.message = `${table.name} saved successfully` // prefer squashed for response ctx.body = row || squashed @@ -177,7 +184,14 @@ async function deleteRows(ctx: UserCtx) { for (let row of rows) { ctx.eventEmitter && - ctx.eventEmitter.emitRow(`row:delete`, appId, row, null, null, ctx.user) + ctx.eventEmitter.emitRow( + `row:delete`, + appId, + row, + null, + null, + sdk.users.getUserContextBindings(ctx.user) + ) gridSocket?.emitRowDeletion(ctx, row) } @@ -200,7 +214,7 @@ async function deleteRow(ctx: UserCtx) { resp.row, null, null, - ctx.user + sdk.users.getUserContextBindings(ctx.user) ) gridSocket?.emitRowDeletion(ctx, resp.row) diff --git a/packages/server/src/automations/triggers.ts b/packages/server/src/automations/triggers.ts index efcbf27644..e3b7318211 100644 --- a/packages/server/src/automations/triggers.ts +++ b/packages/server/src/automations/triggers.ts @@ -19,7 +19,7 @@ import { AutomationStoppedReason, AutomationStatus, AutomationRowEvent, - User, + UserBindings, } from "@budibase/types" import { executeInThread } from "../threads/automation" import { dataFilters, sdk } from "@budibase/shared-core" @@ -145,11 +145,10 @@ export async function externalTrigger( fields: Record timeout?: number appId?: string - user?: User + user?: UserBindings | undefined }, { getResponses }: { getResponses?: boolean } = {} ): Promise { - console.log("user: " + params.user) if (automation.disabled) { throw new Error("Automation is disabled") } @@ -196,7 +195,6 @@ export async function externalTrigger( appId: context.getAppId(), automation, } - console.log(data) return executeInThread({ data } as AutomationJob) } else { return automationQueue.add(data, JOB_OPTS) diff --git a/packages/server/src/definitions/automations.ts b/packages/server/src/definitions/automations.ts index 70faed9327..d551584fd1 100644 --- a/packages/server/src/definitions/automations.ts +++ b/packages/server/src/definitions/automations.ts @@ -1,4 +1,4 @@ -import { AutomationResults, LoopStepType, User } from "@budibase/types" +import { AutomationResults, LoopStepType, UserBindings } from "@budibase/types" export interface LoopInput { option: LoopStepType @@ -18,6 +18,6 @@ export interface AutomationContext extends AutomationResults { stepsById: Record stepsByName: Record env?: Record - user?: User + user?: UserBindings trigger: any } diff --git a/packages/server/src/sdk/users/utils.ts b/packages/server/src/sdk/users/utils.ts index 74389a1444..8266d3e28a 100644 --- a/packages/server/src/sdk/users/utils.ts +++ b/packages/server/src/sdk/users/utils.ts @@ -12,6 +12,7 @@ import { UserMetadata, Database, ContextUserMetadata, + UserBindings, } from "@budibase/types" export function combineMetadataAndUser( @@ -125,7 +126,7 @@ export async function syncGlobalUsers() { } } -export function getUserContextBindings(user: ContextUser) { +export function getUserContextBindings(user: ContextUser): UserBindings { if (!user) { return {} } diff --git a/packages/server/src/threads/automation.ts b/packages/server/src/threads/automation.ts index a75c0d2870..0f17b44424 100644 --- a/packages/server/src/threads/automation.ts +++ b/packages/server/src/threads/automation.ts @@ -26,7 +26,7 @@ import { BranchStep, LoopStep, SearchFilters, - User, + UserBindings, } from "@budibase/types" import { AutomationContext, TriggerOutput } from "../definitions/automations" import { WorkerCallback } from "./definitions" @@ -76,7 +76,7 @@ class Orchestrator { private loopStepOutputs: LoopStep[] private stopped: boolean private executionOutput: Omit - private currentUser: User | undefined + private currentUser: UserBindings | undefined constructor(job: AutomationJob) { let automation = job.data.automation diff --git a/packages/types/src/documents/global/user.ts b/packages/types/src/documents/global/user.ts index a1c5b2506f..85641bf4c5 100644 --- a/packages/types/src/documents/global/user.ts +++ b/packages/types/src/documents/global/user.ts @@ -68,6 +68,16 @@ export interface User extends Document { appSort?: string } +export interface UserBindings extends Document { + firstName?: string + lastName?: string + email?: string + status?: string + roleId?: string | undefined | null + globalId?: string + userId?: string +} + export enum UserStatus { ACTIVE = "active", INACTIVE = "inactive", diff --git a/packages/types/src/sdk/automations/index.ts b/packages/types/src/sdk/automations/index.ts index f5c57b54d8..9ceded03ee 100644 --- a/packages/types/src/sdk/automations/index.ts +++ b/packages/types/src/sdk/automations/index.ts @@ -1,4 +1,9 @@ -import { Automation, AutomationMetadata, Row, User } from "../../documents" +import { + Automation, + AutomationMetadata, + Row, + UserBindings, +} from "../../documents" import { Job } from "bull" export interface AutomationDataEvent { @@ -8,7 +13,7 @@ export interface AutomationDataEvent { timeout?: number row?: Row oldRow?: Row - user?: User + user?: UserBindings } export interface AutomationData {