@@ -10,6 +10,8 @@ import {
10
10
REQUEST_ALREADY_PENDING ,
11
11
} from "../constants/requests" ;
12
12
import * as admin from "firebase-admin" ;
13
+ import { getUserId } from "../utils/users" ;
14
+ const SIZE = 5 ;
13
15
14
16
export const createRequest = async ( body : any ) => {
15
17
try {
@@ -76,41 +78,90 @@ export const updateRequest = async (id: string, body: any, lastModifiedBy: strin
76
78
}
77
79
} ;
78
80
79
- export const getRequests = async ( query : RequestQuery ) => {
80
- const { id, type, requestedBy, state } = query ;
81
+ export const getRequests = async ( query : any ) => {
82
+ let { id, type, requestedBy, state, prev, next, page, size = SIZE } = query ;
83
+ size = parseInt ( size ) ;
84
+ page = parseInt ( page ) ;
81
85
try {
82
- let requestQuery : admin . firestore . Query = requestModel ;
86
+ let requestQuery : any = requestModel ;
87
+
83
88
if ( id ) {
84
- const requestsDoc = await requestModel . doc ( id ) . get ( ) ;
85
- const request = requestsDoc . data ( ) ;
86
- if ( ! request ) {
89
+ const requestDoc = await requestModel . doc ( id ) . get ( ) ;
90
+ if ( ! requestDoc . exists ) {
87
91
return null ;
88
92
}
89
93
return {
90
- id,
91
- ...request ,
94
+ allRequests : [
95
+ {
96
+ id : requestDoc . id ,
97
+ ...requestDoc . data ( ) ,
98
+ } ,
99
+ ] ,
92
100
} ;
93
101
}
102
+
103
+ if ( requestedBy ) {
104
+ const requestedByUserId = await getUserId ( requestedBy ) ;
105
+ requestQuery = requestQuery . where ( "requestedBy" , "==" , requestedByUserId ) ;
106
+ }
94
107
if ( type ) {
95
108
requestQuery = requestQuery . where ( "type" , "==" , type ) ;
96
109
}
97
- if ( requestedBy ) {
98
- requestQuery = requestQuery . where ( "requestedBy" , "==" , requestedBy ) ;
99
- }
100
110
if ( state ) {
101
111
requestQuery = requestQuery . where ( "state" , "==" , state ) ;
102
112
}
103
113
104
- const requestsDoc = await requestQuery . get ( ) ;
105
- if ( requestsDoc . empty ) {
114
+ requestQuery = requestQuery . orderBy ( "createdAt" , "desc" ) ;
115
+
116
+ let requestQueryDoc = requestQuery ;
117
+
118
+ if ( prev ) {
119
+ requestQueryDoc = requestQueryDoc . limitToLast ( size ) ;
120
+ } else {
121
+ requestQueryDoc = requestQueryDoc . limit ( size ) ;
122
+ }
123
+
124
+ if ( page ) {
125
+ const startAfter = ( page - 1 ) * size ;
126
+ requestQueryDoc = requestQueryDoc . offset ( startAfter ) ;
127
+ } else if ( next ) {
128
+ const doc = await requestModel . doc ( next ) . get ( ) ;
129
+ requestQueryDoc = requestQueryDoc . startAt ( doc ) ;
130
+ } else if ( prev ) {
131
+ const doc = await requestModel . doc ( prev ) . get ( ) ;
132
+ requestQueryDoc = requestQueryDoc . endAt ( doc ) ;
133
+ }
134
+
135
+ const snapshot = await requestQueryDoc . get ( ) ;
136
+ let nextDoc : any , prevDoc : any ;
137
+
138
+ if ( ! snapshot . empty ) {
139
+ const first = snapshot . docs [ 0 ] ;
140
+ prevDoc = await requestQuery . endBefore ( first ) . limitToLast ( 1 ) . get ( ) ;
141
+
142
+ const last = snapshot . docs [ snapshot . docs . length - 1 ] ;
143
+ nextDoc = await requestQuery . startAfter ( last ) . limit ( 1 ) . get ( ) ;
144
+ }
145
+
146
+ let allRequests = [ ] ;
147
+ if ( ! snapshot . empty ) {
148
+ snapshot . forEach ( ( doc : any ) => {
149
+ allRequests . push ( {
150
+ id : doc . id ,
151
+ ...doc . data ( ) ,
152
+ } ) ;
153
+ } ) ;
154
+ }
155
+ if ( allRequests . length === 0 ) {
106
156
return null ;
107
157
}
108
- return requestsDoc . docs . map ( ( doc : any ) => {
109
- return {
110
- id : doc . id ,
111
- ...doc . data ( ) ,
112
- } ;
113
- } ) ;
158
+
159
+ return {
160
+ allRequests,
161
+ prev : prevDoc . empty ? null : prevDoc . docs [ 0 ] . id ,
162
+ next : nextDoc . empty ? null : nextDoc . docs [ 0 ] . id ,
163
+ page : page ? page + 1 : null ,
164
+ } ;
114
165
} catch ( error ) {
115
166
logger . error ( ERROR_WHILE_FETCHING_REQUEST , error ) ;
116
167
throw error ;
0 commit comments