From 232369cd5c0b86e6afae43cd7f552ff93b111b8f Mon Sep 17 00:00:00 2001 From: Vishnu Narayanan Date: Tue, 23 Jan 2024 23:48:02 +0530 Subject: [PATCH] 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 --- ...31223033019_refactor_sla_policy_columns.rb | 8 +++++++ .../20231223040257_create_applied_slas.rb | 13 +++++++++++ db/schema.rb | 20 +++++++++++++--- .../v1/accounts/sla_policies_controller.rb | 3 ++- enterprise/app/models/applied_sla.rb | 23 +++++++++++++++++++ enterprise/app/models/sla_policy.rb | 18 ++++++++------- .../api/v1/models/_sla_policy.json.jbuilder | 6 +++-- .../accounts/sla_policies_controller_spec.rb | 6 +++-- spec/enterprise/models/applied_sla_spec.rb | 16 +++++++++++++ spec/enterprise/models/sla_policy_spec.rb | 5 ++++ spec/factories/applied_slas.rb | 8 +++++++ spec/factories/sla_policies.rb | 6 +++-- 12 files changed, 114 insertions(+), 18 deletions(-) create mode 100644 db/migrate/20231223033019_refactor_sla_policy_columns.rb create mode 100644 db/migrate/20231223040257_create_applied_slas.rb create mode 100644 enterprise/app/models/applied_sla.rb create mode 100644 spec/enterprise/models/applied_sla_spec.rb create mode 100644 spec/factories/applied_slas.rb diff --git a/db/migrate/20231223033019_refactor_sla_policy_columns.rb b/db/migrate/20231223033019_refactor_sla_policy_columns.rb new file mode 100644 index 000000000..2a6913799 --- /dev/null +++ b/db/migrate/20231223033019_refactor_sla_policy_columns.rb @@ -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 diff --git a/db/migrate/20231223040257_create_applied_slas.rb b/db/migrate/20231223040257_create_applied_slas.rb new file mode 100644 index 000000000..1aedb7d05 --- /dev/null +++ b/db/migrate/20231223040257_create_applied_slas.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 2d7debe09..0fcf7ba61 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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 diff --git a/enterprise/app/controllers/api/v1/accounts/sla_policies_controller.rb b/enterprise/app/controllers/api/v1/accounts/sla_policies_controller.rb index fa79c5362..e64256bc7 100644 --- a/enterprise/app/controllers/api/v1/accounts/sla_policies_controller.rb +++ b/enterprise/app/controllers/api/v1/accounts/sla_policies_controller.rb @@ -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 diff --git a/enterprise/app/models/applied_sla.rb b/enterprise/app/models/applied_sla.rb new file mode 100644 index 000000000..8329f643b --- /dev/null +++ b/enterprise/app/models/applied_sla.rb @@ -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 diff --git a/enterprise/app/models/sla_policy.rb b/enterprise/app/models/sla_policy.rb index 14ea1c34a..328367aeb 100644 --- a/enterprise/app/models/sla_policy.rb +++ b/enterprise/app/models/sla_policy.rb @@ -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 # diff --git a/enterprise/app/views/api/v1/models/_sla_policy.json.jbuilder b/enterprise/app/views/api/v1/models/_sla_policy.json.jbuilder index cd03d50ad..686b4317f 100644 --- a/enterprise/app/views/api/v1/models/_sla_policy.json.jbuilder +++ b/enterprise/app/views/api/v1/models/_sla_policy.json.jbuilder @@ -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 diff --git a/spec/enterprise/controllers/api/v1/accounts/sla_policies_controller_spec.rb b/spec/enterprise/controllers/api/v1/accounts/sla_policies_controller_spec.rb index 99a3bf0f0..b1619ef85 100644 --- a/spec/enterprise/controllers/api/v1/accounts/sla_policies_controller_spec.rb +++ b/spec/enterprise/controllers/api/v1/accounts/sla_policies_controller_spec.rb @@ -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 diff --git a/spec/enterprise/models/applied_sla_spec.rb b/spec/enterprise/models/applied_sla_spec.rb new file mode 100644 index 000000000..af73395a6 --- /dev/null +++ b/spec/enterprise/models/applied_sla_spec.rb @@ -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 diff --git a/spec/enterprise/models/sla_policy_spec.rb b/spec/enterprise/models/sla_policy_spec.rb index 6fa8044cd..dc3c5cb87 100644 --- a/spec/enterprise/models/sla_policy_spec.rb +++ b/spec/enterprise/models/sla_policy_spec.rb @@ -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 diff --git a/spec/factories/applied_slas.rb b/spec/factories/applied_slas.rb new file mode 100644 index 000000000..8ab48c558 --- /dev/null +++ b/spec/factories/applied_slas.rb @@ -0,0 +1,8 @@ +FactoryBot.define do + factory :applied_sla do + account + sla_policy + conversation + sla_status { 'active' } + end +end diff --git a/spec/factories/sla_policies.rb b/spec/factories/sla_policies.rb index 3f1d33b43..fbbd892ce 100644 --- a/spec/factories/sla_policies.rb +++ b/spec/factories/sla_policies.rb @@ -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