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

@@ -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.createActionStepAfterStepId(
request.body.previousStepId request.body.previousStepId
); );

View File

@@ -151,27 +151,44 @@ 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 alignStepsPositionsAsOfPosition(steps, startPosition) {
return await nextStep.$query().patchAndFetch({ const nextStepQueries = steps.map(async (nextStep, index) => {
position: createdStep.position + index + 1, 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; return createdStep;
} }

View File

@@ -198,7 +198,72 @@ 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('alignStepsPositionsAsOfPosition', 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.alignStepsPositionsAsOfPosition(
await flow.$relatedQuery('steps'),
1
);
expect(await flow.$relatedQuery('steps')).toMatchObject([
{ position: 1, type: 'trigger' },
{ position: 2, type: 'action' },
{ position: 3, type: 'action' },
]);
});
it('createActionStepAfterStep 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.createActionStepAfterStepId(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');