import * as widgets from "oasi-widgets"; import { DocumentLinkRestService } from "isogd-document-link-rest"; import { IParcelDossierWrapper, DossierRestService, DossierLinkRestService } from "isogd-dossier-rest"; import * as _ from "lodash"; import { LinkedDocumentService } from "../../services/LinkedDocumentService"; import { DocumentIsogd, DocumentRestService, DocumentIsogdWrapper } from "isogd-document-rest"; import { NsiRestService } from "oasi-nsi-rest"; class DossierEditController { static $inject = ['linkedDocumentService', '$timeout', 'documentLinkRestService', 'blockUI', 'nsiRestService', '$q', 'dossierRestService', 'dossierLinkRestService', 'documentRestService']; private document: DocumentIsogd; private hasDoc: boolean; private dossiers: IParcelDossierWrapper[] = []; private dossierCandidates: IParcelDossierWrapper[] = []; private loadingStatus: widgets.LoadingStatus = widgets.LoadingStatus.LOADING; private districts: any[]; private prefects: any[]; private chapters: any[] = []; private myBlockUI: any; constructor(private linkedDocumentService: LinkedDocumentService, private $timeout: ng.ITimeoutService, private documentLinkRestService: DocumentLinkRestService, private blockUI: any, private nsiRestService: NsiRestService, private $q: ng.IQService, private dossierRestService: DossierRestService, private dossierLinkRestService: DossierLinkRestService, private documentRestService: DocumentRestService) { } update() { this.myBlockUI.start(); this.$timeout(1500).then(response => { this.getDossiersByDocumentId(this.document.documentID).then(response => { this.dossiers = response; this.documentLinkRestService.getAll(this.document.documentID).then(response => { let uniqDocIds = _.map(response, d => { return (d.doc1Id === this.document.documentID) ? d.doc2Id : d.doc1Id; }) let promises: ng.IPromise[] = []; uniqDocIds.forEach(id => { promises.push(this.getDossiersByDocumentId(id)); }); if (this.document.territory && this.document.territory.cadastralNumber && this.document.territory.cadastralNumber.length > 0) { promises.push(this.getDossiersByCadastrals(this.document.territory.cadastralNumber)); } this.$q.all(promises).then(response => { response.forEach((r: any[]) => { r.forEach(p => { this.dossierCandidates.push(p); }); }); this.dossierCandidates = (_).uniqBy(this.dossierCandidates, d => { return d.parcelDossier.dossierId }); let newDossierCandidates: any[] = []; this.dossierCandidates.forEach(dc => { let exist: boolean = false; this.dossiers.forEach(d => { if (d.parcelDossier.dossierId === dc.parcelDossier.dossierId) { exist = true; } }); if (!exist) { newDossierCandidates.push(dc); } }); this.dossierCandidates = newDossierCandidates; this.myBlockUI.stop(); }).catch(error => { this.myBlockUI.stop(); }); }).catch(error => { this.myBlockUI.stop(); }); }).catch(error => { this.myBlockUI.stop(); }); }); } getDossiersByDocumentId(id: string): ng.IPromise { let defer: ng.IDeferred = this.$q.defer(); let dossiers: IParcelDossierWrapper[] = []; this.dossierLinkRestService.getToDocument(id).then(response => { let links = response; if (response.length > 0) { let data = _.map(response, r => { return r.dossierId; }); this.dossierRestService.getDossiers(data).then(response => { dossiers = response; dossiers.forEach(d => { links.forEach(l => { if (d.parcelDossier.dossierId === l.dossierId) { (d).link = l; } }); }); dossiers.forEach(d => { (d).documents = []; (d).loading = widgets.LoadingStatus.LOADING; this.dossierLinkRestService.getFromDossier(d.parcelDossier.dossierId).then(response => { let promises: ng.IPromise[] = []; response.forEach(r => { promises.push(this.documentRestService.getDocument(r.docId)); }); this.$q.all(promises).then(response => { (d).documents = _.map(response, d => { return d.document; }); (d).documents = _.orderBy((d).documents, function (e) { return e.documentRequisites.documentDate }, ['asc']); (d).loading = widgets.LoadingStatus.SUCCESS; }).catch(error => { (d).loading = widgets.LoadingStatus.ERROR; }); }).catch(error => { defer.reject(error); }); }); defer.resolve(dossiers); }).catch(error => { defer.reject(error); }); } else { defer.resolve(dossiers); } }).catch(error => { defer.reject(error); }); return defer.promise; } getDossiersByCadastrals(nums: string[]): ng.IPromise { let defer: ng.IDeferred = this.$q.defer(); let dossiers: IParcelDossierWrapper[] = []; this.dossierRestService.findDossiersByCadastralNumbers(nums).then(response => { dossiers = response; dossiers.forEach(d => { (d).documents = []; (d).loading = widgets.LoadingStatus.LOADING; this.dossierLinkRestService.getFromDossier(d.parcelDossier.dossierId).then(response => { let promises: ng.IPromise[] = []; response.forEach(r => { promises.push(this.documentRestService.getDocument(r.docId)); }); this.$q.all(promises).then(response => { (d).documents = _.map(response, d => { return d.document; }); (d).documents = _.orderBy((d).documents, function (e) { return e.documentRequisites.documentDate }, ['asc']); (d).loading = widgets.LoadingStatus.SUCCESS; }).catch(error => { (d).loading = widgets.LoadingStatus.ERROR; }); }).catch(error => { defer.reject(error); }); }); defer.resolve(dossiers); }).catch(error => { defer.reject(error); }); return defer.promise; } $onInit() { this.myBlockUI = this.blockUI.instances.get("dossierEdit"); this.loadingStatus = widgets.LoadingStatus.LOADING; this.nsiRestService.get("District").then(response => { this.districts = response; return this.nsiRestService.get("Prefect"); }).then(response => { this.prefects = response; return this.nsiRestService.get("OldChapters"); }).then(res => { this.chapters = res; return this.getDossiersByDocumentId(this.document.documentID); }).then(response => { this.dossiers = response; this.loadingStatus = widgets.LoadingStatus.SUCCESS; this.documentLinkRestService.getAll(this.document.documentID).then(response => { let uniqDocIds = _.map(response, d => { return (d.doc1Id === this.document.documentID) ? d.doc2Id : d.doc1Id; }) let promises: ng.IPromise[] = []; uniqDocIds.forEach(id => { promises.push(this.getDossiersByDocumentId(id)); }); if (this.document.territory && this.document.territory.cadastralNumber && this.document.territory.cadastralNumber.length > 0) { promises.push(this.getDossiersByCadastrals(this.document.territory.cadastralNumber)); } this.$q.all(promises).then(response => { response.forEach((r: any[]) => { r.forEach(p => { this.dossierCandidates.push(p); }); }); this.dossierCandidates = (_).uniqBy(this.dossierCandidates, d => { return d.parcelDossier.dossierId }); let newDossierCandidates: any[] = []; this.dossierCandidates.forEach(dc => { let exist: boolean = false; this.dossiers.forEach(d => { if (d.parcelDossier.dossierId === dc.parcelDossier.dossierId) { exist = true; } }); if (!exist) { newDossierCandidates.push(dc); } }); this.dossierCandidates = newDossierCandidates; this.loadingStatus = widgets.LoadingStatus.SUCCESS; }).catch(error => { this.loadingStatus = widgets.LoadingStatus.ERROR; }); }).catch(error => { this.loadingStatus = widgets.LoadingStatus.ERROR; }); }).catch(error => { this.loadingStatus = widgets.LoadingStatus.ERROR; }); } $onDestroy() { this.myBlockUI.reset(); } } export const DossierEditComponent: angular.IComponentOptions = { controller: DossierEditController, template: require('./dossierEdit.html'), bindings: { document: "<" } };