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

100% Statements 61/61
100% Branches 14/14
100% Functions 6/6
100% Lines 61/61

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 168 169                                          1x     1x       4x 4x 4x 4x   4x   2x 2x   2x           1x       10x 10x 10x 10x   10x   8x           8x 2x   6x 2x         4x 4x 1x         3x 2x   8x           1x       4x 4x 4x 4x   4x 1x   3x           1x       3x 3x 3x 3x   3x 1x   2x           1x       4x 4x 4x 4x   4x 3x 1x         2x         1x   3x           1x       3x 3x 3x   3x 2x   1x            
// CRM Reminders 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 reminders = new Hono<Env>();
 
// List reminders for a lead
reminders.get(
	"/:proId/crm/leads/:leadId/reminders",
	requireProAccess,
	async (c) => {
		try {
			const services = c.get("services");
			const proId = c.get("proId");
			const leadId = parseRequiredId(c.req.param("leadId"), "lead");
 
			await services.leads.verifyProOwnership(leadId, proId);
 
			const list = await services.reminders.listByLead(leadId);
			return success(c, list);
		} catch (err) {
			return handleError(c, err);
		}
	},
);
 
// Create reminder for a lead
reminders.post(
	"/:proId/crm/leads/:leadId/reminders",
	requireProAccess,
	async (c) => {
		try {
			const services = c.get("services");
			const proId = c.get("proId");
			const leadId = parseRequiredId(c.req.param("leadId"), "lead");
 
			await services.leads.verifyProOwnership(leadId, proId);
 
			const body = await c.req.json<{
				title: string;
				dueAt: number;
				notes?: string;
			}>();
 
			if (!body.title || typeof body.title !== "string") {
				throw new ValidationError("Reminder title is required");
			}
			if (!Number.isInteger(body.dueAt) || body.dueAt <= 0) {
				throw new ValidationError(
					"dueAt must be a positive integer (unix timestamp)",
				);
			}
			const maxDueAt =
				Math.floor(Date.now() / 1000) + 10 * 365 * 24 * 60 * 60; // ~10 years
			if (body.dueAt > maxDueAt) {
				throw new ValidationError(
					"dueAt is too far in the future. Please provide a Unix timestamp in seconds, not milliseconds.",
				);
			}
 
			const reminder = await services.reminders.create(leadId, body);
			return success(c, reminder, 201);
		} catch (err) {
			return handleError(c, err);
		}
	},
);
 
// Complete reminder
reminders.patch(
	"/:proId/crm/reminders/:reminderId/complete",
	requireProAccess,
	async (c) => {
		try {
			const services = c.get("services");
			const proId = c.get("proId");
			const reminderId = parseRequiredId(c.req.param("reminderId"), "reminder");
 
			const reminder = await services.reminders.complete(proId, reminderId);
			return success(c, reminder);
		} catch (err) {
			return handleError(c, err);
		}
	},
);
 
// Dismiss reminder
reminders.patch(
	"/:proId/crm/reminders/:reminderId/dismiss",
	requireProAccess,
	async (c) => {
		try {
			const services = c.get("services");
			const proId = c.get("proId");
			const reminderId = parseRequiredId(c.req.param("reminderId"), "reminder");
 
			const reminder = await services.reminders.dismiss(proId, reminderId);
			return success(c, reminder);
		} catch (err) {
			return handleError(c, err);
		}
	},
);
 
// Snooze reminder
reminders.patch(
	"/:proId/crm/reminders/:reminderId/snooze",
	requireProAccess,
	async (c) => {
		try {
			const services = c.get("services");
			const proId = c.get("proId");
			const reminderId = parseRequiredId(c.req.param("reminderId"), "reminder");
 
			const body = await c.req.json<{ dueAt: number }>();
			if (!Number.isInteger(body.dueAt) || body.dueAt <= 0) {
				throw new ValidationError(
					"dueAt must be a positive integer (unix timestamp)",
				);
			}
 
			const reminder = await services.reminders.snooze(
				proId,
				reminderId,
				body.dueAt,
			);
			return success(c, reminder);
		} catch (err) {
			return handleError(c, err);
		}
	},
);
 
// Get pro reminder counts (for bell icon)
reminders.get(
	"/:proId/crm/reminder-counts",
	requireProAccess,
	async (c) => {
		try {
			const services = c.get("services");
			const proId = c.get("proId");
 
			const counts = await services.reminders.getProReminderCounts(proId);
			return success(c, counts);
		} catch (err) {
			return handleError(c, err);
		}
	},
);
 
export default reminders;