From 54a720fbc69000803369ac4d504930d2ff4604ab Mon Sep 17 00:00:00 2001 From: Yury Bondarenko Date: Wed, 7 Feb 2024 10:01:39 +0100 Subject: [PATCH] Clean up new version POST --- .../core/data/version-history-data.service.ts | 46 +++++++++---------- 1 file changed, 23 insertions(+), 23 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..0e0082e1b6e 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); } /** @@ -184,7 +184,7 @@ export class VersionHistoryDataService extends IdentifiableDataService