From 6f4ee8c3327128dc823ab89e5f8e05a0579cfbbe Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Sun, 17 Dec 2023 08:37:59 -0800 Subject: [PATCH] Implement more efficient getFew for document retrieval --- .../document-link.component.spec.ts | 30 ++++++++++++++----- .../document-link/document-link.component.ts | 6 ++-- .../rest/abstract-paperless-service.spec.ts | 15 ++++++++++ .../rest/abstract-paperless-service.ts | 13 ++++++++ 4 files changed, 54 insertions(+), 10 deletions(-) diff --git a/src-ui/src/app/components/common/input/document-link/document-link.component.spec.ts b/src-ui/src/app/components/common/input/document-link/document-link.component.spec.ts index d1af7ab2f..34c427cf6 100644 --- a/src-ui/src/app/components/common/input/document-link/document-link.component.spec.ts +++ b/src-ui/src/app/components/common/input/document-link/document-link.component.spec.ts @@ -48,10 +48,15 @@ describe('DocumentLinkComponent', () => { fixture.detectChanges() }) - it('should retrieve selected documents from APIs', () => { - const getSpy = jest.spyOn(documentService, 'getCachedMany') + it('should retrieve selected documents from API', () => { + const getSpy = jest.spyOn(documentService, 'getFew') getSpy.mockImplementation((ids) => { - return of(documents.filter((d) => ids.includes(d.id))) + const docs = documents.filter((d) => ids.includes(d.id)) + return of({ + count: docs.length, + all: docs.map((d) => d.id), + results: docs, + }) }) component.writeValue([1]) expect(getSpy).toHaveBeenCalled() @@ -85,8 +90,14 @@ describe('DocumentLinkComponent', () => { }) it('should load values correctly', () => { - jest.spyOn(documentService, 'getCachedMany').mockImplementation((ids) => { - return of(documents.filter((d) => ids.includes(d.id))) + const getSpy = jest.spyOn(documentService, 'getFew') + getSpy.mockImplementation((ids) => { + const docs = documents.filter((d) => ids.includes(d.id)) + return of({ + count: docs.length, + all: docs.map((d) => d.id), + results: docs, + }) }) component.writeValue([12, 23]) expect(component.value).toEqual([12, 23]) @@ -100,9 +111,14 @@ describe('DocumentLinkComponent', () => { }) it('should support unselect', () => { - const getSpy = jest.spyOn(documentService, 'getCachedMany') + const getSpy = jest.spyOn(documentService, 'getFew') getSpy.mockImplementation((ids) => { - return of(documents.filter((d) => ids.includes(d.id))) + const docs = documents.filter((d) => ids.includes(d.id)) + return of({ + count: docs.length, + all: docs.map((d) => d.id), + results: docs, + }) }) component.writeValue([12, 23]) component.unselect({ id: 23 }) diff --git a/src-ui/src/app/components/common/input/document-link/document-link.component.ts b/src-ui/src/app/components/common/input/document-link/document-link.component.ts index dd7118074..72482d1a1 100644 --- a/src-ui/src/app/components/common/input/document-link/document-link.component.ts +++ b/src-ui/src/app/components/common/input/document-link/document-link.component.ts @@ -58,11 +58,11 @@ export class DocumentLinkComponent } else { this.loading = true this.documentsService - .getCachedMany(documentIDs) + .getFew(documentIDs, { fields: 'id,title' }) .pipe(takeUntil(this.unsubscribeNotifier)) - .subscribe((documents) => { + .subscribe((documentResults) => { this.loading = false - this.selectedDocuments = documents + this.selectedDocuments = documentResults.results super.writeValue(documentIDs) }) } diff --git a/src-ui/src/app/services/rest/abstract-paperless-service.spec.ts b/src-ui/src/app/services/rest/abstract-paperless-service.spec.ts index 92ff923f4..7b5254bfd 100644 --- a/src-ui/src/app/services/rest/abstract-paperless-service.spec.ts +++ b/src-ui/src/app/services/rest/abstract-paperless-service.spec.ts @@ -96,6 +96,21 @@ export const commonAbstractPaperlessServiceTests = (endpoint, ServiceClass) => { expect(req.request.method).toEqual('PATCH') req.flush([]) }) + + test('should call appropriate api endpoint for get a few objects', () => { + subscription = service.getFew([1, 2, 3]).subscribe() + const req = httpTestingController.expectOne( + `${environment.apiBaseUrl}${endpoint}/?id__in=1,2,3` + ) + expect(req.request.method).toEqual('GET') + req.flush([]) + subscription = service.getFew([4, 5, 6], { foo: 'bar' }).subscribe() + const req2 = httpTestingController.expectOne( + `${environment.apiBaseUrl}${endpoint}/?id__in=4,5,6&foo=bar` + ) + expect(req2.request.method).toEqual('GET') + req2.flush([]) + }) }) beforeEach(() => { diff --git a/src-ui/src/app/services/rest/abstract-paperless-service.ts b/src-ui/src/app/services/rest/abstract-paperless-service.ts index 14735b1ad..9305ed8b6 100644 --- a/src-ui/src/app/services/rest/abstract-paperless-service.ts +++ b/src-ui/src/app/services/rest/abstract-paperless-service.ts @@ -91,6 +91,19 @@ export abstract class AbstractPaperlessService { ) } + getFew(ids: number[], extraParams?): Observable> { + let httpParams = new HttpParams() + httpParams = httpParams.set('id__in', ids.join(',')) + for (let extraParamKey in extraParams) { + if (extraParams[extraParamKey] != null) { + httpParams = httpParams.set(extraParamKey, extraParams[extraParamKey]) + } + } + return this.http.get>(this.getResourceUrl(), { + params: httpParams, + }) + } + clearCache() { this._listAll = null }