import { Component , OnInit } from "@angular/core" ; import { ActivatedRoute } from "@angular/router" ; import { ReportingService } from "../reporting.service" ; import { getGroupingInformationString , GroupByDescription , ReportRow , } from "../report-row" ; import { ReportConfig , ReportingComponentConfig , } from "./reporting-component-config" ; import moment from "moment" ; import { RouteData } from "../../../core/view/dynamic-routing/view-config.interface" ; @ Component ( { selector : "app-reporting" , templateUrl : "./reporting.component.html" , styleUrls : [ "./reporting.component.scss" ] , } ) export class ReportingComponent implements OnInit { availableReports : ReportConfig [ ] ; selectedReport : ReportConfig ; results : ReportRow [ ] ; fromDate : Date ; toDate : Date ; exportableTable : { label : string ; result : any ; } [ ] ; loading : boolean ; constructor ( private activatedRoute , private reportingService ) { } ngOnInit ( ) { this . activatedRoute . data . subscribe ( ( data ) => { this . availableReports = data . config ?. reports ; if ( this . availableReports ?. length === 1 ) { this . selectedReport = this . availableReports [ 0 ] ; } } ) ; } async calculateResults ( ) { this . loading = true ; const dayAfterToDate = moment ( this . toDate ) . add ( 1 , "day" ) . toDate ( ) ; this . results = await this . reportingService . calculateReport ( this . selectedReport . aggregationDefinitions , this . fromDate , dayAfterToDate ) ; this . exportableTable = this . flattenReportRows ( ) ; this . loading = false ; } private flattenReportRows ( rows = this . results ) { const tableRows = [ ] ; rows . forEach ( ( result ) => { tableRows . push ( this . createExportableRow ( result . header ) ) ; tableRows . push ( ... this . flattenReportRows ( result . subRows ) ) ; } ) ; return tableRows ; } private createExportableRow ( header ) { let resultLabel = header . label ; const groupByString = getGroupingInformationString ( header . groupedBy ) ; if ( groupByString ) { resultLabel += " " + groupByString ; } return { label : resultLabel , result : header . result } ; } }