From 3c26ecdcdf5046fcd47437642dbcc5119726a26f Mon Sep 17 00:00:00 2001 From: Rezart Vata Date: Mon, 20 Sep 2021 19:43:11 +0200 Subject: [PATCH] [CST-5404] Code refactoring, fixing issues , lint fixes and started unit testing --- .../core/data/relationship-type.service.ts | 15 +++--- src/app/core/data/relationship.service.ts | 24 ++++++---- .../edit-relationship-list.component.ts | 46 +++++++------------ .../item-relationships.component.ts | 2 +- ...dynamic-lookup-relation-modal.component.ts | 6 +-- ...ic-lookup-relation-search-tab.component.ts | 28 +++++------ ...lookup-relation-selection-tab.component.ts | 3 +- 7 files changed, 59 insertions(+), 65 deletions(-) diff --git a/src/app/core/data/relationship-type.service.ts b/src/app/core/data/relationship-type.service.ts index a4f11b217c..66d4b9c7b2 100644 --- a/src/app/core/data/relationship-type.service.ts +++ b/src/app/core/data/relationship-type.service.ts @@ -122,15 +122,12 @@ export class RelationshipTypeService extends DataService { } /** - * Check if the given RelationshipType has the given itemTypes on its left and right sides. + * Search of the given RelationshipType if has the given itemTypes on its left and right sides. * Returns an observable of the given RelationshipType if it matches, null if it doesn't * * @param type The RelationshipType to check - * @param leftItemType The item type that should be on the left side - * @param rightItemType The item type that should be on the right side - * @private */ - searchByEntityType(type: string): Observable { + searchByEntityType(type: string): Observable> { return this.searchBy( 'byEntityType', @@ -139,12 +136,16 @@ export class RelationshipTypeService extends DataService { { fieldName: 'type', fieldValue: type - } + }, + { + fieldName: 'size', + fieldValue: 100 + }, ] }, true,true,followLink('leftType'),followLink('rightType')).pipe( getFirstSucceededRemoteData(), getRemoteDataPayload(), - ) as Observable; + ) as Observable>; } diff --git a/src/app/core/data/relationship.service.ts b/src/app/core/data/relationship.service.ts index 3cc82bd0dc..649d9afcf8 100644 --- a/src/app/core/data/relationship.service.ts +++ b/src/app/core/data/relationship.service.ts @@ -470,9 +470,17 @@ export class RelationshipService extends DataService { return this.put(object); } + /** + * Patch isn't supported on the relationship endpoint, so use put instead. + * + * @param typeId the relationship type id to apply as a filter to the returned relationships + * @param itemUuid The uuid of the item to be checked on the side defined by relationshipLabel + * @param relationshipLabel the name of the relation as defined from the side of the itemUuid + * @param arrayOfItemIds The uuid of the items to be found on the other side of returned relationships + */ searchByItemsAndType(typeId: string,itemUuid: string,relationshipLabel: string, arrayOfItemIds: string[] ): Observable { - let searchParams = [ + const searchParams = [ { fieldName: 'typeId', fieldValue: typeId @@ -485,20 +493,20 @@ export class RelationshipService extends DataService { fieldName: 'relationshipLabel', fieldValue: relationshipLabel }, - // { - // fieldName: 'page', - // fieldValue: 1 - // }, + { + fieldName: 'size', + fieldValue: arrayOfItemIds.length + }, ]; - arrayOfItemIds.forEach((itemId)=>{ + arrayOfItemIds.forEach( (itemId) => { searchParams.push( { fieldName: 'relatedItem', fieldValue: itemId }, - ) - }) + ); + }); return this.searchBy( 'byItemsAndType', diff --git a/src/app/item-page/edit-item-page/item-relationships/edit-relationship-list/edit-relationship-list.component.ts b/src/app/item-page/edit-item-page/item-relationships/edit-relationship-list/edit-relationship-list.component.ts index c125dd0e93..2a19be2437 100644 --- a/src/app/item-page/edit-item-page/item-relationships/edit-relationship-list/edit-relationship-list.component.ts +++ b/src/app/item-page/edit-item-page/item-relationships/edit-relationship-list/edit-relationship-list.component.ts @@ -142,8 +142,6 @@ export class EditRelationshipListComponent implements OnInit, OnDestroy { listOfRelatedItems = []; - relationshipTypeInfo: string; - constructor( protected objectUpdatesService: ObjectUpdatesService, protected linkService: LinkService, @@ -228,7 +226,7 @@ export class EditRelationshipListComponent implements OnInit, OnDestroy { modalComp.select = (...selectableObjects: SearchResult[]) => { selectableObjects.forEach((searchResult) => { const relatedItem: Item = searchResult.indexableObject; - this.getFieldUpdatesForRelatedItemNew(relatedItem) + this.getIsRelatedItem(relatedItem) .subscribe((isRelated: boolean) => { @@ -311,8 +309,6 @@ export class EditRelationshipListComponent implements OnInit, OnDestroy { // console.log(items); // this.selectableListService.select(this.listId, items); }); - - console.log(modalComp); } /** @@ -320,7 +316,6 @@ export class EditRelationshipListComponent implements OnInit, OnDestroy { * @param relatedItem The item for which to get the existing field updates */ private getFieldUpdatesForRelatedItem(relatedItem: Item): Observable { - console.log("getFieldUpdatesForRelatedItem",relatedItem); return this.updates$.pipe( take(1), map((updates) => Object.values(updates) @@ -333,40 +328,34 @@ export class EditRelationshipListComponent implements OnInit, OnDestroy { ).pipe( defaultIfEmpty([]), map((relatedItems) => { - console.log("identifiables",identifiables,relatedItems); - return identifiables.filter((identifiable, index) => - { - console.log(relatedItems[index].uuid,relatedItems,relatedItem.uuid); - return relatedItems[index].uuid === relatedItem.uuid - }) + return identifiables.filter( (identifiable, index) => { + return relatedItems[index].uuid === relatedItem.uuid; + }); } ), - tap(res=> console.log("getFieldUpdatesForRelatedItem -> res",res)) ) ) ); } - - private getFieldUpdatesForRelatedItemNew(relatedItem: Item): Observable { + /** + * Check if the given item is related with the item we are editing relationships + * @param relatedItem The item for which to get the existing field updates + */ + private getIsRelatedItem(relatedItem: Item): Observable { return this.currentItemIsLeftItem$.pipe( take(1), - map(isLeft => { - if(isLeft){ - this.relationshipType.leftwardType; - let listOfRelatedItems = this.item.allMetadataValues('relation.'+this.relationshipType.leftwardType); - return !!listOfRelatedItems.find((uuid) => uuid == relatedItem.uuid); - }else{ - this.relationshipType.rightwardType; - let listOfRelatedItems = this.item.allMetadataValues('relation.'+this.relationshipType.rightwardType); - return !!listOfRelatedItems.find((uuid) => uuid == relatedItem.uuid); + map( isLeft => { + if (isLeft) { + const listOfRelatedItems = this.item.allMetadataValues( 'relation.' + this.relationshipType.leftwardType ); + return !!listOfRelatedItems.find( (uuid) => uuid === relatedItem.uuid ); + } else { + const listOfRelatedItems = this.item.allMetadataValues( 'relation.' + this.relationshipType.rightwardType ); + return !!listOfRelatedItems.find( (uuid) => uuid === relatedItem.uuid ); } }) ); - - // return this.relationshipService.searchByItemsAndType(this.relationshipType.id, this.item.uuid, this.relationshipTypeInfo ,[relatedItem.uuid]) - // .pipe(map((res:any)=> res.page)); } /** @@ -497,12 +486,10 @@ export class EditRelationshipListComponent implements OnInit, OnDestroy { defaultIfEmpty([]) )), switchMap((nextFields: RelationshipIdentifiable[]) => { - console.log("nextFields",nextFields); // Get a list that contains the unsaved changes for the page, as well as the page of // RelationshipIdentifiables, as a single list of FieldUpdates return this.objectUpdatesService.getFieldUpdates(this.url, nextFields).pipe( map((fieldUpdates: FieldUpdates) => { - console.log("fieldUpdates",fieldUpdates); const fieldUpdatesFiltered: FieldUpdates = {}; this.nbAddedFields$.next(0); // iterate over the fieldupdates and filter out the ones that pertain to this @@ -535,7 +522,6 @@ export class EditRelationshipListComponent implements OnInit, OnDestroy { startWith({}), ).subscribe((updates: FieldUpdates) => { this.loading$.next(false); - console.log(updates); this.updates$.next(updates); })); } diff --git a/src/app/item-page/edit-item-page/item-relationships/item-relationships.component.ts b/src/app/item-page/edit-item-page/item-relationships/item-relationships.component.ts index dfc6f7e576..305016ce30 100644 --- a/src/app/item-page/edit-item-page/item-relationships/item-relationships.component.ts +++ b/src/app/item-page/edit-item-page/item-relationships/item-relationships.component.ts @@ -83,7 +83,7 @@ export class ItemRelationshipsComponent extends AbstractItemUpdateComponent { this.relationshipTypes$ = this.relationshipTypeService.searchByEntityType(label) .pipe( - map((relationshipTypes:any) => relationshipTypes.page) + map((relationshipTypes: PaginatedList) => relationshipTypes.page) ); this.entityType$ = this.entityTypeService.getEntityTypeByLabel(label).pipe( 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 fafd150e05..2f126d7707 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 @@ -116,11 +116,11 @@ export class DsDynamicLookupRelationModalComponent implements OnInit, OnDestroy relationshipType; - currentItemIsLeftItem$ : Observable; + currentItemIsLeftItem$: Observable; - isLeft: boolean = false; + isLeft = false; - isEditRelationship: boolean = false; + isEditRelationship = false; constructor( diff --git a/src/app/shared/form/builder/ds-dynamic-form-ui/relation-lookup-modal/search-tab/dynamic-lookup-relation-search-tab.component.ts b/src/app/shared/form/builder/ds-dynamic-form-ui/relation-lookup-modal/search-tab/dynamic-lookup-relation-search-tab.component.ts index 30685cb8f8..5cba67cc01 100644 --- a/src/app/shared/form/builder/ds-dynamic-form-ui/relation-lookup-modal/search-tab/dynamic-lookup-relation-search-tab.component.ts +++ b/src/app/shared/form/builder/ds-dynamic-form-ui/relation-lookup-modal/search-tab/dynamic-lookup-relation-search-tab.component.ts @@ -168,9 +168,9 @@ export class DsDynamicLookupRelationSearchTabComponent implements OnInit, OnDest this.resultsRD$ = this.searchConfigService.paginatedSearchOptions.pipe( switchMap((options) => this.lookupRelationService.getLocalResults(this.relationship, options).pipe( startWith(undefined), - tap(res=> { - if(!!res && res.state == 'Success' && this.isEditRelationship){ - const idOfItems = res.payload.page.map(itemSearchResult => { + tap( res => { + if ( !!res && res.state === 'Success' && this.isEditRelationship ) { + const idOfItems = res.payload.page.map( itemSearchResult => { return itemSearchResult.indexableObject.uuid; }); this.setSelectedIds(idOfItems,res.payload.page); @@ -246,26 +246,26 @@ export class DsDynamicLookupRelationSearchTabComponent implements OnInit, OnDest setSelectedIds(idOfItems, resultListOfItems) { let relationType = this.relationshipType.rightwardType; - if(this.isLeft){ + if ( this.isLeft ) { relationType = this.relationshipType.leftwardType; } - this.relationshipService.searchByItemsAndType(this.relationshipType.id, this.item.uuid, relationType ,idOfItems).subscribe((res: any)=>{ + this.relationshipService.searchByItemsAndType( this.relationshipType.id, this.item.uuid, relationType ,idOfItems ).subscribe( (res: any) => { - const selectableObject = res.page.map((relationship: Relationship) => { + const selectableObject = res.page.map( (relationship: Relationship) => { let arrUrl = []; - if(this.isLeft){ + if ( this.isLeft ) { arrUrl = relationship._links.rightItem.href.split('/'); - }else{ + } else { arrUrl = relationship._links.leftItem.href.split('/'); } - let uuid = arrUrl[arrUrl.length-1]; + const uuid = arrUrl[ arrUrl.length - 1 ]; return this.getRelatedItem(uuid,resultListOfItems); }); - // console.log(selectableObject); - if(selectableObject.length > 0){ + + if ( selectableObject.length > 0 ) { this.selectableListService.select(this.listId, selectableObject); } }); @@ -282,9 +282,9 @@ export class DsDynamicLookupRelationSearchTabComponent implements OnInit, OnDest this.selectableListService.deselectAll(this.listId); } - getRelatedItem(uuid,resultList){ - return resultList.find((resultItem) => { - return resultItem.indexableObject.uuid == uuid; + getRelatedItem(uuid: string, resultList: SearchResult[]) { + return resultList.find( (resultItem) => { + return resultItem.indexableObject.uuid === uuid; }); } diff --git a/src/app/shared/form/builder/ds-dynamic-form-ui/relation-lookup-modal/selection-tab/dynamic-lookup-relation-selection-tab.component.ts b/src/app/shared/form/builder/ds-dynamic-form-ui/relation-lookup-modal/selection-tab/dynamic-lookup-relation-selection-tab.component.ts index 78f8b6fa21..ef85b09d60 100644 --- a/src/app/shared/form/builder/ds-dynamic-form-ui/relation-lookup-modal/selection-tab/dynamic-lookup-relation-selection-tab.component.ts +++ b/src/app/shared/form/builder/ds-dynamic-form-ui/relation-lookup-modal/selection-tab/dynamic-lookup-relation-selection-tab.component.ts @@ -115,8 +115,7 @@ export class DsDynamicLookupRelationSelectionTabComponent { totalPages: Math.ceil(selected.length / pagination.pageSize) }); return createSuccessfulRemoteDataObject(buildPaginatedList(pageInfo, selection)); - }), - tap((res)=> console.log(res)) + }) ); }) );