LP1904036 offline data checks cache date
authorBill Erickson <berickxx@gmail.com>
Mon, 26 Apr 2021 20:20:50 +0000 (16:20 -0400)
committerGalen Charlton <gmc@equinoxOLI.org>
Fri, 28 Oct 2022 00:13:33 +0000 (20:13 -0400)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
Signed-off-by: Jane Sandberg <js7389@princeton.edu>
Signed-off-by: Galen Charlton <gmc@equinoxOLI.org>
Open-ILS/src/eg2/src/app/staff/login.component.ts
Open-ILS/src/eg2/src/app/staff/share/offline.service.ts

index 7846285..6c7b64d 100644 (file)
@@ -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(_ => {
index f9fefe4..4130d24 100644 (file)
@@ -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<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'})
@@ -38,29 +50,36 @@ export class OfflineService {
 
     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;
         });
     }