Skip to content

Commit

Permalink
Added Diabetes Report Section & Total registered patients (#5487)
Browse files Browse the repository at this point in the history
**Story card:**
[sc-14020](https://app.shortcut.com/simpledotorg/story/14020/add-diabetes-report-section-total-registered-patients)
## Because

To add 2 more sections i.e. Diabetes report section and total registered
patient

## This addresses

In this PR we will add 2 sections Diabetes report section (1st section
from figma) and total registered patient(3rd patient from figma).

<img width="1512" alt="Screenshot 2024-11-13 at 10 59 24 PM"
src="https://github.com/user-attachments/assets/83432c79-b0b8-49ec-98b8-4cdaa9fd0ec0">

## Test instructions

Enable this flipper flag :diabetes_progress_report_tab to check the
functionality.
  • Loading branch information
Gyan-Gupta-Rtsl authored Nov 20, 2024
1 parent 8ddb9dc commit 58ca22e
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,27 @@
<%= inline_file("chevron-left.svg") %>
back
</a>
<div class="pr-16px pl-16px">
<h1 class="m-0px mb-8px fw-bold fs-24px">
<%= t("progress_tab.diabetes_report_title") %> <%= t("progress_tab.report") %>
</h1>
<p class="m-0px p-0px ta-left fw-normal fs-16px c-grey-dark">
<%= t("progress_tab.last_updated_at",
date: display_date(@last_updated_at),
time: display_time(@last_updated_at))
%>
</p>
</div>
</div>
<%= render(Reports::ProgressAssignedPatientsComponent.new(
assigned_patients: diabetes_reports_data[:assigned_patients],
region: diabetes_reports_data[:region],
diagnosis: "diabetes"
)) %>
<%= render(Reports::ProgressTotalRegistrationsComponent.new(
total_registrations: diabetes_reports_data[:total_registrations],
period_info: diabetes_reports_data[:period_info],
region: diabetes_reports_data[:region],
diagnosis: "diabetes"
)) %>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ class ProgressTab::Diabetes::DiagnosisReportComponent < ApplicationComponent

attr_reader :diabetes_reports_data

def initialize(diabetes_reports_data:)
def initialize(diabetes_reports_data:, last_updated_at:)
@diabetes_reports_data = diabetes_reports_data
@last_updated_at = last_updated_at
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</h2>
</div>
<p class="m-0px mb-24px p-0px ta-left fw-normal fs-16px lh-150 c-grey-dark">
<%= t("progress_tab.diagnosis_report.total_registered_patients.subtitle", facility_name: @region.name, diagnosis: "hypertension") %>
<%= t("progress_tab.diagnosis_report.total_registered_patients.subtitle", facility_name: @region.name, diagnosis: @diagnosis) %>
</p>
<%= render partial: "api/v3/analytics/user_analytics/data_bar_graph",
locals: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ class Reports::ProgressTotalRegistrationsComponent < ViewComponent::Base

attr_reader :total_registrations, :period_info, :region

def initialize(total_registrations:, period_info:, region:)
def initialize(total_registrations:, period_info:, region:, diagnosis: nil)
@total_registrations = total_registrations
@period_info = period_info
@region = region
@diagnosis = diagnosis || "hypertension"
end
end
2 changes: 2 additions & 0 deletions app/services/reports/facility_progress_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ def hypertension_reports_data

def diabetes_reports_data
{
total_registrations: repository.cumulative_diabetes_registrations[@region.slug],
assigned_patients: repository.cumulative_assigned_diabetic_patients[@region.slug][@period],
period_info: repository.period_info(@region),
region: @region
}
end
Expand Down
3 changes: 2 additions & 1 deletion app/views/api/v3/analytics/user_analytics/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@
%>

<%= render(ProgressTab::Diabetes::DiagnosisReportComponent.new(
diabetes_reports_data: @service.diabetes_reports_data
diabetes_reports_data: @service.diabetes_reports_data,
last_updated_at: @user_analytics.last_updated_at
))
%>
<!-- TODO: The partial "api/v3/analytics/user_analytics/diagnosis_report" need to be removed once the Diabetes reports are released -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,85 @@
require "rails_helper"

RSpec.describe ProgressTab::Diabetes::DiagnosisReportComponent, type: :component do
let(:region) { double("Region", slug: "region_slug", name: "Region 1") }
let(:repository) { double("Repository") }
let(:last_updated_at) { Time.current }

let(:data) do
{
"2024-06-01" => 42,
"2024-07-01" => 44,
"2024-08-01" => 49
}
end

let(:total_registrations) do
data.map { |date_str, value| [Period.new(type: :month, value: date_str), value] }.to_h
end

let(:period_info) do
data.keys.map do |date_str|
period = Period.new(type: :month, value: date_str)
date = Date.parse(date_str)
[
period,
{
name: date.strftime("%b-%Y"),
ltfu_since_date: (date - 1.year).end_of_month.strftime("%d-%b-%Y"),
ltfu_end_date: date.end_of_month.strftime("%d-%b-%Y")
}
]
end.to_h
end

let(:diabetes_reports_data) do
{
total_registrations: total_registrations,
period_info: period_info,
region: region,
assigned_patients: 100,
region: double("Region", name: "Region 1"),
diagnosis: "diabetes"
}
end
subject { render_inline(described_class.new(diabetes_reports_data: diabetes_reports_data)) }

before do
allow(repository).to receive(:cumulative_diabetes_registrations).and_return(total_registrations)
allow(repository).to receive(:cumulative_assigned_diabetic_patients).and_return(diabetes_reports_data[:assigned_patients])
allow(repository).to receive(:period_info).and_return(period_info)
allow(region).to receive(:slug).and_return("region_slug")
end

subject do
render_inline(described_class.new(
diabetes_reports_data: diabetes_reports_data,
last_updated_at: last_updated_at
))
end

it "renders the diabetes report section" do
expect(subject).to have_css("div#diabetes-report")
end

it "renders the back link with correct text and onclick behavior" do
expect(subject).to have_css('a[onclick="goToPage(id=\'diabetes-report\', \'home-page\'); return false;"]', text: "back")
expect(subject).to have_css(
'a[onclick="goToPage(id=\'diabetes-report\', \'home-page\'); return false;"]',
text: "back"
)
end

it "renders the Reports::ProgressAssignedPatientsComponent with correct data" do
expect(subject.text).to include("Region 1")
expect(subject).to have_text(region.name)
expect(subject.text).to include(diabetes_reports_data[:assigned_patients].to_s)
expect(subject.text).to include("diabetes")
expect(subject).to have_text("diabetes")
end

it "displays the last updated date and time" do
formatted_date_time = last_updated_at.strftime("%d-%b-%Y at %I:%M %p")
expect(subject).to have_text("Data last updated on #{formatted_date_time}")
end

it "renders the Reports::ProgressTotalRegistrationsComponent" do
expect(subject).to have_text(region.name)
expect(subject).to have_text("49")
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
require "rails_helper"

RSpec.describe Reports::ProgressTotalRegistrationsComponent, type: :component do
let(:region) { double("Region", name: "Region 1") }
let(:total_registrations_data) do
{
"2024-06-01" => 42,
"2024-07-01" => 44,
"2024-08-01" => 49,
"2024-09-01" => 56,
"2024-10-01" => 61,
"2024-11-01" => 62
}
end

let(:total_registrations) do
total_registrations_data.map { |date, value| [Period.new(type: :month, value: date), value] }.to_h
end

let(:period_info) do
total_registrations_data.keys.map do |date|
period = Period.new(type: :month, value: date)
ltfu_since_date = Date.parse(date).prev_year.end_of_month.strftime("%d-%b-%Y")
[period, {name: Date.parse(date).strftime("%b-%Y"), ltfu_since_date: ltfu_since_date}]
end.to_h
end
let(:diagnosis) { "diabetes" }

subject do
render_inline(described_class.new(
total_registrations: total_registrations,
period_info: period_info,
region: region,
diagnosis: diagnosis
))
end

it "renders the component wrapper div" do
expect(subject).to have_css("div.mb-8px.p-16px.bgc-white.bs-card")
end

it "renders the title with correct translation" do
expect(subject).to have_text(I18n.t("progress_tab.diagnosis_report.total_registered_patients.title"))
end

it "renders the subtitle with the region name and diagnosis" do
expect(subject).to have_text(I18n.t("progress_tab.diagnosis_report.total_registered_patients.subtitle", facility_name: region.name, diagnosis: diagnosis))
end

it "renders the user analytics data bar graph partial" do
expect(subject).to have_selector('div[data-graph-type="bar-chart"]')
end

it "passes the correct data to the data bar graph partial" do
expect(subject).to have_selector('div[data-graph-type="bar-chart"]')

expectations = [
"42", "44", "49", "56", "61", "62",
"Jun-2024", "Jul-2024", "Aug-2024", "Sep-2024", "Oct-2024", "Nov-2024"
]
expectations.each { |expectation| expect(subject).to have_text(expectation) }
end

it "displays the correct number of total registrations" do
total_registrations_data.values.each do |value|
expect(subject).to have_text(value.to_s)
end
end
end
33 changes: 33 additions & 0 deletions spec/services/reports/facility_progress_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
let(:two_days_ago) { 2.days.ago }
let(:one_day_ago) { 1.day.ago }
let(:two_minutes_ago) { 2.minutes.ago }
let(:region) { double("Region", name: "Region 1") }

context "daily registrations" do
it "returns counts for HTN or DM patients if diabetes is enabled" do
Expand Down Expand Up @@ -110,5 +111,37 @@
expect(service.daily_follow_ups(Date.current)).to eq(3)
end
end

context "diabetes reports" do
it "returns the correct diabetes report data" do
facility = create(:facility, enable_diabetes_management: true)
dm_patients = create_list(:patient, 2, :diabetes, registration_facility: facility, registration_user: user, recorded_at: 2.months.ago)
create(:patient, :without_hypertension, registration_facility: facility, registration_user: user, recorded_at: 2.months.ago)

refresh_views
service = described_class.new(facility, Period.current)
result = service.diabetes_reports_data

months = ["2024-09-01", "2024-10-01", "2024-11-01"]
expected_period_info = months.map do |month|
date = Date.parse(month)
[
Period.new(type: :month, value: month),
{
bp_control_end_date: date.end_of_month.strftime("%d-%b-%Y"),
bp_control_registration_date: (date - 3.months).end_of_month.strftime("%d-%b-%Y"),
bp_control_start_date: (date - 2.months).beginning_of_month.strftime("%-d-%b-%Y"),
ltfu_end_date: date.end_of_month.strftime("%d-%b-%Y"),
ltfu_since_date: (date - 1.year).end_of_month.strftime("%d-%b-%Y"),
name: date.strftime("%b-%Y")
}
]
end.to_h

expect(result).to include(:assigned_patients, :period_info, :region, :total_registrations)
expect(result[:assigned_patients]).to eq(dm_patients.count)
expect(result[:period_info]).to eq(expected_period_info)
end
end
end
end

0 comments on commit 58ca22e

Please sign in to comment.