feat: Conversation API to return applied_sla and sla_events (#9174)

* chore: Add sla_events to push_event_data

* chore: Return SLA details in the API

* chore: feature lock sla push event data

* Update _conversation.json.jbuilder

* chore: rubocop fixes
This commit is contained in:
Sojan Jose
2024-04-01 23:30:07 +05:30
committed by GitHub
parent 16282f6a66
commit 4e28481f27
20 changed files with 198 additions and 13 deletions

View File

@@ -0,0 +1,34 @@
require 'rails_helper'
RSpec.describe 'Conversations API', type: :request do
let(:account) { create(:account) }
let(:administrator) { create(:user, account: account, role: :administrator) }
describe 'GET /api/v1/accounts/{account.id}/conversations/:id' do
it 'returns SLA data for the conversation if the feature is enabled' do
account.enable_features!('sla')
conversation = create(:conversation, account: account)
applied_sla = create(:applied_sla, conversation: conversation)
sla_event = create(:sla_event, conversation: conversation, applied_sla: applied_sla)
get "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}", headers: administrator.create_new_auth_token
expect(response).to have_http_status(:ok)
expect(response.parsed_body['applied_sla']['id']).to eq(applied_sla.id)
expect(response.parsed_body['sla_events'].first['id']).to eq(sla_event.id)
end
it 'does not return SLA data for the conversation if the feature is disabled' do
account.disable_features!('sla')
conversation = create(:conversation, account: account)
create(:applied_sla, conversation: conversation)
create(:sla_event, conversation: conversation)
get "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}", headers: administrator.create_new_auth_token
expect(response).to have_http_status(:ok)
expect(response.parsed_body.keys).not_to include('applied_sla')
expect(response.parsed_body.keys).not_to include('sla_events')
end
end
end

View File

@@ -7,6 +7,27 @@ RSpec.describe AppliedSla, type: :model do
it { is_expected.to belong_to(:conversation) }
end
describe 'push_event_data' do
it 'returns the correct hash' do
applied_sla = create(:applied_sla)
expect(applied_sla.push_event_data).to eq(
{
id: applied_sla.id,
sla_id: applied_sla.sla_policy_id,
sla_status: applied_sla.sla_status,
created_at: applied_sla.created_at.to_i,
updated_at: applied_sla.updated_at.to_i,
sla_description: applied_sla.sla_policy.description,
sla_name: applied_sla.sla_policy.name,
sla_first_response_time_threshold: applied_sla.sla_policy.first_response_time_threshold,
sla_next_response_time_threshold: applied_sla.sla_policy.next_response_time_threshold,
sla_only_during_business_hours: applied_sla.sla_policy.only_during_business_hours,
sla_resolution_time_threshold: applied_sla.sla_policy.resolution_time_threshold
}
)
end
end
describe 'validates_factory' do
it 'creates valid applied sla policy object' do
applied_sla = create(:applied_sla)

View File

@@ -9,6 +9,21 @@ RSpec.describe SlaEvent, type: :model do
it { is_expected.to belong_to(:inbox) }
end
describe 'push_event_data' do
it 'returns the correct hash' do
sla_event = create(:sla_event)
expect(sla_event.push_event_data).to eq(
{
id: sla_event.id,
event_type: 'frt',
meta: sla_event.meta,
created_at: sla_event.created_at.to_i,
updated_at: sla_event.updated_at.to_i
}
)
end
end
describe 'validates_factory' do
it 'creates valid sla event object' do
sla_event = create(:sla_event)

View File

@@ -0,0 +1,29 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Conversations::EventDataPresenter do
let!(:presenter) { described_class.new(conversation) }
let!(:conversation) { create(:conversation) }
let!(:applied_sla) { create(:applied_sla, conversation: conversation) }
let!(:sla_event) { create(:sla_event, conversation: conversation, applied_sla: applied_sla) }
describe '#push_data' do
it 'returns push event payload with applied sla & sla events if the feature is enabled' do
conversation.account.enable_features!('sla')
expect(presenter.push_data).to include(
{
applied_sla: applied_sla.push_event_data,
sla_events: [sla_event.push_event_data]
}
)
end
it 'returns push event payload without applied sla & sla events if the feature is disabled' do
conversation.account.disable_features!('sla')
expect(presenter.push_data).not_to include(:applied_sla, :sla_events)
end
end
end