| 
									
										
										
										
											2018-06-07 22:35:06 +01:00
										 |  |  | import { Observable } from 'rx'; | 
					
						
							| 
									
										
										
										
											2018-12-01 11:22:34 +00:00
										 |  |  | import debug from 'debug'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-02 13:38:03 +00:00
										 |  |  | import { reportError } from '../middlewares/error-reporter'; | 
					
						
							| 
									
										
										
										
											2018-12-01 11:22:34 +00:00
										 |  |  | import InMemoryCache from '../utils/in-memory-cache'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const log = debug('fcc:boot:donate'); | 
					
						
							|  |  |  | const fiveMinutes = 1000 * 60 * 5; | 
					
						
							| 
									
										
										
										
											2018-06-07 22:35:06 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | export default function(Donation) { | 
					
						
							| 
									
										
										
										
											2018-12-01 11:22:34 +00:00
										 |  |  |   let activeDonationUpdateInterval = null; | 
					
						
							|  |  |  |   const activeDonationCountCacheTTL = fiveMinutes; | 
					
						
							| 
									
										
										
										
											2018-12-02 13:38:03 +00:00
										 |  |  |   const activeDonationCountCache = InMemoryCache(0, reportError); | 
					
						
							| 
									
										
										
										
											2018-12-01 11:22:34 +00:00
										 |  |  |   const activeDonationsQuery$ = () => | 
					
						
							|  |  |  |     Donation.find$({ | 
					
						
							|  |  |  |       // eslint-disable-next-line no-undefined
 | 
					
						
							|  |  |  |       where: { endDate: undefined } | 
					
						
							|  |  |  |     }).map(instances => instances.length); | 
					
						
							|  |  |  |   function cleanUp() { | 
					
						
							|  |  |  |     if (activeDonationUpdateInterval) { | 
					
						
							|  |  |  |       clearInterval(activeDonationUpdateInterval); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   process.on('exit', cleanUp); | 
					
						
							| 
									
										
										
										
											2018-12-02 13:38:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-07 22:35:06 +01:00
										 |  |  |   Donation.on('dataSourceAttached', () => { | 
					
						
							| 
									
										
										
										
											2018-11-29 14:24:17 +00:00
										 |  |  |     Donation.find$ = Observable.fromNodeCallback(Donation.find.bind(Donation)); | 
					
						
							| 
									
										
										
										
											2018-06-07 22:35:06 +01:00
										 |  |  |     Donation.findOne$ = Observable.fromNodeCallback( | 
					
						
							|  |  |  |       Donation.findOne.bind(Donation) | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2018-12-01 11:22:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-02 13:38:03 +00:00
										 |  |  |     seedTheCache() | 
					
						
							|  |  |  |       .then(setupCacheUpdateInterval) | 
					
						
							|  |  |  |       .catch(err => { | 
					
						
							|  |  |  |         const errMsg = `Error caught seeding the cache: ${err.message}`; | 
					
						
							|  |  |  |         err.message = errMsg; | 
					
						
							|  |  |  |         reportError(err); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function seedTheCache() { | 
					
						
							|  |  |  |     return new Promise((resolve, reject) => | 
					
						
							|  |  |  |       Observable.defer(activeDonationsQuery$).subscribe(count => { | 
					
						
							|  |  |  |         log('activeDonator count: %d', count); | 
					
						
							|  |  |  |         activeDonationCountCache.update(() => count); | 
					
						
							|  |  |  |         return resolve(); | 
					
						
							|  |  |  |       }, reject) | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function setupCacheUpdateInterval() { | 
					
						
							| 
									
										
										
										
											2018-12-01 11:22:34 +00:00
										 |  |  |     activeDonationUpdateInterval = setInterval( | 
					
						
							|  |  |  |       () => | 
					
						
							| 
									
										
										
										
											2018-12-02 13:38:03 +00:00
										 |  |  |         Observable.defer(activeDonationsQuery$).subscribe( | 
					
						
							|  |  |  |           count => { | 
					
						
							|  |  |  |             log('activeDonator count: %d', count); | 
					
						
							|  |  |  |             return activeDonationCountCache.update(() => count); | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |           err => { | 
					
						
							|  |  |  |             const errMsg = `Error caught updating the cache: ${err.message}`; | 
					
						
							|  |  |  |             err.message = errMsg; | 
					
						
							|  |  |  |             reportError(err); | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         ), | 
					
						
							| 
									
										
										
										
											2018-12-01 11:22:34 +00:00
										 |  |  |       activeDonationCountCacheTTL | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2018-12-02 13:38:03 +00:00
										 |  |  |     return null; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-11-29 14:24:17 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   function getCurrentActiveDonationCount$() { | 
					
						
							| 
									
										
										
										
											2018-12-01 11:22:34 +00:00
										 |  |  |     return Observable.of(activeDonationCountCache.get()); | 
					
						
							| 
									
										
										
										
											2018-11-29 14:24:17 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Donation.getCurrentActiveDonationCount$ = getCurrentActiveDonationCount$; | 
					
						
							| 
									
										
										
										
											2018-06-07 22:35:06 +01:00
										 |  |  | } |