Skip to content

Commit

Permalink
Merge pull request #120 from ignacio-chiazzo/wpp-business-vertical
Browse files Browse the repository at this point in the history
Validate vertical on business profile API
  • Loading branch information
ignacio-chiazzo authored Feb 4, 2024
2 parents 6bf0c98 + dcf04bd commit ea611a9
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Unreleased
- Validate Vertical on BusinessProfile update API. @ignacio-chiazzo https://github.com/ignacio-chiazzo/ruby_whatsapp_sdk/pull/120
- Added ability to specify fields param in the busines profile API. @ignacio-chiazzo https://github.com/ignacio-chiazzo/ruby_whatsapp_sdk/pull/119


# v 0.11.0
- Bumped API version to v19. @paulomcnally https://github.com/ignacio-chiazzo/ruby_whatsapp_sdk/pull/116

Expand Down
25 changes: 25 additions & 0 deletions lib/whatsapp_sdk/api/business_profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,27 @@

require_relative "request"
require_relative "response"
require_relative "../resource/business_profile"

module WhatsappSdk
module Api
class BusinessProfile < Request
DEFAULT_FIELDS = 'about,address,description,email,profile_picture_url,websites,vertical'

class InvalidVertical < StandardError
extend T::Sig

sig { returns(String) }
attr_accessor :message

sig { params(vertical: String).void }
def initialize(vertical:)
@message = "invalid vertical #{vertical}. See the supported types in the official documentation " \
"https://developers.facebook.com/docs/whatsapp/cloud-api/reference/business-profiles"
super
end
end

# Get the details of business profile.
#
# @param phone_number_id [Integer] Phone Number Id.
Expand Down Expand Up @@ -45,6 +60,7 @@ def details(phone_number_id, fields: nil)
def update(phone_number_id:, params:)
# this is a required field
params[:messaging_product] = 'whatsapp'
return raise InvalidVertical.new(vertical: params[:vertical]) unless valid_vertical?(params)

response = send_request(
http_method: "post",
Expand All @@ -57,6 +73,15 @@ def update(phone_number_id:, params:)
data_class_type: Api::Responses::SuccessResponse
)
end

private

sig { params(params: T::Hash[T.untyped, T.untyped]).returns(T::Boolean) }
def valid_vertical?(params)
return true unless params[:vertical]

WhatsappSdk::Resource::BusinessProfile::VERTICAL_VALUES.include?(params[:vertical])
end
end
end
end
15 changes: 15 additions & 0 deletions lib/whatsapp_sdk/resource/business_profile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# typed: strict
# frozen_string_literal: true

module WhatsappSdk
module Resource
class BusinessProfile
extend T::Sig

VERTICAL_VALUES = %w[
UNDEFINED OTHER AUTO BEAUTY APPAREL EDU ENTERTAIN EVENT_PLAN FINANCE GROCERY
GOVT HOTEL HEALTH NONPROFIT PROF_SERVICES RETAIL TRAVEL RESTAURANT NOT_A_BIZ
].freeze
end
end
end
16 changes: 16 additions & 0 deletions test/whatsapp/api/business_profile_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,28 @@ def test_details_with_success_response
assert_predicate(response, :ok?)
end

def test_update_returns_an_error_if_vertical_is_invalid
params = { vertical: "invalid_vertical" }
mock_business_profile_response(valid_details_response)
assert_raises(BusinessProfile::InvalidVertical) do
@business_profile_api.update(phone_number_id: 123_123, params: params)
end
end

def test_update_handles_error_response
mocked_error_response = mock_error_response(api: @business_profile_api)
response = @business_profile_api.update(phone_number_id: 123_123, params: valid_detail_response)
assert_mock_error_response(mocked_error_response, response, Responses::MessageErrorResponse)
end

def test_update_does_not_return_an_error_if_vertical_is_valid
params = { vertical: "BEAUTY" }
mock_business_profile_response(valid_update_response)
response = @business_profile_api.update(phone_number_id: 123_123, params: params)
assert_update_details_mock_response(valid_update_response, response)
assert_predicate(response, :ok?)
end

def test_update_with_success_response
mock_business_profile_response(valid_update_response)
response = @business_profile_api.update(phone_number_id: 123_123, params: valid_detail_response)
Expand Down

0 comments on commit ea611a9

Please sign in to comment.