Class: Booker::V41::Merchant

Inherits:
Client
  • Object
show all
Includes:
RequestHelper
Defined in:
lib/booker/v4.1/merchant.rb

Constant Summary collapse

V41_PREFIX =
'/v4.1/merchant'
V41_LOCATION_PREFIX =
"#{V41_PREFIX}/location"
V41_APPOINTMENTS_PREFIX =
"#{V41_PREFIX}/appointments"
API_METHODS =
{
  appointments: "#{V41_APPOINTMENTS_PREFIX}".freeze,
  appointments_partial: "#{V41_APPOINTMENTS_PREFIX}/partial".freeze,
  appointment_confirm: "#{V41_PREFIX}/appointment/confirm".freeze,
  customers: "#{V41_PREFIX}/customers".freeze,
  create_special: "#{V41_PREFIX}/special".freeze,
  employees: "#{V41_PREFIX}/employees".freeze,
  treatments: "#{V41_PREFIX}/treatments".freeze,
}.freeze

Constants included from RequestHelper

RequestHelper::DEFAULT_PAGINATION_PARAMS

Constants inherited from Client

Client::API_GATEWAY_ERRORS, Client::BOOKER_SERVER_TIMEZONE, Client::CLIENT_CREDENTIALS_GRANT_TYPE, Client::CREATE_TOKEN_CONTENT_TYPE, Client::CREATE_TOKEN_PATH, Client::DEFAULT_AUTH_BASE_URL, Client::DEFAULT_BASE_URL, Client::DEFAULT_CONTENT_TYPE, Client::ENV_BASE_URL_KEY, Client::REFRESH_TOKEN_GRANT_TYPE, Client::UPDATE_TOKEN_CONTEXT_PATH, Client::VALID_ACCESS_TOKEN_SCOPES

Instance Attribute Summary

Attributes inherited from Client

#access_token_scope, #api_subscription_key, #auth_base_url, #auth_with_client_credentials, #base_url, #client_id, #client_secret, #location_id, #refresh_token, #temp_access_token, #temp_access_token_expires_at, #token_store, #token_store_callback_method

Instance Method Summary collapse

Methods included from RequestHelper

#build_params

Methods inherited from Client

#access_token, #access_token_response, #delete, #full_url, #get, #get_access_token, #get_base_url, #get_booker_resources, #get_location_access_token, #handle_errors!, #initialize, #paginated_request, #patch, #post, #put, #update_token_store

Constructor Details

This class inherits a constructor from Booker::Client

Instance Method Details

#appointments(location_id:, start_date:, end_date:, fetch_all: true, params: {}) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/booker/v4.1/merchant.rb', line 90

def appointments(location_id:, start_date:, end_date:, fetch_all: true, params: {})
  additional_params = {
    LocationID: location_id,
    FromStartDate: start_date.to_date,
    ToStartDate: end_date.to_date
  }

  paginated_request(
    method: :post,
    path: API_METHODS[:appointments],
    params: build_params(additional_params, params, true),
    model: Booker::V4::Models::Appointment,
    fetch_all: fetch_all
  )
end

#appointments_partial(location_id:, start_date:, end_date:, fetch_all: true, params: {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/booker/v4.1/merchant.rb', line 49

def appointments_partial(location_id:, start_date:, end_date:, fetch_all: true, params: {})
  additional_params = {
    LocationID: location_id,
    FromStartDate: start_date.to_date,
    ToStartDate: end_date.to_date
  }

  paginated_request(
    method: :post,
    path: API_METHODS[:appointments_partial],
    params: build_params(additional_params, params, true),
    model: Booker::V4::Models::Appointment,
    fetch_all: fetch_all
  )
end

#confirm_appointment(appointment_id:) ⇒ Object



45
46
47
# File 'lib/booker/v4.1/merchant.rb', line 45

def confirm_appointment(appointment_id:)
  put API_METHODS[:appointment_confirm], build_params(ID: appointment_id), Booker::V4::Models::Appointment
end

#create_special(location_id:, start_date:, end_date:, coupon_code:, name:, params: {}) ⇒ Object



148
149
150
151
152
153
154
155
156
# File 'lib/booker/v4.1/merchant.rb', line 148

def create_special(location_id:, start_date:, end_date:, coupon_code:, name:, params: {})
  post(API_METHODS[:create_special], build_params({
    LocationID: location_id,
    ApplicableStartDate: start_date.in_time_zone,
    ApplicableEndDate: end_date.in_time_zone,
    CouponCode: coupon_code,
    Name: name
  }, params))
end

#customer(id:, params: {}, model: Booker::V4::Models::Customer) ⇒ Object



122
123
124
125
126
127
128
# File 'lib/booker/v4.1/merchant.rb', line 122

