Skip to content

Commit 0ab2700

Browse files
committed
Document util functions using JsDoc convention
1 parent d9aa912 commit 0ab2700

File tree

1 file changed

+69
-2
lines changed

1 file changed

+69
-2
lines changed

utils/progresses.js

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,63 @@ const {
77
} = require("../constants/progresses");
88
const progressesCollection = fireStore.collection("progresses");
99

10+
/**
11+
* Builds a Firestore query for posting progress documents based on the given parameters.
12+
*
13+
* @param {Object} params - An object containing the parameters for the query.
14+
* @param {string} params.type - The type of query to build, either "user" or "task".
15+
* @param {string} params.userId - The ID of the user to filter progress documents by, if `type` is "user".
16+
* @param {string} params.taskId - The ID of the task to filter progress documents by, if `type` is "task".
17+
* @returns {Query} A Firestore query object that filters progress documents based on the given parameters.
18+
*/
1019
const buildQueryForPostingProgress = ({ type, userId, taskId }) => {
1120
const query =
1221
type === "user"
1322
? progressesCollection.where("userId", "==", userId)
1423
: progressesCollection.where("taskId", "==", taskId);
1524
return query;
1625
};
26+
27+
/**
28+
* Checks if a user with a given ID exists in the system.
29+
*
30+
* @async
31+
* @param {string} userId - The ID of the user to check for existence.
32+
* @throws {NotFound} If the user with the given ID does not exist.
33+
* @returns {Promise<void>} A promise that resolves if the user exists and rejects with a `NotFound` error if the user does not exist.
34+
*/
1735
const assertUserExists = async (userId) => {
1836
const { userExists } = await fetchUser({ userId });
1937
if (!userExists) {
2038
throw new NotFound(`User with id ${userId} does not exist.`);
2139
}
2240
};
2341

42+
/**
43+
* Checks if a user with a given ID exists in the system.
44+
*
45+
* @async
46+
* @param {string} taskData - The ID of the task to check for existence.
47+
* @throws {NotFound} If the task with the given ID does not exist.
48+
* @returns {Promise<void>} A promise that resolves if the task exists and rejects with a `NotFound` error if the task does not exist.
49+
*/
2450
const assertTaskExists = async (taskId) => {
2551
const { taskData } = await fetchTask(taskId);
2652
if (!taskData) {
2753
throw new NotFound(`Task with id ${taskId} does not exist.`);
2854
}
2955
};
3056

57+
/**
58+
* Checks if a user or task with the given ID exists in the system.
59+
*
60+
* @async
61+
* @param {Object} queryParams - An object containing the query parameters.
62+
* @param {string} [queryParams.userId] - (Optional) The ID of the user to check for existence.
63+
* @param {string} [queryParams.taskId] - (Optional) The ID of the task to check for existence.
64+
* @throws {NotFound} If neither a user nor a task with the given ID exists in the system.
65+
* @returns {Promise<void>} A promise that resolves if either the user or the task exists and rejects with a `NotFound` error if neither exists.
66+
*/
3167
const assertUserOrTaskExists = async (queryParams) => {
3268
const { userId, taskId } = queryParams;
3369
if (userId) {
@@ -36,6 +72,15 @@ const assertUserOrTaskExists = async (queryParams) => {
3672
await assertTaskExists(taskId);
3773
}
3874
};
75+
76+
/**
77+
* Builds a Firestore query for retrieving progress documents within a date range and optionally filtered by user ID or task ID.
78+
* @param {Object} queryParams - An object containing the query parameters.
79+
* @param {string} queryParams.userId - (Optional) The user ID to filter progress documents by.
80+
* @param {string} queryParams.taskId - (Optional) The task ID to filter progress documents by.
81+
* @param {string} queryParams.type - (Optional) The type to filter progress documents by.
82+
* @returns {Query} A Firestore query object that filters progress documents based on the given parameters.
83+
*/
3984
const buildQueryToFetchDocs = (queryParams) => {
4085
const { type, userId, taskId } = queryParams;
4186
let query;
@@ -51,6 +96,12 @@ const buildQueryToFetchDocs = (queryParams) => {
5196
return query;
5297
};
5398

99+
/**
100+
* Retrieves progress documents from Firestore based on the given query.
101+
* @param {Query} query - A Firestore query object for fetching progress documents.
102+
* @returns {Array.<Object>} An array of objects representing the retrieved progress documents. Each object contains the document ID and its data.
103+
* @throws {NotFound} If no progress documents are found based on the given query.
104+
*/
54105
const getProgressDocs = async (query) => {
55106
const progressesDocs = await query.get();
56107
if (!progressesDocs.size) {
@@ -62,7 +113,16 @@ const getProgressDocs = async (query) => {
62113
});
63114
return docsData;
64115
};
65-
116+
/**
117+
* Builds a Firestore query for retrieving progress documents within a date range and optionally filtered by user ID or task ID.
118+
* @param {Object} queryParams - An object containing the query parameters.
119+
* @param {string} queryParams.userId - (Optional) The user ID to filter progress documents by.
120+
* @param {string} queryParams.taskId - (Optional) The task ID to filter progress documents by.
121+
* @param {string} queryParams.startDate - The start date of the date range (inclusive). Should be in ISO format (e.g. "2023-05-11").
122+
* @param {string} queryParams.endDate - The end date of the date range (inclusive). Should be in ISO format (e.g. "2023-05-11").
123+
* @returns {Query} A Firestore query object that filters progress documents based on the given parameters.
124+
* @throws {Error} If neither userId nor taskId is provided in the queryParams object.
125+
*/
66126
const buildRangeProgressQuery = (queryParams) => {
67127
const { userId, taskId, startDate, endDate } = queryParams;
68128
let query = progressesCollection;
@@ -78,7 +138,14 @@ const buildRangeProgressQuery = (queryParams) => {
78138
query = query.where("date", ">=", startDateTimestamp).where("date", "<=", endDateTimestamp);
79139
return query;
80140
};
81-
141+
/**
142+
* Retrieves progress records for a given date range.
143+
* @param {QuerySnapshot} query - A reference to the query for fetching progress documents.
144+
* @param {Object} queryParams - An object containing the start and end date for the date range.
145+
* @param {string} queryParams.startDate - The start date of the date range (inclusive).
146+
* @param {string} queryParams.endDate - The end date of the date range (inclusive).
147+
* @returns {Object.<string, boolean>} An object where each key represents a date between the start and end date, and the value for each key represents whether a progress document exists for that date or not.
148+
*/
82149
const getProgressRecords = async (query, queryParams) => {
83150
const { startDate, endDate } = queryParams;
84151
const docsData = {};

0 commit comments

Comments
 (0)