feat: Use active field with flows

This commit is contained in:
Faruk AYDIN
2022-02-18 12:40:01 +03:00
committed by Ömer Faruk Aydın
parent e996e5d0c6
commit 3457ad29d7
2 changed files with 31 additions and 1 deletions

View File

@@ -1,3 +1,4 @@
import { ModelOptions, QueryContext, ValidationError } from 'objection';
import Base from './base';
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;