All files / routes/pro/crm pipeline-stages.routes.ts

100% Statements 55/55
100% Branches 17/17
100% Functions 7/7
100% Lines 55/55

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 164 165 166 167                                          1x     1x       3x 3x 3x   3x   2x 2x   1x           1x       4x 4x 4x 4x   4x 2x     2x       2x   2x           1x       4x 4x 4x 4x   4x 2x 1x     1x         1x   3x           1x       7x 7x 7x 7x   7x   1x   6x         3x         3x         3x   4x           1x       6x 6x 6x 6x   6x 3x   3x     7x     2x         1x       1x   5x            
// CRM Pipeline Stages Routes
import { Hono } from "hono";
import type { Dal } from "../../../dal";
import type { Services } from "../../../services";
import { success, handleError } from "../../../lib/response";
import { parseRequiredId } from "../../../lib/utils";
import { requireProAccess } from "../../../middleware";
import { ValidationError } from "../../../lib/errors";
 
type Env = {
	Bindings: CloudflareBindings;
	Variables: {
		user: { id: string; name: string; email: string } | null;
		session: unknown;
		dal: Dal;
		services: Services;
		proId: string;
		proRole: string;
	};
};
 
const pipelineStages = new Hono<Env>();
 
// List stages (auto-initializes on first access)
pipelineStages.get(
	"/:proId/crm/pipeline-stages",
	requireProAccess,
	async (c) => {
		try {
			const services = c.get("services");
			const proId = c.get("proId");
 
			await services.pipelineStages.ensureInitialized(proId);
 
			const stages = await services.pipelineStages.getStages(proId);
			return success(c, stages);
		} catch (err) {
			return handleError(c, err);
		}
	},
);
 
// Create stage
pipelineStages.post(
	"/:proId/crm/pipeline-stages",
	requireProAccess,
	async (c) => {
		try {
			const services = c.get("services");
			const proId = c.get("proId");
			const body = await c.req.json<{ name: string; position?: number }>();
 
			if (!body.name) {
				throw new ValidationError("Stage name is required");
			}
 
			const stage = await services.pipelineStages.createStage(proId, {
				name: body.name,
				position: body.position,
			});
			return success(c, stage, 201);
		} catch (err) {
			return handleError(c, err);
		}
	},
);
 
// Rename stage
pipelineStages.patch(
	"/:proId/crm/pipeline-stages/:stageId",
	requireProAccess,
	async (c) => {
		try {
			const services = c.get("services");
			const proId = c.get("proId");
			const stageId = parseRequiredId(c.req.param("stageId"), "stage");
 
			const body = await c.req.json<{ name: string }>();
			if (!body.name) {
				throw new ValidationError("Stage name is required");
			}
 
			const stage = await services.pipelineStages.updateStage(
				proId,
				stageId,
				{ name: body.name },
			);
			return success(c, stage);
		} catch (err) {
			return handleError(c, err);
		}
	},
);
 
// Delete stage
pipelineStages.delete(
	"/:proId/crm/pipeline-stages/:stageId",
	requireProAccess,
	async (c) => {
		try {
			const services = c.get("services");
			const proId = c.get("proId");
			const stageId = parseRequiredId(c.req.param("stageId"), "stage");
 
			const body = await c.req
				.json<{ reassignToStageId?: number }>()
				.catch((): { reassignToStageId?: number } => ({}));
 
			if (
				body.reassignToStageId !== undefined &&
				(!Number.isInteger(body.reassignToStageId) ||
					body.reassignToStageId <= 0)
			) {
				throw new ValidationError(
					"reassignToStageId must be a positive integer",
				);
			}
 
			await services.pipelineStages.deleteStage(
				proId,
				stageId,
				body.reassignToStageId,
			);
			return success(c, { deleted: true });
		} catch (err) {
			return handleError(c, err);
		}
	},
);
 
// Reorder stages
pipelineStages.put(
	"/:proId/crm/pipeline-stages/reorder",
	requireProAccess,
	async (c) => {
		try {
			const services = c.get("services");
			const proId = c.get("proId");
			const body = await c.req.json<{ stageIds: number[] }>();
 
			if (!Array.isArray(body.stageIds) || body.stageIds.length === 0) {
				throw new ValidationError("stageIds array is required");
			}
			if (
				!body.stageIds.every(
					(id: unknown) =>
						Number.isInteger(id) && (id as number) > 0,
				)
			) {
				throw new ValidationError(
					"All stageIds must be positive integers",
				);
			}
 
			const stages = await services.pipelineStages.reorderStages(
				proId,
				body.stageIds,
			);
			return success(c, stages);
		} catch (err) {
			return handleError(c, err);
		}
	},
);
 
export default pipelineStages;