This commit is contained in:
Julien Calixte
2023-04-03 23:23:17 +02:00
commit d58cc9c68a
39 changed files with 4179 additions and 0 deletions

View File

@@ -0,0 +1,91 @@
import { describe, expect, it } from 'vitest'
import { Step } from '@/use-cases/task/models/step'
import { faker } from '@faker-js/faker'
import type { Stepable } from '@/use-cases/task/interfaces/stepable'
const createStep = (partialStep?: Partial<Stepable>) =>
new Step(
partialStep?.id ?? faker.datatype.uuid(),
partialStep?.title ?? faker.animal.bird(),
partialStep?.estimation
)
describe('Step', () => {
it('adds substeps', () => {
const step = createStep()
step.addSteps(createStep())
expect(step.steps.length).toEqual(1)
})
it('removes substeps', () => {
const step = createStep()
step.addSteps(createStep())
step.removeStep(0)
expect(step.steps.length).toEqual(0)
})
it('tells the estimation based on the sum of its substeps', () => {
const step = createStep()
.addSteps(
createStep({
estimation: 1
})
)
.addSteps(
createStep({
estimation: 2
})
)
.addSteps(
createStep({
estimation: 3
})
)
expect(step.totalEstimation).toBe(6)
})
it('tells the total estimation if the step estimation is set even with substeps', () => {
const step = createStep({
estimation: 8
})
.addSteps(
createStep({
estimation: 1
})
)
.addSteps(
createStep({
estimation: 2
})
)
expect(step.totalEstimation).toEqual(8)
})
it('flattens the substeps', () => {
const leafs = [
createStep({
id: 'leaf-1'
}),
createStep({
id: 'leaf-2'
}),
createStep({
id: 'leaf-3'
})
]
const step = createStep().addSteps(
createStep().addSteps(leafs[0], leafs[1]),
createStep().addSteps(createStep().addSteps(leafs[2]))
)
const steps = Step.getStepLeafs([step])
expect(steps).toEqual(leafs)
})
})

View File

@@ -0,0 +1,51 @@
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
) {
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]
)
}
}

View File

@@ -0,0 +1,46 @@
import type { Stepable } from '@/use-cases/task/interfaces/stepable'
import type { Taskable } from '@/use-cases/task/interfaces/taskable'
import { Step } from '@/use-cases/task/models/step'
export class Task implements Taskable {
public steps: Step[] = []
public title = ''
public link: string | null = null
constructor(public readonly id: string) {}
public addSteps(...steps: Stepable[]) {
this.steps.push(...Step.fromStepable(...steps))
return this
}
public removeStep(index: number) {
if (index < 0) {
return this
}
if (index < this.steps.length) {
return this
}
this.steps.splice(index)
return this
}
public get flattenSteps() {
return this.steps
}
public static fromTaskable(taskable: Taskable) {
const task = new Task(taskable.id)
task.title = taskable.title
task.link = taskable.link
task.addSteps(...taskable.steps)
return task
}
public static validate(task: Taskable) {
return !!task.id && !!task.title && task.steps.length > 0
}
}