All files / api/routes sync.ts

96% Statements 24/25
100% Branches 10/10
100% Functions 3/3
96% Lines 24/25

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                357x   357x 1x 1x 1x     1x                     357x 8x 8x 8x 8x   8x 8x   8x 2x 2x   8x 3x 3x     8x 8x   8x                   8x     357x    
import type Database from "better-sqlite3-multiple-ciphers";
import { Hono } from "hono";
import type { SyncScheduler } from "../../sync/sync-scheduler.js";
 
export function syncRoutes(
	getScheduler: () => SyncScheduler,
	getDb: () => Database.Database,
): Hono {
	const api = new Hono();
 
	api.get("/status", (c) => {
		const status = getScheduler().getStatus();
		const entries: Record<string, unknown> = {};
		for (const [identityId, s] of status) {
			entries[String(identityId)] = s;
		}
		return c.json(entries);
	});
 
	/**
	 * GET /api/sync/errors?inbound_connector_id=1&resolved=0&limit=100
	 *
	 * Returns recent sync errors, newest first.
	 * - inbound_connector_id: filter by inbound connector (optional)
	 * - resolved: 0 for unresolved only, 1 for resolved only, omit for all
	 * - limit: max rows (default 100)
	 */
	api.get("/errors", (c) => {
		const db = getDb();
		const connectorId = c.req.query("inbound_connector_id");
		const resolved = c.req.query("resolved");
		const limit = Math.min(Number(c.req.query("limit")) || 100, 1000);
 
		const conditions: string[] = [];
		const params: (string | number)[] = [];
 
		if (connectorId) {
			conditions.push("inbound_connector_id = ?");
			params.push(Number(connectorId));
		}
		if (resolved !== undefined && resolved !== "") {
			conditions.push("resolved = ?");
			params.push(Number(resolved));
		}
 
		const where = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
		params.push(limit);
 
		const rows = db
			.prepare(
				`SELECT id, inbound_connector_id, folder_path, uid, error_type, message,
				        retriable, resolved, retry_count, created_at, resolved_at
				 FROM sync_errors ${where}
				 ORDER BY created_at DESC
				 LIMIT ?`,
			)
			.all(...params);
 
		return c.json(rows);
	});
 
	return api;
}