Skip to content
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
155 changes: 155 additions & 0 deletions lib/beyond_api/services/customer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
# frozen_string_literal: true

module BeyondApi
# @example How to instantiate a client
# @client = BeyondApi::Customer.new(api_url: 'https://example.com/api', access_token: 'your_token')
class Customer < BaseService
# Retrieve the details of a customer.
#
# @see https://developer.epages.com/beyond-docs/#show_customer_details
#
# @param id [String] the customer UUID
#
# @return [Hash]
#
# @example
# @client.find('5afcf0ce-4e39-4b4d-88bb-82d5cc07c83a')
def find(id)
get("customers/#{id}")
end

# List all customers of the shop in a paged manner.
#
# @see https://developer.epages.com/beyond-docs/#list_customers
#
# @option params [Boolean] :paginated
# @option params [Integer] :size the page size
# @option params [Integer] :page the page number
#
# @return [Hash]
#
# @example
# @client.all(size: 100, page: 0)
def all(params = {})
fetch_all_pages('customers', params)
end

# Create a `COCKPIT` customer.
#
# @see https://developer.epages.com/beyond-docs/#create_customer
#
# @param body [Hash] the request body
#
# @return [Hash]
#
# @example
# customer_data = {
# billing_address: {
# gender: "FEMALE",
# company: "Astrid Alster GmbH",
# first_name: "Astrid",
# last_name: "Alster",
# street: "Alsterwasserweg",
# house_number: "2",
# postal_code: "20999",
# city: "Alsterwasser",
# country: "DE",
# state: "Hamburg",
# email: "a.alsterh@example.com",
# phone: "(800) 555-0102"
# },
# email: "a.alsterh@example.com",
# customer_comment: "A reliable customer",
# shipping_address: {
# company: "ePages GmbH",
# first_name: "Chayanne",
# last_name: "Team42",
# street: "Pilatuspool",
# house_number: "2",
# postal_code: "20999",
# city: "Alsterwasser",
# country: "DE",
# state: "Hamburg",
# email: "a.chayanne@example.com",
# phone: "(800) 555-0102"
# }
# }
# @client.create(customer_data)
def create(body)
post('customers', body)
end

# Update a customer.
#
# @see https://developer.epages.com/beyond-docs/#update_customer
#
# @param body [Hash] the request body
#
# @return [Hash]
#
# @example
# customer_data = {
# billing_address: {
# company: "ePages GmbH",
# first_name: "Chayanne",
# last_name: "Team42",
# street: "Pilatuspool",
# house_number: "2",
# postal_code: "20999",
# city: "Alsterwasser",
# country: "DE",
# state: "Hamburg",
# email: "a.chayanne@example.com",
# phone: "(800) 555-0102"
# },
# email: "a.alsterh@example.com",
# customer_comment: "A reliable customer",
# shipping_address: {
# gender: "FEMALE",
# company: "Astrid Alster GmbH",
# first_name: "Astrid",
# last_name: "Alster",
# street: "Alsterwasserweg",
# house_number: "2",
# postal_code: "20999",
# city: "Alsterwasser",
# country: "DE",
# state: "Hamburg",
# email: "a.alsterh@example.com",
# phone: "(800) 555-0102"
# }
# }
# @client.update('2bdc787e-4643-4eeb-babf-06bc1d4b1c1b', customer_data)
def update(id, body)
put("customers/#{id}", body)
end

# Delete a customer.
#
# @see https://developer.epages.com/beyond-docs/#delete_customer
#
# @param id [String] the customer UUID
#
# @return [Hash] an empty hash
#
# @example
# @client.delete('71b62b1d-271a-4b56-9514-77b79f8e910a')
def delete(id)
super("customers/#{id}") # Concerns::Connection delete method
end

# List all events of a customer.
#
# @see https://developer.epages.com/beyond-docs/#list_customer_events
#
# @param id [String] the customer UUID
#
# @return [Hash]
#
# @example
# @client.events('df184f00-2367-417f-bc23-c7f927dbf636')
def events(id)
get("customers/#{id}/events")
end
end
end
75 changes: 75 additions & 0 deletions spec/beyond_api/services/customer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# frozen_string_literal: true

