diff --git a/src/use-cases/task/interfaces/stepable.ts b/src/use-cases/task/interfaces/stepable.ts index aa4b2ef..2036036 100644 --- a/src/use-cases/task/interfaces/stepable.ts +++ b/src/use-cases/task/interfaces/stepable.ts @@ -1,10 +1,8 @@ export interface Stepable { id: string title: string - estimation?: number - steps: Stepable[] /** - * total estimation in minutes + * estimation in minutes */ - totalEstimation: number + estimation: number } diff --git a/src/use-cases/task/models/step.test.ts b/src/use-cases/task/models/step.test.ts deleted file mode 100644 index 3764a11..0000000 --- a/src/use-cases/task/models/step.test.ts +++ /dev/null @@ -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) - }) -}) diff --git a/src/use-cases/task/models/step.ts b/src/use-cases/task/models/step.ts index 447b99e..09951f9 100644 --- a/src/use-cases/task/models/step.ts +++ b/src/use-cases/task/models/step.ts @@ -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) ) } } diff --git a/src/use-cases/task/models/task.test.ts b/src/use-cases/task/models/task.test.ts index 06f232b..c887941 100644 --- a/src/use-cases/task/models/task.test.ts +++ b/src/use-cases/task/models/task.test.ts @@ -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) + }) }) diff --git a/src/use-cases/task/models/task.ts b/src/use-cases/task/models/task.ts index 476ead0..13fe993 100644 --- a/src/use-cases/task/models/task.ts +++ b/src/use-cases/task/models/task.ts @@ -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