Skip to content

Commit

Permalink
[TAN-419] added a prioritized scope to Tenant
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesspeake committed Oct 27, 2023
1 parent 56c17de commit 89fd236
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
12 changes: 12 additions & 0 deletions back/engines/commercial/multi_tenancy/app/models/tenant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}..."
Expand Down
20 changes: 20 additions & 0 deletions back/engines/commercial/multi_tenancy/spec/models/tenant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 89fd236

Please sign in to comment.