Class: Bistrochat::Blockages::Update

Inherits:
Object
  • Object
show all
Includes:
ApiHelpers, DefaultErrorContainer, ElasticAPM::SpanHelpers
Defined in:
app/services/bistrochat/blockages/update.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(blockage_id, reservation_id) ⇒ Update

Returns a new instance of Update.



12
13
14
15
16
# File 'app/services/bistrochat/blockages/update.rb', line 12

def initialize(blockage_id, reservation_id)
  @reservation = Reservation.find(reservation_id).decorate
  @blockage_id = blockage_id
  BUSINESS_LOGGER.set_business_context(reservation_id: reservation_id, blockage_id: blockage_id)
end

Instance Attribute Details

#blockage_idObject (readonly)

Returns the value of attribute blockage_id.



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

def blockage_id
  @blockage_id
end

#reservationObject (readonly)

Returns the value of attribute reservation.



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

def reservation
  @reservation
end

Instance Method Details

#executeObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/services/bistrochat/blockages/update.rb', line 18

def execute
  errors.clear

  error_message = Bistrochat::ErrorMessages::DEFAULT_ERROR
  # Check if reservation is blank
  if reservation.blank?
    error_message = 'Reservation is blank'
    APMErrorHandler.report("BISTROCHAT: #{self.class} #{error_message}") unless Bistrochat::ErrorMessages::KNOWN_ERRORS.include?(error_message)
    errors.add(:base, error_message)

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

  # check if blockage_id is not expired
  if blockage_id.present?
    blockage_is_valid = validate_blockage
    unless blockage_is_valid.success?
      error_message = blockage_is_valid.message
      errors.add(:base, error_message)

      BUSINESS_LOGGER.error('BISTROCHAT: Blockage is present but not valid', error_message: error_message)
      return ServiceResult.new errors: errors, message: error_message
    end
  end

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

    BUSINESS_LOGGER.error('BISTROCHAT: Restaurant is not using Bistrochat inventory source',
                          error_message: error_message)
    return ServiceResult.new errors: errors, message: error_message
  end

  shop_id = reservation.restaurant&.bistrochat_restaurant&.shop_id
  shop_slug = reservation.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 #{reservation.restaurant_id}"
    unless Bistrochat::ErrorMessages::KNOWN_ERRORS.include?(error_message)
      APMErrorHandler.report("BISTROCHAT: #{self.class} #{error_message}", reservation_id: reservation.id,
                                                                           restaurant_id: reservation.restaurant_id)
    end
    errors.add(:base, error_message)

    BUSINESS_LOGGER.error('BISTROCHAT: Invalid shop id or shop slug', error_message: error_message)
    return ServiceResult.new errors: errors, message: error_message
  end

  # Update blockage in Bistrochat
  result = update_blockage(blockage_params)
  unless result.success?
    error_message = result.message
    errors.add(:base, error_message)

    BUSINESS_LOGGER.error('BISTROCHAT: Failed to update blockage', error_message: error_message)
    return ServiceResult.new errors: errors, message: error_message
  end

  BUSINESS_LOGGER.info('BISTROCHAT: Blockage updated successfully',
                       blockage_id: blockage_id,
                       blockage_params: blockage_params,
                       data: result.data)
  ServiceResult.new data: result.data
end