[DURACOM-304] Refactored relationship-type-data.service.ts by removing page size of 9999

This commit is contained in:
Alisa Ismailati
2024-11-20 16:51:04 +01:00
committed by FrancescoMolinaro
parent c3b265d213
commit 3922130434

View File

@@ -1,5 +1,5 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { combineLatest as observableCombineLatest, Observable } from 'rxjs'; import { combineLatest as observableCombineLatest, EMPTY, expand, from, Observable, reduce } from 'rxjs';
import { map, mergeMap, switchMap, toArray } from 'rxjs/operators'; import { map, mergeMap, switchMap, toArray } from 'rxjs/operators';
import { hasValue } from '../../shared/empty.util'; import { hasValue } from '../../shared/empty.util';
import { followLink, FollowLinkConfig } from '../../shared/utils/follow-link-config.model'; import { followLink, FollowLinkConfig } from '../../shared/utils/follow-link-config.model';
@@ -62,34 +62,49 @@ export class RelationshipTypeDataService extends BaseDataService<RelationshipTyp
*/ */
getRelationshipTypeByLabelAndTypes(relationshipTypeLabel: string, firstItemType: string, secondItemType: string): Observable<RelationshipType> { getRelationshipTypeByLabelAndTypes(relationshipTypeLabel: string, firstItemType: string, secondItemType: string): Observable<RelationshipType> {
// Retrieve all relationship types from the server in a single page // Retrieve all relationship types from the server in a single page
return this.findAllData.findAll({ currentPage: 1, elementsPerPage: 9999 }, true, true, followLink('leftType'), followLink('rightType')) const initialPageInfo = { currentPage: 1, elementsPerPage: 20 };
.pipe( return this.findAllData.findAll(initialPageInfo, true, true, followLink('leftType'), followLink('rightType'))
getFirstSucceededRemoteData(), .pipe(
// Emit each type in the page array separately getFirstSucceededRemoteData(),
switchMap((typeListRD: RemoteData<PaginatedList<RelationshipType>>) => typeListRD.payload.page), // Emit each type in the page array separately
// Check each type individually, to see if it matches the provided types expand((typeListRD: RemoteData<PaginatedList<RelationshipType>>) => {
mergeMap((relationshipType: RelationshipType) => { const currentPage = typeListRD.payload.pageInfo.currentPage;
if (relationshipType.leftwardType === relationshipTypeLabel) { const totalPages = typeListRD.payload.pageInfo.totalPages;
return this.checkType(relationshipType, firstItemType, secondItemType); if (currentPage < totalPages) {
} else if (relationshipType.rightwardType === relationshipTypeLabel) { const nextPageInfo = { currentPage: currentPage + 1, elementsPerPage: 20 };
return this.checkType(relationshipType, secondItemType, firstItemType); return this.findAllData.findAll(nextPageInfo, true, true, followLink('leftType'), followLink('rightType')).pipe(
} else { getFirstSucceededRemoteData()
return [null]; );
} } else {
}), return EMPTY;
// Wait for all types to be checked and emit once, with the results combined back into an }
// array }),
toArray(), // Collect all pages into a single array
// Look for a match in the array and emit it if found, or null if one isn't found reduce((acc: RelationshipType[], typeListRD: RemoteData<PaginatedList<RelationshipType>>) => acc.concat(typeListRD.payload.page), []),
map((types: RelationshipType[]) => { mergeMap((relationshipTypes: RelationshipType[]) => from(relationshipTypes)),
const match = types.find((type: RelationshipType) => hasValue(type)); // Check each type individually, to see if it matches the provided types
if (hasValue(match)) { mergeMap((relationshipType: RelationshipType) => {
return match; if (relationshipType.leftwardType === relationshipTypeLabel) {
} else { return this.checkType(relationshipType, firstItemType, secondItemType);
return null; } else if (relationshipType.rightwardType === relationshipTypeLabel) {
} return this.checkType(relationshipType, secondItemType, firstItemType);
}), } else {
); return [null];
}
}),
// Wait for all types to be checked and emit once, with the results combined back into an
// array
toArray(),
// Look for a match in the array and emit it if found, or null if one isn't found
map((types: RelationshipType[]) => {
const match = types.find((type: RelationshipType) => hasValue(type));
if (hasValue(match)) {
return match;
} else {
return null;
}
}),
);
} }
/** /**