import { Decimal } from 'decimal.js'; import type { Module } from './module.js'; export interface BlocProps { id: number; formationId: number; nom: string; heuresPrevues: Decimal; ordre?: number; modules?: Module[]; } const ZERO = new Decimal(0); export class Bloc { public readonly id: number; public readonly formationId: number; public nom: string; public heuresPrevues: Decimal; public ordre: number; public readonly modules: Module[]; constructor(props: BlocProps) { if (props.heuresPrevues.lt(0)) { throw new Error('heuresPrevues doit etre >= 0'); } this.id = props.id; this.formationId = props.formationId; this.nom = props.nom; this.heuresPrevues = props.heuresPrevues; this.ordre = props.ordre ?? 0; this.modules = props.modules ?? []; } heuresAttribuees(): Decimal { return this.modules.reduce((acc, m) => acc.plus(m.heuresPrevues), ZERO); } heuresRestantes(): Decimal { return this.heuresPrevues.minus(this.heuresAttribuees()); } ajouterModule(module: Module): void { if (this.modules.some((m) => m.id === module.id)) { throw new Error(`Module ${module.id} deja present dans le bloc`); } const total = this.heuresAttribuees().plus(module.heuresPrevues); if (total.gt(this.heuresPrevues)) { throw new Error( `Capacite bloc depassee: ${total.toString()} > ${this.heuresPrevues.toString()}`, ); } this.modules.push(module); } }