All files / routes/admin communications.routes.ts

88.88% Statements 16/18
100% Branches 6/6
100% Functions 2/2
88.88% Lines 16/18

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                                1x     1x 2x 2x   2x   2x                   2x         2x             1x 2x 2x 2x 2x   2x 1x     1x              
import { Hono } from "hono";
import type { Dal } from "../../dal";
import type { Services } from "../../services";
import { success, handleError } from "../../lib/response";
import type { CommunicationLogFilters } from "../../dal/communication-log.dal";
 
type Env = {
	Bindings: CloudflareBindings;
	Variables: {
		user: { id: string; name: string; email: string } | null;
		session: unknown;
		dal: Dal;
		services: Services;
	};
};
 
const communications = new Hono<Env>();
 
// GET /admin/communications - list all communication logs
communications.get("/", async (c) => {
	try {
		const dal = c.get("dal");
		const { channel, status, eventType, environment, search, limit, offset } =
			c.req.query();
 
		const filters: CommunicationLogFilters = {
			channel: channel as CommunicationLogFilters["channel"],
			status: status as CommunicationLogFilters["status"],
			eventType: eventType as CommunicationLogFilters["eventType"],
			environment,
			search,
			limit: limit ? parseInt(limit, 10) : 50,
			offset: offset ? parseInt(offset, 10) : 0,
		};
 
		const [items, total] = await Promise.all([
			dal.communicationLog.findAll(filters),
			dal.communicationLog.countAll(filters),
		]);
 
		return success(c, { items, total, limit: filters.limit, offset: filters.offset });
	} catch (err) {
		return handleError(c, err);
	}
});
 
// GET /admin/communications/:id - get single log entry
communications.get("/:id", async (c) => {
	try {
		const dal = c.get("dal");
		const id = parseInt(c.req.param("id"), 10);
		const entry = await dal.communicationLog.findById(id);
 
		if (!entry) {
			return c.json({ success: false, error: "Not found" }, 404);
		}
 
		return success(c, entry);
	} catch (err) {
		return handleError(c, err);
	}
});
 
export default communications;