Merge pull request #2153 from automatisch/aut-1338-createActionStep
refactor(flow): distribute createActionStep logic to different methods in model
This commit is contained in:
@@ -6,7 +6,7 @@ export default async (request, response) => {
|
|||||||
.findById(request.params.flowId)
|
.findById(request.params.flowId)
|
||||||
.throwIfNotFound();
|
.throwIfNotFound();
|
||||||
|
|
||||||
const createdActionStep = await flow.createActionStep(
|
const createdActionStep = await flow.createStepAfter(
|
||||||
request.body.previousStepId
|
request.body.previousStepId
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@@ -153,27 +153,41 @@ class Flow extends Base {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async createActionStep(previousStepId) {
|
async getStepById(stepId) {
|
||||||
const previousStep = await this.$relatedQuery('steps')
|
return await this.$relatedQuery('steps').findById(stepId).throwIfNotFound();
|
||||||
.findById(previousStepId)
|
}
|
||||||
.throwIfNotFound();
|
|
||||||
|
|
||||||
const createdStep = await this.$relatedQuery('steps').insertAndFetch({
|
async insertActionStepAtPosition(position) {
|
||||||
|
return await this.$relatedQuery('steps').insertAndFetch({
|
||||||
type: 'action',
|
type: 'action',
|
||||||
position: previousStep.position + 1,
|
position,
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const nextSteps = await this.$relatedQuery('steps')
|
async getStepsAfterPosition(position) {
|
||||||
.where('position', '>=', createdStep.position)
|
return await this.$relatedQuery('steps').where('position', '>', position);
|
||||||
.whereNot('id', createdStep.id);
|
}
|
||||||
|
|
||||||
const nextStepQueries = nextSteps.map(async (nextStep, index) => {
|
async updateStepPositionsFrom(startPosition, steps) {
|
||||||
return await nextStep.$query().patchAndFetch({
|
const stepPositionUpdates = steps.map(async (step, index) => {
|
||||||
position: createdStep.position + index + 1,
|
return await step.$query().patch({
|
||||||
|
position: startPosition + index,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
await Promise.all(nextStepQueries);
|
return await Promise.all(stepPositionUpdates);
|
||||||
|
}
|
||||||
|
|
||||||
|
async createStepAfter(previousStepId) {
|
||||||
|
const previousStep = await this.getStepById(previousStepId);
|
||||||
|
|
||||||
|
const nextSteps = await this.getStepsAfterPosition(previousStep.position);
|
||||||
|
|
||||||
|
const createdStep = await this.insertActionStepAtPosition(
|
||||||
|
previousStep.position + 1
|
||||||
|
);
|
||||||
|
|
||||||
|
await this.updateStepPositionsFrom(createdStep.position + 1, nextSteps);
|
||||||
|
|
||||||
return createdStep;
|
return createdStep;
|
||||||
}
|
}
|
||||||
|
@@ -245,7 +245,69 @@ describe('Flow model', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it.todo('createActionStep');
|
it('getStepById should return the step with the given ID from the flow', async () => {
|
||||||
|
const flow = await createFlow();
|
||||||
|
|
||||||
|
const step = await createStep({ flowId: flow.id });
|
||||||
|
|
||||||
|
expect(await flow.getStepById(step.id)).toStrictEqual(step);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('insertActionStepAtPosition should insert action step at given position', async () => {
|
||||||
|
const flow = await createFlow();
|
||||||
|
|
||||||
|
await flow.createInitialSteps();
|
||||||
|
|
||||||
|
const createdStep = await flow.insertActionStepAtPosition(2);
|
||||||
|
|
||||||
|
expect(createdStep).toMatchObject({
|
||||||
|
type: 'action',
|
||||||
|
position: 2,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('getStepsAfterPosition should return steps after the given position', async () => {
|
||||||
|
const flow = await createFlow();
|
||||||
|
|
||||||
|
await flow.createInitialSteps();
|
||||||
|
|
||||||
|
await createStep({ flowId: flow.id });
|
||||||
|
|
||||||
|
expect(await flow.getStepsAfterPosition(1)).toMatchObject([
|
||||||
|
{ position: 2 },
|
||||||
|
{ position: 3 },
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('updateStepPositionsFrom', async () => {
|
||||||
|
const flow = await createFlow();
|
||||||
|
|
||||||
|
await createStep({ type: 'trigger', flowId: flow.id, position: 6 });
|
||||||
|
await createStep({ type: 'action', flowId: flow.id, position: 8 });
|
||||||
|
await createStep({ type: 'action', flowId: flow.id, position: 10 });
|
||||||
|
|
||||||
|
await flow.updateStepPositionsFrom(2, await flow.$relatedQuery('steps'));
|
||||||
|
|
||||||
|
expect(await flow.$relatedQuery('steps')).toMatchObject([
|
||||||
|
{ position: 2, type: 'trigger' },
|
||||||
|
{ position: 3, type: 'action' },
|
||||||
|
{ position: 4, type: 'action' },
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('createStepAfter should create an action step after given step ID', async () => {
|
||||||
|
const flow = await createFlow();
|
||||||
|
|
||||||
|
const triggerStep = await createStep({ type: 'trigger', flowId: flow.id });
|
||||||
|
const actionStep = await createStep({ type: 'action', flowId: flow.id });
|
||||||
|
|
||||||
|
const createdStep = await flow.createStepAfter(triggerStep.id);
|
||||||
|
|
||||||
|
const refetchedActionStep = await actionStep.$query();
|
||||||
|
|
||||||
|
expect(createdStep).toMatchObject({ type: 'action', position: 2 });
|
||||||
|
expect(refetchedActionStep.position).toBe(3);
|
||||||
|
});
|
||||||
|
|
||||||
it.todo('delete');
|
it.todo('delete');
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user