|
| 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