feat: write REST API endpoint to create step

This commit is contained in:
Ali BARIN
2024-09-06 17:30:10 +00:00
parent 1ce31eefc6
commit 813646e392
8 changed files with 270 additions and 0 deletions

View File

@@ -135,6 +135,31 @@ class Flow extends Base {
return this.$query().withGraphFetched('steps');
}
async createActionStep(previousStepId) {
const previousStep = await this.$relatedQuery('steps')
.findById(previousStepId)
.throwIfNotFound();
const createdStep = await this.$relatedQuery('steps').insertAndFetch({
type: 'action',
position: previousStep.position + 1,
});
const nextSteps = await this.$relatedQuery('steps')
.where('position', '>=', createdStep.position)
.whereNot('id', createdStep.id);
const nextStepQueries = nextSteps.map(async (nextStep, index) => {
return await nextStep.$query().patchAndFetch({
position: createdStep.position + index + 1,
});
});
await Promise.all(nextStepQueries);
return createdStep;
}
async $beforeUpdate(opt, queryContext) {
await super.$beforeUpdate(opt, queryContext);