65272: Clear template-item cache on removal

This commit is contained in:
Kristof De Langhe
2020-02-07 17:18:51 +01:00
parent 53355d1fc4
commit 1885845fef
4 changed files with 35 additions and 19 deletions

View File

@@ -13,6 +13,8 @@ import { Item } from '../../../core/shared/item.model';
import { createSuccessfulRemoteDataObject, createSuccessfulRemoteDataObject$ } from '../../../shared/testing/utils'; import { createSuccessfulRemoteDataObject, createSuccessfulRemoteDataObject$ } from '../../../shared/testing/utils';
import { ItemTemplateDataService } from '../../../core/data/item-template-data.service'; import { ItemTemplateDataService } from '../../../core/data/item-template-data.service';
import { Collection } from '../../../core/shared/collection.model'; import { Collection } from '../../../core/shared/collection.model';
import { ObjectCacheService } from '../../../core/cache/object-cache.service';
import { RequestService } from '../../../core/data/request.service';
describe('CollectionMetadataComponent', () => { describe('CollectionMetadataComponent', () => {
let comp: CollectionMetadataComponent; let comp: CollectionMetadataComponent;
@@ -20,11 +22,14 @@ describe('CollectionMetadataComponent', () => {
let router: Router; let router: Router;
let itemTemplateService: ItemTemplateDataService; let itemTemplateService: ItemTemplateDataService;
const template = new Item(); const template = Object.assign(new Item(), {
self: 'template-selflink'
});
const collection = Object.assign(new Collection(), { const collection = Object.assign(new Collection(), {
uuid: 'collection-id', uuid: 'collection-id',
id: 'collection-id', id: 'collection-id',
name: 'Fake Collection' name: 'Fake Collection',
self: 'collection-selflink'
}); });
const itemTemplateServiceStub = Object.assign({ const itemTemplateServiceStub = Object.assign({
@@ -37,6 +42,12 @@ describe('CollectionMetadataComponent', () => {
success: {}, success: {},
error: {} error: {}
}); });
const objectCache = jasmine.createSpyObj('objectCache', {
remove: {}
});
const requestService = jasmine.createSpyObj('requestService', {
removeByHrefSubstring: {}
});
beforeEach(async(() => { beforeEach(async(() => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
@@ -46,7 +57,9 @@ describe('CollectionMetadataComponent', () => {
{ provide: CollectionDataService, useValue: {} }, { provide: CollectionDataService, useValue: {} },
{ provide: ItemTemplateDataService, useValue: itemTemplateServiceStub }, { provide: ItemTemplateDataService, useValue: itemTemplateServiceStub },
{ provide: ActivatedRoute, useValue: { parent: { data: observableOf({ dso: createSuccessfulRemoteDataObject(collection) }) } } }, { provide: ActivatedRoute, useValue: { parent: { data: observableOf({ dso: createSuccessfulRemoteDataObject(collection) }) } } },
{ provide: NotificationsService, useValue: notificationsService } { provide: NotificationsService, useValue: notificationsService },
{ provide: ObjectCacheService, useValue: objectCache },
{ provide: RequestService, useValue: requestService }
], ],
schemas: [NO_ERRORS_SCHEMA] schemas: [NO_ERRORS_SCHEMA]
}).compileComponents(); }).compileComponents();
@@ -84,6 +97,11 @@ describe('CollectionMetadataComponent', () => {
it('should display a success notification', () => { it('should display a success notification', () => {
expect(notificationsService.success).toHaveBeenCalled(); expect(notificationsService.success).toHaveBeenCalled();
}); });
it('should reset related object and request cache', () => {
expect(objectCache.remove).toHaveBeenCalledWith(template.self);
expect(requestService.removeByHrefSubstring).toHaveBeenCalledWith(collection.self);
});
}); });
describe('when delete returns a failure', () => { describe('when delete returns a failure', () => {

View File

@@ -12,6 +12,8 @@ import { switchMap, take } from 'rxjs/operators';
import { combineLatest as combineLatestObservable } from 'rxjs'; import { combineLatest as combineLatestObservable } from 'rxjs';
import { NotificationsService } from '../../../shared/notifications/notifications.service'; import { NotificationsService } from '../../../shared/notifications/notifications.service';
import { TranslateService } from '@ngx-translate/core'; import { TranslateService } from '@ngx-translate/core';
import { ObjectCacheService } from '../../../core/cache/object-cache.service';
import { RequestService } from '../../../core/data/request.service';
/** /**
* Component for editing a collection's metadata * Component for editing a collection's metadata
@@ -35,7 +37,9 @@ export class CollectionMetadataComponent extends ComcolMetadataComponent<Collect
protected router: Router, protected router: Router,
protected route: ActivatedRoute, protected route: ActivatedRoute,
protected notificationsService: NotificationsService, protected notificationsService: NotificationsService,
protected translate: TranslateService protected translate: TranslateService,
protected objectCache: ObjectCacheService,
protected requestService: RequestService
) { ) {
super(collectionDataService, router, route, notificationsService, translate); super(collectionDataService, router, route, notificationsService, translate);
} }
@@ -94,13 +98,19 @@ export class CollectionMetadataComponent extends ComcolMetadataComponent<Collect
); );
combineLatestObservable(collection$, template$).pipe( combineLatestObservable(collection$, template$).pipe(
switchMap(([collection, template]) => this.itemTemplateService.deleteByCollectionID(template, collection.uuid)) switchMap(([collection, template]) => {
const success$ = this.itemTemplateService.deleteByCollectionID(template, collection.uuid);
this.objectCache.remove(template.self);
this.requestService.removeByHrefSubstring(collection.self);
return success$;
})
).subscribe((success: boolean) => { ).subscribe((success: boolean) => {
if (success) { if (success) {
this.notificationsService.success(null, this.translate.get('collection.edit.template.notifications.delete.success')); this.notificationsService.success(null, this.translate.get('collection.edit.template.notifications.delete.success'));
} else { } else {
this.notificationsService.error(null, this.translate.get('collection.edit.template.notifications.delete.error')); this.notificationsService.error(null, this.translate.get('collection.edit.template.notifications.delete.error'));
} }
this.initTemplateItem();
}); });
} }
} }

View File

@@ -1,4 +1,4 @@
import { inheritSerialization, deserialize, autoserializeAs } from 'cerialize'; import { inheritSerialization, deserialize } from 'cerialize';
import { mapsTo, relationship } from '../builders/build-decorators'; import { mapsTo, relationship } from '../builders/build-decorators';
import { TemplateItem } from '../../shared/template-item.model'; import { TemplateItem } from '../../shared/template-item.model';
@@ -12,18 +12,6 @@ import { Collection } from '../../shared/collection.model';
@inheritSerialization(NormalizedItem) @inheritSerialization(NormalizedItem)
export class NormalizedTemplateItem extends NormalizedItem { export class NormalizedTemplateItem extends NormalizedItem {
/**
* The human-readable identifier of this DSpaceObject
*/
@autoserializeAs(String)
id: string;
/**
* The universally unique identifier of this DSpaceObject
*/
@autoserializeAs(String, 'id')
uuid: string;
/** /**
* The Collection that this item is a template for * The Collection that this item is a template for
*/ */

View File

@@ -8,7 +8,7 @@ import { ResourceType } from './resource-type';
* Class representing a DSpace Template Item * Class representing a DSpace Template Item
*/ */
export class TemplateItem extends Item { export class TemplateItem extends Item {
static type = new ResourceType('templateItem'); static type = new ResourceType('itemtemplate');
/** /**
* The Collection that this item is a template for * The Collection that this item is a template for