# All files / src/db/repositories/drizzle task.ts

74.19% Statements 23/31
72.72% Branches 16/22
91.66% Functions 11/12
73.33% Lines 22/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143

    253x

    415x
    415x

    415x

    414x

    343x

    343x

    5x

    16x

    34x

    100x

    100x
    1x

    99x

    3x

    3x
    1x

    12x
    12x

    12x

    66x

    66x

     /**
     * Drizzle Task Repository Adapter
     *
     * Implements TaskRepository interface using Drizzle ORM.
     */

    import { eq, asc, and } from 'drizzle-orm';
    import { nanoid } from 'nanoid';
    import type { DrizzleDatabase } from '../../index.js';
    import { tasks, taskDependencies, type Task } from '../../schema.js';
    import type {
      TaskRepository,
      CreateTaskData,
      UpdateTaskData,
      PendingApprovalFilters,
    } from '../task-repository.js';

    /**
     * Drizzle adapter for TaskRepository.
     *
     * Uses dependency injection for database instance,
     * enabling isolated test databases.
     */
    export class DrizzleTaskRepository implements TaskRepository {
      constructor(private db: DrizzleDatabase) {}

      async create(data: CreateTaskData): Promise<Task> {
        const id = nanoid();
        const now = new Date();

        const [created] = await this.db.insert(tasks).values({
          id,
          ...data,
          type: data.type ?? 'auto',
          category: data.category ?? 'execute',
          priority: data.priority ?? 'medium',
          status: data.status ?? 'pending',
          order: data.order ?? 0,
          createdAt: now,
          updatedAt: now,
        }).returning();

        return created;
      }

      async findById(id: string): Promise<Task | null> {
        const result = await this.db
          .select()
          .from(tasks)
          .where(eq(tasks.id, id))
          .limit(1);

        return result[0] ?? null;
      }

      async findByParentTaskId(parentTaskId: string): Promise<Task[]> {
        return this.db
          .select()
          .from(tasks)
          .where(eq(tasks.parentTaskId, parentTaskId))
          .orderBy(asc(tasks.order));
      }

      async findByInitiativeId(initiativeId: string): Promise<Task[]> {
        return this.db
          .select()
          .from(tasks)
          .where(eq(tasks.initiativeId, initiativeId))
          .orderBy(asc(tasks.order));
      }

      async findByPhaseId(phaseId: string): Promise<Task[]> {
        return this.db
          .select()
          .from(tasks)
          .where(eq(tasks.phaseId, phaseId))
          .orderBy(asc(tasks.order));
      }

      async findPendingApproval(filters?: PendingApprovalFilters): Promise<Task[]> {
        const conditions = [eq(tasks.status, 'pending_approval')];

        if (filters?.initiativeId) {
          conditions.push(eq(tasks.initiativeId, filters.initiativeId));
        }
        if (filters?.phaseId) {
          conditions.push(eq(tasks.phaseId, filters.phaseId));
        }
        if (filters?.category) {
          conditions.push(eq(tasks.category, filters.category));
        }

        return this.db
          .select()
          .from(tasks)
          .where(and(...conditions))
          .orderBy(asc(tasks.createdAt));
      }

      async update(id: string, data: UpdateTaskData): Promise<Task> {
        const [updated] = await this.db
          .update(tasks)
          .set({ ...data, updatedAt: new Date() })
          .where(eq(tasks.id, id))
          .returning();

        if (!updated) {
          throw new Error(`Task not found: ${id}`);
        }

        return updated;
      }

      async delete(id: string): Promise<void> {
        const [deleted] = await this.db.delete(tasks).where(eq(tasks.id, id)).returning();

        if (!deleted) {
          throw new Error(`Task not found: ${id}`);
        }
      }

      async createDependency(taskId: string, dependsOnTaskId: string): Promise<void> {
        const id = nanoid();
        const now = new Date();

        await this.db.insert(taskDependencies).values({
          id,
          taskId,
          dependsOnTaskId,
          createdAt: now,
        });
      }

      async getDependencies(taskId: string): Promise<string[]> {
        const deps = await this.db
          .select({ dependsOnTaskId: taskDependencies.dependsOnTaskId })
          .from(taskDependencies)
          .where(eq(taskDependencies.taskId, taskId));

        return deps.map((d) => d.dependsOnTaskId);
      }
    }