Skip to content

Commit 0374020

Browse files
committed
Not Found 404 case handled.
1 parent b361de8 commit 0374020

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

constants/progresses.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const PROGRESS_DOCUMENT_CREATED_SUCCEEDED = "Progress document created successfully.";
22
const PROGRESS_DOCUMENT_RETRIEVAL_SUCCEEDED = "Progress document retrieved successfully.";
3-
const PROGRESS_DOCUMENT_NOT_FOUND = "No Progress document found.";
3+
const PROGRESS_DOCUMENT_NOT_FOUND = "No progress records found.";
44
const PROGRESS_ALREADY_CREATED = "Progress for the day has already been created.";
55
const MILLISECONDS_IN_DAY = 24 * 60 * 60 * 1000;
66

controllers/progresses.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
const { Conflict } = require("http-errors");
1+
const { Conflict, NotFound } = require("http-errors");
22
const { createProgressDocument, getProgressDocument, getRangeProgressData } = require("../models/progresses");
33
const { RESPONSE_MESSAGES } = require("../constants/progresses");
4-
const { PROGRESS_DOCUMENT_RETRIEVAL_SUCCEEDED, PROGRESS_DOCUMENT_NOT_FOUND, PROGRESS_DOCUMENT_CREATED_SUCCEEDED } =
5-
RESPONSE_MESSAGES;
4+
const { PROGRESS_DOCUMENT_RETRIEVAL_SUCCEEDED, PROGRESS_DOCUMENT_CREATED_SUCCEEDED } = RESPONSE_MESSAGES;
65

76
/**
87
* Adds Progress Document
@@ -41,13 +40,17 @@ const createProgress = async (req, res) => {
4140
const getProgress = async (req, res) => {
4241
try {
4342
const data = await getProgressDocument(req.query);
44-
const count = data.length;
4543
return res.json({
46-
message: count ? PROGRESS_DOCUMENT_RETRIEVAL_SUCCEEDED : PROGRESS_DOCUMENT_NOT_FOUND,
47-
count,
44+
message: PROGRESS_DOCUMENT_RETRIEVAL_SUCCEEDED,
45+
count: data.length,
4846
data,
4947
});
5048
} catch (error) {
49+
if (error instanceof NotFound) {
50+
return res.status(404).json({
51+
message: error.message,
52+
});
53+
}
5154
return res.status(400).json({
5255
message: error.message,
5356
});
@@ -68,6 +71,11 @@ const getProgressRangeData = async (req, res) => {
6871
data,
6972
});
7073
} catch (error) {
74+
if (error instanceof NotFound) {
75+
return res.status(404).json({
76+
message: error.message,
77+
});
78+
}
7179
return res.status(400).json({
7280
message: error.message,
7381
});

utils/progresses.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
const { NotFound } = require("http-errors");
12
const { fetchTask } = require("../models/tasks");
23
const { fetchUser } = require("../models/users");
34
const fireStore = require("../utils/firestore");
5+
const {
6+
RESPONSE_MESSAGES: { PROGRESS_DOCUMENT_NOT_FOUND },
7+
} = require("../constants/progresses");
48
const progressesCollection = fireStore.collection("progresses");
59

610
const buildQueryForPostingProgress = ({ type, userId, taskId }) => {
@@ -13,14 +17,14 @@ const buildQueryForPostingProgress = ({ type, userId, taskId }) => {
1317
const assertUserExists = async (userId) => {
1418
const { userExists } = await fetchUser({ userId });
1519
if (!userExists) {
16-
throw new Error(`User with id ${userId} does not exist`);
20+
throw new NotFound(`User with id ${userId} does not exist.`);
1721
}
1822
};
1923

2024
const assertTaskExists = async (taskId) => {
2125
const { taskData } = await fetchTask(taskId);
2226
if (!taskData) {
23-
throw new Error(`Task with id ${taskId} does not exist`);
27+
throw new NotFound(`Task with id ${taskId} does not exist.`);
2428
}
2529
};
2630

@@ -49,6 +53,9 @@ const buildQueryToFetchDocs = (queryParams) => {
4953

5054
const getProgressDocs = async (query) => {
5155
const progressesDocs = await query.get();
56+
if (!progressesDocs.size) {
57+
throw new NotFound(PROGRESS_DOCUMENT_NOT_FOUND);
58+
}
5259
const docsData = [];
5360
progressesDocs.forEach((doc) => {
5461
docsData.push({ id: doc.id, ...doc.data() });
@@ -76,6 +83,9 @@ const getProgressRecords = async (query, queryParams) => {
7683
const { startDate, endDate } = queryParams;
7784
const docsData = {};
7885
const progressesDocs = (await query.get()).docs;
86+
if (!progressesDocs.size) {
87+
throw new NotFound(PROGRESS_DOCUMENT_NOT_FOUND);
88+
}
7989
progressesDocs.forEach((doc) => {
8090
const date = new Date(doc.data().date).toISOString().slice(0, 10);
8191
docsData[date] = true;

0 commit comments

Comments
 (0)