RSpec.describe BeyondApi::Customer, vcr: true do
let(:client) { described_class.new(api_url: ENV.fetch('API_URL', nil), access_token: beyond_access_token) }

describe '.all' do
it 'returns all customers' do
response = client.all

expect(response).not_to be nil
expect(response.dig(:embedded, :customers)).to be_kind_of(Array)
expect(response[:page]).to be_kind_of(Hash)
end
end

context 'with customer' do
before(:each) do
@customer = client.create(build(:customer_data))
end

describe '.create' do
it 'creates a new customer' do
expect(@customer).not_to be nil
expect(@customer.dig(:default_billing_address, :company)).to eq('ePages GmbH')
expect(@customer.dig(:default_billing_address, :first_name)).to eq('Chayanne')
expect(@customer.dig(:default_billing_address, :last_name)).to eq('Team42')
expect(@customer.dig(:default_billing_address, :street)).to eq('Pilatuspool')
expect(@customer.dig(:default_billing_address, :house_number)).to eq('2')
expect(@customer.dig(:default_billing_address, :postal_code)).to eq('20999')
expect(@customer.dig(:default_billing_address, :city)).to eq('Alsterwasser')
expect(@customer.dig(:default_billing_address, :country)).to eq('DE')
expect(@customer.dig(:default_billing_address, :state)).to eq('Hamburg')
expect(@customer.dig(:default_billing_address, :email)).to eq('chayanne@example.com')
expect(@customer.dig(:default_billing_address, :phone)).to eq('(800) 555-0102')
end
end

describe '.find' do
it 'returns a customer' do
response = client.find(@customer[:id])
expect(response.dig(:default_billing_address, :first_name)).to eq('Chayanne')
end
end

describe '.update' do
it 'updates an existing customer' do
updated_customer_data = FactoryBot.build(:customer_data, :berlin_shipping)

updated_customer = client.update(@customer[:id], updated_customer_data)
expect(updated_customer).not_to be nil
expect(updated_customer.dig(:default_shipping_address, :company)).to eq('Updated GmbH')
end
end

describe '.events' do
it 'returns all events for a customer' do
response = client.events(@customer[:id])
expect(response).not_to be nil
expect(response.dig(:embedded, :customer_events)).to be_kind_of(Array)
end
end

describe '.delete' do
it 'deletes a customer' do
response = client.delete(@customer[:id])
expect(response).to eq({})
end
end

after(:each) do
client.delete(@customer[:id])
rescue BeyondApi::Error # rubocop:disable Lint/SuppressedException
end
end
end
62 changes: 62 additions & 0 deletions spec/factories/customer_data.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# frozen_string_literal: true

FactoryBot.define do
factory :customer_data, class: Hash do
email { 'chayanne@example.com' }
customer_comment { 'A reliable customer' }

billing_address do
{
company: 'ePages GmbH',
first_name: 'Chayanne',
last_name: 'Team42',
street: 'Pilatuspool',
house_number: '2',
postal_code: '20999',
city: 'Alsterwasser',
country: 'DE',
state: 'Hamburg',
email: 'chayanne@example.com',
phone: '(800) 555-0102'
}
end

shipping_address do
{
gender: 'FEMALE',
company: 'Astrid Alster GmbH',
first_name: 'Astrid',
last_name: 'Alster',
street: 'Alsterwasserweg',
house_number: '2',
postal_code: '20999',
city: 'Alsterwasser',
country: 'DE',
state: 'Hamburg',
email: 'alsterh@example.com',
phone: '(800) 555-0102'
}
end

trait :berlin_shipping do
shipping_address do
{
gender: 'FEMALE',
company: 'Updated GmbH',
first_name: 'Astrid',
last_name: 'Alster',
street: 'Berlin Street',
house_number: '42',
postal_code: '12345',
city: 'Berlin',
country: 'DE',
state: 'Berlin',
email: 'alsterh@example.com',
phone: '(800) 555-0102'
}
end
end

initialize_with { attributes }
end
end
Loading