diff --git a/lib/check_statistics.rb b/lib/check_statistics.rb index 1556fa62ca..8f9ba19d66 100644 --- a/lib/check_statistics.rb +++ b/lib/check_statistics.rb @@ -195,7 +195,7 @@ def get_statistics(start_date, end_date, team_id, platform, language, tracing_at CheckTracer.in_span('CheckStatistics#newsletters_delivered', attributes: tracing_attributes) do # Number of newsletters effectively delivered, accounting for user errors for each platform - statistics[:newsletters_delivered] = TiplineMessage.where(created_at: start_date..end_date, team_id: team_id, platform: platform_name, language: language, direction: 'outgoing', event: 'newsletter').count + statistics[:newsletters_delivered] = TiplineMessage.where(created_at: start_date..end_date, team_id: team_id, platform: platform_name, language: language, direction: 'outgoing', state: 'delivered', event: 'newsletter').count end CheckTracer.in_span('CheckStatistics#whatsapp_conversations', attributes: tracing_attributes) do diff --git a/test/lib/check_statistics_test.rb b/test/lib/check_statistics_test.rb index 96e9c36a5f..a6e8479db1 100644 --- a/test/lib/check_statistics_test.rb +++ b/test/lib/check_statistics_test.rb @@ -74,4 +74,12 @@ def teardown WebMock.stub_request(:get, @url).to_return(status: 400, body: { error: 'Error' }.to_json) assert_nil CheckStatistics.number_of_whatsapp_conversations(create_team.id, @from, @to) end + + test 'should calculate number of delivered newsletters' do + WebMock.stub_request(:get, /graph\.facebook\.com/).to_return(status: 400, body: { error: 'Error' }.to_json) + create_tipline_message team_id: @team.id, event: 'newsletter', direction: :outgoing, state: 'sent' + create_tipline_message team_id: @team.id, event: 'newsletter', direction: :outgoing, state: 'delivered' + data = CheckStatistics.get_statistics(Time.now.yesterday, Time.now.tomorrow, @team.id, 'whatsapp', 'en') + assert_equal 1, data[:newsletters_delivered] + end end