Class: Bistrochat::Blockages::Create

Inherits:
Object
  • Object
show all
Includes:
ApiHelpers, DefaultErrorContainer, ElasticAPM::SpanHelpers
Defined in:
app/services/bistrochat/blockages/create.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DefaultErrorContainer

#error, #error_message_simple, #merge_errors

Methods included from ApiHelpers

#api_call, #channel, #find_bistrochat_vendor_application, #get_purpose, #headers, #parse_phone_number, #parse_user_or_guest_full_name, #parsed_vendor_name, #reservation_start_at, #send_error_availability_notification_to_staff

Constructor Details

#initialize(assignable_id, params = {}, assignable_type = :reservation) ⇒ Create

Returns a new instance of Create.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/services/bistrochat/blockages/create.rb', line 12

def initialize(assignable_id, params = {}, assignable_type = :reservation)
  BUSINESS_LOGGER.set_business_context({ assignable_id: assignable_id, assignable_type: assignable_type })
  BUSINESS_LOGGER.set_request_context(params)

  if assignable_type == :restaurant
    @restaurant = Restaurant.find_by(id: assignable_id) if assignable_id.present?
    @date = params[:date]
    @start_time = params[:start_time]
    @adult = params[:adult].to_i
    @kids = params[:kids].to_i
  else
    reservation = Reservation.find_by(id: assignable_id).decorate
    @restaurant = reservation.restaurant
    @date = reservation.date
    @start_time = reservation.start_time_format
    @adult = reservation.adult
    @kids = reservation.kids
  end
end

Instance Attribute Details

#adultObject (readonly)

Returns the value of attribute adult.



10
11
12
# File 'app/services/bistrochat/blockages/create.rb', line 10

def adult
  @adult
end

#dateObject (readonly)

Returns the value of attribute date.



10
11
12
# File 'app/services/bistrochat/blockages/create.rb', line 10

def date
  @date
end

#kidsObject (readonly)

Returns the value of attribute kids.



10
11
12
# File 'app/services/bistrochat/blockages/create.rb', line 10

def kids
  @kids
end

#restaurantObject (readonly)

Returns the value of attribute restaurant.



10
11
12
# File 'app/services/bistrochat/blockages/create.rb', line 10

def restaurant
  @restaurant
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



10
11
12
# File 'app/services/bistrochat/blockages/create.rb', line 10

def start_time
  @start_time
end

Instance Method Details

#executeObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/services/bistrochat/blockages/create.rb', line 32

def execute
  errors.clear

  # Check if restaurant is using Bistrochat inventory source
  inv_source = restaurant.inventory_source&.inv_source
  if inv_source.blank? || inv_source != ApiVendorV1::Constants::BISTROCHAT_INV_SOURCE_NAME
    error_message = "Restaurant #{restaurant.id} is not using Bistrochat inventory source"
    unless Bistrochat::ErrorMessages::KNOWN_ERRORS.include?(error_message)
      APMErrorHandler.report("BISTROCHAT: #{self.class} #{error_message}", restaurant_id: restaurant.id)
    end
    errors.add(:base, error_message)

    return ServiceResult.new errors: errors, message: error_message
  end

  shop_id = restaurant&.bistrochat_restaurant&.shop_id
  shop_slug = restaurant&.bistrochat_restaurant&.shop_slug
  if shop_id.blank? || shop_slug.blank?
    error_message = "Invalid shop id: #{shop_id} or shop slug: #{shop_slug} of restaurant #{restaurant.id}"
    unless Bistrochat::ErrorMessages::KNOWN_ERRORS.include?(error_message)
      APMErrorHandler.report("BISTROCHAT: #{self.class} #{error_message}", restaurant_id: restaurant.id)
    end
    errors.add(:base, error_message)

    return ServiceResult.new errors: errors, message: error_message
  end

  # Create blockage in Bistrochat
  result = create_blockage(blockage_params)
  unless result.success?
    error_message = result.message
    BUSINESS_LOGGER.error('BISTROCHAT: Failed to create blockage', error_message: error_message)
    errors.add(:base, error_message)

    return ServiceResult.new errors: errors, message: error_message
  end

  BUSINESS_LOGGER.info('BISTROCHAT: Blockage created successfully', blockage: result.data)
  ServiceResult.new data: result.data
end