[CST-4499] Version history (WIP) - Authorization features - Cache issue fixed

This commit is contained in:
Davide Negretti
2021-09-16 18:53:45 +02:00
parent cf1c73bd32
commit b1b2bd4562
2 changed files with 56 additions and 54 deletions

View File

@@ -173,4 +173,8 @@ export class VersionHistoryDataService extends DataService<VersionHistory> {
startWith(false), startWith(false),
); );
} }
invalidateVersionHistoryCache(versionHistoryID: string) {
this.requestService.setStaleByHrefSubstring('versioning/versionhistories/' + versionHistoryID);
}
} }

View File

@@ -19,7 +19,7 @@ import {
getFirstSucceededRemoteDataPayload, getFirstSucceededRemoteDataPayload,
getRemoteDataPayload getRemoteDataPayload
} from '../../../core/shared/operators'; } from '../../../core/shared/operators';
import { map, mergeMap, startWith, switchMap, take } from 'rxjs/operators'; import { map, mergeMap, startWith, switchMap, take, tap } from 'rxjs/operators';
import { PaginatedList } from '../../../core/data/paginated-list.model'; import { PaginatedList } from '../../../core/data/paginated-list.model';
import { PaginationComponentOptions } from '../../pagination/pagination-component-options.model'; import { PaginationComponentOptions } from '../../pagination/pagination-component-options.model';
import { VersionHistoryDataService } from '../../../core/data/version-history-data.service'; import { VersionHistoryDataService } from '../../../core/data/version-history-data.service';
@@ -44,6 +44,8 @@ import { ItemDataService } from '../../../core/data/item-data.service';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
import { AuthorizationDataService } from '../../../core/data/feature-authorization/authorization-data.service'; import { AuthorizationDataService } from '../../../core/data/feature-authorization/authorization-data.service';
import { FeatureID } from '../../../core/data/feature-authorization/feature-id'; import { FeatureID } from '../../../core/data/feature-authorization/feature-id';
import { createPaginatedList } from '../../testing/utils.test';
import { createSuccessfulRemoteDataObject } from '../../remote-data.utils';
@Component({ @Component({
selector: 'ds-item-versions', selector: 'ds-item-versions',
@@ -102,7 +104,7 @@ export class ItemVersionsComponent implements OnInit {
/** /**
* The version history's list of versions * The version history's list of versions
*/ */
versionsRD$: Observable<RemoteData<PaginatedList<Version>>>; versionsRD$: BehaviorSubject<RemoteData<PaginatedList<Version>>> = new BehaviorSubject<RemoteData<PaginatedList<Version>>>(createSuccessfulRemoteDataObject(createPaginatedList()));
/** /**
* Verify if the list of versions has at least one e-person to display * Verify if the list of versions has at least one e-person to display
@@ -140,12 +142,6 @@ export class ItemVersionsComponent implements OnInit {
[itemId: string]: string [itemId: string]: string
}>; }>;
/**
* Emits when the versionsRD$ must be refreshed
* (should be used when a new version has been created)
*/
refreshSubject = new BehaviorSubject<any>(null);
/** /**
* The number of the version whose summary is currently being edited * The number of the version whose summary is currently being edited
*/ */
@@ -276,7 +272,7 @@ export class ItemVersionsComponent implements OnInit {
* @param version the version to be deleted * @param version the version to be deleted
* @param redirectToLatest force the redirect to the latest version in the history * @param redirectToLatest force the redirect to the latest version in the history
*/ */
deleteVersion(version: Version, redirectToLatest: boolean) { deleteVersion(version: Version, redirectToLatest: boolean): void {
const successMessageKey = 'item.version.delete.notification.success'; const successMessageKey = 'item.version.delete.notification.success';
const failureMessageKey = 'item.version.delete.notification.failure'; const failureMessageKey = 'item.version.delete.notification.failure';
const versionNumber = version.version; const versionNumber = version.version;
@@ -287,36 +283,40 @@ export class ItemVersionsComponent implements OnInit {
activeModal.componentInstance.versionNumber = version.version; activeModal.componentInstance.versionNumber = version.version;
activeModal.componentInstance.firstVersion = false; activeModal.componentInstance.firstVersion = false;
const versionHistory$ = this.versionHistoryService.getHistoryIdFromVersion$(version).pipe(
take(1),
switchMap((res) => this.versionHistoryService.findById(res)),
getFirstSucceededRemoteDataPayload(),
);
// On modal submit/dismiss // On modal submit/dismiss
activeModal.result.then(() => { activeModal.result.then(() => {
// let versionHistoryOuter: VersionHistory;
versionItem$.pipe( versionItem$.pipe(
getFirstSucceededRemoteDataPayload<Item>(), getFirstSucceededRemoteDataPayload<Item>(),
mergeMap((item) => combineLatest([ mergeMap((item: Item) => combineLatest([
// passa item, recupera vh // pass item
of(item), of(item),
versionHistory$.pipe( // get and return version history
switchMap((vh) => this.versionHistoryService.getLatestVersionFromHistory$(vh)), this.getVersionHistory$(version).pipe(
switchMap((newLatestVersion) => newLatestVersion.item), // invalidate cache
getFirstSucceededRemoteDataPayload() tap((versionHistory) => {
this.versionHistoryService.invalidateVersionHistoryCache(versionHistory.id);
})
) )
])), ])),
mergeMap(([item, versionHistory]) => combineLatest([ mergeMap(([item, versionHistory]: [Item, VersionHistory]) => combineLatest([
// cancella item, passa vh // delete item and return result
of(item).pipe( this.deleteItemAndGetResult$(item),
map((itemBeingDeleted) => itemBeingDeleted.id), // pass version history
switchMap((itemId) => this.itemService.delete(itemId)),
getFirstCompletedRemoteData(),
map((deleteItemRes) => deleteItemRes.hasSucceeded)
),
of(versionHistory) of(versionHistory)
])), ])),
mergeMap(([deleteItemResult, versionHistory]: [boolean, VersionHistory]) => combineLatest([
// pass result
of(deleteItemResult),
// get and return new latest version
this.getLatestVersionItem$(versionHistory).pipe(
tap(() => {
this.getVersionHistory(of(versionHistory));
}),
)
])),
).subscribe(([deleteHasSucceeded, newLatestVersionItem]) => { ).subscribe(([deleteHasSucceeded, newLatestVersionItem]) => {
if (deleteHasSucceeded) { if (deleteHasSucceeded) {
this.notificationsService.success(null, this.translateService.get(successMessageKey, {'version': versionNumber})); this.notificationsService.success(null, this.translateService.get(successMessageKey, {'version': versionNumber}));
@@ -325,13 +325,10 @@ export class ItemVersionsComponent implements OnInit {
} }
console.log('LATEST VERSION = ' + newLatestVersionItem.uuid); console.log('LATEST VERSION = ' + newLatestVersionItem.uuid);
if (redirectToLatest) { if (redirectToLatest) {
const tmpPath = getItemEditVersionhistoryRoute(newLatestVersionItem); const path = getItemEditVersionhistoryRoute(newLatestVersionItem);
console.log('PATH = ' + tmpPath); this.router.navigateByUrl(path);
this.router.navigateByUrl(tmpPath);
} }
return this.versionHistoryService.getLatestVersion$(version);
}); });
}); });
} }
@@ -350,6 +347,9 @@ export class ItemVersionsComponent implements OnInit {
// On modal submit/dismiss // On modal submit/dismiss
activeModal.result.then((modalResult) => { activeModal.result.then((modalResult) => {
// TODO spostare in metodo
// TODO recuperare version history e invalidare cache
// this.versionHistoryService.invalidateVersionHistoryCache(versionHistory.id);
const summary = modalResult; const summary = modalResult;
version.item.pipe(getFirstSucceededRemoteDataPayload()).subscribe((item) => { version.item.pipe(getFirstSucceededRemoteDataPayload()).subscribe((item) => {
@@ -359,7 +359,7 @@ export class ItemVersionsComponent implements OnInit {
if (postResult.hasSucceeded) { if (postResult.hasSucceeded) {
const newVersionNumber = postResult.payload.version; const newVersionNumber = postResult.payload.version;
this.notificationsService.success(null, this.translateService.get(successMessageKey, {version: newVersionNumber})); this.notificationsService.success(null, this.translateService.get(successMessageKey, {version: newVersionNumber}));
this.refreshSubject.next(null); this.getVersionHistory$(version);
} else { } else {
this.notificationsService.error(null, this.translateService.get(failureMessageKey)); this.notificationsService.error(null, this.translateService.get(failureMessageKey));
} }
@@ -395,6 +395,22 @@ export class ItemVersionsComponent implements OnInit {
); );
}*/ }*/
getVersionHistory(versionHistory$: Observable<VersionHistory>): void {
const currentPagination = this.paginationService.getCurrentPagination(this.options.id, this.options);
observableCombineLatest([versionHistory$, currentPagination]).pipe(
switchMap(([versionHistory, options]: [VersionHistory, PaginationComponentOptions]) => {
return this.versionHistoryService.getVersions(versionHistory.id,
new PaginatedSearchOptions({pagination: Object.assign({}, options, {currentPage: options.currentPage})}),
true, true, followLink('item'), followLink('eperson'));
}),
getFirstCompletedRemoteData(),
).subscribe((res) => {
this.versionsRD$.next(res);
console.log(res.payload);
});
}
/** /**
* Initialize all observables * Initialize all observables
*/ */
@@ -421,25 +437,7 @@ export class ItemVersionsComponent implements OnInit {
getRemoteDataPayload(), getRemoteDataPayload(),
hasValueOperator(), hasValueOperator(),
); );
const currentPagination = this.paginationService.getCurrentPagination(this.options.id, this.options); this.getVersionHistory(versionHistory$);
this.versionsRD$ = observableCombineLatest([versionHistory$, currentPagination]).pipe(
switchMap(([versionHistory, options]: [VersionHistory, PaginationComponentOptions]) => {
return this.versionHistoryService.getVersions(versionHistory.id,
new PaginatedSearchOptions({pagination: Object.assign({}, options, {currentPage: options.currentPage})}),
true, true, followLink('item'), followLink('eperson'));
})
);
// Refresh the table when refreshSubject emits
this.subs.push(this.refreshSubject.pipe(switchMap(() => {
return observableCombineLatest([versionHistory$, currentPagination]).pipe(
take(1),
switchMap(([versionHistory, options]: [VersionHistory, PaginationComponentOptions]) => {
return this.versionHistoryService.getVersions(versionHistory.id,
new PaginatedSearchOptions({pagination: Object.assign({}, options, {currentPage: options.currentPage})}),
false, true, followLink('item'), followLink('eperson'));
})
);
})).subscribe());
this.hasEpersons$ = this.versionsRD$.pipe( this.hasEpersons$ = this.versionsRD$.pipe(
getAllSucceededRemoteData(), getAllSucceededRemoteData(),
getRemoteDataPayload(), getRemoteDataPayload(),