From 1885845fefce9a8ab282731e3f3ce62c352f85da Mon Sep 17 00:00:00 2001 From: Kristof De Langhe Date: Fri, 7 Feb 2020 17:18:51 +0100 Subject: [PATCH] 65272: Clear template-item cache on removal --- .../collection-metadata.component.spec.ts | 24 ++++++++++++++++--- .../collection-metadata.component.ts | 14 +++++++++-- .../models/normalized-template-item.model.ts | 14 +---------- src/app/core/shared/template-item.model.ts | 2 +- 4 files changed, 35 insertions(+), 19 deletions(-) diff --git a/src/app/+collection-page/edit-collection-page/collection-metadata/collection-metadata.component.spec.ts b/src/app/+collection-page/edit-collection-page/collection-metadata/collection-metadata.component.spec.ts index a4336201fc..784fa81579 100644 --- a/src/app/+collection-page/edit-collection-page/collection-metadata/collection-metadata.component.spec.ts +++ b/src/app/+collection-page/edit-collection-page/collection-metadata/collection-metadata.component.spec.ts @@ -13,6 +13,8 @@ import { Item } from '../../../core/shared/item.model'; import { createSuccessfulRemoteDataObject, createSuccessfulRemoteDataObject$ } from '../../../shared/testing/utils'; import { ItemTemplateDataService } from '../../../core/data/item-template-data.service'; import { Collection } from '../../../core/shared/collection.model'; +import { ObjectCacheService } from '../../../core/cache/object-cache.service'; +import { RequestService } from '../../../core/data/request.service'; describe('CollectionMetadataComponent', () => { let comp: CollectionMetadataComponent; @@ -20,11 +22,14 @@ describe('CollectionMetadataComponent', () => { let router: Router; let itemTemplateService: ItemTemplateDataService; - const template = new Item(); + const template = Object.assign(new Item(), { + self: 'template-selflink' + }); const collection = Object.assign(new Collection(), { uuid: 'collection-id', id: 'collection-id', - name: 'Fake Collection' + name: 'Fake Collection', + self: 'collection-selflink' }); const itemTemplateServiceStub = Object.assign({ @@ -37,6 +42,12 @@ describe('CollectionMetadataComponent', () => { success: {}, error: {} }); + const objectCache = jasmine.createSpyObj('objectCache', { + remove: {} + }); + const requestService = jasmine.createSpyObj('requestService', { + removeByHrefSubstring: {} + }); beforeEach(async(() => { TestBed.configureTestingModule({ @@ -46,7 +57,9 @@ describe('CollectionMetadataComponent', () => { { provide: CollectionDataService, useValue: {} }, { provide: ItemTemplateDataService, useValue: itemTemplateServiceStub }, { 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] }).compileComponents(); @@ -84,6 +97,11 @@ describe('CollectionMetadataComponent', () => { it('should display a success notification', () => { 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', () => { diff --git a/src/app/+collection-page/edit-collection-page/collection-metadata/collection-metadata.component.ts b/src/app/+collection-page/edit-collection-page/collection-metadata/collection-metadata.component.ts index 72ebbb8508..ca2a38aa86 100644 --- a/src/app/+collection-page/edit-collection-page/collection-metadata/collection-metadata.component.ts +++ b/src/app/+collection-page/edit-collection-page/collection-metadata/collection-metadata.component.ts @@ -12,6 +12,8 @@ import { switchMap, take } from 'rxjs/operators'; import { combineLatest as combineLatestObservable } from 'rxjs'; import { NotificationsService } from '../../../shared/notifications/notifications.service'; 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 @@ -35,7 +37,9 @@ export class CollectionMetadataComponent extends ComcolMetadataComponent 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) => { if (success) { this.notificationsService.success(null, this.translate.get('collection.edit.template.notifications.delete.success')); } else { this.notificationsService.error(null, this.translate.get('collection.edit.template.notifications.delete.error')); } + this.initTemplateItem(); }); } } diff --git a/src/app/core/cache/models/normalized-template-item.model.ts b/src/app/core/cache/models/normalized-template-item.model.ts index 029306b2c3..7a3259d74d 100644 --- a/src/app/core/cache/models/normalized-template-item.model.ts +++ b/src/app/core/cache/models/normalized-template-item.model.ts @@ -1,4 +1,4 @@ -import { inheritSerialization, deserialize, autoserializeAs } from 'cerialize'; +import { inheritSerialization, deserialize } from 'cerialize'; import { mapsTo, relationship } from '../builders/build-decorators'; import { TemplateItem } from '../../shared/template-item.model'; @@ -12,18 +12,6 @@ import { Collection } from '../../shared/collection.model'; @inheritSerialization(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 */ diff --git a/src/app/core/shared/template-item.model.ts b/src/app/core/shared/template-item.model.ts index a58ec047ce..ef60c6906e 100644 --- a/src/app/core/shared/template-item.model.ts +++ b/src/app/core/shared/template-item.model.ts @@ -8,7 +8,7 @@ import { ResourceType } from './resource-type'; * Class representing a DSpace Template 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