/** Angular Imports */ import { Component , ViewChild } from '@angular/core' ; import { ActivatedRoute } from '@angular/router' ; import { MatTable } from '@angular/material/table' ; import { MatDialog } from '@angular/material/dialog' ; /** Custom Models */ import { FormfieldBase } from 'app/shared/form-dialog/formfield/model/formfield-base' ; import { InputBase } from 'app/shared/form-dialog/formfield/model/input-base' ; import { SelectBase } from 'app/shared/form-dialog/formfield/model/select-base' ; /** Custom Components */ import { UploadDocumentDialogComponent } from '../custom-dialogs/upload-document-dialog/upload-document-dialog.component' ; import { DeleteDialogComponent } from '../../../shared/delete-dialog/delete-dialog.component' ; import { FormDialogComponent } from 'app/shared/form-dialog/form-dialog.component' ; /** Custom Services */ import { ClientsService } from '../../clients.service' ; /**
 * Identities Tab Component
 */ @ Component ( { selector : 'mifosx-identities-tab' , templateUrl : './identities-tab.component.html' , styleUrls : [ './identities-tab.component.scss' ] } ) export class IdentitiesTabComponent { /** Client Identities */ clientIdentities : any ; /** Client Identifier Template */ clientIdentifierTemplate : any ; /** Client Id */ clientId : string ; /** Identities Columns */ identitiesColumns : string [ ] = [ 'id' , 'description' , 'type' , 'documents' , 'status' , 'actions' ] ; /** Identifiers Table */ @ ViewChild ( 'identifiersTable' , { static : true } ) identifiersTable : MatTable < Element > ; param { ActivatedRoute } route param { MatDialog } dialog param { ClientsService } clientService constructor ( private route , public dialog , private clientService ) { this . clientId = this . route . parent . snapshot . paramMap . get ( 'clientId' ) ; this . route . data . subscribe ( ( data ) => { this . clientIdentities = data . clientIdentities ; this . clientIdentifierTemplate = data . clientIdentifierTemplate ; } ) ; } param { string } parentEntityId param { string } documentId download ( parentEntityId , documentId ) { this . clientService . downloadClientIdentificationDocument ( parentEntityId , documentId ) . subscribe ( res => { const url = window . URL . createObjectURL ( res ) ; window . open ( url ) ; } ) ; } /**
     * Add Client Identifier
     */ addIdentifier ( ) { const formfields = [ new SelectBase ( { controlName : 'documentTypeId' , label : 'Document Type' , value : '' , options : { label : 'name' , value : 'id' , data : this . clientIdentifierTemplate . allowedDocumentTypes } , required : true , order : 1 } ) , new SelectBase ( { controlName : 'status' , label : 'Status' , value : '2' , options : { label : 'value' , value : 'value' , data : [ { value : 'Active' } , { value : 'Inactive' } ] } , required : true , order : 2 } ) , new InputBase ( { controlName : 'documentKey' , label : 'Unique Id' , value : '' , type : 'text' , required : true , order : 3 } ) , new InputBase ( { controlName : 'description' , label : 'Description' , value : '' , type : 'text' , order : 4 } ) ] ; const data = { title : 'Add Client Identifier' , formfields : formfields } ; const addIdentifierDialogRef = this . dialog . open ( FormDialogComponent , { data } ) ; addIdentifierDialogRef . afterClosed ( ) . subscribe ( ( response ) => { if ( response . data ) { this . clientService . addClientIdentifier ( this . clientId , response . data . value ) . subscribe ( ( res ) => { this . clientIdentities . push ( { id : res . resourceId , description : response . data . value . description , documentType : this . clientIdentifierTemplate . allowedDocumentTypes . filter ( ( doc ) => ( doc . id === response . data . value . documentTypeId ) ) [ 0 ] , documentKey : response . data . value . documentKey , documents : [ ] , clientId : this . clientId , status : ( response . data . value . status === 'Active' ? 'clientIdentifierStatusType.active' : 'clientIdentifierStatusType.inactive' ) } ) ; this . identifiersTable . renderRows ( ) ; } ) ; } } ) ; } param { string } clientId param { string } identifierId param { number } index deleteIdentifier ( clientId , identifierId , index ) { const deleteIdentifierDialogRef = this . dialog . open ( DeleteDialogComponent , { data : { deleteContext : `identifier id:${ identifierId }` } } ) ; deleteIdentifierDialogRef . afterClosed ( ) . subscribe ( ( response ) => { if ( response . delete ) { this . clientService . deleteClientIdentifier ( clientId , identifierId ) . subscribe ( res => { this . clientIdentities . splice ( index , 1 ) ; this . identifiersTable . renderRows ( ) ; } ) ; } } ) ; } param { number } index param { string } identifierId uploadDocument ( index , identifierId ) { const uploadDocumentDialogRef = this . dialog . open ( UploadDocumentDialogComponent , { data : { documentIdentifier : true } } ) ; uploadDocumentDialogRef . afterClosed ( ) . subscribe ( ( dialogResponse ) => { if ( dialogResponse ) { const formData = new FormData ; formData . append ( 'name' , dialogResponse . fileName ) ; formData . append ( 'file' , dialogResponse . file ) ; this . clientService . uploadClientIdentifierDocument ( identifierId , formData ) . subscribe ( ( res ) => { this . clientIdentities [ index ] . documents . push ( { id : res . resourceId , parentEntityType : 'client_identifiers' , parentEntityId : identifierId , name : dialogResponse . fileName , fileName : dialogResponse . file . name } ) ; this . identifiersTable . renderRows ( ) ; } ) ; } } ) ; } }