@@ -4,9 +4,9 @@ const util = require('util');
44const assert = require ( 'assert' ) ;
55
66const miteApi = require ( 'mite-api' ) ;
7-
87const MiteTracker = require ( './mite-tracker' ) ;
98const { BUDGET_TYPE } = require ( './../lib/constants' ) ;
9+ const Cache = require ( './cache' ) ;
1010
1111/**
1212 * @typedef MiteTimeEntryTracker
@@ -58,6 +58,7 @@ class MiteApiWrapper {
5858 this . config = config ;
5959 this . mite = miteApi ( config ) ;
6060 this . tracker = new MiteTracker ( config ) ;
61+ this . cache = new Cache ( config . cacheFilename ) ;
6162 }
6263
6364 /**
@@ -292,14 +293,19 @@ class MiteApiWrapper {
292293 return a - b ;
293294 }
294295
295- filterItem ( item , regexp ) {
296- return regexp . test ( ( item || { } ) . name ) ;
296+ removeItemByArchived ( item , archivedFlag ) {
297+ if ( typeof archivedFlag === 'boolean' ) {
298+ return item . archived === archivedFlag ;
299+ }
300+ return true ;
297301 }
298302
299- filterItems ( items , query ) {
300- if ( ! query ) return items ;
301- const queryRegexp = new RegExp ( query , 'i' ) ;
302- return items . filter ( item => this . filterItem ( item , queryRegexp ) ) ;
303+ itemMatchQuery ( item , query ) {
304+ if ( query ) {
305+ const regexp = new RegExp ( query , 'i' ) ;
306+ return regexp . test ( ( item || { } ) . name ) ;
307+ }
308+ return true ;
303309 }
304310
305311 /**
@@ -318,21 +324,26 @@ class MiteApiWrapper {
318324 } ;
319325 const itemNamePluralCamelCased = itemName . substr ( 0 , 1 ) . toUpperCase ( ) + itemName . substr ( 1 ) + 's' ;
320326 const opts = Object . assign ( { } , defaultOpts , options ) ;
321- return Promise . all ( [
322- util . promisify ( this . mite [ 'get' + itemNamePluralCamelCased ] ) ( opts ) ,
323- util . promisify ( this . mite [ 'getArchived' + itemNamePluralCamelCased ] ) ( opts ) ,
324- ] )
325- . then ( results => Array . prototype . concat . apply ( [ ] , results ) )
326- . then ( items => items . map ( c => c [ itemName ] ) )
327- . then ( items => items . filter ( item => {
328- if ( typeof options . archived === 'boolean' ) {
329- return item . archived === options . archived ;
330- }
331- return true ;
332- } ) )
333- . then ( items => this . filterItems ( items , options . query ) )
334- // always sort by name
335- . then ( items => this . sort ( items , 'name' ) ) ;
327+
328+ const cacheKey = [ 'getItemsAndArchived' , itemName , options ] ;
329+ let items = await this . cache . get ( cacheKey ) ;
330+ if ( ! items ) {
331+ items = await Promise . all ( [
332+ util . promisify ( this . mite [ 'get' + itemNamePluralCamelCased ] ) ( opts ) ,
333+ util . promisify ( this . mite [ 'getArchived' + itemNamePluralCamelCased ] ) ( opts ) ,
334+ ] ) ;
335+ items = Array . prototype . concat . apply ( [ ] , items )
336+ . map ( c => c [ itemName ] )
337+ . filter ( item => this . removeItemByArchived ( item , options . archived ) )
338+ . filter ( item => this . itemMatchQuery ( item , options . query ) ) ;
339+ this . sort ( items , 'name' ) ;
340+
341+ // cache values for 24 hours
342+ await this . cache . set ( cacheKey , items , { ttl : this . config . cacheTtl } ) ;
343+ await this . cache . save ( ) ;
344+ }
345+
346+ return items ;
336347 }
337348
338349 async getCustomers ( options = { } ) {
0 commit comments