🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
import { eq } from "drizzle-orm";
import { FastifyReply, FastifyRequest } from "fastify";
import { db } from "../../../db/postgres/postgres.js";
import { funnels as funnelsTable } from "../../../db/postgres/schema.js";
export async function getFunnels(
request: FastifyRequest<{
Params: {
siteId: string;
};
}>,
reply: FastifyReply
) {
const { siteId } = request.params;
try {
// Fetch all funnels for the site
const funnelRecords = await db
.select()
.from(funnelsTable)
.where(eq(funnelsTable.siteId, Number(siteId)))
.orderBy(funnelsTable.createdAt);
// Transform the records to a more frontend-friendly structure
const funnels = funnelRecords.map(record => {
const data = record.data as any;
return {
id: record.reportId,
name: data.name || "Unnamed Funnel",
steps: data.steps || [],
configuration: data.configuration || {},
createdAt: record.createdAt,
updatedAt: record.updatedAt,
// Sessions and conversion are computed per period by getFunnelSummaries;
// nothing is stored with the definition.
};
});
return reply.send({ data: funnels });
} catch (error) {
request.log.error({ err: error }, "Error fetching funnels");
return reply.status(500).send({ error: "Failed to fetch funnels" });
}
}