From 3afaedd878d7fe1c139e7d616218519dd55da15a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adina=20=C8=9Aeudan?= Date: Thu, 18 Nov 2021 16:11:02 +0200 Subject: [PATCH] Prepare for dossier changes check --- .../pdf-viewer/pdf-viewer.component.ts | 2 +- .../type-annotation-icon.component.ts | 2 +- .../file-workload-column.component.ts | 2 +- .../dossiers-listing-screen.component.ts | 13 +++--- .../entity-services/dossiers.service.ts | 42 +++++++++++-------- apps/red-ui/src/app/state/app-state.guard.ts | 2 +- 6 files changed, 37 insertions(+), 26 deletions(-) diff --git a/apps/red-ui/src/app/modules/dossier/components/pdf-viewer/pdf-viewer.component.ts b/apps/red-ui/src/app/modules/dossier/components/pdf-viewer/pdf-viewer.component.ts index 8db951a0d..2b98ab406 100644 --- a/apps/red-ui/src/app/modules/dossier/components/pdf-viewer/pdf-viewer.component.ts +++ b/apps/red-ui/src/app/modules/dossier/components/pdf-viewer/pdf-viewer.component.ts @@ -34,7 +34,7 @@ import { PdfViewerUtils } from '../../utils/pdf-viewer.utils'; import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker'; import { ActivatedRoute } from '@angular/router'; import { toPosition } from '../../utils/pdf-calculation.utils'; -import { DossiersService } from '../../../../services/entity-services/dossiers.service'; +import { DossiersService } from '@services/entity-services/dossiers.service'; import Tools = Core.Tools; import TextTool = Tools.TextTool; import Annotation = Core.Annotations.Annotation; diff --git a/apps/red-ui/src/app/modules/dossier/components/type-annotation-icon/type-annotation-icon.component.ts b/apps/red-ui/src/app/modules/dossier/components/type-annotation-icon/type-annotation-icon.component.ts index ebdc5ee76..7476a9027 100644 --- a/apps/red-ui/src/app/modules/dossier/components/type-annotation-icon/type-annotation-icon.component.ts +++ b/apps/red-ui/src/app/modules/dossier/components/type-annotation-icon/type-annotation-icon.component.ts @@ -1,7 +1,7 @@ import { Component, Input, OnChanges } from '@angular/core'; import { AnnotationWrapper } from '@models/file/annotation.wrapper'; import { AppStateService } from '@state/app-state.service'; -import { DossiersService } from '../../../../services/entity-services/dossiers.service'; +import { DossiersService } from '@services/entity-services/dossiers.service'; import { File } from '@red/domain'; @Component({ diff --git a/apps/red-ui/src/app/modules/dossier/screens/dossier-overview/components/table-item/file-workload-column/file-workload-column.component.ts b/apps/red-ui/src/app/modules/dossier/screens/dossier-overview/components/table-item/file-workload-column/file-workload-column.component.ts index 744f05b58..5894045a9 100644 --- a/apps/red-ui/src/app/modules/dossier/screens/dossier-overview/components/table-item/file-workload-column/file-workload-column.component.ts +++ b/apps/red-ui/src/app/modules/dossier/screens/dossier-overview/components/table-item/file-workload-column/file-workload-column.component.ts @@ -1,7 +1,7 @@ import { ChangeDetectionStrategy, Component, Input } from '@angular/core'; import { AppStateService } from '@state/app-state.service'; import { File } from '@red/domain'; -import { DossiersService } from '../../../../../../../services/entity-services/dossiers.service'; +import { DossiersService } from '@services/entity-services/dossiers.service'; @Component({ selector: 'redaction-file-workload-column', diff --git a/apps/red-ui/src/app/modules/dossier/screens/dossiers-listing/screen/dossiers-listing-screen.component.ts b/apps/red-ui/src/app/modules/dossier/screens/dossiers-listing/screen/dossiers-listing-screen.component.ts index 8e87d9ef6..70634dfc4 100644 --- a/apps/red-ui/src/app/modules/dossier/screens/dossiers-listing/screen/dossiers-listing-screen.component.ts +++ b/apps/red-ui/src/app/modules/dossier/screens/dossiers-listing/screen/dossiers-listing-screen.component.ts @@ -12,7 +12,8 @@ import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker'; import { ConfigService } from '../config.service'; import { DossiersService } from '@services/entity-services/dossiers.service'; import { FilesService } from '@services/entity-services/files.service'; -import { DossierTemplatesService } from '../../../../../services/entity-services/dossier-templates.service'; +import { DossierTemplatesService } from '@services/entity-services/dossier-templates.service'; +import { switchMap, tap } from 'rxjs/operators'; @Component({ templateUrl: './dossiers-listing-screen.component.html', @@ -59,10 +60,12 @@ export class DossiersListingScreenComponent extends ListingComponent im ngOnInit(): void { this.computeAllFilters(); - this.addSubscription = timer(0, 20000).subscribe(async () => { - await this._dossiersService.loadAll().toPromise(); - this.computeAllFilters(); - }); + this.addSubscription = timer(20000, 20000) + .pipe( + switchMap(() => this._dossiersService.loadAll()), + tap(() => this.computeAllFilters()), + ) + .subscribe(); } ngOnAttach(): void { diff --git a/apps/red-ui/src/app/services/entity-services/dossiers.service.ts b/apps/red-ui/src/app/services/entity-services/dossiers.service.ts index 479512702..fa5475ea0 100644 --- a/apps/red-ui/src/app/services/entity-services/dossiers.service.ts +++ b/apps/red-ui/src/app/services/entity-services/dossiers.service.ts @@ -1,8 +1,8 @@ import { Injectable, Injector } from '@angular/core'; -import { EntitiesService, List, mapEach, QueryParam, RequiredParam, shareLast, Toaster, Validate } from '@iqser/common-ui'; +import { EntitiesService, List, QueryParam, RequiredParam, shareLast, Toaster, Validate } from '@iqser/common-ui'; import { Dossier, IDossier, IDossierRequest } from '@red/domain'; -import { catchError, filter, map, switchMap, tap, withLatestFrom } from 'rxjs/operators'; -import { combineLatest, Observable, of, throwError } from 'rxjs'; +import { catchError, filter, map, mapTo, switchMap, tap } from 'rxjs/operators'; +import { combineLatest, iif, Observable, of, throwError } from 'rxjs'; import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker'; import { HttpErrorResponse } from '@angular/common/http'; import { DossierStatsService } from '@services/entity-services/dossier-stats.service'; @@ -20,6 +20,7 @@ const GENERIC_MGS = _('add-dossier-dialog.errors.generic'); }) export class DossiersService extends EntitiesService { readonly generalStats$ = this.all$.pipe(switchMap(entities => this._generalStats$(entities))); + private _lastCheckedForChanges = '0'; constructor( private readonly _toaster: Toaster, @@ -29,18 +30,25 @@ export class DossiersService extends EntitiesService { super(_injector, Dossier, 'dossier'); } - loadAll(modelPath: string = this._defaultModelPath, queryParams?: List): Observable { - const req$ = super.loadAll(modelPath, queryParams).pipe(shareLast()); - const getStats$ = req$.pipe( - mapEach(dossier => dossier.dossierId), - switchMap(ids => this._dossierStatsService.getFor(ids)), + private get _hasChanges$(): Observable { + return this._post<{ value: boolean }>({ since: this._lastCheckedForChanges }, `${this._defaultModelPath}/changes`).pipe( + map(res => res.value), + tap(() => this._updateLastChanged()), ); - return getStats$.pipe( - withLatestFrom(req$), - map(([, dossiers]) => dossiers), + } + + loadAll(): Observable { + const dossierIds = (dossiers: Dossier[]) => dossiers.map(d => d.dossierId); + return super.loadAll().pipe( + tap(() => this._updateLastChanged()), + switchMap(dossiers => this._dossierStatsService.getFor(dossierIds(dossiers)).pipe(mapTo(dossiers))), ); } + loadAllIfChanged(): Observable { + return this._hasChanges$.pipe(switchMap(changed => iif(() => changed, this.loadAll()).pipe(mapTo(changed)))); + } + @Validate() createOrUpdate(@RequiredParam() dossier: IDossierRequest): Observable { const showToast = (error: HttpErrorResponse) => { @@ -48,12 +56,8 @@ export class DossiersService extends EntitiesService { return throwError(error); }; - const dossier$ = this._post(dossier).pipe(shareLast()); - const stats$ = dossier$.pipe(switchMap(updatedDossier => this._dossierStatsService.getFor([updatedDossier.dossierId]))); - - return combineLatest([dossier$, stats$]).pipe( - map(([updatedDossier]) => new Dossier(updatedDossier)), - tap(newDossier => this.replace([newDossier])), + return this._post(dossier).pipe( + switchMap(newDossier => this.loadAll().pipe(map(() => this.find(newDossier.dossierId)))), catchError(showToast), ); } @@ -84,6 +88,10 @@ export class DossiersService extends EntitiesService { return super.delete(body, 'deleted-dossiers/hard-delete', body).toPromise(); } + private _updateLastChanged(): void { + this._lastCheckedForChanges = new Date().toISOString(); + } + private _computeStats(entities: List): IDossiersStats { let totalAnalyzedPages = 0; const totalPeople = new Set(); diff --git a/apps/red-ui/src/app/state/app-state.guard.ts b/apps/red-ui/src/app/state/app-state.guard.ts index 1f257e980..ea33088c3 100644 --- a/apps/red-ui/src/app/state/app-state.guard.ts +++ b/apps/red-ui/src/app/state/app-state.guard.ts @@ -31,7 +31,7 @@ export class AppStateGuard implements CanActivate { } if (this._userService.currentUser.isUser) { - await this._dossiersService.loadAllIfEmpty(); + await this._dossiersService.loadAll().toPromise(); } const { dossierId, fileId, dossierTemplateId, type } = route.params;