/** Angular Imports */ import { Component , OnInit , ViewChild } from '@angular/core' ; import { ActivatedRoute } from '@angular/router' ; import { MatPaginator } from '@angular/material/paginator' ; import { MatSort } from '@angular/material/sort' ; import { MatTableDataSource , MatTable } from '@angular/material/table' ; import { FormGroup , FormBuilder } from '@angular/forms' ; /** Custom Imports */ import { OrganizationService } from '../../organization.service' ; import { BulkImports } from './bulk-imports' ; /**
 * View Bulk Imports Component
 */ @ Component ( { selector : 'mifosx-view-bulk-import' , templateUrl : './view-bulk-import.component.html' , styleUrls : [ './view-bulk-import.component.scss' ] } ) export class ViewBulkImportComponent implements OnInit { /** offices Data */ officeData : any ; /** staff Data */ staffData : any ; /** Entity Template */ template : File ; /** imports Data */ importsData : any ; /** bulk-import form. */ bulkImportForm : FormGroup ; /** array of deined bulk-imports */ bulkImportsArray = BulkImports ; /** bulk-import which user navigated to */ bulkImport : any = { } ; /** Data source for imports table. */ dataSource = new MatTableDataSource ( ) ; /** Columns to be displayed in imports table. */ displayedColumns : string [ ] = [ 'name' , 'importTime' , 'endTime' , 'completed' , 'totalRecords' , 'successCount' , 'failureCount' , 'download' ] ; /** Paginator for imports table. */ @ ViewChild ( MatPaginator , { static : true } ) paginator : MatPaginator ; /** Sorter for imports table. */ @ ViewChild ( MatSort , { static : true } ) sort : MatSort ; /** Imports table reference */ @ ViewChild ( 'importsTable' , { static : true } ) importsTableRef : MatTable < Element > ; param { ActivatedRoute } route param { FormBuilder } formBuilder param { OrganizationService } organizationService constructor ( private route , private formBuilder , private organizationService ) { this . bulkImport . name = this . route . snapshot . params [ 'import-name' ] ; this . route . data . subscribe ( ( data ) => { this . officeData = data . offices ; this . importsData = data . imports ; } ) ; } /**
     * Gets bulk import's properties.
     */ ngOnInit ( ) { this . bulkImport = this . bulkImportsArray . find ( ( entry ) => entry . name === this . bulkImport . name ) ; this . createBulkImportForm ( ) ; this . buildDependencies ( ) ; this . setImports ( ) ; } /**
     * Creates the bulk import form.
     */ createBulkImportForm ( ) { this . bulkImportForm = this . formBuilder . group ( { 'officeId' : [ '' ] , 'staffId' : [ '' ] , 'legalForm' : [ '' ] , } ) ; } /**
     * Subscribe to value changes and fetches select options accordingly.
     */ buildDependencies ( ) { this . bulkImportForm . get ( 'officeId' ) . valueChanges . subscribe ( ( value ) => { if ( this . bulkImport . formFields >= 2 ) { this . organizationService . getStaff ( value ) . subscribe ( ( data ) => { this . staffData = data ; } ) ; } } ) ; } /**
     * Initializes the data source, paginator and sorter for imports table.
     */ setImports ( ) { this . dataSource = new MatTableDataSource ( this . importsData ) ; this . dataSource . paginator = this . paginator ; this . dataSource . sort = this . sort ; } /**
     * Gets bulk import's downloadable template from API.
     */ downloadTemplate ( ) { const officeId = this . bulkImportForm . get ( 'officeId' ) . value ; const staffId = this . bulkImportForm . get ( 'staffId' ) . value ; let legalFormType = '' ; /** Only for Client Bulk Imports */ switch ( this . bulkImportForm . get ( 'legalForm' ) . value ) { case 'Person' : legalFormType = 'CLIENTS_PERSON' ; break ; case 'Entity' : legalFormType = 'CLIENTS_ENTTTY' ; break ; } this . organizationService . getImportTemplate ( this . bulkImport . urlSuffix , officeId , staffId , legalFormType ) . subscribe ( ( res ) => { const contentType = res . headers . get ( 'Content-Type' ) ; const blob = new Blob ( [ res . body ] , { type : contentType } ) ; const fileOfBlob = new File ( [ blob ] , 'template.xls' , { type : contentType } ) ; window . open ( window . URL . createObjectURL ( fileOfBlob ) ) ; } ) ; } param { any } $event onFileSelect ( $event ) { if ( $event . target . files . length > 0 ) { this . template = $event . target . files [ 0 ] ; } } /**
     * Upload excel file containing bulk import data.
     */ uploadTemplate ( ) { let legalFormType = '' ; /** Only for Client Bulk Imports */ if ( this . bulkImport . name === 'Clients' ) { if ( this . template . name . toLowerCase ( ) . includes ( 'entity' ) ) { legalFormType = 'CLIENTS_ENTTTY' ; } else if ( this . template . name . toLowerCase ( ) . includes ( 'person' ) ) { legalFormType = 'CLIENTS_PERSON' ; } } this . organizationService . uploadImportDocument ( this . template , this . bulkImport . urlSuffix , legalFormType ) . subscribe ( ( ) => { } ) ; } /**
     * Reloads imports data table.
     */ refreshDocuments ( ) { this . organizationService . getImports ( this . bulkImport . entityType ) . subscribe ( ( data ) => { this . dataSource = new MatTableDataSource ( data ) ; this . importsTableRef . renderRows ( ) ; } ) ; } param { string } name param { any } id downloadDocument ( name , id ) { this . organizationService . getImportDocument ( id ) . subscribe ( ( res ) => { const contentType = res . headers . get ( 'Content-Type' ) ; const blob = new Blob ( [ res . body ] , { type : contentType } ) ; const fileOfBlob = new File ( [ blob ] , name , { type : contentType } ) ; window . open ( window . URL . createObjectURL ( fileOfBlob ) ) ; } ) ; } }