Implement more efficient getFew for document retrieval

This commit is contained in:
shamoon 2023-12-17 08:37:59 -08:00
parent d22b27afe7
commit 6f4ee8c332
4 changed files with 54 additions and 10 deletions

View File

@ -48,10 +48,15 @@ describe('DocumentLinkComponent', () => {
fixture.detectChanges() fixture.detectChanges()
}) })
it('should retrieve selected documents from APIs', () => { it('should retrieve selected documents from API', () => {
const getSpy = jest.spyOn(documentService, 'getCachedMany') const getSpy = jest.spyOn(documentService, 'getFew')
getSpy.mockImplementation((ids) => { 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]) component.writeValue([1])
expect(getSpy).toHaveBeenCalled() expect(getSpy).toHaveBeenCalled()
@ -85,8 +90,14 @@ describe('DocumentLinkComponent', () => {
}) })
it('should load values correctly', () => { it('should load values correctly', () => {
jest.spyOn(documentService, 'getCachedMany').mockImplementation((ids) => { const getSpy = jest.spyOn(documentService, 'getFew')
return of(documents.filter((d) => ids.includes(d.id))) 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]) component.writeValue([12, 23])
expect(component.value).toEqual([12, 23]) expect(component.value).toEqual([12, 23])
@ -100,9 +111,14 @@ describe('DocumentLinkComponent', () => {
}) })
it('should support unselect', () => { it('should support unselect', () => {
const getSpy = jest.spyOn(documentService, 'getCachedMany') const getSpy = jest.spyOn(documentService, 'getFew')
getSpy.mockImplementation((ids) => { 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.writeValue([12, 23])
component.unselect({ id: 23 }) component.unselect({ id: 23 })

View File

@ -58,11 +58,11 @@ export class DocumentLinkComponent
} else { } else {
this.loading = true this.loading = true
this.documentsService this.documentsService
.getCachedMany(documentIDs) .getFew(documentIDs, { fields: 'id,title' })
.pipe(takeUntil(this.unsubscribeNotifier)) .pipe(takeUntil(this.unsubscribeNotifier))
.subscribe((documents) => { .subscribe((documentResults) => {
this.loading = false this.loading = false
this.selectedDocuments = documents this.selectedDocuments = documentResults.results
super.writeValue(documentIDs) super.writeValue(documentIDs)
}) })
} }

View File

@ -96,6 +96,21 @@ export const commonAbstractPaperlessServiceTests = (endpoint, ServiceClass) => {
expect(req.request.method).toEqual('PATCH') expect(req.request.method).toEqual('PATCH')
req.flush([]) 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(() => { beforeEach(() => {

View File

@ -91,6 +91,19 @@ export abstract class AbstractPaperlessService<T extends ObjectWithId> {
) )
} }
getFew(ids: number[], extraParams?): Observable<Results<T>> {
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<Results<T>>(this.getResourceUrl(), {
params: httpParams,
})
}
clearCache() { clearCache() {
this._listAll = null this._listAll = null
} }