Skip to content

Commit 7a93ef7

Browse files
committed
fixed metrics
1 parent 6cc3c50 commit 7a93ef7

File tree

3 files changed

+34
-15
lines changed

3 files changed

+34
-15
lines changed

server/__tests__/controllers/metricsTest.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ describe("createMetrics", () => {
1414
const mockMetricsData = {
1515
clicks: 0,
1616
total_distribution: 50,
17+
metrics_name: "TestMetric",
1718
};
18-
// Mock the save function to return an object with the input data
1919
MetricsModel.prototype.save = jest.fn().mockImplementation(function () {
2020
return { ...mockMetricsData, _id: this._id };
2121
});
2222

2323
const req = httpMocks.createRequest({
24-
body: { total_distribution: 50 },
24+
body: { total_distribution: 50, metrics_name: "TestMetric" },
2525
});
2626
const res = httpMocks.createResponse();
2727

@@ -31,19 +31,17 @@ describe("createMetrics", () => {
3131

3232
expect(MetricsModel.prototype.save).toHaveBeenCalled();
3333
expect(res.statusCode).toBe(200);
34-
expect(responseData.clicks).toBe(mockMetricsData.clicks);
35-
expect(responseData.total_distribution).toBe(
36-
mockMetricsData.total_distribution
37-
);
34+
expect(responseData).toMatchObject(mockMetricsData);
3835
expect(responseData).toHaveProperty("_id");
3936
});
37+
4038
it("should return 500 on server errors", async () => {
4139
MetricsModel.prototype.save = jest.fn().mockImplementation(() => {
4240
throw new Error("Internal Server Error");
4341
});
4442

4543
const req = httpMocks.createRequest({
46-
body: { total_distribution: 50 },
44+
body: { total_distribution: 50, metrics_name: "TestMetric" },
4745
});
4846
const res = httpMocks.createResponse();
4947

@@ -78,7 +76,9 @@ describe("incrementClicks", () => {
7876
{ new: true }
7977
);
8078
expect(res.statusCode).toBe(200);
81-
expect(JSON.parse(res._getData())).toEqual(mockMetricsData);
79+
expect(JSON.parse(res._getData())).toEqual({
80+
message: "Metrics incremented sucessfully",
81+
});
8282
});
8383
it("should return 404 if the metrics record is not found", async () => {
8484
MetricsModel.findOneAndUpdate = jest.fn().mockResolvedValue(null);

server/controllers/metrics.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
/**
2+
* Metrics Controller
3+
*
4+
* This file serves as the controller for metrics-related operations in the API.
5+
* It includes functionalities for managing metrics records such as creating new records,
6+
* incrementing click counts, and retrieving metrics data by name.
7+
*
8+
* Dependencies:
9+
* - MetricsModel: The Mongoose model used for metrics data interactions with the MongoDB database.
10+
* - Handlers: Utility functions for handling various HTTP response scenarios, such as server errors,
11+
* successful responses, and resource not found errors.
12+
*/
13+
114
import MetricsModel from "../models/Metrics.js";
215
import {
316
handleServerError,
@@ -15,11 +28,14 @@ import {
1528
*/
1629
export const createMetrics = async (req, res) => {
1730
try {
18-
const totalDistribution = req.body.total_distribution || 50; // Default to 50 if not provided
31+
const { total_distribution = 50, metrics_name } = req.body;
32+
1933
const metrics = new MetricsModel({
2034
clicks: 0,
21-
total_distribution: totalDistribution,
35+
total_distribution,
36+
metrics_name,
2237
});
38+
2339
await metrics.save();
2440
handleSuccess(res, metrics);
2541
} catch (err) {
@@ -37,9 +53,10 @@ export const createMetrics = async (req, res) => {
3753
*/
3854
export const incrementClicks = async (req, res) => {
3955
try {
40-
const { metricsName } = req.body;
56+
const { metrics_name } = req.body;
57+
console.log(metrics_name);
4158
const metrics = await MetricsModel.findOneAndUpdate(
42-
{ metrics_name: metricsName },
59+
{ metrics_name: metrics_name },
4360
{ $inc: { clicks: 1 } },
4461
{ new: true }
4562
);
@@ -48,7 +65,7 @@ export const incrementClicks = async (req, res) => {
4865
return handleNotFound(res, "Metrics record not found");
4966
}
5067

51-
handleSuccess(res, metrics);
68+
handleSuccess(res, { message: "Metrics incremented sucessfully" });
5269
} catch (err) {
5370
handleServerError(res, err);
5471
}
@@ -64,9 +81,9 @@ export const incrementClicks = async (req, res) => {
6481
*/
6582
export const getMetricsByName = async (req, res) => {
6683
try {
67-
const { metricsName } = req.body;
84+
const { metrics_name } = req.body;
6885
const metrics = await MetricsModel.findOne({
69-
metrics_name: metricsName,
86+
metrics_name: metrics_name,
7087
});
7188

7289
if (!metrics) {

server/server.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import authRoutes from "./routes/auth.js";
1717
import messageRoutes from "./routes/message.js";
1818
import replyRoutes from "./routes/reply.js";
1919
import userRoutes from "./routes/user.js";
20+
import metricRoutes from "./routes/metrics.js";
2021

2122
// Load environment variables from .env file
2223
dotenv.config();
@@ -56,6 +57,7 @@ app.use("/auth", authRoutes);
5657
app.use("/message", messageRoutes);
5758
app.use("/reply", replyRoutes);
5859
app.use("/user", userRoutes);
60+
app.use("/metrics", metricRoutes);
5961

6062
const server = app.listen(PORT, console.log(`Server running on port ${PORT}`));
6163

0 commit comments

Comments
 (0)