Skip to content

Commit 31b6deb

Browse files
committed
fix the review comments by @RitikJaiswal75
1 parent 1490248 commit 31b6deb

File tree

3 files changed

+18
-27
lines changed

3 files changed

+18
-27
lines changed

middlewares/validators/monitor.js

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const baseSchema = joi
77
.keys({
88
type: joi
99
.string()
10-
.trim()
1110
.valid(...VALID_PROGRESS_TYPES)
1211
.required()
1312
.messages({
@@ -35,28 +34,20 @@ const baseSchema = joi
3534
"number.positive": "'frequency' field must be a positive integer",
3635
"any.only": "'frequency' field must be equal to 1 for type 'user'",
3736
}),
38-
taskId: joi
39-
.string()
40-
.trim()
41-
.when("type", {
42-
is: "task",
43-
then: joi.required().messages({
44-
"any.required": "Required field 'taskId' is missing.",
45-
"string.trim": "taskId must not have leading or trailing whitespace",
46-
}),
47-
otherwise: joi.optional(),
37+
taskId: joi.string().when("type", {
38+
is: "task",
39+
then: joi.required().messages({
40+
"any.required": "Required field 'taskId' is missing.",
4841
}),
49-
userId: joi
50-
.string()
51-
.trim()
52-
.when("type", {
53-
is: "user",
54-
then: joi.required().messages({
55-
"any.required": "Required field 'userId' is missing.",
56-
"string.trim": "userId must not have leading or trailing whitespace",
57-
}),
58-
otherwise: joi.optional(),
42+
otherwise: joi.optional(),
43+
}),
44+
userId: joi.string().when("type", {
45+
is: "user",
46+
then: joi.required().messages({
47+
"any.required": "Required field 'userId' is missing.",
5948
}),
49+
otherwise: joi.optional(),
50+
}),
6051
})
6152
.messages({ "object.unknown": "Invalid field provided." });
6253

models/monitor.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const fireStore = require("../utils/firestore");
33
const trackedProgressesCollection = fireStore.collection("trackedProgresses");
44
const { assertUserOrTaskExists } = require("../utils/progresses");
55
const {
6-
buildQueryToCheckIfDocExists,
6+
buildTrackedProgressQueryByType,
77
buildQueryForFetchingDocsOfType,
88
getTrackedProgressDocs,
99
buildQueryToFetchTrackedDoc,
@@ -27,7 +27,7 @@ const { RESOURCE_NOT_FOUND } = RESPONSE_MESSAGES;
2727
const createTrackedProgressDocument = async (documentData) => {
2828
const { userId, taskId } = documentData;
2929
await assertUserOrTaskExists({ userId, taskId });
30-
const query = buildQueryToCheckIfDocExists({ userId, taskId });
30+
const query = buildTrackedProgressQueryByType({ userId, taskId });
3131
const existingDocumentSnapshot = await query.get();
3232
if (!existingDocumentSnapshot.empty) {
3333
throw new Conflict("Resource is already being tracked.");
@@ -59,7 +59,7 @@ const createTrackedProgressDocument = async (documentData) => {
5959
const updateTrackedProgressDocument = async (req) => {
6060
const { type, typeId } = req.params;
6161
const updatedData = { type, [`${type}Id`]: typeId };
62-
const query = buildQueryToCheckIfDocExists(updatedData);
62+
const query = buildTrackedProgressQueryByType(updatedData);
6363
const existingDocumentSnapshot = await query.get();
6464
if (existingDocumentSnapshot.empty) {
6565
throw new NotFound(RESOURCE_NOT_FOUND);

utils/monitor.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ const { RESPONSE_MESSAGES } = require("../constants/monitor");
55
const { RESOURCE_NOT_FOUND } = RESPONSE_MESSAGES;
66

77
/**
8-
* Builds a Firestore query to check if a document exists based on the provided query parameters.
8+
* Builds a Firestore query based on the provided query parameters.
99
*
1010
* @param {Object} queryParams - The query parameters for checking if a document exists.
1111
* @param {string} queryParams.userId - The ID of the user (optional).
1212
* @param {string} queryParams.taskId - The ID of the task (optional).
1313
* @returns {Firestore.Query} - A Firestore query to check if a document exists.
1414
*/
15-
const buildQueryToCheckIfDocExists = (queryParams) => {
15+
const buildTrackedProgressQueryByType = (queryParams) => {
1616
const { userId, taskId } = queryParams;
1717
if (userId) {
1818
return trackedProgressesCollection.where("userId", "==", userId);
@@ -74,7 +74,7 @@ const getTrackedProgressDocs = async (query) => {
7474
};
7575

7676
module.exports = {
77-
buildQueryToCheckIfDocExists,
77+
buildTrackedProgressQueryByType,
7878
buildQueryForFetchingDocsOfType,
7979
getTrackedProgressDocs,
8080
buildQueryToFetchTrackedDoc,

0 commit comments

Comments
 (0)