[CST-4499] Version history - Test TBD

This commit is contained in:
Davide Negretti
2021-09-17 17:35:57 +02:00
parent 55a1a1c0eb
commit 97ed08e5c8
3 changed files with 97 additions and 65 deletions

View File

@@ -14,6 +14,10 @@ import { FindListOptions } from './request.models';
import { Observable } from 'rxjs';
import { dataService } from '../cache/builders/build-decorators';
import { VERSION } from '../shared/version.resource-type';
import { VersionHistory } from '../shared/version-history.model';
import { followLink } from '../../shared/utils/follow-link-config.model';
import { getFirstSucceededRemoteDataPayload } from '../shared/operators';
import { map, switchMap } from 'rxjs/operators';
/**
* Service responsible for handling requests related to the Version object
@@ -41,4 +45,27 @@ export class VersionDataService extends DataService<Version> {
getBrowseEndpoint(options: FindListOptions = {}, linkPath?: string): Observable<string> {
return this.halService.getEndpoint(this.linkPath);
}
/**
* Get the version history for the given version
* @param version
*/
getHistoryFromVersion$(version: Version): Observable<VersionHistory> {
return this.findById(version.id, false, true, followLink('versionhistory')).pipe(
getFirstSucceededRemoteDataPayload(),
switchMap((res) => res.versionhistory),
getFirstSucceededRemoteDataPayload(),
);
}
/**
* Get the ID of the version history for the given version
* @param version
*/
getHistoryIdFromVersion$(version: Version): Observable<string> {
return this.getHistoryFromVersion$(version).pipe(
map((versionHistory) => versionHistory.id),
);
}
}

View File

