feat: Create step with previous step id and assign position

This commit is contained in:
Faruk AYDIN
2022-01-28 16:24:46 +03:00
committed by Ömer Faruk Aydın
parent 95a63affe7
commit 3c2188ebf2
2 changed files with 42 additions and 8 deletions

View File

@@ -14,6 +14,9 @@ type Params = {
connection: { connection: {
id: number; id: number;
}; };
previousStep: {
id: number;
};
}; };
}; };
@@ -30,15 +33,38 @@ const createStepResolver = async (
}) })
.throwIfNotFound(); .throwIfNotFound();
const previousStep = await Step.query()
.findOne({
id: input.previousStep.id,
flow_id: flow.id,
})
.throwIfNotFound();
const step = await Step.query().insertAndFetch({ const step = await Step.query().insertAndFetch({
flowId: flow.id, flowId: flow.id,
key: input.key, key: input.key,
appKey: input.appKey, appKey: input.appKey,
type: 'action', type: 'action',
connectionId: input.connection?.id, 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; return step;
}; };

View File

@@ -40,18 +40,26 @@ export const stepInputType = new GraphQLInputObjectType({
name: 'StepConnectionInput', name: 'StepConnectionInput',
fields: { fields: {
id: { type: GraphQLInt }, id: { type: GraphQLInt },
} },
}) }),
}, },
flow: { flow: {
type: new GraphQLInputObjectType({ type: new GraphQLInputObjectType({
name: 'StepFlowInput', name: 'StepFlowInput',
fields: { fields: {
id: { type: GraphQLInt }, id: { type: GraphQLInt },
} },
}) }),
} },
} previousStep: {
}) type: new GraphQLInputObjectType({
name: 'PreviousStepInput',
fields: {
id: { type: GraphQLInt },
},
}),
},
},
});
export default stepType; export default stepType;