fix: Handle invalid metric in ReportsController (#8086)

Co-authored-by: Pranav Raj S <pranav@chatwoot.com>

Fixes CW-2643
This commit is contained in:
Vishnu Narayanan
2023-10-12 17:01:23 +05:30
committed by GitHub
parent 9e173fca3c
commit 415bb23c37
2 changed files with 42 additions and 1 deletions

View File

@@ -15,7 +15,10 @@ class V2::ReportBuilder
end
def timeseries
send(params[:metric])
return send(params[:metric]) if metric_valid?
Rails.logger.error "ReportBuilder: Invalid metric - #{params[:metric]}"
{}
end
# For backward compatible with old report
@@ -53,6 +56,16 @@ class V2::ReportBuilder
private
def metric_valid?
%w[conversations_count
incoming_messages_count
outgoing_messages_count
avg_first_response_time
avg_resolution_time reply_time
resolutions_count
reply_time].include?(params[:metric])
end
def inbox
@inbox ||= account.inboxes.find(params[:id])
end

View File

@@ -171,6 +171,34 @@ describe V2::ReportBuilder do
builder = described_class.new(account, params)
expect { builder.timeseries }.to raise_error(ArgumentError)
end
it 'logs error when metric is nil' do
params = {
metric: nil, # Set metric to nil to test this case
type: :account,
since: (Time.zone.today - 3.days).to_time.to_i.to_s,
until: Time.zone.today.end_of_day.to_time.to_i.to_s
}
builder = described_class.new(account, params)
expect(Rails.logger).to receive(:error).with('ReportBuilder: Invalid metric - ')
builder.timeseries
end
it 'calls the appropriate metric method for a valid metric' do
params = {
metric: 'not_conversation_count', # Provide a invalid metric
type: :account,
since: (Time.zone.today - 3.days).to_time.to_i.to_s,
until: Time.zone.today.end_of_day.to_time.to_i.to_s
}
builder = described_class.new(account, params)
expect(Rails.logger).to receive(:error).with('ReportBuilder: Invalid metric - not_conversation_count')
builder.timeseries
end
end
context 'when report type is label' do