simplify steps! They can have substeps. Keep clean and simple.

This commit is contained in:
Julien Calixte
2023-04-08 00:48:59 +02:00
parent 707cbcefbb
commit c641ab1033
5 changed files with 22 additions and 125 deletions

View File

@@ -1,83 +0,0 @@
import { describe, expect, it } from 'vitest'
import { Step } from '@/use-cases/task/models/step'
import { createStepFixture } from '@/use-cases/task/models/step.fixture'
describe('Step', () => {
it('adds substeps', () => {
const step = createStepFixture()
step.addSteps(createStepFixture())
expect(step.steps.length).toEqual(1)
})
it('removes substeps', () => {
const step = createStepFixture()
step.addSteps(createStepFixture())
step.removeStep(0)
expect(step.steps.length).toEqual(0)
})
it('tells the estimation based on the sum of its substeps', () => {
const step = createStepFixture()
.addSteps(
createStepFixture({
estimation: 1
})
)
.addSteps(
createStepFixture({
estimation: 2
})
)
.addSteps(
createStepFixture({
estimation: 3
})
)
expect(step.totalEstimation).toBe(6)
})
it('tells the total estimation if the step estimation is set even with substeps', () => {
const step = createStepFixture({
estimation: 8
})
.addSteps(
createStepFixture({
estimation: 1
})
)
.addSteps(
createStepFixture({
estimation: 2
})
)
expect(step.totalEstimation).toEqual(8)
})
it('flattens the substeps', () => {
const leafs = [
createStepFixture({
id: 'leaf-1'
}),
createStepFixture({
id: 'leaf-2'
}),
createStepFixture({
id: 'leaf-3'
})
]
const step = createStepFixture().addSteps(
createStepFixture().addSteps(leafs[0], leafs[1]),
createStepFixture().addSteps(createStepFixture().addSteps(leafs[2]))
)
const steps = Step.getStepLeafs([step])
expect(steps).toEqual(leafs)
})
})

View File

@@ -1,51 +1,17 @@
import type { Stepable } from '@/use-cases/task/interfaces/stepable'
export class Step implements Stepable {
private _steps: Step[] = []
constructor(
readonly id: string,
readonly title: string,
readonly estimation?: number
readonly estimation: number
) {
return this
}
public get steps() {
return this._steps
}
public addSteps(...steps: Stepable[]) {
this._steps.push(...Step.fromStepable(...steps))
return this
}
public removeStep(index: number) {
if (index < 0 || index >= this._steps.length) {
return
}
this._steps.splice(index)
}
public get totalEstimation(): number {
return (
this.estimation ??
this._steps.reduce((acc, step) => acc + step.totalEstimation, 0)
)
}
public static fromStepable(...stepables: Stepable[]): Step[] {
return stepables.map((stepable) =>
new Step(stepable.id, stepable.title, stepable.estimation).addSteps(
...Step.fromStepable(...stepable.steps)
)
)
}
public static getStepLeafs(steps: Stepable[]): Stepable[] {
return steps.flatMap((step) =>
step.steps.length > 0 ? Step.getStepLeafs(step.steps) : [step]
return stepables.map(
(stepable) => new Step(stepable.id, stepable.title, stepable.estimation)
)
}
}

View File

@@ -1,6 +1,6 @@
import type { Taskable } from '@/use-cases/task/interfaces/taskable'
import { Task } from '@/use-cases/task/models/task'
import { createStepFixture } from '@/use-cases/task/models/step.fixture'
import { Task } from '@/use-cases/task/models/task'
import { faker } from '@faker-js/faker'
import { describe, expect, it } from 'vitest'
@@ -51,4 +51,16 @@ describe('Task', () => {
task.addSteps(createStepFixture())
expect(Task.validate(task)).toEqual(true)
})
it('calculates the total estimation of steps', () => {
const task = new Task(faker.datatype.uuid())
task.addSteps(
createStepFixture({ estimation: 1 }),
createStepFixture({ estimation: 2 }),
createStepFixture({ estimation: 3 })
)
expect(task.totalEstimation).toEqual(6)
})
})

View File

@@ -27,6 +27,10 @@ export class Task implements Taskable {
return this
}
public get totalEstimation() {
return this.steps.map((step) => step.estimation).reduce((a, b) => a + b, 0)
}
public static fromTaskable(taskable: Taskable) {
const task = new Task(taskable.id)
task.title = taskable.title