diff --git a/back/engines/commercial/multi_tenancy/app/models/tenant.rb b/back/engines/commercial/multi_tenancy/app/models/tenant.rb index 4c7dbdfd609e..63f9317ff4f6 100644 --- a/back/engines/commercial/multi_tenancy/app/models/tenant.rb +++ b/back/engines/commercial/multi_tenancy/app/models/tenant.rb @@ -51,6 +51,18 @@ class Tenant < ApplicationRecord where(id: ids) } + # Order by most important tenants (active) first + scope :prioritized, lambda { + priority_order = %w[active trial demo expired_trial churned not_applicable] + tenants = AppConfiguration.from_tenants(self).map do |config| + { id: config[:id], lifecycle_stage: config[:settings]['core']['lifecycle_stage'] } + end + ordered_tenants = tenants.sort_by { |tenant| priority_order.index(tenant[:lifecycle_stage]) } + + ordered_ids = ordered_tenants.pluck(:id) + sort_by { |tenant| ordered_ids.index(tenant[:id]) } + } + delegate :active?, :churned?, to: :configuration class << self diff --git a/back/engines/commercial/multi_tenancy/lib/tasks/core/migrate_continuous_projects.rake b/back/engines/commercial/multi_tenancy/lib/tasks/core/migrate_continuous_projects.rake index 40aecbf6af1d..3ff1e1a95e81 100644 --- a/back/engines/commercial/multi_tenancy/lib/tasks/core/migrate_continuous_projects.rake +++ b/back/engines/commercial/multi_tenancy/lib/tasks/core/migrate_continuous_projects.rake @@ -9,7 +9,9 @@ namespace :fix_existing_tenants do specify_host = args[:specify_host] Rails.logger.info 'DRY RUN: Changes will not be persisted' unless persist_changes stats = {} - Tenant.creation_finalized.each do |tenant| + + # TODO: Test that it continues if there are errors + Tenant.creation_finalized.prioritized.each do |tenant| next unless tenant.host == specify_host || specify_host.blank? Rails.logger.info "PROCESSING TENANT: #{tenant.host}..." diff --git a/back/engines/commercial/multi_tenancy/spec/models/tenant_spec.rb b/back/engines/commercial/multi_tenancy/spec/models/tenant_spec.rb index cdb8e43f9d1b..84ea3da40ffa 100644 --- a/back/engines/commercial/multi_tenancy/spec/models/tenant_spec.rb +++ b/back/engines/commercial/multi_tenancy/spec/models/tenant_spec.rb @@ -68,6 +68,26 @@ specify { expect(described_class.churned.count).to eq(1) } end + describe 'prioritized scope' do + let!(:churned_tenant) { create(:tenant, lifecycle: 'churned') } + let!(:expired_trial_tenant) { create(:tenant, lifecycle: 'expired_trial') } + let!(:demo_tenant) { create(:tenant, lifecycle: 'demo') } + let!(:active_tenant) { create(:tenant, lifecycle: 'active') } # 2 active tenants with the test-tenant + let!(:trial_tenant) { create(:tenant, lifecycle: 'trial') } + + it 'returns tenants prioritized by lifecycle' do + # Before prioritization + tenants = described_class.all + expect(tenants.count).to eq(6) + expect(tenants.map { |t| t[:settings]['core']['lifecycle_stage'] }).to eq %w[active churned expired_trial demo active trial] + + # After prioritization + prioritized = described_class.prioritized + expect(prioritized.count).to eq(6) + expect(prioritized.map { |t| t[:settings]['core']['lifecycle_stage'] }).to eq %w[active active trial demo expired_trial churned] + end + end + describe 'Apartment tenant' do it 'is created on create' do host = 'something-else.com' # a different host than the default test tenant