simplify steps! They can have substeps. Keep clean and simple.
This commit is contained in:
@@ -1,10 +1,8 @@
|
|||||||
export interface Stepable {
|
export interface Stepable {
|
||||||
id: string
|
id: string
|
||||||
title: string
|
title: string
|
||||||
estimation?: number
|
|
||||||
steps: Stepable[]
|
|
||||||
/**
|
/**
|
||||||
* total estimation in minutes
|
* estimation in minutes
|
||||||
*/
|
*/
|
||||||
totalEstimation: number
|
estimation: number
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
@@ -1,51 +1,17 @@
|
|||||||
import type { Stepable } from '@/use-cases/task/interfaces/stepable'
|
import type { Stepable } from '@/use-cases/task/interfaces/stepable'
|
||||||
|
|
||||||
export class Step implements Stepable {
|
export class Step implements Stepable {
|
||||||
private _steps: Step[] = []
|
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
readonly id: string,
|
readonly id: string,
|
||||||
readonly title: string,
|
readonly title: string,
|
||||||
readonly estimation?: number
|
readonly estimation: number
|
||||||
) {
|
) {
|
||||||
return this
|
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[] {
|
public static fromStepable(...stepables: Stepable[]): Step[] {
|
||||||
return stepables.map((stepable) =>
|
return stepables.map(
|
||||||
new Step(stepable.id, stepable.title, stepable.estimation).addSteps(
|
(stepable) => new Step(stepable.id, stepable.title, stepable.estimation)
|
||||||
...Step.fromStepable(...stepable.steps)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
public static getStepLeafs(steps: Stepable[]): Stepable[] {
|
|
||||||
return steps.flatMap((step) =>
|
|
||||||
step.steps.length > 0 ? Step.getStepLeafs(step.steps) : [step]
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import type { Taskable } from '@/use-cases/task/interfaces/taskable'
|
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 { createStepFixture } from '@/use-cases/task/models/step.fixture'
|
||||||
|
import { Task } from '@/use-cases/task/models/task'
|
||||||
import { faker } from '@faker-js/faker'
|
import { faker } from '@faker-js/faker'
|
||||||
import { describe, expect, it } from 'vitest'
|
import { describe, expect, it } from 'vitest'
|
||||||
|
|
||||||
@@ -51,4 +51,16 @@ describe('Task', () => {
|
|||||||
task.addSteps(createStepFixture())
|
task.addSteps(createStepFixture())
|
||||||
expect(Task.validate(task)).toEqual(true)
|
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)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -27,6 +27,10 @@ export class Task implements Taskable {
|
|||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public get totalEstimation() {
|
||||||
|
return this.steps.map((step) => step.estimation).reduce((a, b) => a + b, 0)
|
||||||
|
}
|
||||||
|
|
||||||
public static fromTaskable(taskable: Taskable) {
|
public static fromTaskable(taskable: Taskable) {
|
||||||
const task = new Task(taskable.id)
|
const task = new Task(taskable.id)
|
||||||
task.title = taskable.title
|
task.title = taskable.title
|
||||||
|
|||||||
Reference in New Issue
Block a user