From 1268b2cf559027ff96eff0792e0d1a0edd926e71 Mon Sep 17 00:00:00 2001 From: Tanmay Sharma Date: Thu, 9 Oct 2025 16:24:32 +0200 Subject: [PATCH] fix schema file --- db/schema.rb | 47 +-------------- .../billing/v2/stripe_credit_sync_service.rb | 58 +++++++++---------- .../billing/v2/pricing_plan_service_spec.rb | 1 + 3 files changed, 31 insertions(+), 75 deletions(-) diff --git a/db/schema.rb b/db/schema.rb index f42d6d127..8106241ee 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -73,18 +73,7 @@ ActiveRecord::Schema[7.1].define(version: 2025_10_07_111938) do t.integer "status", default: 0 t.jsonb "internal_attributes", default: {}, null: false t.jsonb "settings", default: {} - t.integer "stripe_billing_version", default: 1, null: false - t.string "stripe_cadence_id" - t.string "stripe_pricing_plan_id" - t.integer "monthly_credits", default: 0, null: false - t.integer "topup_credits", default: 0, null: false - t.datetime "last_credit_sync_at" - t.string "stripe_customer_id" t.index ["status"], name: "index_accounts_on_status" - t.index ["stripe_billing_version"], name: "index_accounts_on_stripe_billing_version" - t.index ["stripe_cadence_id"], name: "index_accounts_on_stripe_cadence_id" - t.index ["stripe_customer_id"], name: "index_accounts_on_stripe_customer_id" - t.index ["stripe_pricing_plan_id"], name: "index_accounts_on_stripe_pricing_plan_id" end create_table "action_mailbox_inbound_emails", force: :cascade do |t| @@ -808,21 +797,6 @@ ActiveRecord::Schema[7.1].define(version: 2025_10_07_111938) do t.index ["name", "account_id"], name: "index_email_templates_on_name_and_account_id", unique: true end - create_table "failed_usage_reports", force: :cascade do |t| - t.bigint "account_id", null: false - t.integer "credits", null: false - t.string "feature", null: false - t.text "error" - t.integer "retry_count", default: 0 - t.datetime "retried_at" - t.boolean "resolved", default: false - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.datetime "resolved_at" - t.index ["account_id"], name: "index_failed_usage_reports_on_account_id" - t.index ["resolved", "created_at"], name: "index_failed_usage_reports_on_resolved_and_created_at" - end - create_table "folders", force: :cascade do |t| t.integer "account_id", null: false t.integer "category_id", null: false @@ -923,24 +897,6 @@ ActiveRecord::Schema[7.1].define(version: 2025_10_07_111938) do t.index ["title", "account_id"], name: "index_labels_on_title_and_account_id", unique: true end - create_table "leave_records", force: :cascade do |t| - t.bigint "account_id", null: false - t.bigint "user_id", null: false - t.date "start_date", null: false - t.date "end_date", null: false - t.integer "leave_type", default: 0, null: false - t.integer "status", default: 0, null: false - t.text "reason" - t.bigint "approved_by_id" - t.datetime "approved_at" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.index ["account_id", "status"], name: "index_leave_records_on_account_id_and_status" - t.index ["account_id"], name: "index_leave_records_on_account_id" - t.index ["approved_by_id"], name: "index_leave_records_on_approved_by_id" - t.index ["user_id"], name: "index_leave_records_on_user_id" - end - create_table "leaves", force: :cascade do |t| t.bigint "account_id", null: false t.bigint "user_id", null: false @@ -1254,7 +1210,7 @@ ActiveRecord::Schema[7.1].define(version: 2025_10_07_111938) do t.text "message_signature" t.string "otp_secret" t.integer "consumed_timestep" - t.boolean "otp_required_for_login", default: false, null: false + t.boolean "otp_required_for_login", default: false t.text "otp_backup_codes" t.index ["email"], name: "index_users_on_email" t.index ["otp_required_for_login"], name: "index_users_on_otp_required_for_login" @@ -1293,7 +1249,6 @@ ActiveRecord::Schema[7.1].define(version: 2025_10_07_111938) do add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id" add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id" - add_foreign_key "failed_usage_reports", "accounts" add_foreign_key "inboxes", "portals" create_trigger("accounts_after_insert_row_tr", :generated => true, :compatibility => 1). on("accounts"). diff --git a/enterprise/app/services/enterprise/billing/v2/stripe_credit_sync_service.rb b/enterprise/app/services/enterprise/billing/v2/stripe_credit_sync_service.rb index 142da442f..9805736e9 100644 --- a/enterprise/app/services/enterprise/billing/v2/stripe_credit_sync_service.rb +++ b/enterprise/app/services/enterprise/billing/v2/stripe_credit_sync_service.rb @@ -36,6 +36,35 @@ class Enterprise::Billing::V2::StripeCreditSyncService < Enterprise::Billing::V2 create_stripe_grant(params) end + def calculate_balance_from_stripe(stripe_usage, initial_credits) + total_used = stripe_usage + total_granted = initial_credits[:total_granted] + remaining = [total_granted - total_used, 0].max + + monthly_portion = [remaining, initial_credits[:monthly_granted]].min + topup_portion = [remaining - monthly_portion, 0].max + + { + monthly: monthly_portion, + topup: topup_portion, + total: remaining, + usage_from_stripe: total_used, + granted_from_stripe: total_granted, + last_synced: Time.current, + source: 'stripe' + } + end + + def local_fallback_balance + { + monthly: monthly_credits, + topup: topup_credits, + total: monthly_credits + topup_credits, + last_synced: Time.current, + source: 'local_fallback' + } + end + private def stripe_customer_id @@ -153,33 +182,4 @@ class Enterprise::Billing::V2::StripeCreditSyncService < Enterprise::Billing::V2 { api_key: ENV.fetch('STRIPE_SECRET_KEY', nil), stripe_version: '2025-08-27.preview' } ) end - - def calculate_balance_from_stripe(stripe_usage, initial_credits) - total_used = stripe_usage - total_granted = initial_credits[:total_granted] - remaining = [total_granted - total_used, 0].max - - monthly_portion = [remaining, initial_credits[:monthly_granted]].min - topup_portion = [remaining - monthly_portion, 0].max - - { - monthly: monthly_portion, - topup: topup_portion, - total: remaining, - usage_from_stripe: total_used, - granted_from_stripe: total_granted, - last_synced: Time.current, - source: 'stripe' - } - end - - def local_fallback_balance - { - monthly: monthly_credits, - topup: topup_credits, - total: monthly_credits + topup_credits, - last_synced: Time.current, - source: 'local_fallback' - } - end end diff --git a/spec/enterprise/services/enterprise/billing/v2/pricing_plan_service_spec.rb b/spec/enterprise/services/enterprise/billing/v2/pricing_plan_service_spec.rb index 5ee89060e..4bd22e4b8 100644 --- a/spec/enterprise/services/enterprise/billing/v2/pricing_plan_service_spec.rb +++ b/spec/enterprise/services/enterprise/billing/v2/pricing_plan_service_spec.rb @@ -5,6 +5,7 @@ RSpec.describe Enterprise::Billing::V2::PricingPlanService do let(:service) { described_class.new(account: account) } before do + allow(ENV).to receive(:fetch).and_call_original allow(ENV).to receive(:fetch).with('STRIPE_SECRET_KEY', nil).and_return('sk_test_123') end