RED-4061: allow access to archived dossiers
This commit is contained in:
parent
91715bb248
commit
d1b620eed9
@ -1,15 +1,19 @@
|
||||
import { Injectable, Injector, ProviderToken } from '@angular/core';
|
||||
import { ActivatedRouteSnapshot, CanActivate } from '@angular/router';
|
||||
import { ActivatedRouteSnapshot, CanActivate, Router } from '@angular/router';
|
||||
import { firstValueFrom, forkJoin } from 'rxjs';
|
||||
import { take } from 'rxjs/operators';
|
||||
import { ActiveDossiersService } from '@services/dossiers/active-dossiers.service';
|
||||
import { ArchivedDossiersService } from '@services/dossiers/archived-dossiers.service';
|
||||
import { DossiersService } from '@services/dossiers/dossiers.service';
|
||||
import { ARCHIVE_ROUTE, DOSSIER_TEMPLATE_ID } from '@red/domain';
|
||||
import { DashboardStatsService } from '@services/dossier-templates/dashboard-stats.service';
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class DossiersGuard implements CanActivate {
|
||||
constructor(
|
||||
private readonly _injector: Injector,
|
||||
private readonly _router: Router,
|
||||
private readonly _dashboardStatsService: DashboardStatsService,
|
||||
private readonly _activeDossiersService: ActiveDossiersService,
|
||||
private readonly _archivedDossiersService: ArchivedDossiersService,
|
||||
) {}
|
||||
@ -18,6 +22,17 @@ export class DossiersGuard implements CanActivate {
|
||||
const token: ProviderToken<DossiersService> = route.data.dossiersService;
|
||||
if (token) {
|
||||
const dossiersService: DossiersService = this._injector.get<DossiersService>(token);
|
||||
const isArchive = dossiersService.routerPath === ARCHIVE_ROUTE;
|
||||
const dossierTemplateId = route.paramMap.get(DOSSIER_TEMPLATE_ID);
|
||||
const dossierTemplateStats = this._dashboardStatsService.find(dossierTemplateId);
|
||||
if (isArchive && dossierTemplateStats.numberOfArchivedDossiers === 0) {
|
||||
await this._router.navigate(['main', dossierTemplateId, 'dossiers']);
|
||||
return false;
|
||||
}
|
||||
if (!isArchive && dossierTemplateStats.numberOfActiveDossiers === 0) {
|
||||
await this._router.navigate(['main', dossierTemplateId, 'archive']);
|
||||
return false;
|
||||
}
|
||||
await firstValueFrom(dossiersService.loadAll());
|
||||
} else {
|
||||
const services = [this._archivedDossiersService, this._activeDossiersService];
|
||||
|
||||
@ -1,9 +1,20 @@
|
||||
<a [routerLinkActive]="'active'" [routerLink]="['..', DOSSIERS_ROUTE]" class="red-tab">
|
||||
{{ 'dossiers-type-switch.active' | translate }}
|
||||
</a>
|
||||
|
||||
<a [routerLinkActive]="'active'" [routerLink]="['..', ARCHIVE_ROUTE]" class="red-tab">
|
||||
{{ 'dossiers-type-switch.archive' | translate }}
|
||||
</a>
|
||||
<ng-container *ngIf="dossierTemplate$ | async as dossierTemplate">
|
||||
<button
|
||||
[disabled]="dossierTemplate.numberOfActiveDossiers === 0"
|
||||
[routerLinkActive]="'active'"
|
||||
[routerLink]="['..', DOSSIERS_ROUTE]"
|
||||
class="red-tab"
|
||||
>
|
||||
{{ 'dossiers-type-switch.active' | translate }}
|
||||
</button>
|
||||
|
||||
<button
|
||||
[disabled]="dossierTemplate.numberOfArchivedDossiers === 0"
|
||||
[routerLinkActive]="'active'"
|
||||
[routerLink]="['..', ARCHIVE_ROUTE]"
|
||||
class="red-tab"
|
||||
>
|
||||
{{ 'dossiers-type-switch.archive' | translate }}
|
||||
</button>
|
||||
</ng-container>
|
||||
<div class="separator"></div>
|
||||
|
||||
@ -1,5 +1,8 @@
|
||||
import { ChangeDetectionStrategy, Component } from '@angular/core';
|
||||
import { ARCHIVE_ROUTE, DOSSIERS_ROUTE } from '@red/domain';
|
||||
import { ARCHIVE_ROUTE, DashboardStats, DOSSIER_TEMPLATE_ID, DOSSIERS_ROUTE } from '@red/domain';
|
||||
import { DashboardStatsService } from '@services/dossier-templates/dashboard-stats.service';
|
||||
import { ActivatedRoute } from '@angular/router';
|
||||
import { Observable } from 'rxjs';
|
||||
|
||||
@Component({
|
||||
selector: 'redaction-dossiers-type-switch',
|
||||
@ -10,4 +13,11 @@ import { ARCHIVE_ROUTE, DOSSIERS_ROUTE } from '@red/domain';
|
||||
export class DossiersTypeSwitchComponent {
|
||||
readonly DOSSIERS_ROUTE = DOSSIERS_ROUTE;
|
||||
readonly ARCHIVE_ROUTE = ARCHIVE_ROUTE;
|
||||
|
||||
readonly dossierTemplate$: Observable<DashboardStats>;
|
||||
|
||||
constructor(private readonly _dashboardStatsService: DashboardStatsService, private readonly _route: ActivatedRoute) {
|
||||
const dossierTemplateId = _route.snapshot.paramMap.get(DOSSIER_TEMPLATE_ID);
|
||||
this.dossierTemplate$ = _dashboardStatsService.getEntityChanged$(dossierTemplateId);
|
||||
}
|
||||
}
|
||||
|
||||
@ -7,9 +7,9 @@ import { map, switchMap, tap } from 'rxjs/operators';
|
||||
import { DossierStatesService } from '../entity-services/dossier-states.service';
|
||||
|
||||
const templatesSorter = (a: DashboardStats, b: DashboardStats) => {
|
||||
if (a.numberOfActiveDossiers > 0 && b.numberOfActiveDossiers === 0) {
|
||||
if (!a.isEmpty && b.isEmpty) {
|
||||
return -1;
|
||||
} else if (a.numberOfActiveDossiers === 0 && b.numberOfActiveDossiers > 0) {
|
||||
} else if (a.isEmpty && !b.isEmpty) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
|
||||
@ -41,7 +41,7 @@ export class DashboardStats implements IListable, IDashboardStats {
|
||||
}
|
||||
|
||||
get isEmpty(): boolean {
|
||||
return this.numberOfActiveDossiers === 0;
|
||||
return this.numberOfActiveDossiers === 0 && this.numberOfArchivedDossiers === 0;
|
||||
}
|
||||
|
||||
get id(): string {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user