Skip to content

Commit 750102a

Browse files
committed
fix: dont check for valid key /appkey in create-step
1 parent 6d74a0e commit 750102a

File tree

4 files changed

+59
-24
lines changed

4 files changed

+59
-24
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
@@ -138,8 +138,8 @@ describe('createStep mutation integration tests', async () => {
138138
updatedAt: testFlowTimestampString,
139139
},
140140
previousStep: { id: existingSteps[0].id },
141-
key: 'newStep',
142-
appKey: 'test-app',
141+
key: 'sendMessage',
142+
appKey: 'telegram-bot',
143143
parameters: { newParam: 'value' },
144144
},
145145
}
@@ -149,8 +149,8 @@ describe('createStep mutation integration tests', async () => {
149149
// Ensure the new step is returned as expected.
150150
expect(newStep).toBeDefined()
151151
expect(newStep.type).toBe('action')
152-
expect(newStep.key).toBe('newStep')
153-
expect(newStep.appKey).toBe('test-app')
152+
expect(newStep.key).toBe('sendMessage')
153+
expect(newStep.appKey).toBe('telegram-bot')
154154
// New step's position should be previousStep.position + 1.
155155
expect(newStep.position).toBe(existingSteps[0].position + 1)
156156

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,22 +206,45 @@ describe('updateStep mutation', () => {
206206
it('should set status to incomplete when key or appKey changes', async () => {
207207
const input = {
208208
...genericInputParams,
209-
key: 'newKey',
209+
key: 'sendMessage',
210+
appKey: 'telegram-bot',
211+
// remove connection from input for testing purposes
212+
connection: { id: null } as any,
210213
}
211214

212215
await updateStep(null, { input }, context)
213216

214217
expect(patchAndFetchByIdSpy).toHaveBeenCalledWith(mockStepId, {
215-
key: 'newKey',
216-
appKey: 'postman',
217-
connectionId: mockConnectionId,
218+
key: 'sendMessage',
219+
appKey: 'telegram-bot',
220+
connectionId: null,
218221
parameters: { testParam: 'value' },
219222
status: 'incomplete',
220223
config: {},
221224
updatedBy: context.currentUser.id,
222225
})
223226
})
224227

228+
it('should throw an error if the key or appKey is not found', async () => {
229+
const input = {
230+
...genericInputParams,
231+
key: 'invalidKey',
232+
}
233+
234+
await expect(updateStep(null, { input }, context)).rejects.toThrow(
235+
BadUserInputError,
236+
)
237+
238+
const input2 = {
239+
...genericInputParams,
240+
appKey: 'invalidAppKey',
241+
}
242+
243+
await expect(updateStep(null, { input: input2 }, context)).rejects.toThrow(
244+
BadUserInputError,
245+
)
246+
})
247+
225248
it('should set status to incomplete when explicitly requested', async () => {
226249
const input = {
227250
...genericInputParams,

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

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

18-
const triggerOrAction = await App.findTriggerOrActionByKey(
19-
input.appKey,
20-
input.key,
21-
)
22-
23-
if (!triggerOrAction) {
24-
throw new BadUserInputError('No such trigger or action')
25-
}
26-
if ('hiddenFromUser' in triggerOrAction && triggerOrAction.hiddenFromUser) {
27-
throw new BadUserInputError('Action can only be created by system')
18+
/**
19+
* appKey and key are optional, we allow creating of empty step for if-then branches
20+
*/
21+
if (input.appKey && input.key) {
22+
const triggerOrAction = await App.findTriggerOrActionByKey(
23+
input.appKey,
24+
input.key,
25+
)
26+
27+
if ('hiddenFromUser' in triggerOrAction && triggerOrAction.hiddenFromUser) {
28+
throw new BadUserInputError('Action can only be created by system')
29+
}
2830
}
29-
3031
return await Step.transaction(async (trx) => {
3132
await trx.raw('SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;')
3233

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)