diff --git a/src/lib/services/generic.service.ts b/src/lib/services/generic.service.ts index b879393..5d12fb0 100644 --- a/src/lib/services/generic.service.ts +++ b/src/lib/services/generic.service.ts @@ -1,8 +1,8 @@ import { HttpClient, HttpEvent, HttpParams } from '@angular/common/http'; import { Injector } from '@angular/core'; import { Observable } from 'rxjs'; -import { CustomHttpUrlEncodingCodec, List, RequiredParam, Validate } from '../utils'; -import { HeadersConfiguration } from '../utils/headers-configuration'; +import { CustomHttpUrlEncodingCodec, HeadersConfiguration, List, RequiredParam, Validate } from '../utils'; +import { map, tap } from 'rxjs/operators'; export interface HeaderOptions { readonly authorization?: boolean; @@ -21,23 +21,35 @@ export interface QueryParam { */ export abstract class GenericService { protected readonly _http = this._injector.get(HttpClient); + private _lastCheckedForChanges = '0'; protected constructor(protected readonly _injector: Injector, protected readonly _defaultModelPath: string) {} + protected get _hasChanges$(): Observable { + return this._post<{ value: boolean }>({ since: this._lastCheckedForChanges }, `${this._defaultModelPath}/changes`).pipe( + map(res => res.value), + tap(() => this._updateLastChanged()), + ); + } + get(): Observable; + // eslint-disable-next-line @typescript-eslint/unified-signatures get(id: string, ...args: unknown[]): Observable; + // eslint-disable-next-line @typescript-eslint/no-unused-vars get(id?: string, ...args: unknown[]): Observable { return id ? this._getOne([id]) : this.getAll(); } getAll(modelPath = this._defaultModelPath, queryParams?: List): Observable { - return this._http.get(`/${encodeURI(modelPath)}`, { - headers: HeadersConfiguration.getHeaders({ contentType: false }), - observe: 'body', - params: this._queryParams(queryParams), - }); + return this._http + .get(`/${encodeURI(modelPath)}`, { + headers: HeadersConfiguration.getHeaders({ contentType: false }), + observe: 'body', + params: this._queryParams(queryParams), + }) + .pipe(tap(() => this._updateLastChanged())); } @Validate() @@ -64,8 +76,7 @@ export abstract class GenericService { formParams.append('file', data); } - // TODO: Fix getHeaders()? - const headers = HeadersConfiguration.getHeaders().delete('content-type'); + const headers = HeadersConfiguration.getHeaders({ contentType: false }); return this._http.post(`/${encodeURI(modelPath)}`, formParams, { headers, @@ -125,4 +136,8 @@ export abstract class GenericService { return queryParameters; } + + protected _updateLastChanged(): void { + this._lastCheckedForChanges = new Date().toISOString(); + } }