# All files / src/trpc/routers task.ts

19.51% Statements 8/41
25% Branches 6/24
23.07% Functions 3/13
20.51% Lines 8/39

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
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163

    11x

    4x
    4x

    7x
    7x

    7x
    7x

    7x

     /**
     * Task Router — CRUD, approval, listing by parent/initiative/phase
     */

    import { TRPCError } from '@trpc/server';
    import { z } from 'zod';
    import type { ProcedureBuilder } from '../trpc.js';
    import {
      requireTaskRepository,
      requireInitiativeRepository,
      requirePhaseRepository,
      requireDispatchManager,
    } from './_helpers.js';

    export function taskProcedures(publicProcedure: ProcedureBuilder) {
      return {
        listTasks: publicProcedure
          .input(z.object({ parentTaskId: z.string().min(1) }))
          .query(async ({ ctx, input }) => {
            const taskRepository = requireTaskRepository(ctx);
            return taskRepository.findByParentTaskId(input.parentTaskId);
          }),

        getTask: publicProcedure
          .input(z.object({ id: z.string().min(1) }))
          .query(async ({ ctx, input }) => {
            const taskRepository = requireTaskRepository(ctx);
            const task = await taskRepository.findById(input.id);
            if (!task) {
              throw new TRPCError({
                code: 'NOT_FOUND',
                message: `Task '${input.id}' not found`,
              });
            }
            return task;
          }),

        updateTaskStatus: publicProcedure
          .input(z.object({
            id: z.string().min(1),
            status: z.enum(['pending_approval', 'pending', 'in_progress', 'completed', 'blocked']),
          }))
          .mutation(async ({ ctx, input }) => {
            const taskRepository = requireTaskRepository(ctx);
            const existing = await taskRepository.findById(input.id);
            if (!existing) {
              throw new TRPCError({
                code: 'NOT_FOUND',
                message: `Task '${input.id}' not found`,
              });
            }
            return taskRepository.update(input.id, { status: input.status });
          }),

        createInitiativeTask: publicProcedure
          .input(z.object({
            initiativeId: z.string().min(1),
            name: z.string().min(1),
            description: z.string().optional(),
            category: z.enum(['execute', 'research', 'discuss', 'plan', 'detail', 'refine', 'verify', 'merge', 'review']).optional(),
            type: z.enum(['auto', 'checkpoint:human-verify', 'checkpoint:decision', 'checkpoint:human-action']).optional(),
            requiresApproval: z.boolean().nullable().optional(),
          }))
          .mutation(async ({ ctx, input }) => {
            const taskRepository = requireTaskRepository(ctx);
            const initiativeRepo = requireInitiativeRepository(ctx);

            const initiative = await initiativeRepo.findById(input.initiativeId);
            if (!initiative) {
              throw new TRPCError({
                code: 'NOT_FOUND',
                message: `Initiative '${input.initiativeId}' not found`,
              });
            }

            return taskRepository.create({
              initiativeId: input.initiativeId,
              name: input.name,
              description: input.description ?? null,
              category: input.category ?? 'execute',
              type: input.type ?? 'auto',
              requiresApproval: input.requiresApproval ?? null,
              status: 'pending',
            });
          }),

        createPhaseTask: publicProcedure
          .input(z.object({
            phaseId: z.string().min(1),
            name: z.string().min(1),
            description: z.string().optional(),
            category: z.enum(['execute', 'research', 'discuss', 'plan', 'detail', 'refine', 'verify', 'merge', 'review']).optional(),
            type: z.enum(['auto', 'checkpoint:human-verify', 'checkpoint:decision', 'checkpoint:human-action']).optional(),
            requiresApproval: z.boolean().nullable().optional(),
          }))
          .mutation(async ({ ctx, input }) => {
            const taskRepository = requireTaskRepository(ctx);
            const phaseRepo = requirePhaseRepository(ctx);

            const phase = await phaseRepo.findById(input.phaseId);
            Iif (!phase) {
              throw new TRPCError({
                code: 'NOT_FOUND',
                message: `Phase '${input.phaseId}' not found`,
              });
            }

            return taskRepository.create({
              phaseId: input.phaseId,
              name: input.name,
              description: input.description ?? null,
              category: input.category ?? 'execute',
              type: input.type ?? 'auto',
              requiresApproval: input.requiresApproval ?? null,
              status: 'pending',
            });
          }),

        listPendingApprovals: publicProcedure
          .input(z.object({
            initiativeId: z.string().optional(),
            phaseId: z.string().optional(),
            category: z.enum(['execute', 'research', 'discuss', 'plan', 'detail', 'refine', 'verify', 'merge', 'review']).optional(),
          }).optional())
          .query(async ({ ctx, input }) => {
            const taskRepository = requireTaskRepository(ctx);
            return taskRepository.findPendingApproval(input);
          }),

        listInitiativeTasks: publicProcedure
          .input(z.object({ initiativeId: z.string().min(1) }))
          .query(async ({ ctx, input }) => {
            const taskRepository = requireTaskRepository(ctx);
            const tasks = await taskRepository.findByInitiativeId(input.initiativeId);
            return tasks.filter((t) => t.category !== 'detail');
          }),

        listPhaseTasks: publicProcedure
          .input(z.object({ phaseId: z.string().min(1) }))
          .query(async ({ ctx, input }) => {
            const taskRepository = requireTaskRepository(ctx);
            const tasks = await taskRepository.findByPhaseId(input.phaseId);
            return tasks.filter((t) => t.category !== 'detail');
          }),

        deleteTask: publicProcedure
          .input(z.object({ id: z.string().min(1) }))
          .mutation(async ({ ctx, input }) => {
            const taskRepository = requireTaskRepository(ctx);
            await taskRepository.delete(input.id);
            return { success: true };
          }),

        approveTask: publicProcedure
          .input(z.object({ taskId: z.string().min(1) }))
          .mutation(async ({ ctx, input }) => {
            const dispatchManager = requireDispatchManager(ctx);
            await dispatchManager.approveTask(input.taskId);
            return { success: true };
          }),
      };
    }