Fixes #3472 - Mentions migration fails because of unexpected User notification_config matrix state.
This commit is contained in:
parent
fb3a43b80a
commit
7d881b7253
2 changed files with 85 additions and 10 deletions
|
@ -18,7 +18,7 @@ class MentionInit < ActiveRecord::Migration[5.2]
|
|||
|
||||
Mention.reset_column_information
|
||||
create_overview
|
||||
update_user_matrix
|
||||
update_users
|
||||
end
|
||||
|
||||
def create_overview
|
||||
|
@ -43,24 +43,32 @@ class MentionInit < ActiveRecord::Migration[5.2]
|
|||
)
|
||||
end
|
||||
|
||||
def update_user_matrix
|
||||
def update_users
|
||||
User.with_permissions('ticket.agent').each do |user|
|
||||
next if user.preferences.blank?
|
||||
next if user.preferences['notification_config'].blank?
|
||||
next if user.preferences['notification_config']['matrix'].blank?
|
||||
|
||||
update_user_matrix_by_user(user)
|
||||
update_matrix(user.preferences['notification_config']['matrix'])
|
||||
|
||||
user.save!
|
||||
end
|
||||
end
|
||||
|
||||
def update_user_matrix_by_user(user)
|
||||
%w[create update].each do |type|
|
||||
user.preferences['notification_config']['matrix'][type]['criteria']['subscribed'] = true
|
||||
def update_matrix(matrix)
|
||||
matrix_type_defaults.each do |type, default|
|
||||
matrix[type] ||= {}
|
||||
matrix[type]['criteria'] ||= {}
|
||||
matrix[type]['criteria']['subscribed'] = default
|
||||
end
|
||||
end
|
||||
|
||||
%w[reminder_reached escalation].each do |type|
|
||||
user.preferences['notification_config']['matrix'][type]['criteria']['subscribed'] = false
|
||||
end
|
||||
user.save!
|
||||
def matrix_type_defaults
|
||||
@matrix_type_defaults ||= {
|
||||
'create' => true,
|
||||
'update' => true,
|
||||
'reminder_reached' => false,
|
||||
'escalation' => false,
|
||||
}
|
||||
end
|
||||
end
|
||||
|
|
67
spec/db/migrate/mention_init_spec.rb
Normal file
67
spec/db/migrate/mention_init_spec.rb
Normal file
|
@ -0,0 +1,67 @@
|
|||
require 'rails_helper'
|
||||
|
||||
RSpec.describe MentionInit, type: :db_migration do
|
||||
|
||||
let(:mocked_table_actions) do
|
||||
lambda { |migration|
|
||||
# mock DB connection with null object to "null" all connection actions
|
||||
allow(migration).to receive(:connection).and_return(double('ActiveRecord::ConnectionAdapters::*').as_null_object) # rubocop:disable RSpec/VerifiedDoubles
|
||||
}
|
||||
end
|
||||
|
||||
context 'when agent is present' do
|
||||
|
||||
subject(:user) do
|
||||
agent = create(:agent)
|
||||
agent.preferences['notification_config'] = notification_config
|
||||
agent.tap(&:save!)
|
||||
end
|
||||
|
||||
context 'when matrix misses type key' do
|
||||
|
||||
let(:notification_config) do
|
||||
{
|
||||
'matrix' => {
|
||||
'create' => {
|
||||
'criteria' => {
|
||||
'subscribed' => true
|
||||
}
|
||||
},
|
||||
'update' => {
|
||||
# 'criteria' => {
|
||||
# 'subscribed' => true
|
||||
# }
|
||||
},
|
||||
'reminder_reached' => {
|
||||
'criteria' => {
|
||||
'subscribed' => false
|
||||
}
|
||||
},
|
||||
'escalation' => {
|
||||
'criteria' => {
|
||||
'subscribed' => false
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
it 'adds type' do # rubocop:disable RSpec/ExampleLength
|
||||
expect do
|
||||
migrate(&mocked_table_actions)
|
||||
end
|
||||
.to change {
|
||||
user.reload.preferences['notification_config']['matrix']['update']
|
||||
}
|
||||
.from({})
|
||||
.to(
|
||||
{
|
||||
'criteria' => {
|
||||
'subscribed' => true
|
||||
}
|
||||
}
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue