]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/offline.service.ts
LP2061136 - Stamping 1405 DB upgrade script
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / share / offline.service.ts
1 import {Observable} from 'rxjs';
2 import {from} from 'rxjs';
3 import {tap, concatMap} from 'rxjs/operators';
4 import {Injectable} from '@angular/core';
5 import {AuthService} from '@eg/core/auth.service';
6 import {EventService} from '@eg/core/event.service';
7 import {IdlObject, IdlService} from '@eg/core/idl.service';
8 import {NetService} from '@eg/core/net.service';
9 import {OrgService} from '@eg/core/org.service';
10 import {PcrudService} from '@eg/core/pcrud.service';
11 import {DbStoreService} from '@eg/core/db-store.service';
12
13 /** Service for storing and fetching data related to offline services/interfaces */
14
15 const OFFLINE_CACHE_TIMEOUT = 86400000; // 1 day
16
17 @Injectable()
18 export class OfflineService {
19
20     isOffline = false;
21
22     constructor(
23         private auth: AuthService,
24         private evt: EventService,
25         private idl: IdlService,
26         private net: NetService,
27         private org: OrgService,
28         private pcrud: PcrudService,
29         private db: DbStoreService
30     ) {}
31
32     refreshOfflineData(): Promise<any> {
33         return this.cacheNeedsUpdating()
34         .then(needs => {
35             if (needs) {
36                 return this.clearOfflineCache()
37                 .then(_ => this.fetchOfflineData());
38             }
39         });
40     }
41
42     clearOfflineCache(): Promise<any> {
43         return this.db.request(
44             {schema: 'cache', table: 'Object', action: 'deleteAll'})
45
46         .then(_ => this.db.request(
47             {schema: 'cache', table: 'StatCat', action: 'deleteAll'})
48         );
49     }
50
51     fetchOfflineData(): Promise<any> {
52
53         // Start with the org unit list which is already loaded.
54         this.addListToCache('aou', this.org.list());
55
56         return this.net.request(
57             'open-ils.circ',
58             'open-ils.circ.offline.data.retrieve',
59             this.auth.token()
60         )
61         .pipe(concatMap(data => {
62             if (data.idl_class === 'actsc') {
63                 return from(this.addStatCatsToCache(data.data));
64             } else {
65                 return from(this.addListToCache(data.idl_class, data.data));
66             }
67         }))
68         .toPromise();
69     }
70
71     cacheNeedsUpdating(): Promise<boolean> {
72
73         return this.db.request({
74             schema: 'cache',
75             table: 'CacheDate',
76             action: 'selectWhereEqual',
77             field: 'type',
78             value: 'pgt' // most likely to have data
79         }).then(rows => {
80             const row = rows[0];
81             if (!row) { return true; }
82             return (new Date().getTime() - row.cachedate.getTime()) > OFFLINE_CACHE_TIMEOUT;
83         });
84     }
85
86     addListToCache(idlClass: string, list: IdlObject[]): Promise<any> {
87
88         const pkey = this.idl.classes[idlClass].pkey;
89         const rows = list.map(item => {
90             return {
91                 type: idlClass,
92                 id: '' + item[pkey](),
93                 object: this.idl.toHash(item)
94             };
95         });
96
97         return this.db.request({
98             schema: 'cache',
99             table: 'Object',
100             action: 'insert',
101             rows: rows
102         }).then(resp => {
103             return this.db.request({
104                 schema: 'cache',
105                 table: 'CacheDate',
106                 action: 'insertOrReplace',
107                 rows: [{type: idlClass, cachedate: new Date()}]
108             });
109         });
110     }
111
112     addStatCatsToCache(statcats: IdlObject[]): Promise<any> {
113         if (!statcats || statcats.length === 0) {
114             return Promise.resolve();
115         }
116
117         const rows = statcats.map(
118             cat => ({id: cat.id(), value: this.idl.toHash(cat)}));
119
120         return this.db.request({
121             schema: 'cache',
122             table: 'StatCat',
123             action: 'insert',
124             rows: rows
125         });
126     }
127 }