All files / routes/pro notifications.routes.ts

100% Statements 31/31
100% Branches 14/14
100% Functions 2/2
100% Lines 31/31

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                                      1x     1x       3x 3x 3x 1x     2x 2x     2x         1x   2x           1x       8x 8x 8x 1x     7x 7x 7x       7x 2x         5x 5x 1x   4x 1x   3x 1x         2x           1x   7x            
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 { requireProAccess } 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 notifications = new Hono<Env>();
 
// GET /api/pro/:proId/notification-preferences
notifications.get(
	"/:proId/notification-preferences",
	requireProAccess,
	async (c) => {
		try {
			const user = c.get("user");
			if (!user) {
				throw new ValidationError("Authentication required");
			}
 
			const proId = c.get("proId");
			const services = c.get("services");
 
			const preferences =
				await services.notifPreferences.getEffectivePreferences(
					user.id,
					proId,
				);
 
			return success(c, { preferences });
		} catch (err) {
			return handleError(c, err);
		}
	},
);
 
// PUT /api/pro/:proId/notification-preferences
notifications.put(
	"/:proId/notification-preferences",
	requireProAccess,
	async (c) => {
		try {
			const user = c.get("user");
			if (!user) {
				throw new ValidationError("Authentication required");
			}
 
			const proId = c.get("proId");
			const services = c.get("services");
			const body = await c.req.json<{
				preferences: { eventType: string; channel: string; enabled: boolean }[];
			}>();
 
			if (!Array.isArray(body.preferences) || body.preferences.length === 0) {
				throw new ValidationError(
					"preferences must be a non-empty array of { eventType, channel, enabled }",
				);
			}
 
			for (const pref of body.preferences) {
				if (typeof pref.eventType !== "string") {
					throw new ValidationError("Each preference must have an eventType string");
				}
				if (typeof pref.channel !== "string") {
					throw new ValidationError("Each preference must have a channel string");
				}
				if (typeof pref.enabled !== "boolean") {
					throw new ValidationError("Each preference must have an enabled boolean");
				}
			}
 
			const preferences =
				await services.notifPreferences.updatePreferences(
					user.id,
					proId,
					body.preferences,
				);
 
			return success(c, { preferences });
		} catch (err) {
			return handleError(c, err);
		}
	},
);
 
export default notifications;