diff --git a/src/db/repositories/drizzle/phase.ts b/src/db/repositories/drizzle/phase.ts index b5cd53a..abfb869 100644 --- a/src/db/repositories/drizzle/phase.ts +++ b/src/db/repositories/drizzle/phase.ts @@ -7,7 +7,7 @@ import { eq, asc, and, max } from 'drizzle-orm'; import { nanoid } from 'nanoid'; import type { DrizzleDatabase } from '../../index.js'; -import { phases, type Phase } from '../../schema.js'; +import { phases, phaseDependencies, type Phase } from '../../schema.js'; import type { PhaseRepository, CreatePhaseData, @@ -102,4 +102,34 @@ export class DrizzlePhaseRepository implements PhaseRepository { await this.db.delete(phases).where(eq(phases.id, id)); } + + async createDependency(phaseId: string, dependsOnPhaseId: string): Promise { + const id = nanoid(); + const now = new Date(); + + await this.db.insert(phaseDependencies).values({ + id, + phaseId, + dependsOnPhaseId, + createdAt: now, + }); + } + + async getDependencies(phaseId: string): Promise { + const result = await this.db + .select({ dependsOnPhaseId: phaseDependencies.dependsOnPhaseId }) + .from(phaseDependencies) + .where(eq(phaseDependencies.phaseId, phaseId)); + + return result.map((row) => row.dependsOnPhaseId); + } + + async getDependents(phaseId: string): Promise { + const result = await this.db + .select({ phaseId: phaseDependencies.phaseId }) + .from(phaseDependencies) + .where(eq(phaseDependencies.dependsOnPhaseId, phaseId)); + + return result.map((row) => row.phaseId); + } } diff --git a/src/db/repositories/phase-repository.ts b/src/db/repositories/phase-repository.ts index 9ebb601..b9d7e82 100644 --- a/src/db/repositories/phase-repository.ts +++ b/src/db/repositories/phase-repository.ts @@ -71,4 +71,23 @@ export interface PhaseRepository { * Cascades to child plans and tasks via FK constraints. */ delete(id: string): Promise; + + /** + * Create a dependency between two phases. + * The phase identified by phaseId will depend on dependsOnPhaseId. + * Both phases must exist. + */ + createDependency(phaseId: string, dependsOnPhaseId: string): Promise; + + /** + * Get IDs of phases that this phase depends on. + * Returns empty array if no dependencies. + */ + getDependencies(phaseId: string): Promise; + + /** + * Get IDs of phases that depend on this phase. + * Returns empty array if no dependents. + */ + getDependents(phaseId: string): Promise; }