From d2883b83c5914022c9ad76b33c7103f2810f2396 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Sat, 1 Jun 2024 19:28:31 -0700 Subject: [PATCH] Fix: including ordering param for id__in retrievals from frontend (#6875) --- .../custom-field-display.component.spec.ts | 2 +- .../custom-field-display.component.ts | 4 +++- .../document-link/document-link.component.spec.ts | 14 ++++++++++++++ .../input/document-link/document-link.component.ts | 4 +++- .../rest/abstract-paperless-service.spec.ts | 4 ++-- .../services/rest/abstract-paperless-service.ts | 1 + 6 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src-ui/src/app/components/common/custom-field-display/custom-field-display.component.spec.ts b/src-ui/src/app/components/common/custom-field-display/custom-field-display.component.spec.ts index 4d263aa86..738198335 100644 --- a/src-ui/src/app/components/common/custom-field-display/custom-field-display.component.spec.ts +++ b/src-ui/src/app/components/common/custom-field-display/custom-field-display.component.spec.ts @@ -18,7 +18,7 @@ const document: Document = { custom_fields: [ { field: 1, document: 1, created: null, value: 'Text value' }, { field: 2, document: 1, created: null, value: 'USD100' }, - { field: 3, document: 1, created: null, value: '1,2,3' }, + { field: 3, document: 1, created: null, value: [1, 2, 3] }, ], } diff --git a/src-ui/src/app/components/common/custom-field-display/custom-field-display.component.ts b/src-ui/src/app/components/common/custom-field-display/custom-field-display.component.ts index eb1fdb830..086ac217e 100644 --- a/src-ui/src/app/components/common/custom-field-display/custom-field-display.component.ts +++ b/src-ui/src/app/components/common/custom-field-display/custom-field-display.component.ts @@ -105,7 +105,9 @@ export class CustomFieldDisplayComponent implements OnInit, OnDestroy { .getFew(this.value, { fields: 'id,title' }) .pipe(takeUntil(this.unsubscribeNotifier)) .subscribe((result: Results) => { - this.docLinkDocuments = result.results + this.docLinkDocuments = this.value.map((id) => + result.results.find((d) => d.id === id) + ) }) } 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 e00460ec5..a8686bb05 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 @@ -66,6 +66,20 @@ describe('DocumentLinkComponent', () => { expect(getSpy).toHaveBeenCalled() }) + it('shoud maintain ordering of selected documents', () => { + 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, 1]) + expect(component.selectedDocuments).toEqual([documents[1], documents[0]]) + }) + it('should search API on select text input', () => { const listSpy = jest.spyOn(documentService, 'listFiltered') listSpy.mockImplementation( 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 3ec26cb60..83a6a742e 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 @@ -65,7 +65,9 @@ export class DocumentLinkComponent .pipe(takeUntil(this.unsubscribeNotifier)) .subscribe((documentResults) => { this.loading = false - this.selectedDocuments = documentResults.results + this.selectedDocuments = documentIDs.map((id) => + documentResults.results.find((d) => d.id === id) + ) 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 7b5254bfd..0cabcef02 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 @@ -100,13 +100,13 @@ export const commonAbstractPaperlessServiceTests = (endpoint, ServiceClass) => { 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` + `${environment.apiBaseUrl}${endpoint}/?id__in=1,2,3&ordering=-id` ) 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` + `${environment.apiBaseUrl}${endpoint}/?id__in=4,5,6&ordering=-id&foo=bar` ) expect(req2.request.method).toEqual('GET') req2.flush([]) 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 9305ed8b6..363575344 100644 --- a/src-ui/src/app/services/rest/abstract-paperless-service.ts +++ b/src-ui/src/app/services/rest/abstract-paperless-service.ts @@ -94,6 +94,7 @@ export abstract class AbstractPaperlessService { getFew(ids: number[], extraParams?): Observable> { let httpParams = new HttpParams() httpParams = httpParams.set('id__in', ids.join(',')) + httpParams = httpParams.set('ordering', '-id') for (let extraParamKey in extraParams) { if (extraParams[extraParamKey] != null) { httpParams = httpParams.set(extraParamKey, extraParams[extraParamKey])