All files / routes/admin/blogs suggestions.routes.ts

100% Statements 53/53
100% Branches 14/14
100% Functions 5/5
100% Lines 53/53

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                                            3x     3x 10x 10x 10x 10x   10x             10x 10x   10x         9x           1x         3x 3x 3x 3x   2x 1x     1x   2x         3x 4x 4x 4x 4x     4x 3x 1x     2x         2x   2x         3x 4x 4x 4x 4x     4x 3x 1x     2x       2x   2x         3x 3x 3x 3x 3x     3x 2x 1x       1x   1x   2x          
// Admin Blog Suggestion Management Routes
import { Hono } from "hono";
import type { Dal } from "../../../dal";
import type { Services } from "../../../services";
import {
	success,
	successWithPagination,
	handleError,
} from "../../../lib/response";
import { buildPaginationMeta } from "../../../lib/utils";
import { NotFoundError } from "../../../lib/errors";
 
type Env = {
	Bindings: CloudflareBindings;
	Variables: {
		user: { id: string; name: string; email: string } | null;
		session: unknown;
		dal: Dal;
		services: Services;
	};
};
 
const suggestions = new Hono<Env>();
 
// List pro blog suggestions — mounted at /suggestions, so path is /all → /api/admin/blogs/suggestions/all
suggestions.get("/all", async (c) => {
	try {
		const dal = c.get("dal");
		const limitParam = Number(c.req.query("limit") || 50);
		const safeLimit = Math.min(100, Math.max(1, limitParam));
 
		const filters = {
			proId: c.req.query("proId"),
			status: c.req.query("status")
				? c.req.query("status")?.split(",")
				: undefined,
		};
 
		const page = Math.max(1, Number(c.req.query("page") || 1));
		const offset = (page - 1) * safeLimit;
 
		const [data, total] = await Promise.all([
			dal.proBlogSuggestions.findAll(filters, offset, safeLimit),
			dal.proBlogSuggestions.count(filters),
		]);
 
		return successWithPagination(
			c,
			data,
			buildPaginationMeta(total, page, safeLimit),
		);
	} catch (err) {
		return handleError(c, err);
	}
});
 
// Get single suggestion
suggestions.get("/:id", async (c) => {
	try {
		const dal = c.get("dal");
		const suggestion = await dal.proBlogSuggestions.findById(c.req.param("id"));
 
		if (!suggestion) {
			throw new NotFoundError("Suggestion not found");
		}
 
		return success(c, suggestion);
	} catch (err) {
		return handleError(c, err);
	}
});
 
// Accept suggestion
suggestions.put("/:id/accept", async (c) => {
	try {
		const dal = c.get("dal");
		const body = await c.req.json();
		const id = c.req.param("id");
 
		// Check if suggestion exists
		const existing = await dal.proBlogSuggestions.findById(id);
		if (!existing) {
			throw new NotFoundError("Suggestion not found");
		}
 
		const updated = await dal.proBlogSuggestions.updateStatus(id, "accepted", {
			acceptedBlogId: body.acceptedBlogId,
			adminNotes: body.adminNotes,
		});
 
		return success(c, updated);
	} catch (err) {
		return handleError(c, err);
	}
});
 
// Reject suggestion
suggestions.put("/:id/reject", async (c) => {
	try {
		const dal = c.get("dal");
		const body = await c.req.json();
		const id = c.req.param("id");
 
		// Check if suggestion exists
		const existing = await dal.proBlogSuggestions.findById(id);
		if (!existing) {
			throw new NotFoundError("Suggestion not found");
		}
 
		const updated = await dal.proBlogSuggestions.updateStatus(id, "rejected", {
			adminNotes: body.adminNotes,
		});
 
		return success(c, updated);
	} catch (err) {
		return handleError(c, err);
	}
});
 
// Update suggestion (generic)
suggestions.put("/:id", async (c) => {
	try {
		const dal = c.get("dal");
		const body = await c.req.json();
		const id = c.req.param("id");
 
		// Check if suggestion exists
		const existing = await dal.proBlogSuggestions.findById(id);
		if (!existing) {
			throw new NotFoundError("Suggestion not found");
		}
 
		// Update suggestion
		const updated = await dal.proBlogSuggestions.update(id, body);
 
		return success(c, updated);
	} catch (err) {
		return handleError(c, err);
	}
});
 
export default suggestions;