@@ -31,6 +31,7 @@ import {
} from '../shared/operators';
import { PaginationComponentOptions } from '../../shared/pagination/pagination-component-options.model';
import { hasValueOperator } from '../../shared/empty.util';
import { Item } from '../shared/item.model';
/**
* Service responsible for handling requests related to the VersionHistory object
@@ -90,6 +91,11 @@ export class VersionHistoryDataService extends DataService<VersionHistory> {
return this.versionDataService.findAllByHref(hrefObs, undefined, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow);
}
/**
* Create a new version for an item
* @param itemHref the item for which create a new version
* @param summary the summary of the new version
*/
createVersion(itemHref: string, summary: string): Observable<RemoteData<Version>> {
const requestOptions: HttpOptions = Object.create({});
let requestHeaders = new HttpHeaders();
@@ -106,6 +112,10 @@ export class VersionHistoryDataService extends DataService<VersionHistory> {
) as Observable<RemoteData<Version>>;
}
/**
* Get the latest version in a version history
* @param versionHistory
*/
getLatestVersionFromHistory$(versionHistory: VersionHistory): Observable<Version> {
// Pagination options to fetch a single version on the first page (this is the latest version in the history)
@@ -127,23 +137,10 @@ export class VersionHistoryDataService extends DataService<VersionHistory> {
}
// TODO move to versionDataService
getHistoryIdFromVersion$(version: Version): Observable<string> {
return this.getHistoryFromVersion$(version).pipe(
map((versionHistory) => versionHistory.id),
);
}
// TODO move to versionDataService
getHistoryFromVersion$(version: Version): Observable<VersionHistory> {
return this.versionDataService.findById(version.id, false, true, followLink('versionhistory')).pipe(
getFirstSucceededRemoteDataPayload(),
switchMap((res) => res.versionhistory),
getFirstSucceededRemoteDataPayload(),
);
}
// TODO move to versionDataService
/**
* Get the latest version
* @param version
*/
getLatestVersion$(version: Version): Observable<Version> {
// retrieve again version, including with versionHistory
return this.versionDataService.findById(version.id, false, true, followLink('versionhistory')).pipe(
@@ -154,6 +151,10 @@ export class VersionHistoryDataService extends DataService<VersionHistory> {
);
}
/**
* Check if the given version is the latest
* @param version
*/
isLatest$(version: Version): Observable<boolean> {
return this.getLatestVersion$(version).pipe(
take(1),
@@ -168,12 +169,31 @@ export class VersionHistoryDataService extends DataService<VersionHistory> {
hasDraftVersion$(versionHref: string): Observable<boolean> {
return this.versionDataService.findByHref(versionHref, true, true, followLink('versionhistory')).pipe(
getFirstSucceededRemoteDataPayload(),
switchMap((version) => this.getHistoryFromVersion$(version)),
switchMap((version) => this.versionDataService.getHistoryFromVersion$(version)),
map((versionHistory) => versionHistory.draftVersion),
startWith(false),
);
}
/**
* Get the item of the latest version in a version history
* @param versionHistory
*/
getLatestVersionItemFromHistory$(versionHistory: VersionHistory): Observable<Item> {
return this.getLatestVersionFromHistory$(versionHistory).pipe(
switchMap((newLatestVersion) => newLatestVersion.item),
getFirstSucceededRemoteDataPayload(),
);
}
getVersionHistoryFromVersion$(version: Version): Observable<VersionHistory> {
return this.versionDataService.getHistoryIdFromVersion$(version).pipe(
take(1),
switchMap((res) => this.findById(res)),
getFirstSucceededRemoteDataPayload(),
);
}
invalidateVersionHistoryCache(versionHistoryID: string) {
this.requestService.setStaleByHrefSubstring('versioning/versionhistories/' + versionHistoryID);
}

View File

@@ -242,14 +242,10 @@ export class ItemVersionsComponent implements OnInit {
);
}
getVersionHistoryFromVersion$(version: Version): Observable<VersionHistory> {
return this.versionHistoryService.getHistoryIdFromVersion$(version).pipe(
take(1),
switchMap((res) => this.versionHistoryService.findById(res)),
getFirstSucceededRemoteDataPayload(),
);
}
/**
* Delete the item and get the result of the operation
* @param item
*/
deleteItemAndGetResult$(item: Item): Observable<boolean> {
return this.itemService.delete(item.id).pipe(
getFirstCompletedRemoteData(),
@@ -258,13 +254,6 @@ export class ItemVersionsComponent implements OnInit {
);
}
getLatestVersionItem$(versionHistory: VersionHistory): Observable<Item> {
return this.versionHistoryService.getLatestVersionFromHistory$(versionHistory).pipe(
switchMap((newLatestVersion) => newLatestVersion.item),
getFirstSucceededRemoteDataPayload(),
);
}
/**
* Deletes the specified version
* @param version the version to be deleted
@@ -292,7 +281,7 @@ export class ItemVersionsComponent implements OnInit {
// pass item
of(item),
// get and return version history
this.getVersionHistoryFromVersion$(version).pipe(
this.versionHistoryService.getVersionHistoryFromVersion$(version).pipe(
// invalidate cache
tap((versionHistory) => {
this.versionHistoryService.invalidateVersionHistoryCache(versionHistory.id);
@@ -309,9 +298,9 @@ export class ItemVersionsComponent implements OnInit {
// pass result
of(deleteItemResult),
// get and return new latest version
this.getLatestVersionItem$(versionHistory).pipe(
this.versionHistoryService.getLatestVersionItemFromHistory$(versionHistory).pipe(
tap(() => {
this.getVersionHistory(of(versionHistory));
this.getAllVersions(of(versionHistory));
}),
)
])),
@@ -358,12 +347,12 @@ export class ItemVersionsComponent implements OnInit {
const newVersionNumber = postResult.payload.version;
this.notificationsService.success(null, this.translateService.get(successMessageKey, {version: newVersionNumber}));
console.log(version);
const versionHistory$ = this.versionHistoryService.getHistoryFromVersion$(version).pipe(
const versionHistory$ = this.versionService.getHistoryFromVersion$(version).pipe(
tap((res) => {
this.versionHistoryService.invalidateVersionHistoryCache(res.id);
}),
);
this.getVersionHistory(versionHistory$);
this.getAllVersions(versionHistory$);
} else {
this.notificationsService.error(null, this.translateService.get(failureMessageKey));
}
@@ -372,34 +361,27 @@ export class ItemVersionsComponent implements OnInit {
});
}
// TODO eliminare
/*hasDraftVersion$(versionItem: Observable<RemoteData<Item>>): Observable<boolean> {
return versionItem.pipe(
getFirstSucceededRemoteDataPayload(),
map((res) => res._links.version.href ),
switchMap( (res) => this.versionHistoryService.hasDraftVersion$(res))
);
}*/
canEditVersion$(versionItem: Version): Observable<boolean> {
return this.authorizationService.isAuthorized(FeatureID.CanEditVersion, versionItem.self);
/**
* Check is the current user can edit the version summary
* @param version
*/
canEditVersion$(version: Version): Observable<boolean> {
return this.authorizationService.isAuthorized(FeatureID.CanEditVersion, version.self);
}
canDeleteVersion$(versionItem: Version): Observable<boolean> {
return this.authorizationService.isAuthorized(FeatureID.CanDeleteVersion, versionItem.self);
/**
* Check if the current user can delete the version
* @param version
*/
canDeleteVersion$(version: Version): Observable<boolean> {
return this.authorizationService.isAuthorized(FeatureID.CanDeleteVersion, version.self);
}
// TODO eliminare (usa item anziché vrsion)
/*canDeleteVersion$(version: Version) {
return version.item.pipe(
getFirstSucceededRemoteDataPayload(),
map((item) => item.self),
switchMap((url) => this.authorizationService.isAuthorized(FeatureID.CanDeleteVersion, url)),
take(1),
);
}*/
getVersionHistory(versionHistory$: Observable<VersionHistory>): void {
/**
* Get all versions for the given version history and store them in versionRD$
* @param versionHistory$
*/
getAllVersions(versionHistory$: Observable<VersionHistory>): void {
const currentPagination = this.paginationService.getCurrentPagination(this.options.id, this.options);
observableCombineLatest([versionHistory$, currentPagination]).pipe(
switchMap(([versionHistory, options]: [VersionHistory, PaginationComponentOptions]) => {
@@ -414,7 +396,6 @@ export class ItemVersionsComponent implements OnInit {
});
}
/**
* Initialize all observables
*/
@@ -427,21 +408,25 @@ export class ItemVersionsComponent implements OnInit {
hasValueOperator(),
switchMap((version: Version) => version.versionhistory)
);
this.canCreateVersion$ = this.authorizationService.isAuthorized(FeatureID.CanCreateVersion, this.item.self);
// If there is a draft item in the version history the 'Create version' button is disabled and a different tooltip message is shown
this.hasDraftVersion$ = this.versionHistoryRD$.pipe(
getFirstSucceededRemoteDataPayload(),
map((res) => res.draftVersion)
);
this.canCreateVersion$ = this.authorizationService.isAuthorized(FeatureID.CanCreateVersion, this.item.self);
this.createVersionTitle$ = this.hasDraftVersion$.pipe(
take(1),
switchMap((res) => of(res ? 'item.version.history.table.action.hasDraft' : 'item.version.history.table.action.newVersion'))
);
const versionHistory$ = this.versionHistoryRD$.pipe(
getAllSucceededRemoteData(),
getRemoteDataPayload(),
hasValueOperator(),
);
this.getVersionHistory(versionHistory$);
this.getAllVersions(versionHistory$);
this.hasEpersons$ = this.versionsRD$.pipe(
getAllSucceededRemoteData(),
getRemoteDataPayload(),