Class: RestaurantService::CreateFromApi

Inherits:
ApplicationService show all
Includes:
ElasticAPM::SpanHelpers
Defined in:
app/services/restaurant_service/create_from_api.rb

Constant Summary collapse

PARKING_ENABLED =

Constants for parking virtual attribute

1
PARKING_DISABLED =
0

Instance Attribute Summary collapse

Attributes inherited from ApplicationService

#object

Instance Method Summary collapse

Methods inherited from ApplicationService

#execute

Constructor Details

#initialize(params:) ⇒ CreateFromApi

Returns a new instance of CreateFromApi.



13
14
15
# File 'app/services/restaurant_service/create_from_api.rb', line 13

def initialize(params:)
  @params = params
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



11
12
13
# File 'app/services/restaurant_service/create_from_api.rb', line 11

def params
  @params
end

Instance Method Details

#execute!Object



17
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
# File 'app/services/restaurant_service/create_from_api.rb', line 17

def execute!
  restaurant = nil
  staff = nil

  ActiveRecord::Base.transaction do
    restaurant = create_restaurant!
    assign_tags!(restaurant)

    # Create staff account automatically from owner credentials
    # The owner becomes the first staff member with owner role
    # If staff creation fails, entire transaction (including restaurant) will be rolled back
    staff = begin
      create_staff_from_owner!(restaurant)
    rescue ActiveRecord::RecordInvalid => e
      # Report staff-specific validation errors with context
      APMErrorHandler.report(e, context: {
                               restaurant_id: restaurant.id,
                               owner_id: restaurant.owner.id,
                               owner_email: restaurant.owner.email,
                             })
      raise ActiveRecord::RecordInvalid.new(e.record), "Staff account creation failed: #{e.message}"
    rescue StandardError => e
      # Report other staff creation errors
      APMErrorHandler.report(e, context: {
                               restaurant_id: restaurant.id,
                               owner_id: restaurant.owner.id,
                               owner_email: restaurant.owner.email,
                             })
      raise
    end

    restaurant.touch
  end

  ServiceResult.new(
    data: {
      restaurant: restaurant,
      owner: restaurant.owner,
      staff: staff,
    },
    message: 'Restaurant created successfully',
  )
rescue StandardError => e
  HH_LOGGER.error('Restaurant creation failed', {
                    error: e.message,
                    params: params,
                  })
  APMErrorHandler.report(e, context: { params: params })
  ServiceResult.new(
    errors: [e.message],
    message: "Failed to create restaurant: #{e.message}",
  )
end