fix: Handle invalid metric in ReportsController (#8086)
Co-authored-by: Pranav Raj S <pranav@chatwoot.com> Fixes CW-2643
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user