def customer(id:, params: {}, model: Booker::V4::Models::Customer)
  additional_params = {
    LoadUnpaidAppointments: false,
    includeFieldValues: false
  }
  get("#{V41_PREFIX}/customer/#{id}", build_params(additional_params, params), model)
end

#customers(location_id:, fetch_all: true, params: {}) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/booker/v4.1/merchant.rb', line 106

def customers(location_id:, fetch_all: true, params: {})
  additional_params = {
    FilterByExactLocationID: true,
    LocationID: location_id,
    CustomerRecordType: 1,
  }

  paginated_request(
    method: :post,
    path: API_METHODS[:customers],
    params: build_params(additional_params, params, true),
    model: Booker::V4::Models::Customer,
    fetch_all: fetch_all
  )
end

#employees(location_id:, fetch_all: true, params: {}) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/booker/v4.1/merchant.rb', line 65

def employees(location_id:, fetch_all: true, params: {})
  paginated_request(
    method: :post,
    path: API_METHODS[:employees],
    params: build_params({LocationID: location_id}, params, true),
    model: Booker::V4::Models::Employee,
    fetch_all: fetch_all
  )
end

#extract_default_customer_fields(customer_attributes) ⇒ Object



144
145
146
# File 'lib/booker/v4.1/merchant.rb', line 144

def extract_default_customer_fields(customer_attributes)
  customer_attributes.slice("Email", "FirstName", "LastName", "HomePhone", "WorkPhone", "CellPhone")
end

#location(id:) ⇒ Object



85
86
87
88
# File 'lib/booker/v4.1/merchant.rb', line 85

def location(id:)
  response = get("#{V41_LOCATION_PREFIX}/#{id}", build_params)
  Booker::V4::Models::Location.from_hash(response)
end

#location_day_schedules(location_id:, params: {}) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/booker/v4.1/merchant.rb', line 30

def location_day_schedules(location_id:, params: {})
  # Booker requires fromDate and toDate for JSON API, but does not use them when getDefaultDaySchedule is true
  # So datetime used for these fields does not matter
  random_datetime = Booker::V4::Models::Model.time_to_booker_datetime(Time.now)

  additional_params = {getDefaultDaySchedule: true, fromDate: random_datetime, toDate: random_datetime}
  response = get("#{V41_LOCATION_PREFIX}/#{location_id}/schedule", build_params(additional_params, params))
  response['LocationDaySchedules'].map { |sched| Booker::V4::Models::LocationDaySchedule.from_hash(sched) }
end

#location_feature_settings(location_id:) ⇒ Object



25
26
27
28
# File 'lib/booker/v4.1/merchant.rb', line 25

def location_feature_settings(location_id:)
  response = get "#{V41_LOCATION_PREFIX}/#{location_id}/feature_settings", build_params
  Booker::V4::Models::FeatureSettings.from_hash response['FeatureSettings']
end

#online_booking_settings(location_id:) ⇒ Object



19
20
21
22
23
# File 'lib/booker/v4.1/merchant.rb', line 19

def online_booking_settings(location_id:)
  path = "#{V41_LOCATION_PREFIX}/#{location_id}/online_booking_settings"
  response = get path, build_params
  Booker::V4::Models::OnlineBookingSettings.from_hash(response['OnlineBookingSettings'])
end

#treatments(location_id:, fetch_all: true, params: {}) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/booker/v4.1/merchant.rb', line 75

def treatments(location_id:, fetch_all: true, params: {})
  paginated_request(
    method: :post,
    path: API_METHODS[:treatments],
    params: build_params({ LocationID: location_id }, params, true),
    model: Booker::V4::Models::Treatment,
    fetch_all: fetch_all
  )
end

#update_customer(id:, update_params: {}) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/booker/v4.1/merchant.rb', line 130

def update_customer(id:, update_params: {})
  # get a raw json response because we need to send all fields back with modifications
  customer_response = customer(id: id, model: nil)

  if customer_response.present? && customer = customer_response["Customer"]
    # extract the minimum required fields to send back
    customer["Customer"] = extract_default_customer_fields(customer["Customer"])
    customer["Customer"].merge!(update_params)
    customer["LocationID"] = self.location_id
    put("#{V41_PREFIX}/customer/#{id}", build_params(customer))
  end

end

#update_location_notification_settings(location_id:, send_appointment_reminders:) ⇒ Object



40
41
42
43
# File 'lib/booker/v4.1/merchant.rb', line 40

def update_location_notification_settings(location_id:, send_appointment_reminders:)
  params = build_params({NotificationSettings: { SendAppointmentReminders: send_appointment_reminders } })
  put "#{V41_LOCATION_PREFIX}/#{location_id}/notification_settings", params
end