From f0ffb333557a316c61fca716a6af7543a50f8055 Mon Sep 17 00:00:00 2001 From: Felix Maiko Date: Fri, 23 Sep 2022 16:56:11 +0300 Subject: [PATCH] Fix issue of incorrect appointments count in appointments overview (#121) --- .../appointments-overview.component.ts | 90 ++++++++++++------- 1 file changed, 59 insertions(+), 31 deletions(-) diff --git a/projects/ngx-formentry/src/components/appointments-overview/appointments-overview.component.ts b/projects/ngx-formentry/src/components/appointments-overview/appointments-overview.component.ts index 51b31397..bf36bf4c 100644 --- a/projects/ngx-formentry/src/components/appointments-overview/appointments-overview.component.ts +++ b/projects/ngx-formentry/src/components/appointments-overview/appointments-overview.component.ts @@ -5,6 +5,30 @@ import * as moment_ from 'moment'; const moment = moment_; +interface DailyScheduled { + date: string; + count: { + scheduled: number; + }; +} +interface WeeeklyScheduleReport { + reports: { + attended: any; + hasNotReturned: any; + scheduled: any; + }; + results: DailyScheduled[]; + totals: { + attended: number; + hasNotReturned: number; + scheduled: number; + }; +} +interface DayCount { + date: string; + count: number; +} + @Component({ selector: 'appointments-overview', templateUrl: './appointments-overview.component.html', @@ -55,49 +79,53 @@ export class AppointmentsOverviewComponent implements OnInit, OnChanges { .subtract(1, 'day') .format('YYYY-MM-DD'); this.today = moment(v).format('DD-MM-YYYY'); - // create 5 week days - const _data = []; - const programTypes = [ - '781d85b0-1359-11df-a1f1-0026b9348838', - '781d897a-1359-11df-a1f1-0026b9348838', - '96047aaf-7ab3-45e9-be6a-b61810fe617d', - 'c19aec66-1a40-4588-9b03-b6be55a8dd1d', - 'f7793d42-11ac-4cfd-9b35-e0a21a7a7c31', - '334c9e98-173f-4454-a8ce-f80b20b7fdf0', - '96ba279b-b23b-4e78-aba9-dcbd46a96b7b', - '781d8880-1359-11df-a1f1-0026b9348838' - ]; - const programTypeParams = programTypes.join(); - for (let i = 1; i <= 5; i++) { - _data.push({ - date: moment(v) - .startOf('week') - .add(i, 'day') - .format('DD-MM-YYYY'), - count: 0 - }); - } dataSource .getMonthlySchedule({ startDate: startDate, endDate: endDate, limit: 5, locationUuids: locationUuid, - programType: programTypeParams + department: 'HIV', + groupBy: 'groupByPerson,groupByAttendedDate,groupByRtcDate' }) .subscribe( - (data) => { + (data: WeeeklyScheduleReport) => { + const _data: DayCount[] = []; + const weeklyMap = new Map(); + // create the weeks schedule with zero appointments + for (let i = 0; i < 5; i++) { + const scheduleDate = moment(v) + .startOf('week') + .add(i, 'day') + .format('YYYY-MM-DD'); + const scheduledObj: DailyScheduled = { + date: scheduleDate, + count: { + scheduled: 0 + } + }; + weeklyMap.set(scheduleDate, scheduledObj); + } + + const results: DailyScheduled[] = data.results || []; + // replace placeholder schedules with actual schedules in the map obj + results.forEach((scheduled: DailyScheduled) => { + weeklyMap.set(scheduled.date, scheduled); + }); + // retrieve scheduled obj from map to data array + weeklyMap.forEach((value: DailyScheduled, key: string) => { + const dayCount: DayCount = { + date: key, + count: value.count.scheduled || 0 + }; + _data.push(dayCount); + }); this.appointmentsLoaded = true; this.loadingAppointments = false; - _data.map((appointment, index) => { - appointment.count = - data.results[index] !== undefined - ? data.results[index].count.scheduled - : 0; - }); + this.appointments = _data; }, - (error) => { + (error: any) => { this.loadingAppointments = false; this.errorLoadingAppointments = true; this.showAppointments = false;