init
This commit is contained in:
12
src/use-cases/task/interfaces/result.ts
Normal file
12
src/use-cases/task/interfaces/result.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
export enum ResultStatus {
|
||||
RUN,
|
||||
DEBRIEF
|
||||
}
|
||||
|
||||
export interface Result {
|
||||
taskId: string
|
||||
time: number
|
||||
steps: Record<string, number>
|
||||
currentStepId: string | null
|
||||
status: ResultStatus
|
||||
}
|
||||
10
src/use-cases/task/interfaces/stepable.ts
Normal file
10
src/use-cases/task/interfaces/stepable.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
export interface Stepable {
|
||||
id: string
|
||||
title: string
|
||||
estimation?: number
|
||||
steps: Stepable[]
|
||||
/**
|
||||
* total estimation in minutes
|
||||
*/
|
||||
totalEstimation: number
|
||||
}
|
||||
8
src/use-cases/task/interfaces/taskable.ts
Normal file
8
src/use-cases/task/interfaces/taskable.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import type { Stepable } from '@/use-cases/task/interfaces/stepable'
|
||||
|
||||
export interface Taskable {
|
||||
id: string
|
||||
title: string
|
||||
link: string | null
|
||||
steps: Stepable[]
|
||||
}
|
||||
91
src/use-cases/task/models/step.test.ts
Normal file
91
src/use-cases/task/models/step.test.ts
Normal 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)
|
||||
})
|
||||
})
|
||||
51
src/use-cases/task/models/step.ts
Normal file
51
src/use-cases/task/models/step.ts
Normal 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]
|
||||
)
|
||||
}
|
||||
}
|
||||
46
src/use-cases/task/models/task.ts
Normal file
46
src/use-cases/task/models/task.ts
Normal 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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user