Class: RestaurantService::CreateBasic

Inherits:
ApplicationService show all
Includes:
DefaultErrorContainer
Defined in:
app/services/restaurant_service/create_basic.rb

Instance Attribute Summary

Attributes inherited from ApplicationService

#object

Instance Method Summary collapse

Methods included from DefaultErrorContainer

#error, #error_message_simple, #merge_errors

Methods inherited from ApplicationService

#execute

Constructor Details

#initialize(other_params, staff_param, restaurant_param, inventory_template_param) ⇒ CreateBasic

Returns a new instance of CreateBasic.

Parameters:

  • inventory_template_param (Hash)

    mon: [{start_Time, end_time, quantity_available, start_time, end_time, quantity_available],
    tue: [end_time, quantity_available, start_time, end_time, quantity_available]
    

    }



12
13
14
15
16
17
18
# File 'app/services/restaurant_service/create_basic.rb', line 12

def initialize(other_params, staff_param, restaurant_param, inventory_template_param)
  self.staff_param = staff_param
  self.owner_params = other_params[:owner]
  self.property_params = other_params[:properties]
  self.restaurant_param = restaurant_param
  self.inventory_template_param = inventory_template_param
end

Instance Method Details

#assign_restaurant_tagsObject



105
106
107
108
109
110
111
112
113
# File 'app/services/restaurant_service/create_basic.rb', line 105

def assign_restaurant_tags
  cuisine_tag = RestaurantTag.find_by(id: property_params[:cuisine_id])
  restaurant.restaurant_tags << cuisine_tag if cuisine_tag.present?
  restaurant.parking = property_params[:parking]
  if cuisine_tag.present?
    PrimaryTagCpt::Operations::UpdateRelation.call(id: cuisine_tag.id,
                                                   restaurant_id: restaurant.id)
  end
end

#create_inventory_templateObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/services/restaurant_service/create_basic.rb', line 77

def create_inventory_template
  inventory_template_param.each do |name, inventory_templates|
    # TODO set error message
    itg = InventoryTemplateGroup.new(name: name, restaurant: restaurant)
    unless itg.save
      merge_errors(itg.errors)
      raise ActiveRecord::Rollback
    end
    restaurant.update!("#{name}": itg.id)
    inventory_templates.each do |inventory_template|
      start_time = Time.zone.parse(inventory_template[:start_time])
      end_time = Time.zone.parse(inventory_template[:end_time])
      while start_time <= end_time
        it = InventoryTemplate.new(
          start_time: start_time.strftime('%H:%M'),
          end_time: (start_time += 15.minutes).strftime('%H:%M'),
          quantity_available: inventory_template[:quantity_available],
          inventory_template_group_id: itg.id,
        )
        unless it.save
          merge_errors(it.errors)
          raise ActiveRecord::Rollback
        end
      end
    end
  end
end

#create_ownerObject



68
69
70
71
72
73
74
75
# File 'app/services/restaurant_service/create_basic.rb', line 68

def create_owner
  owner = Owner.new owner_params
  unless owner.save && owner.confirm
    merge_errors(owner.errors)
    raise ActiveRecord::Rollback
  end
  owner
end

#create_restaurantObject



44
45
46
47
48
49
50
# File 'app/services/restaurant_service/create_basic.rb', line 44

def create_restaurant
  restaurant.assign_attributes restaurant_param.merge(city_id: City.first&.id, owner_id: create_owner.id)
  unless restaurant.save
    merge_errors(restaurant.errors)
    raise ActiveRecord::Rollback
  end
end

#create_roleObject



60
61
62
63
64
65
66
# File 'app/services/restaurant_service/create_basic.rb', line 60

def create_role
  staffrole = StaffRole.new restaurant: restaurant, staff: staff, role: :owner
  unless staffrole.save
    merge_errors(staffrole.errors)
    raise ActiveRecord::Rollback
  end
end

#create_staffObject



52
53
54
55
56
57
58
# File 'app/services/restaurant_service/create_basic.rb', line 52

def create_staff
  staff.assign_attributes staff_param
  unless staff.save
    merge_errors(staff.errors)
    raise ActiveRecord::Rollback
  end
end

#execute!Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/services/restaurant_service/create_basic.rb', line 20

def execute!
  result = false
  message = nil
  ActiveRecord::Base.transaction do
    create_staff
    create_restaurant
    assign_restaurant_tags
    create_role
    create_inventory_template
    Restaurant.generate_schedule(restaurant.id, inventory_type: :dine_in)
    Restaurant.generate_schedule(restaurant.id, inventory_type: :delivery)
    result = true
  end

  if result
    ServiceResult.new data: staff
  else
    ServiceResult.new message: 'Sorry, an error happened', errors: [error_message_simple]
  end
rescue StandardError => e
  APMErrorHandler.report(e)
  ServiceResult.new message: 'Something went wrong', errors: [e.message]
end