@@ -4,10 +4,11 @@ import { combineLatest, Subscription } from 'rxjs';
4
4
5
5
import { Selectors } from '@mm-selectors/index' ;
6
6
import { TargetAggregatesActions } from '@mm-actions/target-aggregates' ;
7
- import { TargetAggregatesService } from '@mm-services/target-aggregates.service' ;
7
+ import { AggregateTarget , TargetAggregatesService } from '@mm-services/target-aggregates.service' ;
8
8
import { PerformanceService } from '@mm-services/performance.service' ;
9
9
import { GlobalActions } from '@mm-actions/global' ;
10
10
import { UserSettingsService } from '@mm-services/user-settings.service' ;
11
+ import { ReportingPeriod } from '@mm-modules/analytics/analytics-target-aggregates-sidebar-filter.component' ;
11
12
12
13
@Component ( {
13
14
selector : 'analytics-target-aggregates' ,
@@ -19,13 +20,15 @@ export class AnalyticsTargetAggregatesComponent implements OnInit, OnDestroy {
19
20
private globalActions : GlobalActions ;
20
21
private trackPerformance ;
21
22
subscriptions : Subscription = new Subscription ( ) ;
22
- userFacilities ;
23
23
loading = true ;
24
24
enabled = false ;
25
25
aggregates : any = null ;
26
26
selected = null ;
27
27
error = null ;
28
+ userFacilities ;
28
29
sidebarFilter ;
30
+ reportingPeriodFilter ;
31
+ facilityFilter ;
29
32
30
33
constructor (
31
34
private store : Store ,
@@ -78,12 +81,22 @@ export class AnalyticsTargetAggregatesComponent implements OnInit, OnDestroy {
78
81
this . subscriptions . add ( selectorsSubscription ) ;
79
82
}
80
83
81
- async getTargetAggregates ( userFacility ) {
84
+ async getTargetAggregates ( userFacility , reportingPeriod ) {
82
85
try {
83
- const aggregates = this . enabled ? await this . targetAggregatesService . getAggregates ( userFacility ?. _id ) : [ ] ;
84
- if ( this . userFacilities . length > 1 ) {
85
- aggregates . forEach ( ( aggregate ) => aggregate . facility = userFacility ?. name ) ;
86
+ let aggregates : AggregateTarget [ ] = [ ] ;
87
+ if ( this . enabled ) {
88
+ this . facilityFilter = userFacility ;
89
+ this . reportingPeriodFilter = reportingPeriod ;
90
+ aggregates = await this . targetAggregatesService . getAggregates (
91
+ this . facilityFilter ?. _id , this . reportingPeriodFilter
92
+ ) ;
86
93
}
94
+
95
+ const reportingMonth = await this . targetAggregatesService . getReportingMonth ( reportingPeriod ) ;
96
+
97
+ aggregates = aggregates
98
+ . map ( aggregate => this . formatAggregate ( aggregate , userFacility , reportingPeriod , reportingMonth ) ) ;
99
+
87
100
this . targetAggregatesActions . setTargetAggregates ( aggregates ) ;
88
101
this . targetAggregatesActions . setTargetAggregatesLoaded ( true ) ;
89
102
@@ -93,18 +106,38 @@ export class AnalyticsTargetAggregatesComponent implements OnInit, OnDestroy {
93
106
} finally {
94
107
this . loading = false ;
95
108
this . trackPerformance ?. stop ( {
96
- name : [ 'analytics' , 'target_aggregates' , 'load' ] . join ( ':' ) ,
109
+ name : [ 'analytics' , 'target_aggregates' , 'load' ] . join ( ':' ) ,
97
110
recordApdex : true ,
98
111
} ) ;
99
112
}
100
113
}
101
114
115
+ private formatAggregate ( aggregate , userFacility , reportingPeriod , reportingMonth ) {
116
+ const filtersToDisplay : string [ ] = [ ] ;
117
+
118
+ if ( this . userFacilities . length > 1 && this . facilityFilter ?. name ) {
119
+ aggregate . facility = userFacility ?. name ;
120
+ filtersToDisplay . push ( this . facilityFilter . name ) ;
121
+ }
122
+
123
+ aggregate . reportingMonth = reportingMonth ;
124
+ aggregate . reportingPeriod = reportingPeriod ;
125
+ if ( this . targetAggregatesService . isPreviousPeriod ( aggregate . reportingPeriod ) ) {
126
+ filtersToDisplay . push ( aggregate . reportingMonth ) ;
127
+ }
128
+ aggregate . filtersToDisplay = filtersToDisplay ;
129
+
130
+ return aggregate ;
131
+ }
132
+
102
133
private async setDefaultFilters ( ) {
103
134
this . userFacilities = await this . userSettingsService . getUserFacilities ( ) ;
135
+ this . userFacilities . sort ( ( a , b ) => a . name . localeCompare ( b . name ) ) ;
104
136
const defaultFilters = {
105
137
facility : this . userFacilities . length ? { ...this . userFacilities [ 0 ] } : null ,
138
+ reportingPeriod : ReportingPeriod . CURRENT ,
106
139
} ;
107
140
this . globalActions . setSidebarFilter ( { defaultFilters } ) ;
108
- await this . getTargetAggregates ( defaultFilters . facility ) ;
141
+ await this . getTargetAggregates ( defaultFilters . facility , defaultFilters . reportingPeriod ) ;
109
142
}
110
143
}
0 commit comments