[CST-5404] Code refactoring, fixing issues , lint fixes and started unit testing

This commit is contained in:
Rezart Vata
2021-09-20 19:43:11 +02:00
parent a2605cdddd
commit 3c26ecdcdf
7 changed files with 59 additions and 65 deletions

View File

@@ -122,15 +122,12 @@ export class RelationshipTypeService extends DataService<RelationshipType> {
}
/**
* 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<RelationshipType[]> {
searchByEntityType(type: string): Observable<PaginatedList<RelationshipType>> {
return this.searchBy(
'byEntityType',
@@ -139,12 +136,16 @@ export class RelationshipTypeService extends DataService<RelationshipType> {
{
fieldName: 'type',
fieldValue: type
}
},
{
fieldName: 'size',
fieldValue: 100
},
]
}, true,true,followLink('leftType'),followLink('rightType')).pipe(
getFirstSucceededRemoteData(),
getRemoteDataPayload(),
) as Observable<RelationshipType[]>;
) as Observable<PaginatedList<RelationshipType>>;
}

View File

@@ -470,9 +470,17 @@ export class RelationshipService extends DataService<Relationship> {
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<Relationship[]> {
let searchParams = [
const searchParams = [
{
fieldName: 'typeId',
fieldValue: typeId
@@ -485,20 +493,20 @@ export class RelationshipService extends DataService<Relationship> {
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',

View File

@@ -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<Item>[]) => {
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<RelationshipIdentifiable[]> {
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<boolean> {
/**
* 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<boolean> {
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);
}));
}

View File

@@ -83,7 +83,7 @@ export class ItemRelationshipsComponent extends AbstractItemUpdateComponent {
this.relationshipTypes$ = this.relationshipTypeService.searchByEntityType(label)
.pipe(
map((relationshipTypes:any) => relationshipTypes.page)
map((relationshipTypes: PaginatedList<RelationshipType>) => relationshipTypes.page)
);
this.entityType$ = this.entityTypeService.getEntityTypeByLabel(label).pipe(

View File

@@ -116,11 +116,11 @@ export class DsDynamicLookupRelationModalComponent implements OnInit, OnDestroy
relationshipType;
currentItemIsLeftItem$ : Observable<boolean>;
currentItemIsLeftItem$: Observable<boolean>;
isLeft: boolean = false;
isLeft = false;
isEditRelationship: boolean = false;
isEditRelationship = false;
constructor(

View File

@@ -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<Item>[]) {
return resultList.find( (resultItem) => {
return resultItem.indexableObject.uuid === uuid;
});
}

View File

@@ -115,8 +115,7 @@ export class DsDynamicLookupRelationSelectionTabComponent {
totalPages: Math.ceil(selected.length / pagination.pageSize)
});
return createSuccessfulRemoteDataObject(buildPaginatedList(pageInfo, selection));
}),
tap((res)=> console.log(res))
})
);
})
);