diff --git a/packages/backend/src/graphql/mutations/create-step.ts b/packages/backend/src/graphql/mutations/create-step.ts index 66d325d2..fdb8ea76 100644 --- a/packages/backend/src/graphql/mutations/create-step.ts +++ b/packages/backend/src/graphql/mutations/create-step.ts @@ -14,6 +14,9 @@ type Params = { connection: { id: number; }; + previousStep: { + id: number; + }; }; }; @@ -30,15 +33,38 @@ const createStepResolver = async ( }) .throwIfNotFound(); + const previousStep = await Step.query() + .findOne({ + id: input.previousStep.id, + flow_id: flow.id, + }) + .throwIfNotFound(); + const step = await Step.query().insertAndFetch({ flowId: flow.id, key: input.key, appKey: input.appKey, type: 'action', connectionId: input.connection?.id, - position: 1, + position: previousStep.position + 1, }); + const nextSteps = await Step.query() + .where({ + flow_id: flow.id, + }) + .andWhere('position', '>=', step.position) + .whereNot('id', step.id); + + const nextStepQueries = nextSteps.map(async (nextStep, index) => { + await nextStep.$query().patchAndFetch({ + ...nextStep, + position: step.position + index + 1, + }); + }); + + await Promise.all(nextStepQueries); + return step; }; diff --git a/packages/backend/src/graphql/types/step.ts b/packages/backend/src/graphql/types/step.ts index e2b974ee..09a2dea0 100644 --- a/packages/backend/src/graphql/types/step.ts +++ b/packages/backend/src/graphql/types/step.ts @@ -40,18 +40,26 @@ export const stepInputType = new GraphQLInputObjectType({ name: 'StepConnectionInput', fields: { id: { type: GraphQLInt }, - } - }) + }, + }), }, flow: { type: new GraphQLInputObjectType({ name: 'StepFlowInput', fields: { id: { type: GraphQLInt }, - } - }) - } - } -}) + }, + }), + }, + previousStep: { + type: new GraphQLInputObjectType({ + name: 'PreviousStepInput', + fields: { + id: { type: GraphQLInt }, + }, + }), + }, + }, +}); export default stepType;