All files / routes/pro/crm analytics.routes.ts

100% Statements 12/12
100% Branches 4/4
100% Functions 1/1
100% Lines 12/12

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                                        1x   1x                   1x 7x 7x 7x 7x   7x 1x         6x 5x   2x          
// CRM Analytics Routes
import { Hono } from "hono";
import type { Dal } from "../../../dal";
import type { Services } from "../../../services";
import { success, handleError } from "../../../lib/response";
import { ValidationError } from "../../../lib/errors";
import { requireProManager } from "../../../middleware";
 
type Env = {
	Bindings: CloudflareBindings;
	Variables: {
		user: { id: string; name: string; email: string } | null;
		session: unknown;
		dal: Dal;
		services: Services;
		proId: string;
		proRole: string;
	};
};
 
const analytics = new Hono<Env>();
 
const VALID_PERIODS = new Set([
	"this_week",
	"this_month",
	"last_month",
	"this_quarter",
	"this_year",
	"all_time",
]);
 
// Get CRM analytics dashboard data
analytics.get("/:proId/crm/analytics", requireProManager, async (c) => {
	try {
		const services = c.get("services");
		const proId = c.get("proId");
		const period = c.req.query("period") || "this_month";
 
		if (!VALID_PERIODS.has(period)) {
			throw new ValidationError(
				`Invalid period. Allowed: ${[...VALID_PERIODS].join(", ")}`,
			);
		}
 
		const data = await services.crmAnalytics.getDashboard(proId, period);
		return success(c, data);
	} catch (err) {
		return handleError(c, err);
	}
});
 
export default analytics;