@@ -15,8 +15,8 @@ const FormData = require('form-data');
15
15
const NodeCache = require ( 'node-cache' ) ;
16
16
17
17
// gigs list caching
18
- const gigsCache = new NodeCache ( { stdTTL : config . GIGS_LISTING_CACHE_TIME , checkperiod : 10 } ) ;
19
18
const CACHE_KEY = 'jobs' ;
19
+ const gigsCache = new NodeCache ( { stdTTL : config . GIGS_LISTING_CACHE_TIME , checkperiod : 10 } ) ;
20
20
21
21
const JOB_FIELDS_RESPONSE = [
22
22
'id' ,
@@ -174,6 +174,63 @@ export default class RecruitCRMService {
174
174
}
175
175
}
176
176
177
+ /**
178
+ * Gets all jobs method.
179
+ * @return {Promise }
180
+ * @param {Object } query the request query.
181
+ */
182
+ async getAll ( query ) {
183
+ try {
184
+ const response = await fetch ( `${ this . private . baseUrl } /v1/jobs/search?${ qs . stringify ( query ) } ` , {
185
+ method : 'GET' ,
186
+ headers : {
187
+ 'Content-Type' : 'application/json' ,
188
+ Authorization : this . private . authorization ,
189
+ } ,
190
+ } ) ;
191
+ if ( response . status === 429 ) {
192
+ await new Promise ( resolve => setTimeout ( resolve , 30000 ) ) ; // wait 30sec
193
+ return this . getAll ( query ) ;
194
+ }
195
+ if ( response . status >= 400 ) {
196
+ const error = {
197
+ error : true ,
198
+ status : response . status ,
199
+ url : `${ this . private . baseUrl } /v1/jobs/search?${ qs . stringify ( query ) } ` ,
200
+ errObj : await response . json ( ) ,
201
+ } ;
202
+ return error ;
203
+ }
204
+ const data = await response . json ( ) ;
205
+ if ( data . current_page < data . last_page ) {
206
+ const pages = _ . range ( 2 , data . last_page + 1 ) ;
207
+ return Promise . all (
208
+ pages . map ( page => fetch ( `${ this . private . baseUrl } /v1/jobs/search?${ qs . stringify ( query ) } &page=${ page } ` , {
209
+ method : 'GET' ,
210
+ headers : {
211
+ 'Content-Type' : 'application/json' ,
212
+ Authorization : this . private . authorization ,
213
+ } ,
214
+ } ) ) ,
215
+ )
216
+ . then ( async ( allPages ) => {
217
+ // eslint-disable-next-line no-restricted-syntax
218
+ for ( const pageDataRsp of allPages ) {
219
+ // eslint-disable-next-line no-await-in-loop
220
+ const pageData = await pageDataRsp . json ( ) ;
221
+ data . data = _ . flatten ( data . data . concat ( pageData . data ) ) ;
222
+ }
223
+ const toSend = _ . map ( data . data , j => _ . pick ( j , JOB_FIELDS_RESPONSE ) ) ;
224
+ return toSend ;
225
+ } ) ;
226
+ }
227
+ const toSend = _ . map ( data . data , j => _ . pick ( j , JOB_FIELDS_RESPONSE ) ) ;
228
+ return toSend ;
229
+ } catch ( err ) {
230
+ return err ;
231
+ }
232
+ }
233
+
177
234
/**
178
235
* Gets all jobs endpoint.
179
236
* @return {Promise }
@@ -694,3 +751,16 @@ export default class RecruitCRMService {
694
751
return data . data [ 0 ] ;
695
752
}
696
753
}
754
+
755
+ // Self update cache on expire to keep it fresh
756
+ gigsCache . on ( 'expired' , async ( key ) => {
757
+ if ( key === CACHE_KEY ) {
758
+ const ss = new RecruitCRMService ( ) ;
759
+ const gigs = await ss . getAll ( {
760
+ job_status : 1 ,
761
+ } ) ;
762
+ if ( ! gigs . error ) {
763
+ gigsCache . set ( CACHE_KEY , gigs ) ;
764
+ }
765
+ }
766
+ } ) ;
0 commit comments