1
+ const { NotFound } = require ( "http-errors" ) ;
1
2
const { fetchTask } = require ( "../models/tasks" ) ;
2
3
const { fetchUser } = require ( "../models/users" ) ;
3
4
const fireStore = require ( "../utils/firestore" ) ;
5
+ const {
6
+ RESPONSE_MESSAGES : { PROGRESS_DOCUMENT_NOT_FOUND } ,
7
+ } = require ( "../constants/progresses" ) ;
4
8
const progressesCollection = fireStore . collection ( "progresses" ) ;
5
9
6
10
const buildQueryForPostingProgress = ( { type, userId, taskId } ) => {
@@ -13,14 +17,14 @@ const buildQueryForPostingProgress = ({ type, userId, taskId }) => {
13
17
const assertUserExists = async ( userId ) => {
14
18
const { userExists } = await fetchUser ( { userId } ) ;
15
19
if ( ! userExists ) {
16
- throw new Error ( `User with id ${ userId } does not exist` ) ;
20
+ throw new NotFound ( `User with id ${ userId } does not exist. ` ) ;
17
21
}
18
22
} ;
19
23
20
24
const assertTaskExists = async ( taskId ) => {
21
25
const { taskData } = await fetchTask ( taskId ) ;
22
26
if ( ! taskData ) {
23
- throw new Error ( `Task with id ${ taskId } does not exist` ) ;
27
+ throw new NotFound ( `Task with id ${ taskId } does not exist. ` ) ;
24
28
}
25
29
} ;
26
30
@@ -49,6 +53,9 @@ const buildQueryToFetchDocs = (queryParams) => {
49
53
50
54
const getProgressDocs = async ( query ) => {
51
55
const progressesDocs = await query . get ( ) ;
56
+ if ( ! progressesDocs . size ) {
57
+ throw new NotFound ( PROGRESS_DOCUMENT_NOT_FOUND ) ;
58
+ }
52
59
const docsData = [ ] ;
53
60
progressesDocs . forEach ( ( doc ) => {
54
61
docsData . push ( { id : doc . id , ...doc . data ( ) } ) ;
@@ -76,6 +83,9 @@ const getProgressRecords = async (query, queryParams) => {
76
83
const { startDate, endDate } = queryParams ;
77
84
const docsData = { } ;
78
85
const progressesDocs = ( await query . get ( ) ) . docs ;
86
+ if ( ! progressesDocs . size ) {
87
+ throw new NotFound ( PROGRESS_DOCUMENT_NOT_FOUND ) ;
88
+ }
79
89
progressesDocs . forEach ( ( doc ) => {
80
90
const date = new Date ( doc . data ( ) . date ) . toISOString ( ) . slice ( 0 , 10 ) ;
81
91
docsData [ date ] = true ;
0 commit comments