From 82f6a4c3f15c44c0b4170485ecd48bd1d908f100 Mon Sep 17 00:00:00 2001 From: Steve Chaloner Date: Mon, 16 Oct 2023 12:59:13 +0200 Subject: [PATCH] Allow for-sale config to be specified when events and seasons are created (#89) Co-authored-by: Steve Chaloner --- lib/seatsio/domain.rb | 24 ++++++++++++++++++------ lib/seatsio/events.rb | 17 ++++++++++++++--- lib/seatsio/seasons.rb | 16 +++++++++++++--- test/events/create_event_test.rb | 9 +++++++++ test/events/create_events_test.rb | 16 ++++++++++++++++ test/seasons/create_season_test.rb | 9 +++++++++ 6 files changed, 79 insertions(+), 12 deletions(-) diff --git a/lib/seatsio/domain.rb b/lib/seatsio/domain.rb index 3a5851b..b39490e 100644 --- a/lib/seatsio/domain.rb +++ b/lib/seatsio/domain.rb @@ -46,14 +46,26 @@ class ForSaleConfig attr_reader :for_sale, :objects, :area_places, :categories - def initialize(data) + def initialize(for_sale, objects = nil, area_places = nil, categories = nil) + @for_sale = for_sale + @objects = objects + @area_places = area_places + @categories = categories + end + + def self.from_json(data) if data - @for_sale = data['forSale'] - @objects = data['objects'] - @area_places = data['areaPlaces'] - @categories = data['categories'] + ForSaleConfig.new(data['forSale'], data['objects'], data['areaPlaces'], data['categories']) end end + + def == (other) + other != nil && + for_sale == other.for_sale && + objects == other.objects && + area_places == other.area_places && + categories == other.categories + end end class Category @@ -160,7 +172,7 @@ def initialize(data) @date = Date.iso8601(data['date']) if data['date'] @supports_best_available = data['supportsBestAvailable'] @table_booking_config = TableBookingConfig::from_json(data['tableBookingConfig']) - @for_sale_config = ForSaleConfig.new(data['forSaleConfig']) if data['forSaleConfig'] + @for_sale_config = ForSaleConfig::from_json(data['forSaleConfig']) if data['forSaleConfig'] @created_on = parse_date(data['createdOn']) @updated_on = parse_date(data['updatedOn']) @channels = data['channels'].map { diff --git a/lib/seatsio/events.rb b/lib/seatsio/events.rb index d29a481..3682d08 100644 --- a/lib/seatsio/events.rb +++ b/lib/seatsio/events.rb @@ -18,8 +18,8 @@ def initialize(http_client) @channels = ChannelsClient.new(@http_client) end - def create(chart_key: nil, event_key: nil, name: nil, date: nil, table_booking_config: nil, object_categories: nil, categories: nil, channels: nil) - payload = build_event_request(chart_key, event_key, name, date, table_booking_config, object_categories, categories, channels: channels, is_in_the_past: nil) + def create(chart_key: nil, event_key: nil, name: nil, date: nil, table_booking_config: nil, object_categories: nil, categories: nil, channels: nil, for_sale_config: nil) + payload = build_event_request(chart_key, event_key, name, date, table_booking_config, object_categories, categories, channels: channels, is_in_the_past: nil, for_sale_config: for_sale_config) response = @http_client.post("events", payload) Event.new(response) end @@ -159,7 +159,7 @@ def build_extra_data_request(extra_data) payload end - def build_event_request(chart_key, event_key, name, date, table_booking_config, object_categories, categories, channels: nil, is_in_the_past: nil) + def build_event_request(chart_key, event_key, name, date, table_booking_config, object_categories, categories, channels: nil, is_in_the_past: nil, for_sale_config: nil) result = {} result["chartKey"] = chart_key if chart_key result["eventKey"] = event_key if event_key @@ -170,6 +170,7 @@ def build_event_request(chart_key, event_key, name, date, table_booking_config, result["categories"] = categories_to_request(categories) if categories != nil result["channels"] = ChannelsClient::channels_to_request(channels) if channels != nil result["isInThePast"] = is_in_the_past if is_in_the_past != nil + result["forSaleConfig"] = for_sale_config_to_request(for_sale_config) if for_sale_config != nil result end @@ -191,6 +192,7 @@ def event_creation_params_to_request(params) r["objectCategories"] = param[:object_categories] if param[:object_categories] != nil r["categories"] = categories_to_request(param[:categories]) if param[:categories] != nil r["channels"] = ChannelsClient::channels_to_request(param[:channels]) if param[:channels] != nil + r["forSaleConfig"] = for_sale_config_to_request(param[:for_sale_config]) if param[:for_sale_config] != nil result.push(r) end result @@ -216,5 +218,14 @@ def categories_to_request(categories) result end + def for_sale_config_to_request(for_sale_config) + result = {} + result["forSale"] = for_sale_config.for_sale + result["objects"] = for_sale_config.objects if for_sale_config.objects != nil + result["areaPlaces"] = for_sale_config.area_places if for_sale_config.area_places != nil + result["categories"] = for_sale_config.categories if for_sale_config.categories != nil + result + end + end end diff --git a/lib/seatsio/seasons.rb b/lib/seatsio/seasons.rb index 5a3907e..1b4c299 100644 --- a/lib/seatsio/seasons.rb +++ b/lib/seatsio/seasons.rb @@ -14,8 +14,8 @@ def initialize(http_client, seatsio_client) end def create(chart_key:, key: nil, number_of_events: nil, event_keys: nil, - table_booking_config: nil, channels: nil) - request = build_create_season_request(chart_key, key, number_of_events, event_keys, table_booking_config, channels) + table_booking_config: nil, channels: nil, for_sale_config: nil) + request = build_create_season_request(chart_key, key, number_of_events, event_keys, table_booking_config, channels, for_sale_config) response = @http_client.post('seasons', request) Season.new(response) end @@ -54,7 +54,7 @@ def remove_event_from_partial_season(top_level_season_key:, partial_season_key:, private - def build_create_season_request(chart_key, key, number_of_events, event_keys, table_booking_config, channels) + def build_create_season_request(chart_key, key, number_of_events, event_keys, table_booking_config, channels, for_sale_config) request = {} request['chartKey'] = chart_key if chart_key request['key'] = key if key @@ -62,6 +62,7 @@ def build_create_season_request(chart_key, key, number_of_events, event_keys, ta request['eventKeys'] = event_keys if event_keys request['tableBookingConfig'] = table_booking_config_to_request(table_booking_config) if table_booking_config != nil request['channels'] = ChannelsClient::channels_to_request(channels) if channels != nil + request['forSaleConfig'] = for_sale_config_to_request(for_sale_config) if for_sale_config != nil request end @@ -72,5 +73,14 @@ def table_booking_config_to_request(table_booking_config) request end + def for_sale_config_to_request(for_sale_config) + result = {} + result["forSale"] = for_sale_config.for_sale + result["objects"] = for_sale_config.objects if for_sale_config.objects != nil + result["areaPlaces"] = for_sale_config.area_places if for_sale_config.area_places != nil + result["categories"] = for_sale_config.categories if for_sale_config.categories != nil + result + end + end end diff --git a/test/events/create_event_test.rb b/test/events/create_event_test.rb index 7808fa1..c2816e7 100644 --- a/test/events/create_event_test.rb +++ b/test/events/create_event_test.rb @@ -91,4 +91,13 @@ def test_channels assert_equal(channels, event.channels) end + + def test_for_sale_config + chart_key = create_test_chart + for_sale_config = Seatsio::ForSaleConfig.new(false, ["A-1"], {"GA1" => 3}, ["Cat1"]) + + event = @seatsio.events.create chart_key: chart_key, for_sale_config: for_sale_config + + assert_equal(for_sale_config, event.for_sale_config) + end end diff --git a/test/events/create_events_test.rb b/test/events/create_events_test.rb index 8c3c0b2..748e36d 100644 --- a/test/events/create_events_test.rb +++ b/test/events/create_events_test.rb @@ -130,4 +130,20 @@ def test_channels_can_be_passed_in assert_equal(channels, event.channels) end + def test_for_sale_config_can_be_passed_in + chart_key = create_test_chart + for_sale_config_1 = Seatsio::ForSaleConfig.new(false, ["A-1"], {"GA1" => 3}, ["Cat1"]) + for_sale_config_2 = Seatsio::ForSaleConfig.new(false, ["A-2"], {"GA1" => 7}, ["Cat1"]) + event_creation_params = [ + {:for_sale_config => for_sale_config_1}, + {:for_sale_config => for_sale_config_2} + ] + + events = @seatsio.events.create_multiple(key: chart_key, event_creation_params: event_creation_params) + + assert_equal(2, events.length) + assert_equal(for_sale_config_1, events[0].for_sale_config) + assert_equal(for_sale_config_2, events[1].for_sale_config) + end + end diff --git a/test/seasons/create_season_test.rb b/test/seasons/create_season_test.rb index caf39fe..ef1f02a 100644 --- a/test/seasons/create_season_test.rb +++ b/test/seasons/create_season_test.rb @@ -65,4 +65,13 @@ def test_channels_can_be_passed_in assert_equal(channels, season.channels) end + + def test_for_sale_config_can_be_passed_in + chart_key = create_test_chart + for_sale_config = Seatsio::ForSaleConfig.new(false, ["A-1"], {"GA1" => 3}, ["Cat1"]) + + season = @seatsio.seasons.create chart_key: chart_key, for_sale_config: for_sale_config + + assert_equal(for_sale_config, season.for_sale_config) + end end