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 });
|
||||
}
|
||||
@@ -16,6 +16,7 @@ import { processedRoutes } from "./processed/processed.route";
|
||||
import { ctaskRoutes } from "./ctask/ctask.route";
|
||||
import { paymentRoutes } from "./payments/payment.route";
|
||||
import { alertRoutes } from "./alert/alert.route";
|
||||
import { analyticsRoutes } from "./analytics/analytics.routes";
|
||||
|
||||
export default async function routes(fastify: FastifyInstance) {
|
||||
fastify.addHook("preHandler", authHandler);
|
||||
@@ -34,5 +35,6 @@ export default async function routes(fastify: FastifyInstance) {
|
||||
fastify.register(processedRoutes, { prefix: "/processed" });
|
||||
fastify.register(paymentRoutes, { prefix: "/payments" });
|
||||
fastify.register(alertRoutes, { prefix: "/alerts" });
|
||||
fastify.register(analyticsRoutes, { prefix: "/analytics" });
|
||||
fastify.register(realTimeRoutes);
|
||||
}
|
||||
|
||||
@@ -37,4 +37,5 @@ export type Claim =
|
||||
| "note:delete"
|
||||
| "payment:read"
|
||||
| "alert:read"
|
||||
| "alert:write";
|
||||
| "alert:write"
|
||||
| "analytics:read";
|
||||
|
||||
@@ -39,6 +39,7 @@ export const rules: Record<
|
||||
"payment:read",
|
||||
"alert:read",
|
||||
"alert:write",
|
||||
"analytics:read",
|
||||
],
|
||||
hiddenFields: {
|
||||
orgs: ["__v"],
|
||||
@@ -79,6 +80,7 @@ export const rules: Record<
|
||||
"payment:read",
|
||||
"alert:read",
|
||||
"alert:write",
|
||||
"analytics:read",
|
||||
],
|
||||
hiddenFields: {
|
||||
orgs: ["__v"],
|
||||
@@ -111,6 +113,7 @@ export const rules: Record<
|
||||
"note:delete",
|
||||
"alert:read",
|
||||
"alert:write",
|
||||
"analytics:read",
|
||||
],
|
||||
hiddenFields: {
|
||||
orgs: ["__v"],
|
||||
|
||||
Reference in New Issue
Block a user