feat: Use active field with flows
This commit is contained in:

committed by
Ömer Faruk Aydın

parent
e996e5d0c6
commit
3457ad29d7
@@ -1,10 +1,11 @@
|
|||||||
import { GraphQLString, GraphQLNonNull } from 'graphql';
|
import { GraphQLString, GraphQLNonNull, GraphQLBoolean } from 'graphql';
|
||||||
import flowType from '../types/flow';
|
import flowType from '../types/flow';
|
||||||
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
|
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
|
||||||
|
|
||||||
type Params = {
|
type Params = {
|
||||||
id: string;
|
id: string;
|
||||||
name: string;
|
name: string;
|
||||||
|
active: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
const updateFlowResolver = async (
|
const updateFlowResolver = async (
|
||||||
@@ -31,6 +32,7 @@ const updateFlow = {
|
|||||||
args: {
|
args: {
|
||||||
id: { type: GraphQLNonNull(GraphQLString) },
|
id: { type: GraphQLNonNull(GraphQLString) },
|
||||||
name: { type: GraphQLNonNull(GraphQLString) },
|
name: { type: GraphQLNonNull(GraphQLString) },
|
||||||
|
active: { type: GraphQLBoolean },
|
||||||
},
|
},
|
||||||
resolve: (_: any, params: Params, req: RequestWithCurrentUser) =>
|
resolve: (_: any, params: Params, req: RequestWithCurrentUser) =>
|
||||||
updateFlowResolver(params, req),
|
updateFlowResolver(params, req),
|
||||||
|
@@ -1,3 +1,4 @@
|
|||||||
|
import { ModelOptions, QueryContext, ValidationError } from 'objection';
|
||||||
import Base from './base';
|
import Base from './base';
|
||||||
import Step from './step';
|
import Step from './step';
|
||||||
|
|
||||||
@@ -31,6 +32,33 @@ class Flow extends Base {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
async $beforeUpdate(): Promise<void> {
|
||||||
|
if (!this.active) return;
|
||||||
|
|
||||||
|
const incompleteStep = await this.$relatedQuery('steps').findOne({
|
||||||
|
status: 'incomplete',
|
||||||
|
});
|
||||||
|
|
||||||
|
if (incompleteStep) {
|
||||||
|
throw new ValidationError({
|
||||||
|
message: 'All steps should be completed before updating flow status!',
|
||||||
|
type: 'incompleteStepsError',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const allSteps = await this.$relatedQuery('steps');
|
||||||
|
|
||||||
|
if (allSteps.length < 2) {
|
||||||
|
throw new ValidationError({
|
||||||
|
message:
|
||||||
|
'There should be at least one trigger and one action steps in the flow!',
|
||||||
|
type: 'insufficientStepsError',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Flow;
|
export default Flow;
|
||||||
|
Reference in New Issue
Block a user