diff --git a/Guardfile b/Guardfile index b4183124a0b..be482673140 100644 --- a/Guardfile +++ b/Guardfile @@ -18,6 +18,7 @@ guard :rspec, cmd: 'bundle exec rspec' do end watch('app/services/integrations/aggregator/base_service.rb') { 'spec/services/integrations/aggregator/' } watch('app/services/base_service.rb') { 'spec/services/' } + watch('app/models/application_record.rb') { 'spec/models/' } watch(%r{^spec/.+_spec\.rb$}) watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" } watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" } diff --git a/app/models/plan.rb b/app/models/plan.rb index 50ffd050909..97251c24913 100644 --- a/app/models/plan.rb +++ b/app/models/plan.rb @@ -20,7 +20,7 @@ class Plan < ApplicationRecord has_many :coupon_targets has_many :coupons, through: :coupon_targets has_many :invoices, through: :subscriptions - has_many :progressive_billing_tresholds + has_many :usage_tresholds has_many :applied_taxes, class_name: 'Plan::AppliedTax', dependent: :destroy has_many :taxes, through: :applied_taxes diff --git a/app/models/progressive_billing_treshold.rb b/app/models/usage_treshold.rb similarity index 88% rename from app/models/progressive_billing_treshold.rb rename to app/models/usage_treshold.rb index 6a70e665fbb..7393cfc9374 100644 --- a/app/models/progressive_billing_treshold.rb +++ b/app/models/usage_treshold.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -class ProgressiveBillingTreshold < ApplicationRecord +class UsageTreshold < ApplicationRecord include PaperTrailTraceable include Currencies diff --git a/app/serializers/v1/plan_serializer.rb b/app/serializers/v1/plan_serializer.rb index 8fcdf17e697..0df23a70231 100644 --- a/app/serializers/v1/plan_serializer.rb +++ b/app/serializers/v1/plan_serializer.rb @@ -23,7 +23,7 @@ def serialize } payload.merge!(charges) if include?(:charges) - payload.merge!(progressive_billing_tresholds) if include?(:progressive_billing_tresholds) + payload.merge!(usage_tresholds) if include?(:usage_tresholds) payload.merge!(taxes) if include?(:taxes) payload.merge!(minimum_commitment) if include?(:minimum_commitment) && model.minimum_commitment @@ -41,11 +41,11 @@ def charges ).serialize end - def progressive_billing_tresholds + def usage_tresholds ::CollectionSerializer.new( - model.progressive_billing_tresholds, - ::V1::ProgressiveBillingTresholdSerializer, - collection_name: 'progressive_billing_tresholds' + model.usage_tresholds, + ::V1::UsageTresholdSerializer, + collection_name: 'usage_tresholds' ).serialize end diff --git a/app/serializers/v1/progressive_billing_treshold_serializer.rb b/app/serializers/v1/usage_treshold_serializer.rb similarity index 86% rename from app/serializers/v1/progressive_billing_treshold_serializer.rb rename to app/serializers/v1/usage_treshold_serializer.rb index 3c9c622c5e1..dc365ea50c8 100644 --- a/app/serializers/v1/progressive_billing_treshold_serializer.rb +++ b/app/serializers/v1/usage_treshold_serializer.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true module V1 - class ProgressiveBillingTresholdSerializer < ModelSerializer + class UsageTresholdSerializer < ModelSerializer def serialize { lago_id: model.id, diff --git a/db/migrate/20240807072052_rename_progressive_billing_tresholds.rb b/db/migrate/20240807072052_rename_progressive_billing_tresholds.rb new file mode 100644 index 00000000000..8920b57ee17 --- /dev/null +++ b/db/migrate/20240807072052_rename_progressive_billing_tresholds.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class RenameProgressiveBillingTresholds < ActiveRecord::Migration[7.1] + def change + rename_table :progressive_billing_tresholds, :usage_tresholds + end +end diff --git a/db/schema.rb b/db/schema.rb index 2c9161ff8f9..ca921a248a7 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_08_02_115017) do +ActiveRecord::Schema[7.1].define(version: 2024_08_07_072052) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" enable_extension "plpgsql" @@ -911,19 +911,6 @@ t.index ["tax_id"], name: "index_plans_taxes_on_tax_id" end - create_table "progressive_billing_tresholds", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| - t.uuid "plan_id", null: false - t.string "treshold_display_name" - t.bigint "amount_cents", null: false - t.string "amount_currency", null: false - t.boolean "recurring", default: false, null: false - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.index ["amount_cents", "plan_id", "recurring"], name: "idx_on_amount_cents_plan_id_recurring_b2c921d82b", unique: true - t.index ["plan_id", "recurring"], name: "index_progressive_billing_tresholds_on_plan_id_and_recurring", unique: true, where: "(recurring IS TRUE)" - t.index ["plan_id"], name: "index_progressive_billing_tresholds_on_plan_id" - end - create_table "quantified_events", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| t.string "external_subscription_id", null: false t.string "external_id" @@ -1020,6 +1007,19 @@ t.index ["organization_id"], name: "index_taxes_on_organization_id" end + create_table "usage_tresholds", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| + t.uuid "plan_id", null: false + t.string "treshold_display_name" + t.bigint "amount_cents", null: false + t.string "amount_currency", null: false + t.boolean "recurring", default: false, null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["amount_cents", "plan_id", "recurring"], name: "idx_on_amount_cents_plan_id_recurring_d28c0d19e4", unique: true + t.index ["plan_id", "recurring"], name: "index_usage_tresholds_on_plan_id_and_recurring", unique: true, where: "(recurring IS TRUE)" + t.index ["plan_id"], name: "index_usage_tresholds_on_plan_id" + end + create_table "users", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| t.string "email" t.string "password_digest" @@ -1194,7 +1194,6 @@ add_foreign_key "plans", "plans", column: "parent_id" add_foreign_key "plans_taxes", "plans" add_foreign_key "plans_taxes", "taxes" - add_foreign_key "progressive_billing_tresholds", "plans" add_foreign_key "quantified_events", "groups" add_foreign_key "quantified_events", "organizations" add_foreign_key "recurring_transaction_rules", "wallets" @@ -1205,6 +1204,7 @@ add_foreign_key "subscriptions", "customers" add_foreign_key "subscriptions", "plans" add_foreign_key "taxes", "organizations" + add_foreign_key "usage_tresholds", "plans" add_foreign_key "wallet_transactions", "invoices" add_foreign_key "wallet_transactions", "wallets" add_foreign_key "wallets", "customers" diff --git a/spec/factories/progressive_billing_tresholds.rb b/spec/factories/usage_tresholds.rb similarity index 85% rename from spec/factories/progressive_billing_tresholds.rb rename to spec/factories/usage_tresholds.rb index 85bb9be6fbf..bd9a06c921f 100644 --- a/spec/factories/progressive_billing_tresholds.rb +++ b/spec/factories/usage_tresholds.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true FactoryBot.define do - factory :progressive_billing_treshold do + factory :usage_treshold do plan treshold_display_name { Faker::Name.name } amount_cents { 100 } diff --git a/spec/models/plan_spec.rb b/spec/models/plan_spec.rb index 5ef15efba59..428aa491806 100644 --- a/spec/models/plan_spec.rb +++ b/spec/models/plan_spec.rb @@ -6,7 +6,7 @@ subject(:plan) { build(:plan, trial_period: 3) } it { is_expected.to have_one(:minimum_commitment) } - it { is_expected.to have_many(:progressive_billing_tresholds) } + it { is_expected.to have_many(:usage_tresholds) } it_behaves_like 'paper_trail traceable' diff --git a/spec/models/progressive_billing_treshold_spec.rb b/spec/models/usage_treshold_spec.rb similarity index 76% rename from spec/models/progressive_billing_treshold_spec.rb rename to spec/models/usage_treshold_spec.rb index 9aa9cfdf5e0..5aaae22acba 100644 --- a/spec/models/progressive_billing_treshold_spec.rb +++ b/spec/models/usage_treshold_spec.rb @@ -2,8 +2,8 @@ require 'rails_helper' -RSpec.describe ProgressiveBillingTreshold, type: :model do - subject(:progressive_billing_treshold) { build(:progressive_billing_treshold) } +RSpec.describe UsageTreshold, type: :model do + subject(:usage_treshold) { build(:usage_treshold) } it { is_expected.to belong_to(:plan) } diff --git a/spec/serializers/v1/plan_serializer_spec.rb b/spec/serializers/v1/plan_serializer_spec.rb index 563292a9e0c..338d20f9527 100644 --- a/spec/serializers/v1/plan_serializer_spec.rb +++ b/spec/serializers/v1/plan_serializer_spec.rb @@ -7,7 +7,7 @@ described_class.new( plan, root_name: 'plan', - includes: %i[charges taxes minimum_commitment progressive_billing_tresholds] + includes: %i[charges taxes minimum_commitment usage_tresholds] ) end @@ -15,9 +15,9 @@ let(:customer) { create(:customer, organization: plan.organization) } let(:subscription) { create(:subscription, customer:, plan:) } let(:charge) { create(:standard_charge, plan:) } - let(:progressive_billing_treshold) { create(:progressive_billing_treshold, plan:) } + let(:usage_treshold) { create(:usage_treshold, plan:) } - before { subscription && charge && progressive_billing_treshold } + before { subscription && charge && usage_treshold } context 'when plan has one minimium commitment' do let(:commitment) { create(:commitment, plan:) } @@ -55,14 +55,14 @@ 'lago_id' => charge.id ) - expect(result['plan']['progressive_billing_tresholds'].first).to include( - 'lago_id' => progressive_billing_treshold.id, - 'treshold_display_name' => progressive_billing_treshold.treshold_display_name, - 'amount_cents' => progressive_billing_treshold.amount_cents, - 'amount_currency' => progressive_billing_treshold.amount_currency, - 'recurring' => progressive_billing_treshold.recurring?, - 'created_at' => progressive_billing_treshold.created_at.iso8601, - 'updated_at' => progressive_billing_treshold.updated_at.iso8601 + expect(result['plan']['usage_tresholds'].first).to include( + 'lago_id' => usage_treshold.id, + 'treshold_display_name' => usage_treshold.treshold_display_name, + 'amount_cents' => usage_treshold.amount_cents, + 'amount_currency' => usage_treshold.amount_currency, + 'recurring' => usage_treshold.recurring?, + 'created_at' => usage_treshold.created_at.iso8601, + 'updated_at' => usage_treshold.updated_at.iso8601 ) expect(result['plan']['minimum_commitment']).to include( @@ -113,14 +113,14 @@ 'lago_id' => charge.id ) - expect(result['plan']['progressive_billing_tresholds'].first).to include( - 'lago_id' => progressive_billing_treshold.id, - 'treshold_display_name' => progressive_billing_treshold.treshold_display_name, - 'amount_cents' => progressive_billing_treshold.amount_cents, - 'amount_currency' => progressive_billing_treshold.amount_currency, - 'recurring' => progressive_billing_treshold.recurring?, - 'created_at' => progressive_billing_treshold.created_at.iso8601, - 'updated_at' => progressive_billing_treshold.updated_at.iso8601 + expect(result['plan']['usage_tresholds'].first).to include( + 'lago_id' => usage_treshold.id, + 'treshold_display_name' => usage_treshold.treshold_display_name, + 'amount_cents' => usage_treshold.amount_cents, + 'amount_currency' => usage_treshold.amount_currency, + 'recurring' => usage_treshold.recurring?, + 'created_at' => usage_treshold.created_at.iso8601, + 'updated_at' => usage_treshold.updated_at.iso8601 ) expect(result['plan']['minimum_commitment']).to be_nil diff --git a/spec/serializers/v1/progressive_billing_treshold_serializer_spec.rb b/spec/serializers/v1/progressive_billing_treshold_serializer_spec.rb deleted file mode 100644 index db62486d7e5..00000000000 --- a/spec/serializers/v1/progressive_billing_treshold_serializer_spec.rb +++ /dev/null @@ -1,25 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -RSpec.describe ::V1::ProgressiveBillingTresholdSerializer do - subject(:serializer) { described_class.new(progressive_billing_treshold, root_name: 'progressive_billing_treshold') } - - let(:progressive_billing_treshold) { create(:progressive_billing_treshold) } - - it 'serializes the object' do - result = JSON.parse(serializer.to_json) - - aggregate_failures do - expect(result['progressive_billing_treshold']).to include( - 'lago_id' => progressive_billing_treshold.id, - 'treshold_display_name' => progressive_billing_treshold.treshold_display_name, - 'amount_cents' => progressive_billing_treshold.amount_cents, - 'amount_currency' => progressive_billing_treshold.amount_currency, - 'recurring' => progressive_billing_treshold.recurring?, - 'created_at' => progressive_billing_treshold.created_at.iso8601, - 'updated_at' => progressive_billing_treshold.updated_at.iso8601 - ) - end - end -end diff --git a/spec/serializers/v1/usage_treshold_serializer_spec.rb b/spec/serializers/v1/usage_treshold_serializer_spec.rb new file mode 100644 index 00000000000..bb60761162f --- /dev/null +++ b/spec/serializers/v1/usage_treshold_serializer_spec.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe ::V1::UsageTresholdSerializer do + subject(:serializer) { described_class.new(usage_treshold, root_name: 'usage_treshold') } + + let(:usage_treshold) { create(:usage_treshold) } + + it 'serializes the object' do + result = JSON.parse(serializer.to_json) + + aggregate_failures do + expect(result['usage_treshold']).to include( + 'lago_id' => usage_treshold.id, + 'treshold_display_name' => usage_treshold.treshold_display_name, + 'amount_cents' => usage_treshold.amount_cents, + 'amount_currency' => usage_treshold.amount_currency, + 'recurring' => usage_treshold.recurring?, + 'created_at' => usage_treshold.created_at.iso8601, + 'updated_at' => usage_treshold.updated_at.iso8601 + ) + end + end +end