Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

misc: Use external_id instead of customer_id and subscription_id #419

Merged
merged 9 commits into from
Sep 5, 2022
2 changes: 1 addition & 1 deletion app/controllers/api/v1/applied_add_ons_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def create

def create_params
params.require(:applied_add_on).permit(
:customer_id,
:external_customer_id,
:add_on_code,
:amount_cents,
:amount_currency,
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v1/applied_coupons_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def create

def create_params
params.require(:applied_coupon).permit(
:customer_id,
:external_customer_id,
:coupon_code,
:amount_cents,
:amount_currency,
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/api/v1/customers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ def current_usage
service = Invoices::CustomerUsageService
.new(
nil,
customer_id: params[:customer_id],
subscription_id: params[:subscription_id],
customer_id: params[:customer_external_id],
subscription_id: params[:external_subscription_id],
organization_id: current_organization.id,
)
result = service.usage
Expand All @@ -49,7 +49,7 @@ def current_usage

def create_params
params.require(:customer).permit(
:customer_id,
:external_id,
:name,
:country,
:address_line1,
Expand Down
8 changes: 4 additions & 4 deletions app/controllers/api/v1/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ def create_params
.require(:event)
.permit(
:transaction_id,
:customer_id,
:external_customer_id,
:code,
:timestamp,
:subscription_id,
:external_subscription_id,
properties: {},
)
end
Expand All @@ -67,10 +67,10 @@ def batch_params
.require(:event)
.permit(
:transaction_id,
:customer_id,
:external_customer_id,
:code,
:timestamp,
subscription_ids: [],
external_subscription_ids: [],
properties: {},
)
end
Expand Down
8 changes: 4 additions & 4 deletions app/controllers/api/v1/subscriptions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def terminate
result = Subscriptions::TerminateService.new
.terminate_from_api(
organization: current_organization,
subscription_id: params[:id]
external_id: params[:external_id]
)

if result.success?
Expand All @@ -47,7 +47,7 @@ def update

result = service.update_from_api(
organization: current_organization,
id: params[:id],
external_id: params[:external_id],
params: update_params
)

Expand All @@ -64,7 +64,7 @@ def update
end

def index
customer = current_organization.customers.find_by(customer_id: params[:customer_id])
customer = current_organization.customers.find_by(external_id: params[:external_customer_id])

return not_found_error unless customer

Expand All @@ -86,7 +86,7 @@ def index

def create_params
params.require(:subscription)
.permit(:customer_id, :plan_code, :name, :subscription_id, :unique_id, :billing_time)
.permit(:external_customer_id, :plan_code, :name, :subscription_id, :external_id, :billing_time)
end

def update_params
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/api/v1/wallets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def show
end

def index
customer = Customer.find_by(customer_id: params[:external_customer_id])
customer = Customer.find_by(external_id: params[:external_customer_id])

return not_found_error unless customer

Expand Down Expand Up @@ -99,7 +99,7 @@ def update_params
end

def customer
Customer.find_by(customer_id: customer_params[:external_customer_id], organization_id: current_organization.id)
Customer.find_by(external_id: customer_params[:external_customer_id], organization_id: current_organization.id)
end

def render_wallet(wallet)
Expand Down
2 changes: 1 addition & 1 deletion app/graphql/mutations/customers/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Create < BaseMutation
description 'Creates a new customer'

argument :name, String, required: true
argument :customer_id, String, required: true
argument :external_id, String, required: true
argument :country, Types::CountryCodeEnum, required: false
argument :address_line1, String, required: false
argument :address_line2, String, required: false
Expand Down
2 changes: 1 addition & 1 deletion app/graphql/mutations/customers/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Update < BaseMutation

argument :id, ID, required: true
argument :name, String, required: true
argument :customer_id, String, required: true
argument :external_id, String, required: true
argument :country, Types::CountryCodeEnum, required: false
argument :address_line1, String, required: false
argument :address_line2, String, required: false
Expand Down
2 changes: 1 addition & 1 deletion app/graphql/types/customers/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Object < Types::BaseObject

field :id, ID, null: false

field :customer_id, String, null: false
field :external_id, String, null: false
field :name, String
field :sequential_id, String, null: false
field :slug, String, null: false
Expand Down
18 changes: 10 additions & 8 deletions app/graphql/types/events/object.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# r

module Types
module Events
class Object < Types::BaseObject
Expand All @@ -8,8 +6,8 @@ class Object < Types::BaseObject
field :id, ID, null: false
field :code, String, null: false

field :customer_id, String, null: false
field :subscription_id, String, null: false
field :external_customer_id, String, null: false
field :external_subscription_id, String, null: false
field :transaction_id, String, null: true

field :timestamp, GraphQL::Types::ISO8601DateTime, null: true
Expand All @@ -27,16 +25,20 @@ def received_at
object.created_at
end

def customer_id
object.customer.customer_id
def external_customer_id
object.customer.external_id
end

def external_subscription_id
object.subscription.external_id
end

def payload
{
event: {
transaction_id: object.transaction_id,
customer_id: object.customer.customer_id,
subscription_id: object.subscription_id,
external_customer_id: object.customer.external_id,
external_subscription_id: object.subscription.external_id,
code: object.code,
timestamp: object.timestamp.to_i,
properties: object.properties || {},
Expand Down
1 change: 1 addition & 0 deletions app/graphql/types/subscriptions/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Object < Types::BaseObject
graphql_name 'Subscription'

field :id, ID, null: false
field :external_id, String, null: false
field :customer, Types::Customers::Object, null: false
field :plan, Types::Plans::Object, null: false

Expand Down
2 changes: 1 addition & 1 deletion app/models/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Customer < ApplicationRecord

sequenced scope: ->(customer) { customer.organization.customers }

validates :customer_id, presence: true, uniqueness: { scope: :organization_id }
validates :external_id, presence: true, uniqueness: { scope: :organization_id }
validates :country, country_code: true, unless: -> { country.nil? }
validates :vat_rate, numericality: { less_than_or_equal_to: 100, greater_than_or_equal_to: 0 }, allow_nil: true
validates :payment_provider, inclusion: { in: PAYMENT_PROVIDERS }, allow_nil: true
Expand Down
11 changes: 6 additions & 5 deletions app/models/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class Subscription < ApplicationRecord
has_many :invoices, through: :invoice_subscriptions
has_many :fees

validates :unique_id, presence: true
validate :validate_unique_id, on: :create
validates :external_id, presence: true
validate :validate_external_id, on: :create

STATUSES = [
:pending,
Expand Down Expand Up @@ -71,10 +71,11 @@ def already_billed?
fees.subscription_kind.any?
end

def validate_unique_id
def validate_external_id
return unless active?

used_ids = customer&.active_subscriptions&.pluck(:unique_id)
errors.add(:unique_id, :value_already_exists) if used_ids&.include?(unique_id)
# NOTE: We want unique external id per organization.
used_ids = organization.subscriptions.active.pluck(:external_id)
errors.add(:external_id, :value_already_exists) if used_ids&.include?(external_id)
end
end
2 changes: 1 addition & 1 deletion app/serializers/v1/applied_add_on_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ def serialize
lago_id: model.id,
lago_add_on_id: model.add_on.id,
add_on_code: model.add_on.code,
customer_id: model.customer.customer_id,
lago_customer_id: model.customer.id,
external_customer_id: model.customer.external_id,
amount_cents: model.amount_cents,
amount_currency: model.amount_currency,
created_at: model.created_at.iso8601,
Expand Down
2 changes: 1 addition & 1 deletion app/serializers/v1/applied_coupon_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ def serialize
lago_id: model.id,
lago_coupon_id: model.coupon.id,
coupon_code: model.coupon.code,
customer_id: model.customer.customer_id,
lago_customer_id: model.customer.id,
external_customer_id: model.customer.external_id,
amount_cents: model.amount_cents,
amount_currency: model.amount_currency,
expiration_date: model.coupon.expiration_date&.iso8601,
Expand Down
2 changes: 1 addition & 1 deletion app/serializers/v1/customer_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class CustomerSerializer < ModelSerializer
def serialize
{
lago_id: model.id,
customer_id: model.customer_id,
external_id: model.external_id,
name: model.name,
sequential_id: model.sequential_id,
slug: model.slug,
Expand Down
5 changes: 2 additions & 3 deletions app/serializers/v1/event_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
# frozen_string_literal: true
#
module V1
class EventSerializer < ModelSerializer
def serialize
{
lago_id: model.id,
transaction_id: model.transaction_id,
lago_customer_id: model.customer_id,
customer_id: model.customer&.customer_id,
external_customer_id: model.customer&.external_id,
code: model.code,
timestamp: model.timestamp.iso8601,
properties: model.properties,
lago_subscription_id: model.subscription_id,
subscription_unique_id: model.subscription&.unique_id,
external_subscription_id: model.subscription&.external_id,
created_at: model.created_at.iso8601
}
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class CustomerErrorSerializer < ModelSerializer
def serialize
{
lago_customer_id: model.id,
customer_id: model.customer_id,
external_customer_id: model.external_id,
payment_provider: model.payment_provider,
provider_error: options[:provider_error],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def serialize
{
lago_invoice_id: invoice.id,
lago_customer_id: invoice.customer.id,
customer_id: invoice.customer.customer_id,
external_customer_id: invoice.customer.external_id,
provider_customer_id: options[:provider_customer_id],
payment_provider: invoice.customer.payment_provider,
provider_error: options[:provider_error],
Expand Down
4 changes: 2 additions & 2 deletions app/serializers/v1/subscription_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ class SubscriptionSerializer < ModelSerializer
def serialize
{
lago_id: model.id,
external_id: model.external_id,
lago_customer_id: model.customer_id,
customer_id: model.customer.customer_id,
external_customer_id: model.customer.external_id,
name: model.name,
unique_id: model.unique_id,
plan_code: model.plan.code,
status: model.status,
billing_time: model.billing_time,
Expand Down
2 changes: 1 addition & 1 deletion app/serializers/v1/wallet_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def serialize
{
lago_id: model.id,
lago_customer_id: model.customer_id,
external_customer_id: model.customer.customer_id,
external_customer_id: model.customer.external_id,
status: model.status,
currency: model.currency,
name: model.name,
Expand Down
2 changes: 1 addition & 1 deletion app/services/applied_add_ons/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def create(**args)

def create_from_api(organization:, args:)
@customer = Customer.find_by(
customer_id: args[:customer_id],
external_id: args[:external_customer_id],
organization_id: organization.id,
)

Expand Down
2 changes: 1 addition & 1 deletion app/services/applied_coupons/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def create(**args)

def create_from_api(organization:, args:)
@customer = Customer.find_by(
customer_id: args[:customer_id],
external_id: args[:external_customer_id],
organization_id: organization.id,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def base_scope
PersistedEvent
.where(billable_metric_id: billable_metric.id)
.where(customer_id: subscription.customer_id)
.where(external_subscription_id: subscription.unique_id)
.where(external_subscription_id: subscription.external_id)
end

# NOTE: Full period duration to take upgrade, terminate
Expand Down
4 changes: 2 additions & 2 deletions app/services/customers/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Customers
class CreateService < BaseService
def create_from_api(organization:, params:)
customer = organization.customers.find_or_initialize_by(customer_id: params[:customer_id])
customer = organization.customers.find_or_initialize_by(external_id: params[:external_id])

customer.name = params[:name] if params.key?(:name)
customer.country = params[:country]&.upcase if params.key?(:country)
Expand Down Expand Up @@ -35,7 +35,7 @@ def create_from_api(organization:, params:)
def create(**args)
customer = Customer.create!(
organization_id: args[:organization_id],
customer_id: args[:customer_id],
external_id: args[:external_id],
name: args[:name],
country: args[:country]&.upcase,
address_line1: args[:address_line1],
Expand Down
6 changes: 3 additions & 3 deletions app/services/customers/update_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ def update(**args)
customer.vat_rate = args[:vat_rate] if args.key?(:vat_rate)
customer.payment_provider = args[:payment_provider] if args.key?(:payment_provider)

# NOTE: Customer_id is not editable if customer is attached to subscriptions
if !customer.attached_to_subscriptions? && args.key?(:customer_id)
customer.customer_id = args[:customer_id]
# NOTE: external_id is not editable if customer is attached to subscriptions
if !customer.attached_to_subscriptions? && args.key?(:external_id)
customer.external_id = args[:external_id]
end

customer.save!
Expand Down
Loading