Skip to content

Commit ab76af5

Browse files
committed
added path and resolver to module for load query
1 parent d094157 commit ab76af5

File tree

2 files changed

+108
-2
lines changed

2 files changed

+108
-2
lines changed

src/app/modules/data-query/data-query-routing.module.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { NgModule } from '@angular/core';
2-
import { RouterModule, Routes } from '@angular/router';
31
import { CohortDefinitionComponent } from './data-query/cohort-definition/cohort-definition.component';
42
import { DataSelectionComponent } from './data-query/data-selection/data-selection.component';
3+
import { LoadQueryIntoEditorFromUrlService } from 'src/app/service/Resolver/LoadQueryIntoEditorFromUrl.service';
4+
import { NgModule } from '@angular/core';
55
import { PathSegments } from 'src/app/app-paths';
6+
import { RouterModule, Routes } from '@angular/router';
67

78
const routes: Routes = [
89
{
@@ -11,6 +12,14 @@ const routes: Routes = [
1112
pathMatch: 'full',
1213
data: { animation: 'Search', title: 'TAB_TITLE.DATA_QUERY.COHORT_DEFINITION' },
1314
},
15+
{
16+
path: PathSegments.loadQuery,
17+
resolve: {
18+
preLoadedQuery: LoadQueryIntoEditorFromUrlService,
19+
},
20+
component: CohortDefinitionComponent,
21+
data: { animation: 'Cohort' },
22+
},
1423
{
1524
path: PathSegments.cohortDefinition,
1625
component: CohortDefinitionComponent,
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import { ActivatedRouteSnapshot } from '@angular/router';
2+
import { catchError, map, Observable, of } from 'rxjs';
3+
import { ConsentService } from '../Consent/Consent.service';
4+
import { DataQueryStorageService } from '../DataQuery/DataQueryStorage.service';
5+
import { Injectable } from '@angular/core';
6+
import { NavigationHelperService } from '../NavigationHelper.service';
7+
import { SavedDataQuery } from 'src/app/model/SavedDataQuery/SavedDataQuery';
8+
import { SnackbarService } from 'src/app/shared/service/Snackbar/Snackbar.service';
9+
import { UiCRTDL } from 'src/app/model/UiCRTDL';
10+
11+
/**
12+
* This resolver extracts query ID from route parameters, validates it, and loads the corresponding
13+
* saved query data to initialize the editor state.
14+
*
15+
* Used as a route resolver to preload query data before component initialization.
16+
*/
17+
@Injectable({
18+
providedIn: 'root',
19+
})
20+
export class LoadQueryIntoEditorFromUrlService {
21+
constructor(
22+
private dataQueryStorageService: DataQueryStorageService,
23+
private consentService: ConsentService,
24+
private snackbarService: SnackbarService,
25+
private navigationHelper: NavigationHelperService
26+
) {}
27+
28+
/**
29+
* Main resolver method that orchestrates the query loading process.
30+
*
31+
* @param route - The activated route snapshot containing route parameters and query params
32+
* @returns Observable of the loaded saved query or undefined if loading fails
33+
*/
34+
resolve(route: ActivatedRouteSnapshot): Observable<UiCRTDL> | undefined {
35+
this.clearPreviousConsent();
36+
const queryId = this.extractAndValidateQueryId(route);
37+
if (queryId === null) {
38+
return undefined;
39+
}
40+
return this.loadSavedQuery(queryId);
41+
}
42+
43+
/**
44+
* This prevents consent from previous queries affecting the current query loading.
45+
* @returns void
46+
*/
47+
private clearPreviousConsent(): void {
48+
this.consentService.clearConsent();
49+
}
50+
51+
/**
52+
* Extracts the query ID from route parameters and validates its format and value.
53+
*
54+
* @param route - The activated route snapshot containing query parameters
55+
* @returns The validated query ID as a number, or null if validation fails
56+
*/
57+
private extractAndValidateQueryId(route: ActivatedRouteSnapshot): number | null {
58+
const idParam = route.queryParams.id;
59+
if (!idParam) {
60+
console.warn('No "id" parameter in query string.');
61+
return null;
62+
}
63+
return this.parseAndValidateId(idParam);
64+
}
65+
66+
/**
67+
* Ensures the ID is a positive, safe integer.
68+
* @param idParam - The raw ID parameter value to parse and validate
69+
* @returns The parsed and validated ID, or null if validation fails
70+
*/
71+
private parseAndValidateId(idParam: string): number | null {
72+
const id = Number(idParam);
73+
74+
if (isNaN(id) || id <= 0 || !Number.isSafeInteger(id)) {
75+
console.warn(`Invalid "id" parameter: ${idParam}. Must be a positive integer.`);
76+
return null;
77+
}
78+
return id;
79+
}
80+
81+
/**
82+
* Loads the saved query from storage using the validated query ID.
83+
*
84+
* @param queryId - The validated query ID to load
85+
* @returns Observable that emits the loaded saved query with processed CRTDL data
86+
*/
87+
private loadSavedQuery(queryId: number): Observable<UiCRTDL> {
88+
return this.dataQueryStorageService.readDataQueryById(queryId).pipe(
89+
map((savedQuery: SavedDataQuery) => savedQuery.getCrtdl()),
90+
catchError((error) => {
91+
this.snackbarService.displayErrorMessageWithNoCode('Error loading saved query, not found');
92+
this.navigationHelper.navigateToDataQueryCohortDefinition();
93+
return of(null);
94+
})
95+
);
96+
}
97+
}

0 commit comments

Comments
 (0)