feat: Adjust positions and return all steps after deletion of the step
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import { GraphQLString, GraphQLNonNull, GraphQLBoolean } from 'graphql';
|
import { GraphQLString, GraphQLNonNull } from 'graphql';
|
||||||
import Step from '../../models/step';
|
|
||||||
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
|
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
|
||||||
|
import stepType from '../types/step';
|
||||||
|
|
||||||
type Params = {
|
type Params = {
|
||||||
id: string;
|
id: string;
|
||||||
@@ -10,19 +10,39 @@ const deleteStepResolver = async (
|
|||||||
params: Params,
|
params: Params,
|
||||||
req: RequestWithCurrentUser
|
req: RequestWithCurrentUser
|
||||||
) => {
|
) => {
|
||||||
await req.currentUser
|
const step = await req.currentUser
|
||||||
.$relatedQuery('steps')
|
.$relatedQuery('steps')
|
||||||
.delete()
|
.withGraphFetched('flow')
|
||||||
.findOne({
|
.findOne({
|
||||||
id: params.id,
|
'steps.id': params.id,
|
||||||
})
|
})
|
||||||
.throwIfNotFound();
|
.throwIfNotFound();
|
||||||
|
|
||||||
return;
|
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({
|
||||||
|
...nextStep,
|
||||||
|
position: nextStep.position - 1,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
await Promise.all(nextStepQueries);
|
||||||
|
|
||||||
|
step.flow = await step.flow
|
||||||
|
.$query()
|
||||||
|
.withGraphJoined('steps')
|
||||||
|
.orderBy('steps.position', 'asc');
|
||||||
|
|
||||||
|
return step;
|
||||||
};
|
};
|
||||||
|
|
||||||
const deleteStep = {
|
const deleteStep = {
|
||||||
type: GraphQLBoolean,
|
type: stepType,
|
||||||
args: {
|
args: {
|
||||||
id: { type: GraphQLNonNull(GraphQLString) },
|
id: { type: GraphQLNonNull(GraphQLString) },
|
||||||
},
|
},
|
||||||
|
@@ -16,8 +16,8 @@ const rootQuery = new GraphQLObjectType({
|
|||||||
getAppConnections,
|
getAppConnections,
|
||||||
testConnection,
|
testConnection,
|
||||||
getFlow,
|
getFlow,
|
||||||
getFlows
|
getFlows,
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
export default rootQuery;
|
export default rootQuery;
|
||||||
|
@@ -5,17 +5,21 @@ import {
|
|||||||
GraphQLString,
|
GraphQLString,
|
||||||
GraphQLBoolean,
|
GraphQLBoolean,
|
||||||
} from 'graphql';
|
} from 'graphql';
|
||||||
import StepType from './step';
|
|
||||||
|
|
||||||
const flowType = new GraphQLObjectType({
|
const flowType = new GraphQLObjectType({
|
||||||
name: 'Flow',
|
name: 'Flow',
|
||||||
fields: {
|
fields: () => {
|
||||||
id: { type: GraphQLString },
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||||
name: { type: GraphQLString },
|
const StepType = require('./step').default;
|
||||||
active: { type: GraphQLBoolean },
|
|
||||||
steps: {
|
return {
|
||||||
type: GraphQLList(StepType),
|
id: { type: GraphQLString },
|
||||||
},
|
name: { type: GraphQLString },
|
||||||
|
active: { type: GraphQLBoolean },
|
||||||
|
steps: {
|
||||||
|
type: GraphQLList(StepType),
|
||||||
|
},
|
||||||
|
};
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@@ -9,23 +9,29 @@ import ConnectionType from './connection';
|
|||||||
|
|
||||||
const stepType = new GraphQLObjectType({
|
const stepType = new GraphQLObjectType({
|
||||||
name: 'Step',
|
name: 'Step',
|
||||||
fields: {
|
fields: () => {
|
||||||
id: { type: GraphQLString },
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||||
previousStepId: { type: GraphQLString },
|
const FlowType = require('./flow').default;
|
||||||
key: { type: GraphQLString },
|
|
||||||
appKey: { type: GraphQLString },
|
return {
|
||||||
type: {
|
id: { type: GraphQLString },
|
||||||
type: new GraphQLEnumType({
|
previousStepId: { type: GraphQLString },
|
||||||
name: 'StepEnumType',
|
key: { type: GraphQLString },
|
||||||
values: {
|
appKey: { type: GraphQLString },
|
||||||
trigger: { value: 'trigger' },
|
type: {
|
||||||
action: { value: 'action' },
|
type: new GraphQLEnumType({
|
||||||
},
|
name: 'StepEnumType',
|
||||||
}),
|
values: {
|
||||||
},
|
trigger: { value: 'trigger' },
|
||||||
parameters: { type: GraphQLString },
|
action: { value: 'action' },
|
||||||
connection: { type: ConnectionType },
|
},
|
||||||
position: { type: GraphQLInt },
|
}),
|
||||||
|
},
|
||||||
|
parameters: { type: GraphQLString },
|
||||||
|
connection: { type: ConnectionType },
|
||||||
|
flow: { type: FlowType },
|
||||||
|
position: { type: GraphQLInt },
|
||||||
|
};
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@@ -17,6 +17,7 @@ class Step extends Base {
|
|||||||
position: number;
|
position: number;
|
||||||
parameters: string;
|
parameters: string;
|
||||||
connection?: Connection;
|
connection?: Connection;
|
||||||
|
flow?: Flow;
|
||||||
|
|
||||||
static tableName = 'steps';
|
static tableName = 'steps';
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user