2023-12-18 11:55:19 +01:00
|
|
|
import { HttpClient, HttpHeaders, HttpResponse } from '@angular/common/http';
|
2023-11-27 17:22:13 +01:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { IsActive } from '@app/core/common/enum/is-active.enum';
|
2023-12-06 22:10:01 +01:00
|
|
|
import { Description, DescriptionPersist, DescriptionStatusPersist, PublicDescription } from '@app/core/model/description/description';
|
2023-11-29 14:26:40 +01:00
|
|
|
import { DescriptionLookup } from '@app/core/query/description.lookup';
|
2023-11-27 17:22:13 +01:00
|
|
|
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';
|
|
|
|
|
|
|
|
@Injectable()
|
2023-11-29 14:26:40 +01:00
|
|
|
export class DescriptionService {
|
2023-11-27 17:22:13 +01:00
|
|
|
|
2023-11-29 14:26:40 +01:00
|
|
|
private headers = new HttpHeaders();
|
|
|
|
|
|
|
|
constructor(private http: BaseHttpV2Service, private httpClient: HttpClient, private configurationService: ConfigurationService, private filterService: FilterService) {
|
2023-11-27 17:22:13 +01:00
|
|
|
}
|
|
|
|
|
2023-11-29 14:26:40 +01:00
|
|
|
private get apiBase(): string { return `${this.configurationService.server}description`; }
|
2023-11-27 17:22:13 +01:00
|
|
|
|
2023-11-29 14:26:40 +01:00
|
|
|
query(q: DescriptionLookup): Observable<QueryResult<Description>> {
|
2023-11-27 17:22:13 +01:00
|
|
|
const url = `${this.apiBase}/query`;
|
2023-11-29 14:26:40 +01:00
|
|
|
return this.http.post<QueryResult<Description>>(url, q).pipe(catchError((error: any) => throwError(error)));
|
|
|
|
}
|
|
|
|
|
|
|
|
publicQuery(q: DescriptionLookup): Observable<QueryResult<PublicDescription>> {
|
|
|
|
const url = `${this.apiBase}/public/query`;
|
|
|
|
return this.http.post<QueryResult<PublicDescription>>(url, q).pipe(catchError((error: any) => throwError(error)));
|
2023-11-27 17:22:13 +01:00
|
|
|
}
|
|
|
|
|
2023-11-29 14:26:40 +01:00
|
|
|
getSingle(id: Guid, reqFields: string[] = []): Observable<Description> {
|
2023-11-27 17:22:13 +01:00
|
|
|
const url = `${this.apiBase}/${id}`;
|
|
|
|
const options = { params: { f: reqFields } };
|
|
|
|
|
|
|
|
return this.http
|
2023-11-29 14:26:40 +01:00
|
|
|
.get<Description>(url, options).pipe(
|
2023-11-27 17:22:13 +01:00
|
|
|
catchError((error: any) => throwError(error)));
|
|
|
|
}
|
|
|
|
|
2023-11-29 14:26:40 +01:00
|
|
|
getPublicSingle(id: Guid, reqFields: string[] = []): Observable<Description> {
|
|
|
|
const url = `${this.apiBase}/public/${id}`;
|
2023-11-28 18:10:04 +01:00
|
|
|
const options = { params: { f: reqFields } };
|
|
|
|
|
|
|
|
return this.http
|
2023-11-29 14:26:40 +01:00
|
|
|
.get<Description>(url, options).pipe(
|
2023-11-28 18:10:04 +01:00
|
|
|
catchError((error: any) => throwError(error)));
|
|
|
|
}
|
|
|
|
|
2023-11-29 14:26:40 +01:00
|
|
|
persist(item: DescriptionPersist): Observable<Description> {
|
2023-11-27 17:22:13 +01:00
|
|
|
const url = `${this.apiBase}/persist`;
|
|
|
|
|
|
|
|
return this.http
|
2023-11-29 14:26:40 +01:00
|
|
|
.post<Description>(url, item).pipe(
|
2023-11-27 17:22:13 +01:00
|
|
|
catchError((error: any) => throwError(error)));
|
|
|
|
}
|
|
|
|
|
2023-12-06 22:10:01 +01:00
|
|
|
persistStatus(item: DescriptionStatusPersist, reqFields: string[] = []): Observable<Description> {
|
|
|
|
const url = `${this.apiBase}/persist-status`;
|
|
|
|
|
|
|
|
return this.http
|
|
|
|
.post<Description>(url, item).pipe(
|
|
|
|
catchError((error: any) => throwError(error)));
|
|
|
|
}
|
|
|
|
|
2023-11-29 14:26:40 +01:00
|
|
|
delete(id: Guid): Observable<Description> {
|
2023-11-27 17:22:13 +01:00
|
|
|
const url = `${this.apiBase}/${id}`;
|
|
|
|
|
|
|
|
return this.http
|
2023-11-29 14:26:40 +01:00
|
|
|
.delete<Description>(url).pipe(
|
2023-11-27 17:22:13 +01:00
|
|
|
catchError((error: any) => throwError(error)));
|
|
|
|
}
|
|
|
|
|
2023-12-18 11:55:19 +01:00
|
|
|
public downloadPDF(id: string): Observable<HttpResponse<Blob>> {
|
|
|
|
return this.httpClient.get(`${this.apiBase}/${id}/export/Pdf`, { responseType: 'blob', observe: 'response', headers: this.headers });
|
|
|
|
}
|
|
|
|
|
|
|
|
public downloadDOCX(id: string): Observable<HttpResponse<Blob>> {
|
|
|
|
let headerDocx: HttpHeaders = this.headers.set('Content-Type', 'application/msword')
|
|
|
|
return this.httpClient.get(`${this.apiBase}/${id}/export/Word`, { responseType: 'blob', observe: 'response', headers: headerDocx });
|
|
|
|
}
|
|
|
|
|
2023-11-27 17:22:13 +01:00
|
|
|
//
|
|
|
|
// Autocomplete Commons
|
|
|
|
//
|
|
|
|
// tslint:disable-next-line: member-ordering
|
|
|
|
singleAutocompleteConfiguration: SingleAutoCompleteConfiguration = {
|
|
|
|
initialItems: (data?: any) => this.query(this.buildAutocompleteLookup()).pipe(map(x => x.items)),
|
|
|
|
filterFn: (searchQuery: string, data?: any) => this.query(this.buildAutocompleteLookup(searchQuery)).pipe(map(x => x.items)),
|
|
|
|
getSelectedItem: (selectedItem: any) => this.query(this.buildAutocompleteLookup(null, null, [selectedItem])).pipe(map(x => x.items[0])),
|
2023-11-29 14:26:40 +01:00
|
|
|
displayFn: (item: Description) => item.label,
|
|
|
|
titleFn: (item: Description) => item.label,
|
|
|
|
valueAssign: (item: Description) => item.id,
|
2023-11-27 17:22:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// tslint:disable-next-line: member-ordering
|
|
|
|
multipleAutocompleteConfiguration: MultipleAutoCompleteConfiguration = {
|
|
|
|
initialItems: (excludedItems: any[], data?: any) => this.query(this.buildAutocompleteLookup(null, excludedItems ? excludedItems : null)).pipe(map(x => x.items)),
|
|
|
|
filterFn: (searchQuery: string, excludedItems: any[]) => this.query(this.buildAutocompleteLookup(searchQuery, excludedItems)).pipe(map(x => x.items)),
|
|
|
|
getSelectedItems: (selectedItems: any[]) => this.query(this.buildAutocompleteLookup(null, null, selectedItems)).pipe(map(x => x.items)),
|
2023-11-29 14:26:40 +01:00
|
|
|
displayFn: (item: Description) => item.label,
|
|
|
|
titleFn: (item: Description) => item.label,
|
|
|
|
valueAssign: (item: Description) => item.id,
|
2023-11-27 17:22:13 +01:00
|
|
|
};
|
|
|
|
|
2023-11-29 14:26:40 +01:00
|
|
|
private buildAutocompleteLookup(like?: string, excludedIds?: Guid[], ids?: Guid[]): DescriptionLookup {
|
|
|
|
const lookup: DescriptionLookup = new DescriptionLookup();
|
2023-11-27 17:22:13 +01:00
|
|
|
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: [
|
2023-11-29 14:26:40 +01:00
|
|
|
nameof<Description>(x => x.id),
|
|
|
|
nameof<Description>(x => x.label)
|
2023-11-27 17:22:13 +01:00
|
|
|
]
|
|
|
|
};
|
2023-11-29 14:26:40 +01:00
|
|
|
lookup.order = { items: [nameof<Description>(x => x.label)] };
|
2023-11-27 17:22:13 +01:00
|
|
|
if (like) { lookup.like = this.filterService.transformLike(like); }
|
|
|
|
return lookup;
|
|
|
|
}
|
2023-11-29 14:26:40 +01:00
|
|
|
}
|