From bf68cc4e2dda5bb09858e13c98ce0f1f0fe9ba63 Mon Sep 17 00:00:00 2001 From: Bill Erickson Date: Mon, 26 Apr 2021 16:20:50 -0400 Subject: [PATCH] LP1904036 offline data checks cache date Signed-off-by: Bill Erickson Signed-off-by: Jane Sandberg Signed-off-by: Galen Charlton --- Open-ILS/src/eg2/src/app/staff/login.component.ts | 2 +- .../src/eg2/src/app/staff/share/offline.service.ts | 65 ++++++++++++++-------- 2 files changed, 43 insertions(+), 24 deletions(-) diff --git a/Open-ILS/src/eg2/src/app/staff/login.component.ts b/Open-ILS/src/eg2/src/app/staff/login.component.ts index 7846285922..6c7b64d94f 100644 --- a/Open-ILS/src/eg2/src/app/staff/login.component.ts +++ b/Open-ILS/src/eg2/src/app/staff/login.component.ts @@ -100,7 +100,7 @@ export class StaffLoginComponent implements OnInit { } else { - this.offline.fetchOfflineData() + this.offline.refreshOfflineData() // Initial login clears cached org unit settings. .then(_ => this.org.clearCachedSettings()) .then(_ => { diff --git a/Open-ILS/src/eg2/src/app/staff/share/offline.service.ts b/Open-ILS/src/eg2/src/app/staff/share/offline.service.ts index f9fefe4513..4130d24fe8 100644 --- a/Open-ILS/src/eg2/src/app/staff/share/offline.service.ts +++ b/Open-ILS/src/eg2/src/app/staff/share/offline.service.ts @@ -12,6 +12,8 @@ import {DbStoreService} from '@eg/core/db-store.service'; /** Service for storing and fetching data related to offline services/interfaces */ +const OFFLINE_CACHE_TIMEOUT = 86400000; // 1 day + @Injectable() export class OfflineService { @@ -27,6 +29,16 @@ export class OfflineService { private db: DbStoreService ) {} + refreshOfflineData(): Promise { + return this.cacheNeedsUpdating() + .then(needs => { + if (needs) { + return this.clearOfflineCache() + .then(_ => this.fetchOfflineData()); + } + }); + } + clearOfflineCache(): Promise { return this.db.request( {schema: 'cache', table: 'Object', action: 'deleteAll'}) @@ -38,29 +50,36 @@ export class OfflineService { fetchOfflineData(): Promise { - // 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 { + + 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; }); } -- 2.11.0