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

feat: #95 Add metadatas to customers #100

Merged
merged 4 commits into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ ruby '3.0.1'
gem 'bcrypt'
gem 'bootsnap', require: false
gem 'clockwork', require: false
gem 'countries'
gem 'graphql'
gem 'graphql-pagination'
gem 'jwt'
Expand Down
8 changes: 8 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ GEM
coffee-script-source (1.12.2)
concurrent-ruby (1.1.10)
connection_pool (2.2.5)
countries (5.0.0)
i18n_data (~> 0.16.0)
sixarm_ruby_unaccent (~> 1.1)
crass (1.0.6)
debug (1.4.0)
irb (>= 1.3.6)
Expand All @@ -119,6 +122,8 @@ GEM
graphql
i18n (1.10.0)
concurrent-ruby (~> 1.0)
i18n_data (0.16.0)
simple_po_parser (~> 1.1)
io-console (0.5.11)
io-wait (0.2.1)
irb (1.4.1)
Expand Down Expand Up @@ -237,12 +242,14 @@ GEM
connection_pool (>= 2.2.2)
rack (~> 2.0)
redis (>= 4.2.0)
simple_po_parser (1.1.6)
simplecov (0.21.2)
docile (~> 1.1)
simplecov-html (~> 0.11)
simplecov_json_formatter (~> 0.1)
simplecov-html (0.12.3)
simplecov_json_formatter (0.1.4)
sixarm_ruby_unaccent (1.2.0)
sprockets (4.0.2)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
Expand Down Expand Up @@ -275,6 +282,7 @@ DEPENDENCIES
clockwork
clockwork-test
coffee-rails
countries
debug
dotenv
factory_bot_rails
Expand Down
19 changes: 17 additions & 2 deletions app/controllers/api/v1/customers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def create
render(
json: ::V1::CustomerSerializer.new(
result.customer,
root_name: 'customer'
root_name: 'customer',
)
)
else
Expand All @@ -25,7 +25,22 @@ def create
private

def create_params
params.require(:customer).permit(:customer_id, :name)
params.require(:customer).permit(
:customer_id,
:name,
:country,
:address_line1,
:address_line2,
:state,
:zipcode,
:email,
:city,
:url,
:phone,
:logo_url,
:legal_name,
:legal_number,
)
end
end
end
Expand Down
12 changes: 12 additions & 0 deletions app/graphql/mutations/customers/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ class Create < BaseMutation

argument :name, String, required: true
argument :customer_id, String, required: true
argument :country, Types::Customers::CountryCodeEnum, required: false
argument :address_line1, String, required: false
argument :address_line2, String, required: false
argument :state, String, required: false
argument :zipcode, String, required: false
argument :email, String, required: false
argument :city, String, required: false
argument :url, String, required: false
argument :phone, String, required: false
argument :logo_url, String, required: false
argument :legal_name, String, required: false
argument :legal_number, String, required: false

type Types::Customers::Object

Expand Down
12 changes: 12 additions & 0 deletions app/graphql/mutations/customers/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ class Update < BaseMutation
argument :id, ID, required: true
argument :name, String, required: true
argument :customer_id, String, required: true
argument :country, Types::Customers::CountryCodeEnum, required: false
argument :address_line1, String, required: false
argument :address_line2, String, required: false
argument :state, String, required: false
argument :zipcode, String, required: false
argument :email, String, required: false
argument :city, String, required: false
argument :url, String, required: false
argument :phone, String, required: false
argument :logo_url, String, required: false
argument :legal_name, String, required: false
argument :legal_number, String, required: false

type Types::Customers::Object

Expand Down
13 changes: 13 additions & 0 deletions app/graphql/mutations/subscriptions/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ class Create < BaseMutation
argument :customer_id, String, required: true
argument :plan_code, String, required: true

argument :country, Types::Customers::CountryCodeEnum, required: false
argument :address_line1, String, required: false
argument :address_line2, String, required: false
argument :state, String, required: false
argument :zipcode, String, required: false
argument :email, String, required: false
argument :city, String, required: false
argument :url, String, required: false
argument :phone, String, required: false
argument :logo_url, String, required: false
argument :legal_name, String, required: false
argument :legal_number, String, required: false

