mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-03-05 04:03:45 +08:00
Merge pull request #1472 from biosistemika/mm-sci-2953-system-notifications-data-migration
System Notifications - Migration of existing data
This commit is contained in:
commit
3538e74b03
1 changed files with 66 additions and 16 deletions
|
@ -1,23 +1,73 @@
|
||||||
namespace :notifications do
|
# frozen_string_literal: true
|
||||||
desc 'Creates new system notification for all active users'
|
|
||||||
task :new_system, [:title, :message] => :environment do |_, args|
|
|
||||||
include NotificationsHelper
|
|
||||||
|
|
||||||
if args.blank? ||
|
namespace :notifications do
|
||||||
args.empty? ||
|
desc 'Copies system notifications to newly created data structure. IT SHOULD BE RUN ONE TIME ONLY'
|
||||||
args[:title].blank? ||
|
task copy_system_notifications: :environment do
|
||||||
args[:message].blank?
|
t0 = Time.now
|
||||||
puts 'One or both of arguments are missing'
|
system_notifications = Notification
|
||||||
return
|
.where(type_of: :system_message)
|
||||||
|
.where(generator_user_id: nil)
|
||||||
|
.where.not('title like ?', 'Congratulations%')
|
||||||
|
|
||||||
|
system_notifications.each do |system_notification|
|
||||||
|
new_notification = SystemNotification.create!(
|
||||||
|
source_id: -1,
|
||||||
|
title: system_notification.title,
|
||||||
|
description: system_notification.message,
|
||||||
|
modal_title: system_notification.title,
|
||||||
|
modal_body: system_notification.message,
|
||||||
|
show_on_login: false,
|
||||||
|
source_created_at: system_notification.created_at,
|
||||||
|
last_time_changed_at: system_notification.created_at
|
||||||
|
)
|
||||||
|
|
||||||
|
created_at = system_notification.created_at
|
||||||
|
|
||||||
|
sql = ' INSERT INTO user_system_notifications
|
||||||
|
(
|
||||||
|
user_id,
|
||||||
|
created_at,
|
||||||
|
updated_at,
|
||||||
|
system_notification_id,
|
||||||
|
seen_at,
|
||||||
|
read_at
|
||||||
|
)
|
||||||
|
VALUES
|
||||||
|
'
|
||||||
|
user_notifications = UserNotification
|
||||||
|
.where(notification_id: system_notification.id)
|
||||||
|
values_array = user_notifications.map do |user_notification|
|
||||||
|
user_notification
|
||||||
|
.slice(:user_id, :created_at, :updated_at)
|
||||||
|
.merge(system_notification_id: new_notification.id)
|
||||||
|
.merge(seen_at: created_at, read_at: created_at)
|
||||||
|
.values
|
||||||
|
.map { |v| "'#{v}'" }
|
||||||
|
.join(',')
|
||||||
end
|
end
|
||||||
|
|
||||||
title = args[:title]
|
values_sql = values_array
|
||||||
message = args[:message]
|
.map { |v| "(#{v})" }
|
||||||
|
.join(',')
|
||||||
|
|
||||||
puts 'Creating following system notification:'
|
sql += values_sql
|
||||||
puts " *** #{title} ***"
|
ActiveRecord::Base.connection.execute(sql)
|
||||||
puts " #{I18n.l(Time.now, format: :full)} | #{message}"
|
end
|
||||||
|
|
||||||
create_system_notification(title, message)
|
t1 = Time.now
|
||||||
|
puts "Task took #{t1 - t0}"
|
||||||
|
end
|
||||||
|
|
||||||
|
desc 'Removes obsolete system notifications from notifications table.'
|
||||||
|
task delete_obsolete_system_notifications: :environment do
|
||||||
|
system_notifications = Notification
|
||||||
|
.where(type_of: :system_message)
|
||||||
|
.where(generator_user_id: nil)
|
||||||
|
.where.not('title like ?', 'Congratulations%')
|
||||||
|
UserNotification
|
||||||
|
.where(notification_id: system_notifications.pluck(:id))
|
||||||
|
.delete_all
|
||||||
|
|
||||||
|
system_notifications.delete_all
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue