Merge pull request #1792 from Freika/feature/trial-imports-limit

Feature/trial imports limit
This commit is contained in:
Evgenii Burmakin 2025-09-23 21:17:09 +02:00 committed by GitHub
commit da3d7ccdf4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 67 additions and 1 deletions

View file

@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## Added
- Added foundation for upcoming authentication from iOS app.
- [Dawarich Cloud] Trial users can now create up to 5 imports. After that, they will be prompted to subscribe to a paid plan.
# [0.32.0] - 2025-09-13

View file

@ -6,7 +6,8 @@ export default class extends Controller {
static targets = ["input", "progress", "progressBar", "submit", "form"]
static values = {
url: String,
userTrial: Boolean
userTrial: Boolean,
currentImportsCount: Number
}
connect() {
@ -51,6 +52,16 @@ export default class extends Controller {
const files = this.inputTarget.files
if (files.length === 0) return
// Check import count limits for trial users
if (this.userTrialValue && this.currentImportsCountValue >= 5) {
const message = 'Import limit reached. Trial users can only create up to 5 imports. Please subscribe to import more files.'
showFlashMessage('error', message)
// Clear the file input
this.inputTarget.value = ''
return
}
// Check file size limits for trial users
if (this.userTrialValue) {
const MAX_FILE_SIZE = 11 * 1024 * 1024 // 11MB in bytes

View file

@ -15,6 +15,7 @@ class Import < ApplicationRecord
validates :name, presence: true, uniqueness: { scope: :user_id }
validate :file_size_within_limit, if: -> { user.trial? }
validate :import_count_within_limit, if: -> { user.trial? }
enum :status, { created: 0, processing: 1, completed: 2, failed: 3 }
@ -69,6 +70,15 @@ class Import < ApplicationRecord
errors.add(:file, 'is too large. Trial users can only upload files up to 10MB.')
end
def import_count_within_limit
return unless new_record?
existing_imports_count = user.imports.count
return unless existing_imports_count >= 5
errors.add(:base, 'Trial users can only create up to 5 imports. Please subscribe to import more files.')
end
def recalculate_stats
years_and_months_tracked.each do |year, month|
Stats::CalculatingJob.perform_later(user.id, year, month)

View file

@ -11,6 +11,13 @@
<div class="text-xs text-gray-500 mt-2">
File format is automatically detected during upload.
</div>
<% if current_user.trial? %>
<div class="text-xs text-orange-600 mt-2 font-medium">
Trial limitations: Max 5 imports, 10MB per file.
<br>
Current imports: <%= current_user.imports.count %>/5
</div>
<% end %>
</div>
</div>
@ -18,6 +25,7 @@
controller: "direct-upload",
direct_upload_url_value: rails_direct_uploads_url,
direct_upload_user_trial_value: current_user.trial?,
direct_upload_current_imports_count_value: current_user.imports.count,
direct_upload_target: "form"
} do |form| %>
<label class="form-control w-full max-w-xs my-5">

View file

@ -66,6 +66,42 @@ RSpec.describe Import, type: :model do
end
end
end
describe 'import count validation' do
context 'when user is a trial user' do
let(:user) do
user = create(:user)
user.update!(status: :trial)
user
end
it 'allows imports when under the limit' do
3.times { |i| create(:import, user: user, name: "import_#{i}") }
new_import = build(:import, user: user, name: 'new_import')
expect(new_import).to be_valid
end
it 'prevents creating more than 5 imports' do
5.times { |i| create(:import, user: user, name: "import_#{i}") }
new_import = build(:import, user: user, name: 'import_6')
expect(new_import).not_to be_valid
expect(new_import.errors[:base]).to include('Trial users can only create up to 5 imports. Please subscribe to import more files.')
end
end
context 'when user is an active user' do
let(:user) { create(:user, status: :active) }
it 'does not validate import count limit' do
7.times { |i| create(:import, user: user, name: "import_#{i}") }
new_import = build(:import, user: user, name: 'import_8')
expect(new_import).to be_valid
end
end
end
end
describe 'enums' do