import { DocumentLinkRestService, IDocumentLink } from 'isogd-document-link-rest'; import { SolarRestService } from 'isogd-solar-rest'; import * as widgets from "oasi-widgets"; import * as _ from 'lodash'; import { SearchResultDocumentEx } from './search-candidates'; import { LinkedDocumentService } from '../../services/LinkedDocumentService'; import { DocumentIsogd, DocumentRestService, DocumentIsogdWrapper } from 'isogd-document-rest'; import { NsiRestService } from 'oasi-nsi-rest'; class LinkedDocumentsViewController { static $inject = ['$filter', 'solarRestService', 'linkedDocumentService', '$timeout', 'toastr', 'blockUI', 'documentLinkRestService', 'documentRestService', '$q', 'nsiRestService']; private document: DocumentIsogd; private linkedDocuments: IDocumentLink[]; private documents: any[]; private chapters: any[] = []; private linkdocTypes: any[]; private loadingStatus: widgets.LoadingStatus = widgets.LoadingStatus.LOADING; constructor(private $filter: ng.IFilterService, private solarService: SolarRestService, private linkedDocumentService: LinkedDocumentService, private $timeout: ng.ITimeoutService, private toastr: Toastr, private blockUI: any, private documentLinkRestService: DocumentLinkRestService, private documentRestService: DocumentRestService, private $q: ng.IQService, private nsiRestService: NsiRestService) { } $onInit() { this.documentLinkRestService.getAll(this.document.documentID).then(response => { this.linkedDocuments = _.filter(response, r => { return r.status === 'approve'; }); if (this.linkedDocuments.length > 0) { this.nsiRestService.get("LinkdocTypes").then(res => { this.linkdocTypes = res; return this.nsiRestService.get("OldChapters").then(res => { this.chapters = res; let docIds: any[] = _.map(this.linkedDocuments, d => { return d.doc2Id === this.document.documentID ? { id: d.doc1Id, linkType: d.linkType} : { id: d.doc2Id, linkType: d.linkType}; }); let promises: ng.IPromise[] = []; docIds.forEach(i => { promises.push(this.documentRestService.getDocument(i.id)); }); this.$q.all(promises).then(response => { this.documents = _.map(response, d => { return d.document; }); this.documents = _.orderBy(this.documents, function (e) { return e.documentRequisites.documentDate }, ['asc']); this.documents.forEach(d => { docIds.forEach(di => { if (di.id === d.documentID) { d.linkType = this.linkedDocumentService.findLinkTypeByCode(this.linkdocTypes, di.linkType); } }); }); this.loadingStatus = widgets.LoadingStatus.SUCCESS; }).catch(error => { this.loadingStatus = widgets.LoadingStatus.ERROR; console.log(error); }); }).catch(error => { this.loadingStatus = widgets.LoadingStatus.ERROR; console.log(error); }); }).catch(error => { this.loadingStatus = widgets.LoadingStatus.ERROR; console.log(error); }); } else { this.loadingStatus = widgets.LoadingStatus.SUCCESS; } }).catch(error => { this.loadingStatus = widgets.LoadingStatus.ERROR; console.log(error); }); } } export const LinkedDocumentsViewComponent: angular.IComponentOptions = { controller: LinkedDocumentsViewController, template: require('./linked-documents-view.html'), bindings: { document: '<' } };