|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# The product catalog is the v2 billing surface. Seed it on a dedicated |
| 4 | +# organization that has the product_catalog integration enabled, keeping |
| 5 | +# Hooli as the legacy (v1) demo so the two flows never mix. |
| 6 | +gavin = User.find_by!(email: "gavin@hooli.com") |
| 7 | + |
| 8 | +organization = Organization.find_by(name: "Hooli v2") || |
| 9 | + Organization.create!(id: "33333333-4444-5555-6666-777777777777", name: "Hooli v2") |
| 10 | +organization.update!( |
| 11 | + premium_integrations: Organization::PREMIUM_INTEGRATIONS, |
| 12 | + invoice_footer: "Hooli v2 is a fictional company on the product catalog." |
| 13 | +) |
| 14 | + |
| 15 | +BillingEntity.find_or_create_by!(organization:, name: "Hooli v2", code: "hooli_v2").update!( |
| 16 | + email: "gavin@hooli.com", |
| 17 | + email_settings: BillingEntity::EMAIL_SETTINGS |
| 18 | +) |
| 19 | + |
| 20 | +membership = Membership.find_or_create_by!(user: gavin, organization:) |
| 21 | +MembershipRole.find_or_create_by!(membership:, organization:, role: Role.find_by!(admin: true)) |
| 22 | + |
| 23 | +unless organization.api_keys.exists? |
| 24 | + api_key = organization.api_keys.create!(name: "Hooli v2 Key", permissions: ApiKey.default_permissions) |
| 25 | + api_key.update_columns(value: "lago_key-hooli-v2-1234567890") # rubocop:disable Rails/SkipsModelValidations |
| 26 | +end |
| 27 | + |
| 28 | +unless Product.exists?(organization:, code: "cloud_platform") |
| 29 | + # A billable metric with a filter, to back a usage product item and a product item filter. |
| 30 | + api_calls_bm = BillableMetric.find_by(organization:, code: "catalog_api_calls") || |
| 31 | + BillableMetrics::CreateService.call!( |
| 32 | + organization_id: organization.id, |
| 33 | + name: "API calls", |
| 34 | + aggregation_type: "count_agg", |
| 35 | + code: "catalog_api_calls", |
| 36 | + filters: [{key: "region", values: %w[us eu]}] |
| 37 | + ).billable_metric |
| 38 | + |
| 39 | + region_filter = api_calls_bm.filters.find_by(key: "region") |
| 40 | + |
| 41 | + product = Products::CreateService.call!( |
| 42 | + organization:, |
| 43 | + params: { |
| 44 | + name: "Cloud Platform", |
| 45 | + code: "cloud_platform", |
| 46 | + description: "Seeded product catalog example" |
| 47 | + } |
| 48 | + ).product |
| 49 | + |
| 50 | + usage_item = ProductItems::CreateService.call!( |
| 51 | + organization:, |
| 52 | + params: { |
| 53 | + name: "API calls", |
| 54 | + code: "api_calls", |
| 55 | + item_type: "usage", |
| 56 | + product_id: product.id, |
| 57 | + billable_metric_id: api_calls_bm.id |
| 58 | + } |
| 59 | + ).product_item |
| 60 | + |
| 61 | + ProductItems::CreateService.call!( |
| 62 | + organization:, |
| 63 | + params: { |
| 64 | + name: "Platform fee", |
| 65 | + code: "platform_fee", |
| 66 | + item_type: "fixed", |
| 67 | + product_id: product.id |
| 68 | + } |
| 69 | + ) |
| 70 | + |
| 71 | + ProductItemFilters::CreateService.call!( |
| 72 | + product_item: usage_item, |
| 73 | + params: { |
| 74 | + name: "EU traffic", |
| 75 | + code: "eu_traffic", |
| 76 | + values: [{billable_metric_filter_id: region_filter.id, value: "eu"}] |
| 77 | + } |
| 78 | + ) |
| 79 | + |
| 80 | + rate_card = RateCards::CreateService.call!( |
| 81 | + product_item: usage_item, |
| 82 | + params: { |
| 83 | + name: "Standard USD", |
| 84 | + code: "standard_usd", |
| 85 | + currency: "USD" |
| 86 | + } |
| 87 | + ).rate_card |
| 88 | + |
| 89 | + RateCardRates::CreateService.call!( |
| 90 | + rate_card:, |
| 91 | + params: { |
| 92 | + effective_datetime: 1.month.ago, |
| 93 | + rate_model: "standard", |
| 94 | + rate_properties: {amount: "0.01"}, |
| 95 | + billing_interval_unit: "month" |
| 96 | + } |
| 97 | + ) |
| 98 | +end |
0 commit comments