refactor: Restructure monorepo to apps/server/ and apps/web/ layout
Move src/ → apps/server/ and packages/web/ → apps/web/ to adopt standard monorepo conventions (apps/ for runnable apps, packages/ for reusable libraries). Update all config files, shared package imports, test fixtures, and documentation to reflect new paths. Key fixes: - Update workspace config to ["apps/*", "packages/*"] - Update tsconfig.json rootDir/include for apps/server/ - Add apps/web/** to vitest exclude list - Update drizzle.config.ts schema path - Fix ensure-schema.ts migration path detection (3 levels up in dev, 2 levels up in dist) - Fix tests/integration/cli-server.test.ts import paths - Update packages/shared imports to apps/server/ paths - Update all docs/ files with new paths
This commit is contained in:
153
apps/server/trpc/routers/initiative.ts
Normal file
153
apps/server/trpc/routers/initiative.ts
Normal file
@@ -0,0 +1,153 @@
|
||||
/**
|
||||
* Initiative Router — create, list, get, update, merge config
|
||||
*/
|
||||
|
||||
import { TRPCError } from '@trpc/server';
|
||||
import { z } from 'zod';
|
||||
import type { ProcedureBuilder } from '../trpc.js';
|
||||
import { requireInitiativeRepository, requireProjectRepository, requireTaskRepository } from './_helpers.js';
|
||||
|
||||
export function initiativeProcedures(publicProcedure: ProcedureBuilder) {
|
||||
return {
|
||||
createInitiative: publicProcedure
|
||||
.input(z.object({
|
||||
name: z.string().min(1),
|
||||
branch: z.string().nullable().optional(),
|
||||
projectIds: z.array(z.string().min(1)).min(1).optional(),
|
||||
executionMode: z.enum(['yolo', 'review_per_phase']).optional(),
|
||||
}))
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const repo = requireInitiativeRepository(ctx);
|
||||
|
||||
if (input.projectIds && input.projectIds.length > 0) {
|
||||
const projectRepo = requireProjectRepository(ctx);
|
||||
for (const pid of input.projectIds) {
|
||||
const project = await projectRepo.findById(pid);
|
||||
if (!project) {
|
||||
throw new TRPCError({
|
||||
code: 'NOT_FOUND',
|
||||
message: `Project '${pid}' not found`,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const initiative = await repo.create({
|
||||
name: input.name,
|
||||
status: 'active',
|
||||
...(input.executionMode && { executionMode: input.executionMode }),
|
||||
...(input.branch && { branch: input.branch }),
|
||||
});
|
||||
|
||||
if (input.projectIds && input.projectIds.length > 0) {
|
||||
const projectRepo = requireProjectRepository(ctx);
|
||||
await projectRepo.setInitiativeProjects(initiative.id, input.projectIds);
|
||||
}
|
||||
|
||||
if (ctx.pageRepository) {
|
||||
await ctx.pageRepository.create({
|
||||
initiativeId: initiative.id,
|
||||
parentPageId: null,
|
||||
title: input.name,
|
||||
content: null,
|
||||
sortOrder: 0,
|
||||
});
|
||||
}
|
||||
|
||||
return initiative;
|
||||
}),
|
||||
|
||||
listInitiatives: publicProcedure
|
||||
.input(z.object({
|
||||
status: z.enum(['active', 'completed', 'archived']).optional(),
|
||||
}).optional())
|
||||
.query(async ({ ctx, input }) => {
|
||||
const repo = requireInitiativeRepository(ctx);
|
||||
if (input?.status) {
|
||||
return repo.findByStatus(input.status);
|
||||
}
|
||||
return repo.findAll();
|
||||
}),
|
||||
|
||||
getInitiative: publicProcedure
|
||||
.input(z.object({ id: z.string().min(1) }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const repo = requireInitiativeRepository(ctx);
|
||||
const initiative = await repo.findById(input.id);
|
||||
if (!initiative) {
|
||||
throw new TRPCError({
|
||||
code: 'NOT_FOUND',
|
||||
message: `Initiative '${input.id}' not found`,
|
||||
});
|
||||
}
|
||||
|
||||
let projects: Array<{ id: string; name: string; url: string }> = [];
|
||||
if (ctx.projectRepository) {
|
||||
const fullProjects = await ctx.projectRepository.findProjectsByInitiativeId(input.id);
|
||||
projects = fullProjects.map((p) => ({ id: p.id, name: p.name, url: p.url }));
|
||||
}
|
||||
|
||||
let branchLocked = false;
|
||||
if (ctx.taskRepository) {
|
||||
const tasks = await ctx.taskRepository.findByInitiativeId(input.id);
|
||||
branchLocked = tasks.some((t) => t.status !== 'pending');
|
||||
}
|
||||
|
||||
return { ...initiative, projects, branchLocked };
|
||||
}),
|
||||
|
||||
updateInitiative: publicProcedure
|
||||
.input(z.object({
|
||||
id: z.string().min(1),
|
||||
name: z.string().min(1).optional(),
|
||||
status: z.enum(['active', 'completed', 'archived']).optional(),
|
||||
}))
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const repo = requireInitiativeRepository(ctx);
|
||||
const { id, ...data } = input;
|
||||
return repo.update(id, data);
|
||||
}),
|
||||
|
||||
deleteInitiative: publicProcedure
|
||||
.input(z.object({ id: z.string().min(1) }))
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const repo = requireInitiativeRepository(ctx);
|
||||
await repo.delete(input.id);
|
||||
return { success: true };
|
||||
}),
|
||||
|
||||
updateInitiativeConfig: publicProcedure
|
||||
.input(z.object({
|
||||
initiativeId: z.string().min(1),
|
||||
mergeRequiresApproval: z.boolean().optional(),
|
||||
executionMode: z.enum(['yolo', 'review_per_phase']).optional(),
|
||||
branch: z.string().nullable().optional(),
|
||||
}))
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const repo = requireInitiativeRepository(ctx);
|
||||
const { initiativeId, ...data } = input;
|
||||
|
||||
const existing = await repo.findById(initiativeId);
|
||||
if (!existing) {
|
||||
throw new TRPCError({
|
||||
code: 'NOT_FOUND',
|
||||
message: `Initiative '${initiativeId}' not found`,
|
||||
});
|
||||
}
|
||||
|
||||
// Prevent branch changes once work has started
|
||||
if (data.branch !== undefined && ctx.taskRepository) {
|
||||
const tasks = await ctx.taskRepository.findByInitiativeId(initiativeId);
|
||||
const hasStarted = tasks.some((t) => t.status !== 'pending');
|
||||
if (hasStarted) {
|
||||
throw new TRPCError({
|
||||
code: 'PRECONDITION_FAILED',
|
||||
message: 'Cannot change branch after work has started',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return repo.update(initiativeId, data);
|
||||
}),
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user