From 1270cb1753ee4dea7b27003b3f5bafdcc38afeef Mon Sep 17 00:00:00 2001 From: "Yassine R." Date: Wed, 14 Aug 2024 15:33:44 +0800 Subject: [PATCH] fix(frontend): add update of list every Five minutes --- .../usagers/controllers/usagers.controller.ts | 16 +++++ .../manage-usagers-page.component.ts | 59 +++++++++---------- .../services/manage-usagers.service.ts | 12 ++++ 3 files changed, 57 insertions(+), 30 deletions(-) diff --git a/packages/backend/src/usagers/controllers/usagers.controller.ts b/packages/backend/src/usagers/controllers/usagers.controller.ts index 6fec4843d4..a992aacf78 100644 --- a/packages/backend/src/usagers/controllers/usagers.controller.ts +++ b/packages/backend/src/usagers/controllers/usagers.controller.ts @@ -134,6 +134,22 @@ export class UsagersController { }; } + @Get("update-manage") + @AllowUserStructureRoles(...USER_STRUCTURE_ROLE_ALL) + public async updateManage(@CurrentUser() user: UserStructureAuthenticated) { + return usagerRepository + .createQueryBuilder() + .select(joinSelectFields(USAGER_LIGHT_ATTRIBUTES)) + .where( + `"structureId" = :structureId AND "updatedAt" >= :fiveMinutesAgo`, + { + structureId: user.structureId, + fiveMinutesAgo: new Date(Date.now() - 5 * 60 * 1000), + } + ) + .getRawMany(); + } + @Post("search-radies") @AllowUserStructureRoles(...USER_STRUCTURE_ROLE_ALL) public async searchInRadies( diff --git a/packages/frontend/src/app/modules/manage-usagers/components/manage-usagers-page/manage-usagers-page.component.ts b/packages/frontend/src/app/modules/manage-usagers/components/manage-usagers-page/manage-usagers-page.component.ts index 3068c9ffb5..324411116d 100644 --- a/packages/frontend/src/app/modules/manage-usagers/components/manage-usagers-page/manage-usagers-page.component.ts +++ b/packages/frontend/src/app/modules/manage-usagers/components/manage-usagers-page/manage-usagers-page.component.ts @@ -51,7 +51,7 @@ import { UserStructure } from "@domifa/common"; import { MatomoTracker } from "ngx-matomo-client"; import { AuthService, CustomToastService } from "../../../shared/services"; -const AUTO_REFRESH_PERIOD = 300000; // 5 minutes +const FIVE_MINUTES = 5 * 60 * 1000; @Component({ animations: [fadeInOut], @@ -152,12 +152,6 @@ export class ManageUsagersPageComponent implements OnInit, OnDestroy { if (!searchPageLoadedUsagersData.dataLoaded) { this.loadDataFromAPI(); } else { - if ( - searchPageLoadedUsagersData.usagersRadiesFirsts.length >= - searchPageLoadedUsagersData.usagersRadiesTotalCount - ) { - this.chargerTousRadies$.next(true); - } this.updateComponentState(searchPageLoadedUsagersData); } } @@ -173,9 +167,9 @@ export class ManageUsagersPageComponent implements OnInit, OnDestroy { map((value: string) => value.trim()), filter((value: string) => value !== this.filters.searchString), withLatestFrom(this.chargerTousRadies$), - switchMap(([searchString, chargerTousRadies]) => - this.findRemoteUsagers(chargerTousRadies, searchString) - ), + switchMap(([searchString, chargerTousRadies]) => { + return this.findRemoteUsagers(chargerTousRadies, searchString); + }), tap((searchString: string) => { this.filters.searchString = searchString ?? null; this.filters.page = 0; @@ -189,6 +183,20 @@ export class ManageUsagersPageComponent implements OnInit, OnDestroy { }) ); + this.subscription.add( + timer(FIVE_MINUTES, FIVE_MINUTES) + .pipe( + tap(() => { + this.searching = true; + }), + switchMap(() => this.usagerService.updateManage()) + ) + .subscribe(() => { + this.searching = false; + this.filters$.next(this.filters); + }) + ); + this.subscription.add( this.filters$.subscribe((filters) => { this.applyFilters({ @@ -201,31 +209,22 @@ export class ManageUsagersPageComponent implements OnInit, OnDestroy { private loadDataFromAPI() { this.subscription.add( - timer(0, AUTO_REFRESH_PERIOD) + this.chargerTousRadies$ .pipe( tap(() => { this.searching = true; }), + switchMap((chargerTousRadies) => + this.usagerService.getSearchPageUsagerData({ chargerTousRadies }) + ), switchMap(() => this.chargerTousRadies$), - switchMap((chargerTousRadies) => { - this.searching = true; - return this.usagerService.getSearchPageUsagerData({ - chargerTousRadies, - }); - }), - switchMap(() => this.chargerTousRadies$), - switchMap((chargerTousRadies) => { - // Call remote usagers to update list - if (!chargerTousRadies) { - return this.findRemoteUsagers( - chargerTousRadies, - this.filters.searchString - ); - } - return of(chargerTousRadies); - }) + switchMap((chargerTousRadies) => + this.findRemoteUsagers(chargerTousRadies, this.filters.searchString) + ) ) - .subscribe() + .subscribe(() => { + this.searching = false; + }) ); } @@ -235,7 +234,7 @@ export class ManageUsagersPageComponent implements OnInit, OnDestroy { ): Observable { if ( !chargerTousRadies && - searchString.length >= 3 && + searchString?.length >= 3 && (this.filters.statut === "TOUS" || this.filters.statut === "RADIE") ) { this.searching = true; diff --git a/packages/frontend/src/app/modules/manage-usagers/services/manage-usagers.service.ts b/packages/frontend/src/app/modules/manage-usagers/services/manage-usagers.service.ts index 77eb85c024..ef13fddfd1 100644 --- a/packages/frontend/src/app/modules/manage-usagers/services/manage-usagers.service.ts +++ b/packages/frontend/src/app/modules/manage-usagers/services/manage-usagers.service.ts @@ -72,4 +72,16 @@ export class ManageUsagersService { }) ); } + + public updateManage(): Observable { + return this.http + .get(`${environment.apiUrl}usagers/update-manage`) + .pipe( + tap((usagers: UsagerLight[]) => { + usagers.forEach((usager) => + this.store.dispatch(cacheManager.updateUsager({ usager })) + ); + }) + ); + } }