/** Service for storing and fetching data related to offline services/interfaces */
+const OFFLINE_CACHE_TIMEOUT = 86400000; // 1 day
+
@Injectable()
export class OfflineService {
private db: DbStoreService
) {}
+ refreshOfflineData(): Promise<any> {
+ return this.cacheNeedsUpdating()
+ .then(needs => {
+ if (needs) {
+ return this.clearOfflineCache()
+ .then(_ => this.fetchOfflineData());
+ }
+ });
+ }
+
clearOfflineCache(): Promise<any> {
return this.db.request(
{schema: 'cache', table: 'Object', action: 'deleteAll'})
fetchOfflineData(): Promise<any> {
- // TODO check cache date first or just always grab it?
- // TODO add setting that let's users opt-out of loading offline data.
-
- return this.clearOfflineCache()
-
- .then(_ => {
-
- // Start with the org unit list which is already loaded.
- this.addListToCache('aou', this.org.list());
-
- return this.net.request(
- 'open-ils.circ',
- 'open-ils.circ.offline.data.retrieve',
- this.auth.token()
- )
- .pipe(concatMap(data => {
- if (data.idl_class === 'actsc') {
- return from(this.addStatCatsToCache(data.data));
- } else {
- return from(this.addListToCache(data.idl_class, data.data));
- }
- }))
- .toPromise();
+ // Start with the org unit list which is already loaded.
+ this.addListToCache('aou', this.org.list());
+
+ return this.net.request(
+ 'open-ils.circ',
+ 'open-ils.circ.offline.data.retrieve',
+ this.auth.token()
+ )
+ .pipe(concatMap(data => {
+ if (data.idl_class === 'actsc') {
+ return from(this.addStatCatsToCache(data.data));
+ } else {
+ return from(this.addListToCache(data.idl_class, data.data));
+ }
+ }))
+ .toPromise();
+ }
+
+ cacheNeedsUpdating(): Promise<boolean> {
+
+ return this.db.request({
+ schema: 'cache',
+ table: 'CacheDate',
+ action: 'selectWhereEqual',
+ field: 'type',
+ value: 'pgt' // most likely to have data
+ }).then(rows => {
+ const row = rows[0];
+ if (!row) { return true; }
+ return (new Date().getTime() - row.cachedate.getTime()) > OFFLINE_CACHE_TIMEOUT;
});
}