diff --git a/app/controllers/teams_controller.rb b/app/controllers/teams_controller.rb index 74709eb8a..50db7a324 100644 --- a/app/controllers/teams_controller.rb +++ b/app/controllers/teams_controller.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 614a273e3..31291398b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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? diff --git a/lib/tasks/exportable_items.rake b/lib/tasks/exportable_items.rake index 04f9130f3..2d178eac2 100644 --- a/lib/tasks/exportable_items.rake +++ b/lib/tasks/exportable_items.rake @@ -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 diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 167dabb5a..15ab3be5e 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -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