/** Angular Imports */ import { Component , OnInit , ViewChild , AfterViewInit } from '@angular/core' ; import { MatPaginator } from '@angular/material/paginator' ; import { MatSort } from '@angular/material/sort' ; import { ActivatedRoute } from '@angular/router' ; import { FormControl } from '@angular/forms' ; import { DatePipe } from '@angular/common' ; /** Custom Data Source */ import { AuditTrailsDataSource } from './audit-trail.datasource' ; /** Custom Services */ import { SystemService } from '../system.service' ; import { SettingsService } from 'app/settings/settings.service' ; /** rxjs Imports */ import { merge } from 'rxjs' ; import { tap , debounceTime , distinctUntilChanged , startWith , map } from 'rxjs/operators' ; /**
 * Audit Trails Component.
 */ @ Component ( { selector : 'mifosx-audit-trails' , templateUrl : './audit-trails.component.html' , styleUrls : [ './audit-trails.component.scss' ] } ) export class AuditTrailsComponent implements OnInit , AfterViewInit { /** Minimum date allowed. */ minDate = new Date ( 2000 , 0 , 1 ) ; /** Maximum date allowed. */ maxDate = new Date ( ) ; /** Audit Trails Data */ auditTrailsData : any ; /** Filtered user data for autocomplete. */ filteredUserData : any ; /** Filtered action data for autocomplete. */ filteredActionData : any ; /** Filtered entity data for autocomplete. */ filteredEntityData : any ; /** Filtered checker data for autocomplete. */ filteredCheckerData : any ; /** Audit Trail Search Template Data. */ auditTrailSearchTemplateData : any ; /** Columns to be displayed in audit trails table. */ displayedColumns : string [ ] = [ 'id' , 'resourceId' , 'processingResult' , 'maker' , 'actionName' , 'entityName' , 'officeName' , 'madeOnDate' , 'checker' , 'checkedOnDate' ] ; /** Data source for audit trails table. */ dataSource : AuditTrailsDataSource ; /** Audit Trails filter. */ filterAuditTrailsBy = [ { type : 'actionName' , value : '' } , { type : 'entityName' , value : '' } , { type : 'resourceId' , value : '' } , { type : 'makerId' , value : '' } , { type : 'makerDateTimeFrom' , value : '' } , { type : 'makerDateTimeTo' , value : '' } , { type : 'checkerDateTimeFrom' , value : '' } , { type : 'checkerDateTimeTo' , value : '' } , { type : 'checkerId' , value : '' } , { type : 'processingResult' , value : '' } , { type : 'dateFormat' , value : this . settingsService . dateFormat } , { type : 'locale' , value : this . settingsService . language . code } ] ; /** User form control. */ user = new FormControl ( '' ) ; /** From date form control. */ fromDate = new FormControl ( ) ; /** Checked from date form control. */ checkedFromDate = new FormControl ( ) ; /** Processing result form control. */ processingResult = new FormControl ( ) ; /** Action name form control. */ actionName = new FormControl ( ) ; /** Resource ID form control. */ resourceId = new FormControl ( '' ) ; /** To date form control. */ toDate = new FormControl ( ) ; /** Checked to date form control. */ checkedToDate = new FormControl ( ) ; /** Entity name form control. */ entityName = new FormControl ( ) ; /** Checker form control. */ checker = new FormControl ( ) ; /** Paginator for audit trails table. */ @ ViewChild ( MatPaginator , { static : true } ) paginator : MatPaginator ; /** Sorter for audit trails table. */ @ ViewChild ( MatSort , { static : true } ) sort : MatSort ; param { ActivatedRoute } route param { SystemService } systemService constructor ( private route , private systemService , private datePipe , private settingsService ) { this . route . data . subscribe ( ( data ) => { this . auditTrailSearchTemplateData = data . auditTrailSearchTemplate ; } ) ; } /**
     * Sets filtered users, actions and entities for autocomplete and audit trails table.
     */ ngOnInit ( ) { this . setFilteredUsers ( ) ; this . setFilteredActions ( ) ; this . setFilteredEntities ( ) ; this . setFilteredCheckers ( ) ; this . getAuditTrails ( ) ; } /**
     * Subscribes to all search filters:
     * User Name, From Date, To Date, Checked From Date, Checked To Date, Resource ID, Action Name, Entity Name, Checker
     * sort change and page change.
     */ ngAfterViewInit ( ) { this . user . valueChanges . pipe ( map ( value => value . id ? value . id : '' ) , debounceTime ( 500 ) , distinctUntilChanged ( ) , tap ( ( filterValue ) => { this . applyFilter ( filterValue , 'makerId' ) ; } ) ) . subscribe ( ) ; this . fromDate . valueChanges . pipe ( debounceTime ( 500 ) , distinctUntilChanged ( ) , tap ( ( filterValue ) => { this . applyFilter ( this . getDate ( filterValue ) , 'makerDateTimeFrom' ) ; } ) ) . subscribe ( ) ; this . toDate . valueChanges . pipe ( debounceTime ( 500 ) , distinctUntilChanged ( ) , tap ( ( filterValue ) => { this . applyFilter ( this . getDate ( filterValue ) , 'makerDateTimeTo' ) ; } ) ) . subscribe ( ) ; this . checkedFromDate . valueChanges . pipe ( debounceTime ( 500 ) , distinctUntilChanged ( ) , tap ( ( filterValue ) => { this . applyFilter ( this . getDate ( filterValue ) , 'checkerDateTimeFrom' ) ; } ) ) . subscribe ( ) ; this . checkedToDate . valueChanges . pipe ( debounceTime ( 500 ) , distinctUntilChanged ( ) , tap ( ( filterValue ) => { this . applyFilter ( this . getDate ( filterValue ) , 'checkerDateTimeTo' ) ; } ) ) . subscribe ( ) ; this . resourceId . valueChanges . pipe ( debounceTime ( 500 ) , distinctUntilChanged ( ) , tap ( ( filterValue ) => { this . applyFilter ( filterValue , 'resourceId' ) ; } ) ) . subscribe ( ) ; this . actionName . valueChanges . pipe ( map ( value => value ? value : '' ) , debounceTime ( 500 ) , distinctUntilChanged ( ) , tap ( ( filterValue ) => { this . applyFilter ( filterValue , 'actionName' ) ; } ) ) . subscribe ( ) ; this . entityName . valueChanges . pipe ( map ( value => value ? value : '' ) , debounceTime ( 500 ) , distinctUntilChanged ( ) , tap ( ( filterValue ) => { this . applyFilter ( filterValue , 'entityName' ) ; } ) ) . subscribe ( ) ; this . checker . valueChanges . pipe ( map ( value => value ? value : '' ) , debounceTime ( 500 ) , distinctUntilChanged ( ) , tap ( ( filterValue ) => { this . applyFilter ( filterValue , 'checkerId' ) ; } ) ) . subscribe ( ) ; this . sort . sortChange . subscribe ( ( ) => this . paginator . pageIndex = 0 ) ; merge ( this . sort . sortChange , this . paginator . page ) . pipe ( tap ( ( ) => this . loadAuditTrailsPage ( ) ) ) . subscribe ( ) ; } /**
     * Initializes the data source for audit trails table and loads the first page.
     */ getAuditTrails ( ) { this . dataSource = new AuditTrailsDataSource ( this . systemService ) ; this . dataSource . getAuditTrails ( this . filterAuditTrailsBy , this . sort . active , this . sort . direction , this . paginator . pageIndex , this . paginator . pageSize ) ; } /**
     * Loads a page of audit trails.
     */ loadAuditTrailsPage ( ) { if ( ! this . sort . direction ) { delete this . sort . active ; } this . dataSource . getAuditTrails ( this . filterAuditTrailsBy , this . sort . active , this . sort . direction , this . paginator . pageIndex , this . paginator . pageSize ) ; } param { string } filterValue param { string } property applyFilter ( filterValue , property ) { this . paginator . pageIndex = 0 ; const findIndex = this . filterAuditTrailsBy . findIndex ( filter => filter . type === property ) ; this . filterAuditTrailsBy [ findIndex ] . value = filterValue ; this . loadAuditTrailsPage ( ) ; } param { any } user returns { string } displayUserName ( user ? ) { return user ? user . name : undefined ; } param { any } action returns { string } displayActionName ( action ? ) { return action ? action : undefined ; } param { any } entity returns { string } displayEntityName ( entity ? ) { return entity ? entity : undefined ; } /**
     * Sets filtered users for autocomplete.
     */ setFilteredUsers ( ) { this . filteredUserData = this . user . valueChanges . pipe ( startWith ( '' ) , map ( ( user ) => typeof user === 'string' ? user : user . name ) , map ( ( userName ) => userName ? this . filterUserAutocompleteData ( userName ) : this . auditTrailSearchTemplateData . appUsers ) ) ; } /**
     * Sets filtered checkers for autocomplete.
     */ setFilteredCheckers ( ) { this . filteredCheckerData = this . checker . valueChanges . pipe ( startWith ( '' ) , map ( ( user ) => typeof user === 'string' ? user : user . name ) , map ( ( userName ) => userName ? this . filterUserAutocompleteData ( userName ) : this . auditTrailSearchTemplateData . appUsers ) ) ; } /**
     * Sets filtered actions for autocomplete.
     */ setFilteredActions ( ) { this . filteredActionData = this . actionName . valueChanges . pipe ( startWith ( '' ) , map ( ( action ) => typeof action === 'string' ? action : '' ) , map ( ( actionName ) => actionName ? this . filterActionAutocompleteData ( actionName ) : this . auditTrailSearchTemplateData . actionNames ) ) ; } /**
     * Sets filtered entities for autocomplete.
     */ setFilteredEntities ( ) { this . filteredEntityData = this . entityName . valueChanges . pipe ( startWith ( '' ) , map ( ( entity ) => typeof entity === 'string' ? entity : '' ) , map ( ( entityName ) => entityName ? this . filterEntityAutocompleteData ( entityName ) : this . auditTrailSearchTemplateData . entityNames ) ) ; } param { string } userName returns { any } private filterUserAutocompleteData ( userName ) { return this . auditTrailSearchTemplateData . appUsers . filter ( ( user ) => user . username . toLowerCase ( ) . includes ( userName . toLowerCase ( ) ) ) ; } param { string } actionName returns { any } private filterActionAutocompleteData ( actionName ) { return this . auditTrailSearchTemplateData . actionNames . filter ( ( action ) => action . toLowerCase ( ) . includes ( actionName . toLowerCase ( ) ) ) ; } param { string } entityName returns { any } private filterEntityAutocompleteData ( entityName ) { return this . auditTrailSearchTemplateData . entityNames . filter ( ( entity ) => entity . toLowerCase ( ) . includes ( entityName . toLowerCase ( ) ) ) ; } /**
     * Generates the CSV file of Audit Trails Data.
     */ downloadCSV ( ) { const dateFormat = this . settingsService . dateFormat ; const replacer = ( key , value ) => value === undefined ? '' : value ; const header = [ 'ID' , 'Resource ID' , 'Status' , 'Office' , 'Made On' , 'Maker' , 'Checked On' , 'Checker' , 'Entity' , 'Action' , 'Client' ] ; const headerCode = [ 'id' , 'resourceId' , 'processingResult' , 'officeName' , 'madeOnDate' , 'maker' , 'checkedOnDate' , 'checker' , 'entityName' , 'actionName' , 'clientName' ] ; this . systemService . getAuditTrails ( this . filterAuditTrailsBy , this . sort . active ? this . sort . active : '' , this . sort . direction , 0 , - 1 ) . subscribe ( ( response ) => { if ( response !== undefined ) { let csv = response . pageItems . map ( ( row ) => headerCode . map ( fieldName => ( fieldName === 'madeOnDate' || fieldName === 'checkedOnDate' ) && ( JSON . stringify ( row [ fieldName ] , replacer ) !== '""' ) ? this . datePipe . transform ( JSON . stringify ( row [ fieldName ] , replacer ) , dateFormat ) : JSON . stringify ( row [ fieldName ] , replacer ) ) ) ; csv . unshift ( `data:text/csv;charset=utf-8,${ header . join ( ) }` ) ; csv = csv . join ( '\r\n' ) ; const link = document . createElement ( 'a' ) ; link . setAttribute ( 'href' , encodeURI ( csv ) ) ; link . setAttribute ( 'download' , 'Audit Trails.csv' ) ; document . body . appendChild ( link ) ; link . click ( ) ; document . body . removeChild ( link ) ; } } ) ; } param { any } timestamp private getDate ( timestamp ) { const dateFormat = this . settingsService . dateFormat ; return this . datePipe . transform ( timestamp , dateFormat ) ; } }