chore: Add step validation to import step API endpoint
This commit is contained in:
@@ -1,11 +1,15 @@
|
||||
import { renderObject } from '../../../../helpers/renderer.js';
|
||||
import importFlow from '../../../../helpers/import-flow.js';
|
||||
|
||||
export default async (request, response) => {
|
||||
const flow = await importFlow(request.currentUser, flowParams(request));
|
||||
export default async function importFlowController(request, response) {
|
||||
const flow = await importFlow(
|
||||
request.currentUser,
|
||||
flowParams(request),
|
||||
response
|
||||
);
|
||||
|
||||
return renderObject(response, flow, { status: 201 });
|
||||
};
|
||||
}
|
||||
|
||||
const flowParams = (request) => {
|
||||
return {
|
||||
|
||||
@@ -325,4 +325,31 @@ describe('POST /api/v1/flows/import', () => {
|
||||
`{{step.${newTriggerStepId}.query.sample}}`
|
||||
);
|
||||
});
|
||||
|
||||
it('should throw an error in case there is no trigger step', async () => {
|
||||
const currentUserFlow = await createFlow({ userId: currentUser.id });
|
||||
|
||||
await createPermission({
|
||||
action: 'create',
|
||||
subject: 'Flow',
|
||||
roleId: currentUserRole.id,
|
||||
conditions: ['isCreator'],
|
||||
});
|
||||
|
||||
const importFlowData = {
|
||||
id: currentUserFlow.id,
|
||||
name: currentUserFlow.name,
|
||||
steps: [],
|
||||
};
|
||||
|
||||
const response = await request(app)
|
||||
.post('/api/v1/flows/import')
|
||||
.set('Authorization', token)
|
||||
.send(importFlowData)
|
||||
.expect(422);
|
||||
|
||||
expect(response.body.errors.steps).toStrictEqual([
|
||||
'The first step must be a trigger!',
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,10 +1,23 @@
|
||||
import Crypto from 'crypto';
|
||||
import Step from '../models/step.js';
|
||||
import { renderObjectionError } from './renderer.js';
|
||||
|
||||
const importFlow = async (user, flowData) => {
|
||||
const newFlowId = Crypto.randomUUID();
|
||||
const importFlow = async (user, flowData, response) => {
|
||||
const steps = flowData.steps || [];
|
||||
|
||||
// Validation: the first step must be a trigger
|
||||
if (!steps.length || steps[0].type !== 'trigger') {
|
||||
return renderObjectionError(response, {
|
||||
statusCode: 422,
|
||||
type: 'ValidationError',
|
||||
data: {
|
||||
steps: [{ message: 'The first step must be a trigger!' }],
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
const newFlowId = Crypto.randomUUID();
|
||||
|
||||
const newFlow = await user.$relatedQuery('flows').insertAndFetch({
|
||||
id: newFlowId,
|
||||
name: flowData.name,
|
||||
|
||||
Reference in New Issue
Block a user