security-c4po/security-c4po-angular/src/shared/services/pentest.service.ts

53 lines
1.8 KiB
TypeScript

import {Injectable} from '@angular/core';
import {environment} from '../../environments/environment';
import {HttpClient} from '@angular/common/http';
import {Observable, of} from 'rxjs';
import {Category} from '@shared/models/category.model';
import {Pentest} from '@shared/models/pentest.model';
import {Store} from '@ngxs/store';
import {ProjectState} from '@shared/stores/project-state/project-state';
import {catchError, map, switchMap} from 'rxjs/operators';
import {getTempPentestsForCategory} from '@shared/functions/categories/get-temp-pentests-for-category.function';
@Injectable({
providedIn: 'root'
})
export class PentestService {
private apiBaseURL = `${environment.apiEndpoint}/pentests`;
constructor(
private http: HttpClient,
private readonly store: Store) {
}
/**
* Load Pentests
* @param category the categories of which the pentests should be requested
*/
public loadPentests(category: Category): Observable<Pentest[]> {
return this.store.selectOnce(ProjectState.project).pipe(
switchMap(project => this.getPentestByProjectIdAndCategory(project.id, category)),
catchError(_ => of(null)),
map(response => {
let pentests = response;
if (!pentests) {
pentests = getTempPentestsForCategory(category);
// tslint:disable-next-line:no-console
console.info('Initial pentest data loaded.');
}
return pentests;
})
);
}
/**
* Get Pentests
* @param projectId the id of the project
* @param category the categories of which the pentests should be requested
*/
private getPentestByProjectIdAndCategory(projectId: string, category: Category): Observable<Pentest[]> {
return this.http.get<Pentest[]>(`${this.apiBaseURL}?projectId=${projectId}?category=${category}`);
}
}