Class: CloverSandboxSimulator::Services::Clover::CustomerService

Inherits:
BaseService
  • Object
show all
Defined in:
lib/clover_sandbox_simulator/services/clover/customer_service.rb

Overview

Manages Clover customers

Constant Summary collapse

DEFAULT_CUSTOMERS =

Create sample customers if needed Default customer names for deterministic setup

[
  { first: "John", last: "Smith", phone: "555-100-0001" },
  { first: "Jane", last: "Doe", phone: "555-100-0002" },
  { first: "Bob", last: "Johnson", phone: "555-100-0003" },
  { first: "Alice", last: "Williams", phone: "555-100-0004" },
  { first: "Charlie", last: "Brown", phone: "555-100-0005" },
  { first: "Diana", last: "Davis", phone: "555-100-0006" },
  { first: "Eve", last: "Miller", phone: "555-100-0007" },
  { first: "Frank", last: "Wilson", phone: "555-100-0008" },
  { first: "Grace", last: "Moore", phone: "555-100-0009" },
  { first: "Henry", last: "Taylor", phone: "555-100-0010" }
].freeze

Instance Attribute Summary

Attributes inherited from BaseService

#config, #logger

Instance Method Summary collapse

Methods inherited from BaseService

#initialize

Constructor Details

This class inherits a constructor from CloverSandboxSimulator::Services::BaseService

Instance Method Details

#create_customer(first_name:, last_name:, email: nil, phone: nil) ⇒ Object

Create a customer



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/clover_sandbox_simulator/services/clover/customer_service.rb', line 25

def create_customer(first_name:, last_name:, email: nil, phone: nil)
  logger.info "Creating customer: #{first_name} #{last_name}"

  payload = {
    "firstName" => first_name,
    "lastName" => last_name
  }
  payload["emailAddresses"] = [{ "emailAddress" => email }] if email
  payload["phoneNumbers"] = [{ "phoneNumber" => phone }] if phone

  request(:post, endpoint("customers"), payload: payload)
end

#delete_customer(customer_id) ⇒ Object

Delete a customer



125
126
127
128
# File 'lib/clover_sandbox_simulator/services/clover/customer_service.rb', line 125

def delete_customer(customer_id)
  logger.info "Deleting customer: #{customer_id}"
  request(:delete, endpoint("customers/#{customer_id}"))
end

#ensure_customers(count: 10, deterministic: true) ⇒ Object

Create sample customers if needed

Parameters:

  • count (Integer) (defaults to: 10)

    Minimum number of customers to ensure exist

  • deterministic (Boolean) (defaults to: true)

    If true, uses predefined names for consistency



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/clover_sandbox_simulator/services/clover/customer_service.rb', line 56

def ensure_customers(count: 10, deterministic: true)
  existing = get_customers
  return existing if existing.size >= count

  needed = count - existing.size
  logger.info "Creating #{needed} sample customers..."

  new_customers = []
  needed.times do |i|
    if deterministic && i < DEFAULT_CUSTOMERS.size
      cust_data = DEFAULT_CUSTOMERS[existing.size + i] || DEFAULT_CUSTOMERS.last
      first = cust_data[:first]
      last = cust_data[:last]
      phone = cust_data[:phone]
    else
      first = Faker::Name.first_name
      last = Faker::Name.last_name
      phone = Faker::PhoneNumber.cell_phone
    end

    # Use example.com domain - Clover rejects .test domains
    # Remove special chars, collapse dots, strip leading/trailing dots
    safe_first = first.downcase.gsub(/[^a-z0-9]/, "")
    safe_last = last.downcase.gsub(/[^a-z0-9]/, "")
    customer = create_customer(
      first_name: first,
      last_name: last,
      email: "#{safe_first}.#{safe_last}@example.com",
      phone: phone
    )
    new_customers << customer if customer
  end

  existing + new_customers
end

#ensure_specific_customers(customer_list) ⇒ Array<Hash>

Ensure specific customers exist by name (idempotent)

Parameters:

  • customer_list (Array<Hash>)

    Array of { first_name:, last_name:, email:, phone: }

Returns:

  • (Array<Hash>)

    All customers (existing + created)



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/clover_sandbox_simulator/services/clover/customer_service.rb', line 95

def ensure_specific_customers(customer_list)
  existing = get_customers
  existing_emails = existing.map { |c| c["emailAddresses"]&.first&.dig("emailAddress")&.downcase }.compact

  new_customers = []
  customer_list.each do |cust_data|
    email = cust_data[:email]&.downcase
    next if email && existing_emails.include?(email)

    customer = create_customer(
      first_name: cust_data[:first_name],
      last_name: cust_data[:last_name],
      email: cust_data[:email],
      phone: cust_data[:phone]
    )
    new_customers << customer if customer
  end

  existing + new_customers
end

#get_customer(customer_id) ⇒ Object

Get a specific customer



20
21
22
# File 'lib/clover_sandbox_simulator/services/clover/customer_service.rb', line 20

def get_customer(customer_id)
  request(:get, endpoint("customers/#{customer_id}"))
end

#get_customersObject

Fetch all customers



11
12
13
14
15
16
17
# File 'lib/clover_sandbox_simulator/services/clover/customer_service.rb', line 11

def get_customers
  logger.info "Fetching customers..."
  response = request(:get, endpoint("customers"))
  elements = response&.dig("elements") || []
  logger.info "Found #{elements.size} customers"
  elements
end

#random_customerObject

Get a random customer (70% chance of returning a customer, 30% anonymous)



117
118
119
120
121
122
# File 'lib/clover_sandbox_simulator/services/clover/customer_service.rb', line 117

def random_customer
  return nil if rand < 0.3 # 30% anonymous orders

  customers = get_customers
  customers.sample
end