document service adds observables for linked data to its results

This commit is contained in:
jonaswinkler 2020-12-04 01:24:07 +01:00
parent 1d8765100c
commit 34f353f399
2 changed files with 28 additions and 5 deletions

View File

@ -2,14 +2,15 @@ import { PaperlessCorrespondent } from './paperless-correspondent'
import { ObjectWithId } from './object-with-id' import { ObjectWithId } from './object-with-id'
import { PaperlessTag } from './paperless-tag' import { PaperlessTag } from './paperless-tag'
import { PaperlessDocumentType } from './paperless-document-type' import { PaperlessDocumentType } from './paperless-document-type'
import { Observable } from 'rxjs'
export interface PaperlessDocument extends ObjectWithId { export interface PaperlessDocument extends ObjectWithId {
correspondent_object?: PaperlessCorrespondent correspondent$?: Observable<PaperlessCorrespondent>
correspondent?: number correspondent?: number
document_type_object?: PaperlessDocumentType document_type$?: Observable<PaperlessDocumentType>
document_type?: number document_type?: number
@ -19,7 +20,7 @@ export interface PaperlessDocument extends ObjectWithId {
file_type?: string file_type?: string
tags_objects?: PaperlessTag[] tags$?: Observable<PaperlessTag[]>
tags?: number[] tags?: number[]

View File

@ -6,6 +6,10 @@ import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
import { Results } from 'src/app/data/results'; import { Results } from 'src/app/data/results';
import { FilterRule } from 'src/app/data/filter-rule'; import { FilterRule } from 'src/app/data/filter-rule';
import { map } from 'rxjs/operators';
import { CorrespondentService } from './correspondent.service';
import { DocumentTypeService } from './document-type.service';
import { TagService } from './tag.service';
export const DOCUMENT_SORT_FIELDS = [ export const DOCUMENT_SORT_FIELDS = [
@ -27,7 +31,7 @@ export const SORT_DIRECTION_DESCENDING = "des"
}) })
export class DocumentService extends AbstractPaperlessService<PaperlessDocument> { export class DocumentService extends AbstractPaperlessService<PaperlessDocument> {
constructor(http: HttpClient) { constructor(http: HttpClient, private correspondentService: CorrespondentService, private documentTypeService: DocumentTypeService, private tagService: TagService) {
super(http, 'documents') super(http, 'documents')
} }
@ -47,8 +51,26 @@ export class DocumentService extends AbstractPaperlessService<PaperlessDocument>
} }
} }
addObservablesToDocument(doc: PaperlessDocument) {
if (doc.correspondent) {
doc.correspondent$ = this.correspondentService.getCached(doc.correspondent)
}
if (doc.document_type) {
doc.document_type$ = this.documentTypeService.getCached(doc.document_type)
}
if (doc.tags) {
doc.tags$ = this.tagService.getCachedMany(doc.tags)
}
return doc
}
list(page?: number, pageSize?: number, sortField?: string, sortDirection?: string, filterRules?: FilterRule[]): Observable<Results<PaperlessDocument>> { list(page?: number, pageSize?: number, sortField?: string, sortDirection?: string, filterRules?: FilterRule[]): Observable<Results<PaperlessDocument>> {
return super.list(page, pageSize, sortField, sortDirection, this.filterRulesToQueryParams(filterRules)) return super.list(page, pageSize, sortField, sortDirection, this.filterRulesToQueryParams(filterRules)).pipe(
map(results => {
results.results.forEach(doc => this.addObservablesToDocument(doc))
return results
})
)
} }
getPreviewUrl(id: number, original: boolean = false): string { getPreviewUrl(id: number, original: boolean = false): string {