import { Injectable } from '@angular/core'; import { IsActive } from '@app/core/common/enum/is-active.enum'; import { ReferenceSourceType } from '@app/core/common/enum/reference-source-type'; import { DmpReference } from '@app/core/model/dmp/dmp-reference'; import { Definition, Field, Reference, ReferencePersist } from '@app/core/model/reference/reference'; import { ReferenceSearchDefinitionLookup, ReferenceSearchLookup } from '@app/core/query/reference-search.lookup'; import { ReferenceLookup } from '@app/core/query/reference.lookup'; import { MultipleAutoCompleteConfiguration } from '@app/library/auto-complete/multiple/multiple-auto-complete-configuration'; import { SingleAutoCompleteConfiguration } from '@app/library/auto-complete/single/single-auto-complete-configuration'; import { QueryResult } from '@common/model/query-result'; import { FilterService } from '@common/modules/text-filter/filter-service'; import { Guid } from '@common/types/guid'; import { Observable, throwError } from 'rxjs'; import { catchError, map } from 'rxjs/operators'; import { nameof } from 'ts-simple-nameof'; import { ConfigurationService } from '../configuration/configuration.service'; import { BaseHttpV2Service } from '../http/base-http-v2.service'; import { TranslateService } from '@ngx-translate/core'; import { ReferenceType } from '@app/core/model/reference-type/reference-type'; @Injectable() export class ReferenceService { constructor( private http: BaseHttpV2Service, private configurationService: ConfigurationService, private filterService: FilterService, private language: TranslateService, ) { } private get apiBase(): string { return `${this.configurationService.server}reference`; } query(q: ReferenceLookup): Observable> { const url = `${this.apiBase}/query`; return this.http.post>(url, q).pipe(catchError((error: any) => throwError(error))); } // search(q: ReferenceSearchLookup): Observable { // const url = `${this.apiBase}/search`; // return this.http.post(url, q).pipe(catchError((error: any) => throwError(error))); // } searchWithDefinition(q: ReferenceSearchDefinitionLookup): Observable { const url = `${this.apiBase}/search`; return this.http.post(url, q).pipe(catchError((error: any) => throwError(error))); } getSingle(id: Guid, reqFields: string[] = []): Observable { const url = `${this.apiBase}/${id}`; const options = { params: { f: reqFields } }; return this.http .get(url, options).pipe( catchError((error: any) => throwError(error))); } persist(item: ReferencePersist): Observable { const url = `${this.apiBase}/persist`; return this.http .post(url, item).pipe( catchError((error: any) => throwError(error))); } delete(id: Guid): Observable { const url = `${this.apiBase}/${id}`; return this.http .delete(url).pipe( catchError((error: any) => throwError(error))); } // // Autocomplete Commons - Query // public getSingleAutocompleteQueryConfiguration(referenceTypeIds?: Guid[], sourceTypes?: ReferenceSourceType[]): SingleAutoCompleteConfiguration { return { initialItems: (data?: any) => this.query(this.buildAutocompleteQueryLookup(referenceTypeIds, sourceTypes)).pipe(map(x => x.items)), filterFn: (searchQuery: string, data?: any) => this.query(this.buildAutocompleteQueryLookup(referenceTypeIds, sourceTypes, searchQuery)).pipe(map(x => x.items)), getSelectedItem: (selectedItem: any) => this.query(this.buildAutocompleteQueryLookup(referenceTypeIds, sourceTypes, null, null, [selectedItem])).pipe(map(x => x.items[0])), displayFn: (item: Reference) => item.label, titleFn: (item: Reference) => item.label, valueAssign: (item: Reference) => item.id, }; }; public getMultipleAutoCompleteQueryConfiguration(referenceTypeIds?: Guid[], sourceTypes?: ReferenceSourceType[]): MultipleAutoCompleteConfiguration { return { initialItems: (excludedItems: any[], data?: any) => this.query(this.buildAutocompleteQueryLookup(referenceTypeIds, sourceTypes, null, excludedItems ? excludedItems : null)).pipe(map(x => x.items)), filterFn: (searchQuery: string, excludedItems: any[]) => this.query(this.buildAutocompleteQueryLookup(referenceTypeIds, sourceTypes, searchQuery, excludedItems)).pipe(map(x => x.items)), getSelectedItems: (selectedItems: any[]) => this.query(this.buildAutocompleteQueryLookup(referenceTypeIds, sourceTypes, null, null, selectedItems)).pipe(map(x => x.items)), displayFn: (item: Reference) => item.label, titleFn: (item: Reference) => item.label, valueAssign: (item: Reference) => item.id, }; } private buildAutocompleteQueryLookup(referenceTypeIds?: Guid[], sourceTypes?: ReferenceSourceType[], like?: string, excludedIds?: Guid[], ids?: Guid[]): ReferenceLookup { const lookup: ReferenceLookup = new ReferenceLookup(); lookup.page = { size: 100, offset: 0 }; if (excludedIds && excludedIds.length > 0) { lookup.excludedIds = excludedIds; } if (ids && ids.length > 0) { lookup.ids = ids; } lookup.isActive = [IsActive.Active]; lookup.project = { fields: [ nameof(x => x.id), nameof(x => x.label), ] }; if (referenceTypeIds && referenceTypeIds.length > 0) { lookup.typeIds = referenceTypeIds; } if (sourceTypes && sourceTypes.length > 0) { lookup.sourceTypes = sourceTypes; } lookup.order = { items: [nameof(x => x.label)] }; if (like) { lookup.like = this.filterService.transformLike(like); } return lookup; } // // Autocomplete Commons - Search // public getSingleAutocompleteSearchConfiguration(typeId: Guid): SingleAutoCompleteConfiguration { return { initialItems: (data?: any) => this.searchWithDefinition(this.buildAutocompleteSearchLookup(typeId)).pipe(map(x => x)), filterFn: (searchQuery: string, data?: any) => this.searchWithDefinition(this.buildAutocompleteSearchLookup(typeId, searchQuery)).pipe(map(x => x)), getSelectedItem: (selectedItem: any) => this.query(this.buildAutocompleteSearchSelectedItemsLookup([typeId], null, null, null, [selectedItem])).pipe(map(x => x.items[0])), displayFn: (item: Reference) => item.label, titleFn: (item: Reference) => item.label, valueAssign: (item: Reference) => item.id, }; }; public getMultipleAutoCompleteSearchConfiguration(typeId: Guid): MultipleAutoCompleteConfiguration { return { initialItems: (excludedItems: any[], data?: any) => this.searchWithDefinition(this.buildAutocompleteSearchLookup(typeId, null)).pipe(map(x => x)), filterFn: (searchQuery: string, excludedItems: any[]) => this.searchWithDefinition(this.buildAutocompleteSearchLookup(typeId, searchQuery)).pipe(map(x => x)), getSelectedItems: (selectedItems: any[]) => this.query(this.buildAutocompleteSearchSelectedItemsLookup([typeId], null, null, null, selectedItems?.map(x => x.id))).pipe(map(x => x.items)), displayFn: (item: Reference) => item.label, titleFn: (item: Reference) => item.label, subtitleFn: (item: Reference) => item?.sourceType === ReferenceSourceType.External ? this.language.instant('REFERENCE-FIELD-COMPONENT.EXTERNAL-SOURCE') + ': ' + item.source : this.language.instant('REFERENCE-FIELD-COMPONENT.INTERNAL-SOURCE'), valueAssign: (item: Reference) => item, uniqueAssign: (item: Reference) => item.source + '_' + item.reference, }; } private buildAutocompleteSearchLookup(typeId: Guid, like?: string): ReferenceSearchLookup { const lookup: ReferenceSearchLookup = new ReferenceSearchLookup(); lookup.page = { size: 100, offset: 0 }; lookup.project = { fields: [ nameof(x => x.id), nameof(x => x.hash), nameof(x => x.label), nameof(x => x.type), [nameof(x => x.type), nameof(x => x.id)].join('.'), nameof(x => x.description), [nameof(x => x.definition), nameof(x => x.fields), nameof(x => x.code)].join('.'), [nameof(x => x.definition), nameof(x => x.fields), nameof(x => x.dataType)].join('.'), [nameof(x => x.definition), nameof(x => x.fields), nameof(x => x.value)].join('.'), nameof(x => x.reference), nameof(x => x.abbreviation), nameof(x => x.source), nameof(x => x.sourceType), ] }; lookup.typeId = typeId; lookup.order = { items: [nameof(x => x.label)] }; if (like) { lookup.like = this.filterService.transformLike(like); } return lookup; } private buildAutocompleteSearchSelectedItemsLookup(referenceTypeIds?: Guid[], sourceTypes?: ReferenceSourceType[], like?: string, excludedIds?: Guid[], ids?: Guid[]): ReferenceLookup { const lookup: ReferenceLookup = new ReferenceLookup(); lookup.page = { size: 100, offset: 0 }; if (excludedIds && excludedIds.length > 0) { lookup.excludedIds = excludedIds; } if (ids && ids.length > 0) { lookup.ids = ids; } lookup.isActive = [IsActive.Active]; lookup.project = { fields: [ nameof(x => x.id), nameof(x => x.label), nameof(x => x.type), nameof(x => x.description), [nameof(x => x.definition), nameof(x => x.fields), nameof(x => x.code)].join('.'), [nameof(x => x.definition), nameof(x => x.fields), nameof(x => x.dataType)].join('.'), [nameof(x => x.definition), nameof(x => x.fields), nameof(x => x.value)].join('.'), nameof(x => x.reference), nameof(x => x.abbreviation), nameof(x => x.source), nameof(x => x.sourceType), ] }; if (referenceTypeIds && referenceTypeIds.length > 0) { lookup.typeIds = referenceTypeIds; } if (sourceTypes && sourceTypes.length > 0) { lookup.sourceTypes = sourceTypes; } lookup.order = { items: [nameof(x => x.label)] }; if (like) { lookup.like = this.filterService.transformLike(like); } return lookup; } // // // UI Helpers // // hasRerefenceOfTypes(dmpReferences: DmpReference[], referenceTypeIds?: Guid[]): boolean { return this.getReferencesForTypes(dmpReferences, referenceTypeIds)?.length > 0; } getReferencesForTypes(dmpReferences: DmpReference[], referenceTypeIds?: Guid[]): DmpReference[] { return dmpReferences?.filter(x => referenceTypeIds?.includes(x?.reference?.type?.id)).filter(x=> x.isActive === IsActive.Active);; } getReferencesForTypesFirstSafe(dmpReferences: DmpReference[], referenceTypeIds?: Guid[]): DmpReference { return this.getReferencesForTypes(dmpReferences, referenceTypeIds)?.find(Boolean); } }