From 7406ab5d471c9fbd7be2e6448435108d5e5df981 Mon Sep 17 00:00:00 2001 From: lotte Date: Fri, 9 Aug 2019 16:57:06 +0200 Subject: [PATCH] 62849: fixed delete/add relationships --- src/app/core/data/relationship.service.ts | 55 ++++++++++++------- ...dynamic-lookup-relation-modal.component.ts | 45 ++++++++------- 2 files changed, 56 insertions(+), 44 deletions(-) diff --git a/src/app/core/data/relationship.service.ts b/src/app/core/data/relationship.service.ts index ad3927db90..8922ce0f4e 100644 --- a/src/app/core/data/relationship.service.ts +++ b/src/app/core/data/relationship.service.ts @@ -3,7 +3,7 @@ import { RequestService } from './request.service'; import { HALEndpointService } from '../shared/hal-endpoint.service'; import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service'; import { hasValue, hasValueOperator, isNotEmptyOperator } from '../../shared/empty.util'; -import { distinctUntilChanged, filter, flatMap, map, startWith, switchAll, switchMap, tap } from 'rxjs/operators'; +import { distinctUntilChanged, filter, find, flatMap, map, startWith, switchAll, switchMap, tap } from 'rxjs/operators'; import { configureRequest, getRemoteDataPayload, getResponseFromEntry, getSucceededRemoteData } from '../shared/operators'; import { DeleteRequest, GetRequest, PostRequest, RestRequest } from './request.models'; import { Observable } from 'rxjs/internal/Observable'; @@ -44,10 +44,10 @@ export class RelationshipService { /** * Send a delete request for a relationship by ID - * @param uuid + * @param id */ - deleteRelationship(uuid: string): Observable { - return this.getRelationshipEndpoint(uuid).pipe( + deleteRelationship(id: string): Observable { + return this.getRelationshipEndpoint(id).pipe( isNotEmptyOperator(), distinctUntilChanged(), map((endpointURL: string) => new DeleteRequest(this.requestService.generateRequestId(), endpointURL)), @@ -118,23 +118,6 @@ export class RelationshipService { // ); // } - - /** - * Get an item its relationship types in the form of an array - * @param item - */ - getItemRelationshipTypesArray(item: Item): Observable { - return this.getItemRelationshipsArray(item).pipe( - flatMap((rels: Relationship[]) => - observableZip(...rels.map((rel: Relationship) => rel.relationshipType)).pipe( - map(([...arr]: Array>) => arr.map((d: RemoteData) => d.payload).filter((type) => hasValue(type))), - filter((arr) => arr.length === rels.length) - ) - ), - distinctUntilChanged(compareArraysUsingIds()) - ); - } - /** * Get an array of the labels of an item’s unique relationship types * The array doesn't contain any duplicate labels @@ -248,4 +231,34 @@ export class RelationshipService { map((item: Item) => uuids.includes(item.uuid)) ); } + + + getRelationshipByItemsAndLabel(item1: Item, item2: Item, label: string): Observable { + return this.getItemRelationshipsByLabel(item1, label) + .pipe( + switchMap((relationships: Relationship[]) => { + return observableCombineLatest(...relationships.map((relationship: Relationship) => { + return observableCombineLatest( + this.isItemMatchWithItemRD(relationship.leftItem, item2), + this.isItemMatchWithItemRD(relationship.rightItem, item2) + ).pipe( + filter(([isLeftItem, isRightItem]) => isLeftItem || isRightItem), + map(() => relationship), + startWith(undefined) + ); + })) + }), + map((relationships: Relationship[]) => relationships.find((relationship => hasValue(relationship)))), + ) + } + + + private isItemMatchWithItemRD(itemRD$: Observable>, itemCheck: Item): Observable { + return itemRD$.pipe( + getSucceededRemoteData(), + map((itemRD: RemoteData) => itemRD.payload), + map((item: Item) => item.uuid === itemCheck.uuid) + ); + } + } diff --git a/src/app/shared/form/builder/ds-dynamic-form-ui/relation-lookup-modal/dynamic-lookup-relation-modal.component.ts b/src/app/shared/form/builder/ds-dynamic-form-ui/relation-lookup-modal/dynamic-lookup-relation-modal.component.ts index b923332e8f..09ceb94f21 100644 --- a/src/app/shared/form/builder/ds-dynamic-form-ui/relation-lookup-modal/dynamic-lookup-relation-modal.component.ts +++ b/src/app/shared/form/builder/ds-dynamic-form-ui/relation-lookup-modal/dynamic-lookup-relation-modal.component.ts @@ -23,7 +23,7 @@ import { RelationshipType } from '../../../../../core/shared/item-relationships/ import { RelationshipService } from '../../../../../core/data/relationship.service'; import { Item } from '../../../../../core/shared/item.model'; import { RelationshipOptions } from '../../models/relationship-options.model'; -import { combineLatest as observableCombineLatest } from 'rxjs'; +import { Relationship } from '../../../../../core/shared/item-relationships/relationship.model'; @Component({ selector: 'ds-dynamic-lookup-relation-modal', @@ -146,26 +146,26 @@ export class DsDynamicLookupRelationModalComponent implements OnInit, OnDestroy select(selectableObject: SearchResult) { - const relationshipType$: Observable = this.itemRD$.pipe( - getSucceededRemoteData(), - switchMap((itemRD: RemoteData) => { - const type1: string = itemRD.payload.firstMetadataValue('relationship.type'); - const type2: string = selectableObject.indexableObject.firstMetadataValue('relationship.type'); - return this.relationshipTypeService.getRelationshipTypeByLabelAndTypes(this.relationship.relationshipType, type1, type2); - })); - - this.subscription = observableCombineLatest(relationshipType$, this.itemRD$) + this.subscription = this.itemRD$ .pipe( + getSucceededRemoteData(), take(1), - switchMap(([type, itemRD]: [RelationshipType, RemoteData]) => { - const isSwitched = type.rightLabel === this.relationship.relationshipType; - let result; - if (isSwitched) { - result = this.relationshipService.addRelationship(type.id, selectableObject.indexableObject, itemRD.payload); - } else { - result = this.relationshipService.addRelationship(type.id, itemRD.payload, selectableObject.indexableObject); - } - return result; + switchMap((itemRD: RemoteData) => { + const type1: string = itemRD.payload.firstMetadataValue('relationship.type'); + const type2: string = selectableObject.indexableObject.firstMetadataValue('relationship.type'); + return this.relationshipTypeService.getRelationshipTypeByLabelAndTypes(this.relationship.relationshipType, type1, type2).pipe( + switchMap((type: RelationshipType) => { + const isSwitched = type.rightLabel === this.relationship.relationshipType; + let result; + if (isSwitched) { + result = this.relationshipService.addRelationship(type.id, selectableObject.indexableObject, itemRD.payload); + } else { + result = this.relationshipService.addRelationship(type.id, itemRD.payload, selectableObject.indexableObject); + } + return result; + }) + ); + }) ) .subscribe(); @@ -173,11 +173,10 @@ export class DsDynamicLookupRelationModalComponent implements OnInit, OnDestroy deselect(selectableObject: SearchResult) { - this.itemRD$.pipe( + const subscription = this.itemRD$.pipe( getSucceededRemoteData(), - switchMap((itemRD: RemoteData) => this.relationshipService.getItemRelationshipsByLabel(itemRD.payload, this.relationship.relationshipType)), - - // map((items: Item[]) => items.find((item: Item) => )) + switchMap((itemRD: RemoteData) => this.relationshipService.getRelationshipByItemsAndLabel(itemRD.payload, selectableObject.indexableObject, this.relationship.relationshipType)), + switchMap((relationship: Relationship) => this.relationshipService.deleteRelationship(relationship.id)), ).subscribe(); }