type Types::Subscriptions::Object

def resolve(**args)
Expand Down
13 changes: 13 additions & 0 deletions app/graphql/types/customers/country_code_enum.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module Types
module Customers
class CountryCodeEnum < Types::BaseEnum
graphql_name 'CountryCode'

ISO3166::Country.all.each do |country|
value country.alpha2, country.iso_short_name
end
end
end
end
14 changes: 14 additions & 0 deletions app/graphql/types/customers/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ class Object < Types::BaseObject

field :customer_id, String, null: false
field :name, String

field :country, Types::Customers::CountryCodeEnum, null: true
field :address_line1, String, null: true
field :address_line2, String, null: true
field :state, String, null: true
field :zipcode, String, null: true
field :email, String, null: true
field :city, String, null: true
field :url, String, null: true
field :phone, String, null: true
field :logo_url, String, null: true
field :legal_name, String, null: true
field :legal_number, String, null: true

field :subscriptions, [Types::Subscriptions::Object]

field :created_at, GraphQL::Types::ISO8601DateTime, null: false
Expand Down
1 change: 1 addition & 0 deletions app/models/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Customer < ApplicationRecord
has_many :invoices, through: :subscriptions

validates :customer_id, presence: true, uniqueness: { scope: :organization_id }
validates :country, country_code: true, if: :country?

def attached_to_subscriptions?
subscriptions.exists?
Expand Down
38 changes: 38 additions & 0 deletions app/services/customers_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,20 @@
class CustomersService < BaseService
def create_from_api(organization:, params:)
customer = organization.customers.find_or_initialize_by(customer_id: params[:customer_id])

customer.name = params[:name]
customer.country = params[:country]&.upcase
customer.address_line1 = params[:address_line1]
customer.address_line2 = params[:address_line2]
customer.state = params[:state]
customer.zipcode = params[:zipcode]
customer.email = params[:email]
customer.city = params[:city]
customer.url = params[:url]
customer.phone = params[:phone]
customer.logo_url = params[:logo_url]
customer.legal_name = params[:legal_name]
customer.legal_number = params[:legal_number]
customer.save!

result.customer = customer
Expand All @@ -17,6 +30,18 @@ def create(**args)
organization_id: args[:organization_id],
customer_id: args[:customer_id],
name: args[:name],
country: args[:country]&.upcase,
address_line1: args[:address_line1],
address_line2: args[:address_line2],
state: args[:state],
zipcode: args[:zipcode],
email: args[:email],
city: args[:city],
url: args[:url],
phone: args[:phone],
logo_url: args[:logo_url],
legal_name: args[:legal_name],
legal_number: args[:legal_number],
)

result.customer = customer
Expand All @@ -30,6 +55,19 @@ def update(**args)
return result.fail!('not_found') unless customer

customer.name = args[:name]
customer.country = args[:country]&.upcase
customer.address_line1 = args[:address_line1]
customer.address_line2 = args[:address_line2]
customer.state = args[:state]
customer.zipcode = args[:zipcode]
customer.email = args[:email]
customer.city = args[:city]
customer.url = args[:url]
customer.phone = args[:phone]
customer.logo_url = args[:logo_url]
customer.legal_name = args[:legal_name]
customer.legal_number = args[:legal_number]

# NOTE: Only name is editable if customer is attached to subscriptions
customer.customer_id = args[:customer_id] unless customer.attached_to_subscriptions?
customer.save!
Expand Down
13 changes: 13 additions & 0 deletions app/validators/country_code_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

class CountryCodeValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
record.errors.add(attribute, :country_code_invalid) unless valid?(value)
end

protected

def valid?(value)
value && ISO3166::Country.new(value)
end
end
20 changes: 20 additions & 0 deletions db/migrate/20220422122223_add_metadata_to_customers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

class AddMetadataToCustomers < ActiveRecord::Migration[7.0]
def change
change_table :customers do |t|
t.string :country
t.string :address_line1
t.string :address_line2
t.string :state
t.string :zipcode
t.string :email
t.string :city
t.string :url
t.string :phone
t.string :logo_url
t.string :legal_name
t.string :legal_number
end
end
end
15 changes: 13 additions & 2 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading