-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(geoDB): service to load data from config file into the indexed-db (
#1115) Co-authored-by: Pierre-Étienne Lord <[email protected]>
- Loading branch information
Showing
6 changed files
with
113 additions
and
3 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
packages/geo/src/lib/offline/geoDB/configFileToGeoDB.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { MessageService } from '@igo2/core'; | ||
import { HttpClient } from '@angular/common/http'; | ||
import { catchError, concatMap } from 'rxjs/operators'; | ||
import { GeoDBService } from './geoDB.service'; | ||
import { of, zip } from 'rxjs'; | ||
import { DatasToIDB, GeoDBData } from './geoDB.interface'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class ConfigFileToGeoDBService { | ||
|
||
|
||
constructor( | ||
private http: HttpClient, | ||
private geoDBService: GeoDBService, | ||
private messageService: MessageService | ||
) { } | ||
|
||
|
||
load(url: string) { | ||
let downloadMessage; | ||
this.http.get(url).pipe( | ||
|
||
catchError((error: any): any => { | ||
console.log(`GeoData file ${url} could not be read`); | ||
error.error.caught = true; | ||
throw error; | ||
}), | ||
concatMap((datasToIDB: DatasToIDB) => { | ||
const datas$ = []; | ||
let firstDownload = true; | ||
if (datasToIDB?.geoDatas) { | ||
const currentDate = new Date(); | ||
datasToIDB?.geoDatas.map((geoData) => { | ||
if (typeof geoData.triggerDate === 'string') { | ||
geoData.triggerDate = new Date(Date.parse(geoData.triggerDate.replace(/-/g, ' '))); | ||
} | ||
if (currentDate >= geoData.triggerDate) { | ||
if (geoData.action === 'update') { | ||
const insertEvent = `${geoData.source || 'automatedDataUpdate'} (${geoData.triggerDate})`; | ||
geoData.urls.map((url) => { | ||
datas$.push( | ||
this.geoDBService.getByID(url).pipe(concatMap((res: GeoDBData) => { | ||
if (res?.insertEvent !== insertEvent) { | ||
if (firstDownload) { | ||
downloadMessage = this.messageService | ||
.info('igo.geo.indexedDb.data-download-start', undefined, | ||
{ disableTimeOut: true, progressBar: false, closeButton: true, tapToDismiss: false }); | ||
firstDownload = false; | ||
} | ||
return this.http.get(url) | ||
.pipe(concatMap(r => this.geoDBService.update(url, url as any, r, 'system' as any, insertEvent))); | ||
} else { | ||
return of(false); | ||
} | ||
})) | ||
); | ||
}); | ||
} else if (geoData.action === 'delete') { | ||
geoData.urls.map((url) => { | ||
datas$.push(this.geoDBService.deleteByKey(url)); | ||
}); | ||
} | ||
} | ||
}); | ||
} | ||
return zip(...datas$); | ||
}) | ||
).subscribe(() => { | ||
if (downloadMessage) { | ||
setTimeout(() => { | ||
this.messageService.remove((downloadMessage as any).toastId); | ||
this.messageService.success('igo.geo.indexedDb.data-download-completed', undefined, { timeOut: 40000 }); | ||
}, 2500); | ||
} | ||
}); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './geoDB.enums'; | ||
export * from './geoDB.interface'; | ||
export * from './geoDB.service'; | ||
export * from './configFileToGeoDB.service'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters