refactor(flow): distribute createActionStep logic to different methods in model

This commit is contained in:
Ali BARIN
2024-10-30 13:04:04 +00:00
parent b9cd7c3983
commit d32820ee09
3 changed files with 97 additions and 15 deletions

View File

@@ -151,27 +151,44 @@ class Flow extends Base {
});
}
async createActionStep(previousStepId) {
const previousStep = await this.$relatedQuery('steps')
.findById(previousStepId)
.throwIfNotFound();
async getStepById(stepId) {
return await this.$relatedQuery('steps').findById(stepId).throwIfNotFound();
}
const createdStep = await this.$relatedQuery('steps').insertAndFetch({
async insertActionStepAtPosition(position) {
return await this.$relatedQuery('steps').insertAndFetch({
type: 'action',
position: previousStep.position + 1,
position,
});
}
const nextSteps = await this.$relatedQuery('steps')
.where('position', '>=', createdStep.position)
.whereNot('id', createdStep.id);
async getStepsAfterPosition(position) {
return await this.$relatedQuery('steps').where('position', '>', position);
}
const nextStepQueries = nextSteps.map(async (nextStep, index) => {
return await nextStep.$query().patchAndFetch({
position: createdStep.position + index + 1,
async alignStepsPositionsAsOfPosition(steps, startPosition) {
const nextStepQueries = steps.map(async (nextStep, index) => {
return await nextStep.$query().patch({
position: startPosition + index,
});
});
await Promise.all(nextStepQueries);
return await Promise.all(nextStepQueries);
}
async createActionStepAfterStepId(previousStepId) {
const previousStep = await this.getStepById(previousStepId);
const nextSteps = await this.getStepsAfterPosition(previousStep.position);
const createdStep = await this.insertActionStepAtPosition(
previousStep.position + 1
);
await this.alignStepsPositionsAsOfPosition(
nextSteps,
createdStep.position + 1
);
return createdStep;
}