All files / routes/pro/crm lead-sources.routes.ts

100% Statements 35/35
100% Branches 2/2
100% Functions 4/4
100% Lines 35/35

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                                          1x     1x       4x 4x 4x 4x   4x       3x   1x           1x       4x 4x 4x 4x           4x 2x     2x         2x   2x           1x       4x 4x 4x 4x   4x       2x   2x           1x       3x 3x 3x 3x   3x       1x   2x            
// CRM Lead Sources 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 leadSources = new Hono<Env>();
 
// List sources
leadSources.get(
	"/:proId/crm/lead-sources",
	requireProAccess,
	async (c) => {
		try {
			const services = c.get("services");
			const proId = c.get("proId");
			const includeInactive = c.req.query("includeInactive") === "true";
 
			const sources = await services.leadSources.getSources(
				proId,
				includeInactive,
			);
			return success(c, sources);
		} catch (err) {
			return handleError(c, err);
		}
	},
);
 
// Create custom source
leadSources.post(
	"/:proId/crm/lead-sources",
	requireProAccess,
	async (c) => {
		try {
			const services = c.get("services");
			const proId = c.get("proId");
			const body = await c.req.json<{
				name: string;
				icon?: string;
				color?: string;
			}>();
 
			if (!body.name) {
				throw new ValidationError("Source name is required");
			}
 
			const source = await services.leadSources.createSource(proId, {
				name: body.name,
				icon: body.icon,
				color: body.color,
			});
			return success(c, source, 201);
		} catch (err) {
			return handleError(c, err);
		}
	},
);
 
// Deactivate source
leadSources.patch(
	"/:proId/crm/lead-sources/:sourceId/deactivate",
	requireProAccess,
	async (c) => {
		try {
			const services = c.get("services");
			const proId = c.get("proId");
			const sourceId = parseRequiredId(c.req.param("sourceId"), "source");
 
			const source = await services.leadSources.deactivateSource(
				proId,
				sourceId,
			);
			return success(c, source);
		} catch (err) {
			return handleError(c, err);
		}
	},
);
 
// Reactivate source
leadSources.patch(
	"/:proId/crm/lead-sources/:sourceId/reactivate",
	requireProAccess,
	async (c) => {
		try {
			const services = c.get("services");
			const proId = c.get("proId");
			const sourceId = parseRequiredId(c.req.param("sourceId"), "source");
 
			const source = await services.leadSources.reactivateSource(
				proId,
				sourceId,
			);
			return success(c, source);
		} catch (err) {
			return handleError(c, err);
		}
	},
);
 
export default leadSources;