@@ -7,27 +7,63 @@ const {
7
7
} = require ( "../constants/progresses" ) ;
8
8
const progressesCollection = fireStore . collection ( "progresses" ) ;
9
9
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
+ */
10
19
const buildQueryForPostingProgress = ( { type, userId, taskId } ) => {
11
20
const query =
12
21
type === "user"
13
22
? progressesCollection . where ( "userId" , "==" , userId )
14
23
: progressesCollection . where ( "taskId" , "==" , taskId ) ;
15
24
return query ;
16
25
} ;
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
+ */
17
35
const assertUserExists = async ( userId ) => {
18
36
const { userExists } = await fetchUser ( { userId } ) ;
19
37
if ( ! userExists ) {
20
38
throw new NotFound ( `User with id ${ userId } does not exist.` ) ;
21
39
}
22
40
} ;
23
41
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
+ */
24
50
const assertTaskExists = async ( taskId ) => {
25
51
const { taskData } = await fetchTask ( taskId ) ;
26
52
if ( ! taskData ) {
27
53
throw new NotFound ( `Task with id ${ taskId } does not exist.` ) ;
28
54
}
29
55
} ;
30
56
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
+ */
31
67
const assertUserOrTaskExists = async ( queryParams ) => {
32
68
const { userId, taskId } = queryParams ;
33
69
if ( userId ) {
@@ -36,6 +72,15 @@ const assertUserOrTaskExists = async (queryParams) => {
36
72
await assertTaskExists ( taskId ) ;
37
73
}
38
74
} ;
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
+ */
39
84
const buildQueryToFetchDocs = ( queryParams ) => {
40
85
const { type, userId, taskId } = queryParams ;
41
86
let query ;
@@ -51,6 +96,12 @@ const buildQueryToFetchDocs = (queryParams) => {
51
96
return query ;
52
97
} ;
53
98
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
+ */
54
105
const getProgressDocs = async ( query ) => {
55
106
const progressesDocs = await query . get ( ) ;
56
107
if ( ! progressesDocs . size ) {
@@ -62,7 +113,16 @@ const getProgressDocs = async (query) => {
62
113
} ) ;
63
114
return docsData ;
64
115
} ;
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
+ */
66
126
const buildRangeProgressQuery = ( queryParams ) => {
67
127
const { userId, taskId, startDate, endDate } = queryParams ;
68
128
let query = progressesCollection ;
@@ -78,7 +138,14 @@ const buildRangeProgressQuery = (queryParams) => {
78
138
query = query . where ( "date" , ">=" , startDateTimestamp ) . where ( "date" , "<=" , endDateTimestamp ) ;
79
139
return query ;
80
140
} ;
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
+ */
82
149
const getProgressRecords = async ( query , queryParams ) => {
83
150
const { startDate, endDate } = queryParams ;
84
151
const docsData = { } ;
0 commit comments