From 9f39358efbfadb8b89e5220b1d705b76d120667e Mon Sep 17 00:00:00 2001 From: Yury Bondarenko Date: Wed, 7 Feb 2024 10:01:39 +0100 Subject: [PATCH] Clean up VersionHistoryDataService changes --- .../core/data/version-history-data.service.ts | 59 +++++++++---------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/src/app/core/data/version-history-data.service.ts b/src/app/core/data/version-history-data.service.ts index ee49c82e266..82290835355 100644 --- a/src/app/core/data/version-history-data.service.ts +++ b/src/app/core/data/version-history-data.service.ts @@ -6,23 +6,21 @@ import { ObjectCacheService } from '../cache/object-cache.service'; import { HALEndpointService } from '../shared/hal-endpoint.service'; import { HttpHeaders } from '@angular/common/http'; import { PostRequest } from './request.models'; -import { Observable, of } from 'rxjs'; +import { combineLatest, Observable, of as observableOf } from 'rxjs'; import { PaginatedSearchOptions } from '../../shared/search/models/paginated-search-options.model'; import { RemoteData } from './remote-data'; import { PaginatedList } from './paginated-list.model'; import { Version } from '../shared/version.model'; -import { filter, map, switchMap, take } from 'rxjs/operators'; +import { filter, find, map, switchMap, take } from 'rxjs/operators'; import { VERSION_HISTORY } from '../shared/version-history.resource-type'; import { followLink, FollowLinkConfig } from '../../shared/utils/follow-link-config.model'; import { VersionDataService } from './version-data.service'; import { HttpOptions } from '../dspace-rest/dspace-rest.service'; import { getAllSucceededRemoteData, getFirstCompletedRemoteData, getFirstSucceededRemoteDataPayload, getRemoteDataPayload } from '../shared/operators'; import { PaginationComponentOptions } from '../../shared/pagination/pagination-component-options.model'; -import { hasValueOperator } from '../../shared/empty.util'; +import { hasValue, hasValueOperator } from '../../shared/empty.util'; import { Item } from '../shared/item.model'; import { FindListOptions } from './find-list-options.model'; -import { sendRequest } from '../shared/request.operators'; -import { RestRequest } from './rest-request.model'; import { IdentifiableDataService } from './base/identifiable-data.service'; import { dataService } from './base/data-service.decorator'; @@ -86,29 +84,31 @@ export class VersionHistoryDataService extends IdentifiableDataService> { + const requestId = this.requestService.generateRequestId(); const requestOptions: HttpOptions = Object.create({}); let requestHeaders = new HttpHeaders(); requestHeaders = requestHeaders.append('Content-Type', 'text/uri-list'); requestOptions.headers = requestHeaders; - const response$ = this.halService.getEndpoint(this.versionsEndpoint).pipe( + this.halService.getEndpoint(this.versionsEndpoint).pipe( take(1), map((endpointUrl: string) => (summary?.length > 0) ? `${endpointUrl}?summary=${summary}` : `${endpointUrl}`), - map((endpointURL: string) => new PostRequest(this.requestService.generateRequestId(), endpointURL, itemHref, requestOptions)), - sendRequest(this.requestService), - switchMap((restRequest: RestRequest) => this.rdbService.buildFromRequestUUID(restRequest.uuid)), - getFirstCompletedRemoteData() - ) as Observable>; - - response$.subscribe((versionRD: RemoteData) => { - // invalidate version history - // note: we should do this regardless of whether the request succeeds, - // because it may have failed due to cached data that is out of date - this.requestService.setStaleByHrefSubstring(versionRD.payload._links.self.href); - this.requestService.setStaleByHrefSubstring(versionRD.payload._links.versionhistory.href); + find((href: string) => hasValue(href)), + ).subscribe((href) => { + const request = new PostRequest(requestId, href, itemHref, requestOptions); + if (hasValue(this.responseMsToLive)) { + request.responseMsToLive = this.responseMsToLive; + } + + this.requestService.send(request); }); - return response$; + return this.rdbService.buildFromRequestUUIDAndAwait(requestId, (versionRD) => combineLatest([ + this.requestService.setStaleByHrefSubstring(versionRD.payload._links.self.href), + this.requestService.setStaleByHrefSubstring(versionRD.payload._links.versionhistory.href), + ])).pipe( + getFirstCompletedRemoteData(), + ); } /** @@ -147,7 +147,7 @@ export class VersionHistoryDataService extends IdentifiableDataService res.versionhistory), getFirstSucceededRemoteDataPayload(), switchMap((versionHistoryRD) => this.getLatestVersionFromHistory$(versionHistoryRD)), - ) : of(null); + ) : observableOf(null); } /** @@ -158,8 +158,8 @@ export class VersionHistoryDataService extends IdentifiableDataService { return version ? this.getLatestVersion$(version).pipe( take(1), - switchMap((latestVersion) => of(version.version === latestVersion.version)) - ) : of(null); + switchMap((latestVersion) => observableOf(version.version === latestVersion.version)) + ) : observableOf(null); } /** @@ -170,21 +170,20 @@ export class VersionHistoryDataService extends IdentifiableDataService { return this.versionDataService.findByHref(versionHref, false, true, followLink('versionhistory')).pipe( getFirstCompletedRemoteData(), - switchMap((res) => { - if (res.hasSucceeded && !res.hasNoContent) { - return res.payload.versionhistory.pipe( + switchMap((versionRD: RemoteData) => { + if (versionRD.hasSucceeded && !versionRD.hasNoContent) { + return versionRD.payload.versionhistory.pipe( getFirstCompletedRemoteData(), - map((versionHistoryRD) => { - if (res.hasSucceeded) { - const versionHistory = versionHistoryRD.payload; - return versionHistory ? versionHistory.draftVersion : false; + map((versionHistoryRD: RemoteData) => { + if (versionHistoryRD.hasSucceeded && !versionHistoryRD.hasNoContent) { + return versionHistoryRD.payload.draftVersion; } else { return false; } }), ); } else { - return of(false); + return observableOf(false); } }), );