Files
automatisch/packages/backend/src/graphql/mutations/delete-step.js
2024-01-04 18:04:54 +01:00

36 lines
832 B
JavaScript

const deleteStep = async (_parent, params, context) => {
context.currentUser.can('update', 'Flow');
const step = await context.currentUser
.$relatedQuery('steps')
.withGraphFetched('flow')
.findOne({
'steps.id': params.input.id,
})
.throwIfNotFound();
await step.$relatedQuery('executionSteps').delete();
await step.$query().delete();
const nextSteps = await step.flow
.$relatedQuery('steps')
.where('position', '>', step.position);
const nextStepQueries = nextSteps.map(async (nextStep) => {
await nextStep.$query().patch({
position: nextStep.position - 1,
});
});
await Promise.all(nextStepQueries);
step.flow = await step.flow
.$query()
.withGraphJoined('steps')
.orderBy('steps.position', 'asc');
return step;
};
export default deleteStep;