add analytics

This commit is contained in:
2025-06-27 14:39:12 +05:30
parent 38e6521930
commit a8136b8175
9 changed files with 336 additions and 1 deletions

View File

@@ -0,0 +1,15 @@
import { FastifyReply, FastifyRequest } from "fastify";
import { getAnalytics } from "./analytics.service";
export async function getAnalyticsHandler(
req: FastifyRequest,
res: FastifyReply
) {
try {
const analytics = await getAnalytics(req.user);
if (!analytics) return res.code(404).send({ error: "resource not found" });
return res.code(200).send(analytics);
} catch (err) {
return err;
}
}

View File

@@ -0,0 +1,13 @@
import { FastifyInstance } from "fastify";
import { getAnalyticsHandler } from "./analytics.controller";
export async function analyticsRoutes(fastify: FastifyInstance) {
fastify.get(
"",
{
config: { requiredClaims: ["analytics:read"] },
preHandler: [fastify.authorize],
},
getAnalyticsHandler
);
}

View File

@@ -0,0 +1,12 @@
import mongoose from "mongoose";
export const analyticsModel = mongoose.model(
"analytics",
new mongoose.Schema(
{
tenantId: String,
},
{ strict: false }
),
"analytics"
);

View File

@@ -0,0 +1,6 @@
import { AuthenticatedUser } from "../auth";
import { analyticsModel } from "./analytics.schema";
export async function getAnalytics(user: AuthenticatedUser) {
return await analyticsModel.findOne({ tenantId: user.tenantId });
}