mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2024-12-26 01:35:34 +08:00
Refactor export_all_counter increasing/reseting logic
- Remove daily task - Add method to user Model
This commit is contained in:
parent
a297ef2094
commit
247bae8a9f
4 changed files with 73 additions and 22 deletions
|
@ -229,8 +229,7 @@ class TeamsController < ApplicationController
|
|||
|
||||
def export_projects
|
||||
unless export_proj_requests_exceeded?
|
||||
current_user.export_vars['num_of_export_all_last_24_hours'] += 1
|
||||
current_user.save
|
||||
current_user.increase_daily_exports_counter!
|
||||
|
||||
generate_export_projects_zip
|
||||
|
||||
|
|
|
@ -56,7 +56,8 @@ class User < ApplicationRecord
|
|||
|
||||
default_variables(
|
||||
export_vars: {
|
||||
num_of_export_all_last_24_hours: 0
|
||||
num_of_export_all_last_24_hours: 0,
|
||||
last_export_timestamp: Date.today.to_time.to_i
|
||||
}
|
||||
)
|
||||
|
||||
|
@ -505,6 +506,16 @@ class User < ApplicationRecord
|
|||
end
|
||||
end
|
||||
|
||||
def increase_daily_exports_counter!
|
||||
if Time.at(export_vars['last_export_timestamp'] || 0).to_date == Date.today
|
||||
export_vars['num_of_export_all_last_24_hours'] += 1
|
||||
else
|
||||
export_vars['last_export_timestamp'] = Date.today.to_time.to_i
|
||||
export_vars['num_of_export_all_last_24_hours'] = 1
|
||||
end
|
||||
save
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def confirmation_required?
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
namespace :exportable_items do
|
||||
desc 'Removes exportable zip files'
|
||||
task cleanup: :environment do
|
||||
|
@ -6,23 +8,4 @@ namespace :exportable_items do
|
|||
puts "All exportable zip files older than " \
|
||||
"'#{num.days.ago}' have been removed"
|
||||
end
|
||||
|
||||
desc 'Resets export project counter to 0'
|
||||
task reset_export_projects_counter: :environment do
|
||||
User.find_each do |user|
|
||||
User.transaction do
|
||||
begin
|
||||
user.export_vars['num_of_export_all_last_24_hours'] = 0
|
||||
user.save
|
||||
rescue ActiveRecord::ActiveRecordError,
|
||||
ArgumentError,
|
||||
ActiveRecord::RecordNotSaved => e
|
||||
puts "Error resetting users num_of_export_all_last_24_hours " \
|
||||
"variable to 0, transaction reverted: #{e}"
|
||||
end
|
||||
end
|
||||
end
|
||||
puts 'Export project counter successfully ' \
|
||||
'reset on all users'
|
||||
end
|
||||
end
|
||||
|
|
|
@ -198,4 +198,62 @@ describe User, type: :model do
|
|||
expect(activities).to include activity_two
|
||||
end
|
||||
end
|
||||
|
||||
describe '#increase_daily_exports_counter!' do
|
||||
let(:user) { create :user }
|
||||
context 'when last_export_timestamp is set' do
|
||||
it 'increases counter by 1' do
|
||||
expect { user.increase_daily_exports_counter! }
|
||||
.to change {
|
||||
user.reload.export_vars['num_of_export_all_last_24_hours']
|
||||
}.from(0).to(1)
|
||||
end
|
||||
|
||||
it 'sets last_export_timestamp on today' do
|
||||
user.export_vars['last_export_timestamp'] = Date.yesterday.to_time.to_i
|
||||
user.save
|
||||
|
||||
expect { user.increase_daily_exports_counter! }
|
||||
.to change {
|
||||
user.reload.export_vars['last_export_timestamp']
|
||||
}.to(Date.today.to_time.to_i)
|
||||
end
|
||||
|
||||
it 'sets new counter for today' do
|
||||
user.export_vars = {
|
||||
'num_of_export_all_last_24_hours': 3,
|
||||
'last_export_timestamp': Date.yesterday.to_time.to_i
|
||||
}
|
||||
user.save
|
||||
|
||||
expect { user.increase_daily_exports_counter! }
|
||||
.to change {
|
||||
user.reload.export_vars['num_of_export_all_last_24_hours']
|
||||
}.from(3).to(1)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when last_export_timestamp not exists (existing users)' do
|
||||
it 'sets last_export_timestamp on today' do
|
||||
user.export_vars.delete('last_export_timestamp')
|
||||
user.save
|
||||
|
||||
expect { user.increase_daily_exports_counter! }
|
||||
.to change {
|
||||
user.reload.export_vars['last_export_timestamp']
|
||||
}.from(nil).to(Date.today.to_time.to_i)
|
||||
end
|
||||
|
||||
it 'starts count reports with 1' do
|
||||
user.export_vars.delete('last_export_timestamp')
|
||||
user.export_vars['num_of_export_all_last_24_hours'] = 2
|
||||
user.save
|
||||
|
||||
expect { user.increase_daily_exports_counter! }
|
||||
.to change {
|
||||
user.reload.export_vars['num_of_export_all_last_24_hours']
|
||||
}.from(2).to(1)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue