Skip to content

Commit e6b1458

Browse files
committed
fix: dont check for valid key /appkey in create-step
1 parent 01ee605 commit e6b1458

File tree

4 files changed

+58
-23
lines changed

4 files changed

+58
-23
lines changed

packages/backend/src/graphql/__tests__/mutations/create-step.itest.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ describe('createStep mutation integration tests', async () => {
6969
input: {
7070
flow: { id: testFlow.id },
7171
previousStep: { id: existingSteps[0].id },
72-
key: 'newStep',
73-
appKey: 'test-app',
72+
key: 'sendMessage',
73+
appKey: 'telegram-bot',
7474
parameters: { newParam: 'value' },
7575
},
7676
}
@@ -80,8 +80,8 @@ describe('createStep mutation integration tests', async () => {
8080
// Ensure the new step is returned as expected.
8181
expect(newStep).toBeDefined()
8282
expect(newStep.type).toBe('action')
83-
expect(newStep.key).toBe('newStep')
84-
expect(newStep.appKey).toBe('test-app')
83+
expect(newStep.key).toBe('sendMessage')
84+
expect(newStep.appKey).toBe('telegram-bot')
8585
// New step's position should be previousStep.position + 1.
8686
expect(newStep.position).toBe(existingSteps[0].position + 1)
8787

packages/backend/src/graphql/__tests__/mutations/update-step.itest.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,21 +202,44 @@ describe('updateStep mutation', () => {
202202
it('should set status to incomplete when key or appKey changes', async () => {
203203
const input = {
204204
...genericInputParams,
205-
key: 'newKey',
205+
key: 'sendMessage',
206+
appKey: 'telegram-bot',
207+
// remove connection from input for testing purposes
208+
connection: { id: null } as any,
206209
}
207210

208211
await updateStep(null, { input }, context)
209212

210213
expect(patchAndFetchByIdSpy).toHaveBeenCalledWith(mockStepId, {
211-
key: 'newKey',
212-
appKey: 'postman',
213-
connectionId: mockConnectionId,
214+
key: 'sendMessage',
215+
appKey: 'telegram-bot',
216+
connectionId: null,
214217
parameters: { testParam: 'value' },
215218
status: 'incomplete',
216219
config: {},
217220
})
218221
})
219222

223+
it('should throw an error if the key or appKey is not found', async () => {
224+
const input = {
225+
...genericInputParams,
226+
key: 'invalidKey',
227+
}
228+
229+
await expect(updateStep(null, { input }, context)).rejects.toThrow(
230+
BadUserInputError,
231+
)
232+
233+
const input2 = {
234+
...genericInputParams,
235+
appKey: 'invalidAppKey',
236+
}
237+
238+
await expect(updateStep(null, { input: input2 }, context)).rejects.toThrow(
239+
BadUserInputError,
240+
)
241+
})
242+
220243
it('should set status to incomplete when explicitly requested', async () => {
221244
const input = {
222245
...genericInputParams,

packages/backend/src/graphql/mutations/create-step.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,19 @@ const createStep: MutationResolvers['createStep'] = async (
1313
) => {
1414
const { input } = params
1515

16-
const triggerOrAction = await App.findTriggerOrActionByKey(
17-
input.appKey,
18-
input.key,
19-
)
16+
/**
17+
* appKey and key are optional, we allow creating of empty step for if-then branches
18+
*/
19+
if (input.appKey && input.key) {
20+
const triggerOrAction = await App.findTriggerOrActionByKey(
21+
input.appKey,
22+
input.key,
23+
)
2024

21-
if (!triggerOrAction) {
22-
throw new BadUserInputError('No such trigger or action')
23-
}
24-
if ('hiddenFromUser' in triggerOrAction && triggerOrAction.hiddenFromUser) {
25-
throw new BadUserInputError('Action can only be created by system')
25+
if ('hiddenFromUser' in triggerOrAction && triggerOrAction.hiddenFromUser) {
26+
throw new BadUserInputError('Action can only be created by system')
27+
}
2628
}
27-
2829
return await Step.transaction(async (trx) => {
2930
await trx.raw('SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;')
3031

packages/backend/src/models/app.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { memoize } from 'lodash'
55
import apps from '@/apps'
66
import appInfoConverter from '@/helpers/app-info-converter'
77
import getApp from '@/helpers/get-app'
8+
import logger from '@/helpers/logger'
89

910
class App {
1011
static list = Object.keys(apps)
@@ -41,11 +42,21 @@ class App {
4142
appKey: string,
4243
key: string,
4344
): Promise<ITrigger | IAction> {
44-
const app = await this.findOneByKey(appKey)
45-
return (
46-
app.triggers?.find((trigger) => trigger.key === key) ||
47-
app.actions?.find((action) => action.key === key)
48-
)
45+
try {
46+
const app = await this.findOneByKey(appKey)
47+
return (
48+
app.triggers?.find((trigger) => trigger.key === key) ||
49+
app.actions?.find((action) => action.key === key)
50+
)
51+
} catch {
52+
logger.error({
53+
event: 'findTriggerOrActionByKey',
54+
message: 'No such trigger or action',
55+
appKey,
56+
key,
57+
})
58+
return null
59+
}
4960
}
5061

5162
static getAllAppsWithFunctions = memoize(async () => {

0 commit comments

Comments
 (0)