feat: sla 1 - refactor sla_policies model and add applied_sla model (#8602)

* feat: add models

* chore: refactor sla column names

* chore: remove foreign keys

* chore: fix spec

* chore: refactor models
This commit is contained in:
Vishnu Narayanan
2024-01-23 23:48:02 +05:30
committed by GitHub
parent 4b40c61201
commit 232369cd5c
12 changed files with 114 additions and 18 deletions

View File

@@ -0,0 +1,8 @@
class RefactorSlaPolicyColumns < ActiveRecord::Migration[7.0]
def change
rename_column :sla_policies, :rt_threshold, :next_response_time_threshold
rename_column :sla_policies, :frt_threshold, :first_response_time_threshold
add_column :sla_policies, :description, :string
add_column :sla_policies, :resolution_time_threshold, :float
end
end

View File

@@ -0,0 +1,13 @@
class CreateAppliedSlas < ActiveRecord::Migration[7.0]
def change
create_table :applied_slas do |t|
t.references :account, null: false
t.references :sla_policy, null: false
t.references :conversation, null: false
t.string :sla_status
t.timestamps
end
end
end

View File

@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2023_12_19_073832) do
ActiveRecord::Schema[7.0].define(version: 2023_12_23_040257) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_stat_statements"
enable_extension "pg_trgm"
@@ -115,6 +115,18 @@ ActiveRecord::Schema[7.0].define(version: 2023_12_19_073832) do
t.index ["account_id"], name: "index_agent_bots_on_account_id"
end
create_table "applied_slas", force: :cascade do |t|
t.bigint "account_id", null: false
t.bigint "sla_policy_id", null: false
t.bigint "conversation_id", null: false
t.string "sla_status"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["account_id"], name: "index_applied_slas_on_account_id"
t.index ["conversation_id"], name: "index_applied_slas_on_conversation_id"
t.index ["sla_policy_id"], name: "index_applied_slas_on_sla_policy_id"
end
create_table "articles", force: :cascade do |t|
t.integer "account_id", null: false
t.integer "portal_id", null: false
@@ -824,12 +836,14 @@ ActiveRecord::Schema[7.0].define(version: 2023_12_19_073832) do
create_table "sla_policies", force: :cascade do |t|
t.string "name", null: false
t.float "frt_threshold"
t.float "rt_threshold"
t.float "first_response_time_threshold"
t.float "next_response_time_threshold"
t.boolean "only_during_business_hours", default: false
t.bigint "account_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "description"
t.float "resolution_time_threshold"
t.index ["account_id"], name: "index_sla_policies_on_account_id"
end

View File

@@ -22,7 +22,8 @@ class Api::V1::Accounts::SlaPoliciesController < Api::V1::Accounts::EnterpriseAc
end
def permitted_params
params.require(:sla_policy).permit(:name, :rt_threshold, :frt_threshold, :only_during_business_hours)
params.require(:sla_policy).permit(:name, :description, :first_response_time_threshold, :next_response_time_threshold,
:resolution_time_threshold, :only_during_business_hours)
end
def fetch_sla

View File

@@ -0,0 +1,23 @@
# == Schema Information
#
# Table name: applied_slas
#
# id :bigint not null, primary key
# sla_status :string
# created_at :datetime not null
# updated_at :datetime not null
# account_id :bigint not null
# conversation_id :bigint not null
# sla_policy_id :bigint not null
#
# Indexes
#
# index_applied_slas_on_account_id (account_id)
# index_applied_slas_on_conversation_id (conversation_id)
# index_applied_slas_on_sla_policy_id (sla_policy_id)
#
class AppliedSla < ApplicationRecord
belongs_to :account
belongs_to :sla_policy
belongs_to :conversation
end

View File

@@ -2,14 +2,16 @@
#
# Table name: sla_policies
#
# id :bigint not null, primary key
# frt_threshold :float
# name :string not null
# only_during_business_hours :boolean default(FALSE)
# rt_threshold :float
# created_at :datetime not null
# updated_at :datetime not null
# account_id :bigint not null
# id :bigint not null, primary key
# description :string
# first_response_time_threshold :float
# name :string not null
# next_response_time_threshold :float
# only_during_business_hours :boolean default(FALSE)
# resolution_time_threshold :float
# created_at :datetime not null
# updated_at :datetime not null
# account_id :bigint not null
#
# Indexes
#

View File

@@ -1,5 +1,7 @@
json.id sla_policy.id
json.name sla_policy.name
json.frt_threshold sla_policy.frt_threshold
json.rt_threshold sla_policy.rt_threshold
json.description sla_policy.description
json.first_response_time_threshold sla_policy.first_response_time_threshold
json.next_response_time_threshold sla_policy.next_response_time_threshold
json.resolution_time_threshold sla_policy.resolution_time_threshold
json.only_during_business_hours sla_policy.only_during_business_hours

View File

@@ -80,8 +80,10 @@ RSpec.describe 'Enterprise SLA API', type: :request do
describe 'POST #create' do
let(:valid_params) do
{ sla_policy: { name: 'SLA 2',
frt_threshold: 1000,
rt_threshold: 1000,
description: 'SLA for premium customers',
first_response_time_threshold: 1000,
next_response_time_threshold: 2000,
resolution_time_threshold: 3000,
only_during_business_hours: false } }
end

View File

@@ -0,0 +1,16 @@
require 'rails_helper'
RSpec.describe AppliedSla, type: :model do
describe 'associations' do
it { is_expected.to belong_to(:sla_policy) }
it { is_expected.to belong_to(:account) }
it { is_expected.to belong_to(:conversation) }
end
describe 'validates_factory' do
it 'creates valid applied sla policy object' do
applied_sla = create(:applied_sla)
expect(applied_sla.sla_status).to eq 'active'
end
end
end

View File

@@ -18,6 +18,11 @@ RSpec.describe SlaPolicy, type: :model do
it 'creates valid sla policy object' do
sla_policy = create(:sla_policy)
expect(sla_policy.name).to eq 'sla_1'
expect(sla_policy.first_response_time_threshold).to eq 2000
expect(sla_policy.description).to eq 'SLA policy for enterprise customers'
expect(sla_policy.next_response_time_threshold).to eq 1000
expect(sla_policy.resolution_time_threshold).to eq 3000
expect(sla_policy.only_during_business_hours).to be false
end
end
end

View File

@@ -0,0 +1,8 @@
FactoryBot.define do
factory :applied_sla do
account
sla_policy
conversation
sla_status { 'active' }
end
end

View File

@@ -2,8 +2,10 @@ FactoryBot.define do
factory :sla_policy do
account
name { 'sla_1' }
rt_threshold { 1000 }
frt_threshold { 2000 }
first_response_time_threshold { 2000 }
description { 'SLA policy for enterprise customers' }
next_response_time_threshold { 1000 }
resolution_time_threshold { 3000 }
only_during_business_hours { false }
end
end