Skip to content

Commit

Permalink
feat(plugin-flow-builder): update session in local development with o…
Browse files Browse the repository at this point in the history
…rganization_id and bot_id
  • Loading branch information
Iru89 committed Oct 2, 2024
1 parent b775d2c commit aaaae68
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
19 changes: 17 additions & 2 deletions packages/botonic-plugin-flow-builder/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ import {
HtPayloadNode,
} from './content-fields/hubtype-fields'
import { HtSmartIntentNode } from './content-fields/hubtype-fields/smart-intent'
import { FlowBuilderApiOptions } from './types'
import { FlowBuilderApiOptions, ProcessEnvNodeEnvs } from './types'

export class FlowBuilderApi {
url: string
flowUrl: string
flow: HtFlowBuilderData
request: PluginPreRequest

Expand All @@ -30,19 +31,33 @@ export class FlowBuilderApi {
const newApi = new FlowBuilderApi()

newApi.url = options.url
newApi.flowUrl = options.flowUrl
newApi.flow = options.flow ?? (await newApi.getFlow(options.accessToken))
newApi.request = options.request

if (process.env.NODE_ENV === ProcessEnvNodeEnvs.DEVELOPMENT) {
await newApi.updateSessionWithUserInfo(options.accessToken)
}

return newApi
}

private async getFlow(token: string): Promise<HtFlowBuilderData> {
const { data } = await axios.get(this.url, {
const { data } = await axios.get(this.flowUrl, {
headers: { Authorization: `Bearer ${token}` },
})
return data as HtFlowBuilderData
}

private async updateSessionWithUserInfo(token: string) {
const url = `${this.url}/get-user-info`
const response = await axios.get(url, {
headers: { Authorization: `Bearer ${token}` },
})
this.request.session.organization_id = response.data.organization_id
this.request.session.bot.id = response.data.bot_id
}

getNodeByFlowId(id: string): HtNodeWithContent {
const subFlow = this.flow.flows.find(subFlow => subFlow.id === id)
if (!subFlow) throw Error(`SubFlow with id: '${id}' not found`)
Expand Down
3 changes: 2 additions & 1 deletion packages/botonic-plugin-flow-builder/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ export default class BotonicPluginFlowBuilder implements Plugin {
async pre(request: PluginPreRequest): Promise<void> {
this.currentRequest = request
this.cmsApi = await FlowBuilderApi.create({
url: this.resolveFlowUrl(request),
flowUrl: this.resolveFlowUrl(request),
url: this.apiUrl,
flow: this.flow,
accessToken: this.getAccessToken(request.session),
request: this.currentRequest,
Expand Down
1 change: 1 addition & 0 deletions packages/botonic-plugin-flow-builder/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export type KnowledgeBaseFunction = (

export interface FlowBuilderApiOptions {
url: string
flowUrl: string
flow?: HtFlowBuilderData
accessToken: string
request: PluginPreRequest
Expand Down

0 comments on commit aaaae68

Please sign in to comment.