add analytics
This commit is contained in:
15
src/analytics/analytics.controller.ts
Normal file
15
src/analytics/analytics.controller.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
13
src/analytics/analytics.routes.ts
Normal file
13
src/analytics/analytics.routes.ts
Normal 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
|
||||
);
|
||||
}
|
||||
12
src/analytics/analytics.schema.ts
Normal file
12
src/analytics/analytics.schema.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import mongoose from "mongoose";
|
||||
|
||||
export const analyticsModel = mongoose.model(
|
||||
"analytics",
|
||||
new mongoose.Schema(
|
||||
{
|
||||
tenantId: String,
|
||||
},
|
||||
{ strict: false }
|
||||
),
|
||||
"analytics"
|
||||
);
|
||||
6
src/analytics/analytics.service.ts
Normal file
6
src/analytics/analytics.service.ts
Normal 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 });
|
||||
}
|
||||
Reference in New Issue
Block a user