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(cp-improv): Customer Portal expose and edit customer info #2599

Merged
merged 3 commits into from
Sep 26, 2024
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
24 changes: 24 additions & 0 deletions app/graphql/mutations/customer_portal/update_customer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

module Mutations
module CustomerPortal
class UpdateCustomer < BaseMutation
include AuthenticableCustomerPortalUser

graphql_name "UpdateCustomerPortalCustomer"
description "Update customer data from Customer Portal"

input_object_class Types::CustomerPortal::Customers::UpdateInput
type Types::CustomerPortal::Customers::Object

def resolve(**args)
result = ::CustomerPortal::CustomerUpdateService.call(
customer: context[:customer_portal_user],
args:
)

result.success? ? result.customer : result_error(result)
end
end
end
end
4 changes: 2 additions & 2 deletions app/graphql/resolvers/customer_portal/customer_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ module CustomerPortal
class CustomerResolver < Resolvers::BaseResolver
include AuthenticableCustomerPortalUser

description 'Query a customer portal user'
description "Query a customer portal user"

type Types::Customers::Object, null: true
type Types::CustomerPortal::Customers::Object, null: true

def resolve
context[:customer_portal_user]
Expand Down
42 changes: 42 additions & 0 deletions app/graphql/types/customer_portal/customers/object.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

module Types
module CustomerPortal
module Customers
class Object < Types::BaseObject
ancorcruz marked this conversation as resolved.
Show resolved Hide resolved
graphql_name "CustomerPortalCustomer"

field :id, ID, null: false

field :currency, Types::CurrencyEnum, null: true
field :display_name, String, null: false
field :email, String, null: true
field :firstname, String
field :lastname, String
field :legal_name, String, null: true
field :legal_number, String, null: true
field :name, String
field :tax_identification_number, String, null: true

field :billing_configuration, Types::Customers::BillingConfiguration, null: true

# Billing address
field :address_line1, String, null: true
field :address_line2, String, null: true
field :city, String, null: true
field :country, Types::CountryCodeEnum, null: true
field :state, String, null: true
field :zipcode, String, null: true

field :shipping_address, Types::Customers::Address, null: true

def billing_configuration
{
id: "#{object&.id}-c0nf",
ancorcruz marked this conversation as resolved.
Show resolved Hide resolved
document_locale: object&.document_locale
}
end
end
end
end
end
30 changes: 30 additions & 0 deletions app/graphql/types/customer_portal/customers/update_input.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

module Types
module CustomerPortal
module Customers
class UpdateInput < BaseInputObject
graphql_name "UpdateCustomerPortalCustomerInput"
description "Customer Portal Customer Update input arguments"

argument :document_locale, String, required: false
argument :email, String, required: false
argument :firstname, String, required: false
argument :lastname, String, required: false
argument :legal_name, String, required: false
argument :name, String, required: false
argument :tax_identification_number, String, required: false

# Billing address
argument :address_line1, String, required: false
argument :address_line2, String, required: false
argument :city, String, required: false
argument :country, Types::CountryCodeEnum, required: false
argument :state, String, required: false
argument :zipcode, String, required: false

argument :shipping_address, Types::Customers::AddressInput, required: false
end
end
end
end
1 change: 1 addition & 0 deletions app/graphql/types/mutation_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class MutationType < Types::BaseObject

field :download_customer_portal_invoice, mutation: Mutations::CustomerPortal::DownloadInvoice
field :generate_customer_portal_url, mutation: Mutations::CustomerPortal::GenerateUrl
field :update_customer_portal_customer, mutation: Mutations::CustomerPortal::UpdateCustomer

field :create_invoices_data_export, mutation: Mutations::DataExports::Invoices::Create

Expand Down
67 changes: 67 additions & 0 deletions app/services/customer_portal/customer_update_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# frozen_string_literal: true

module CustomerPortal
class CustomerUpdateService < BaseService
def initialize(customer:, args:)
@customer = customer
@args = args

super
end

def call
return result.not_found_failure!(resource: "customer") unless customer

ActiveRecord::Base.transaction do
customer.name = args[:name] if args.key?(:name)
customer.firstname = args[:firstname] if args.key?(:firstname)
customer.lastname = args[:lastname] if args.key?(:lastname)
customer.legal_name = args[:legal_name] if args.key?(:legal_name)
customer.tax_identification_number = args[:tax_identification_number] if args.key?(:tax_identification_number)
customer.email = args[:email] if args.key?(:email)

customer.document_locale = args[:document_locale] if args.key?(:document_locale)

customer.address_line1 = args[:address_line1] if args.key?(:address_line1)
customer.address_line2 = args[:address_line2] if args.key?(:address_line2)
customer.zipcode = args[:zipcode] if args.key?(:zipcode)
customer.city = args[:city] if args.key?(:city)
customer.state = args[:state] if args.key?(:state)
customer.country = args[:country]&.upcase if args.key?(:country)

shipping_address = args[:shipping_address]&.to_h || {}
customer.shipping_address_line1 = shipping_address[:address_line1] if shipping_address.key?(:address_line1)
customer.shipping_address_line2 = shipping_address[:address_line2] if shipping_address.key?(:address_line2)
customer.shipping_zipcode = shipping_address[:zipcode] if shipping_address.key?(:zipcode)
customer.shipping_city = shipping_address[:city] if shipping_address.key?(:city)
customer.shipping_state = shipping_address[:state] if shipping_address.key?(:state)
customer.shipping_country = shipping_address[:country]&.upcase if shipping_address.key?(:country)

customer.save!
customer.reload

tax_codes = []
if customer.organization.eu_tax_management
# This service does not return a 'result' object but a string
tax_codes << Customers::EuAutoTaxesService.call(customer:)
ancorcruz marked this conversation as resolved.
Show resolved Hide resolved
end

if tax_codes.present?
taxes_result = Customers::ApplyTaxesService.call(customer:, tax_codes:)
taxes_result.raise_if_error!
end
end

result.customer = customer
result
rescue ActiveRecord::RecordInvalid => e
result.record_validation_failure!(record: e.record)
rescue BaseService::FailedResult => e
e.result
end

private

attr_reader :customer, :args
end
end
58 changes: 57 additions & 1 deletion schema.graphql

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

Loading