Module: BetterAuth::Stripe::Schema

Defined in:
lib/better_auth/stripe/schema.rb

Class Method Summary collapse

Class Method Details

.custom_schema(config) ⇒ Object



51
52
53
54
# File 'lib/better_auth/stripe/schema.rb', line 51

def custom_schema(config)
  raw = config && (config[:schema] || config["schema"])
  normalize_custom_schema(raw || {})
end

.deep_merge_schema(base, override) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/better_auth/stripe/schema.rb', line 82

def deep_merge_schema(base, override)
  base.merge(override) do |_key, old_value, new_value|
    if old_value.is_a?(Hash) && new_value.is_a?(Hash)
      deep_merge_schema(old_value, new_value)
    else
      new_value
    end
  end
end

.normalize_custom_model_schema(value) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/better_auth/stripe/schema.rb', line 65

def normalize_custom_model_schema(value)
  return value unless value.is_a?(Hash)

  value.each_with_object({}) do |(key, object), result|
    normalized_key = BetterAuth::Plugins.normalize_key(key)
    result[normalized_key] = if normalized_key == :fields && object.is_a?(Hash)
      object.each_with_object({}) do |(field_name, field_schema), fields|
        fields[field_name] = field_schema
      end
    elsif object.is_a?(Hash)
      BetterAuth::Plugins.normalize_hash(object)
    else
      object
    end
  end
end

.normalize_custom_schema(value) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/better_auth/stripe/schema.rb', line 56

def normalize_custom_schema(value)
  return {} unless value.is_a?(Hash)

  value.each_with_object({}) do |(model_name, model_schema), result|
    normalized_model = BetterAuth::Plugins.normalize_key(model_name)
    result[normalized_model] = normalize_custom_model_schema(model_schema)
  end
end

.schema(config) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/better_auth/stripe/schema.rb', line 8

def schema(config)
  custom_schema = custom_schema(config)
  config = BetterAuth::Plugins.normalize_hash((config || {}).reject { |key, _| key.to_s == "schema" })
  base_schema = {
    user: {
      fields: {
        stripeCustomerId: {type: "string", required: false}
      }
    }
  }

  if config.dig(:subscription, :enabled)
    base_schema[:subscription] = {
      fields: {
        plan: {type: "string", required: true},
        referenceId: {type: "string", required: true},
        stripeCustomerId: {type: "string", required: false},
        stripeSubscriptionId: {type: "string", required: false},
        status: {type: "string", required: false, default_value: "incomplete"},
        periodStart: {type: "date", required: false},
        periodEnd: {type: "date", required: false},
        trialStart: {type: "date", required: false},
        trialEnd: {type: "date", required: false},
        cancelAtPeriodEnd: {type: "boolean", required: false, default_value: false},
        cancelAt: {type: "date", required: false},
        canceledAt: {type: "date", required: false},
        endedAt: {type: "date", required: false},
        seats: {type: "number", required: false},
        billingInterval: {type: "string", required: false},
        stripeScheduleId: {type: "string", required: false},
        limits: {type: "json", required: false}
      }
    }
  end

  if config.dig(:organization, :enabled)
    base_schema[:organization] = {fields: {stripeCustomerId: {type: "string", required: false}}}
  end

  custom_schema = custom_schema.except(:subscription) unless config.dig(:subscription, :enabled)
  deep_merge_schema(base_schema, custom_schema)
end