14
15
16
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
70
71
72
73
74
75
76
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
104
105
106
107
108
|
# File 'app/services/restaurant_service/create.rb', line 14
def execute!
result = false
message = nil
ActiveRecord::Base.transaction do
unless @owner.save && @owner.confirm
message = @owner.errors.full_messages.to_sentence
raise ActiveRecord::Rollback
end
city = case @params[:country_code]
when ApiV5::Constants::COUNTRY_CODE_TH
City.find_bangkok
when ApiV5::Constants::COUNTRY_CODE_SG
City.find_singapore
when ApiV5::Constants::COUNTRY_CODE_MY
City.find_malaysia
else
City.find_bangkok
end
cuisine_tag = RestaurantTag.find_by(id: @params[:cuisine_id])
@restaurant.restaurant_tags << cuisine_tag if cuisine_tag.present?
@restaurant.parking = @params[:parking]
@restaurant.address_en = @params[:address]
@restaurant.city_id = city.id
@restaurant.expiry_date = Date.today + 1.year
@restaurant.created_by = if @creator.present? && @creator.email.include?('@hungryhub.com')
'hungryhub'
else
'owner'
end
unless @restaurant.save
message = @restaurant.errors.full_messages.to_sentence
raise ActiveRecord::Rollback
end
inventory_data = JSON.parse(@params[:inventory_group])
begin
inventory_data.each do |key, weekday_data|
next if weekday_data.to_a.empty?
itg = InventoryTemplateGroup.create!(name: key, restaurant_id: @restaurant.id)
@restaurant.assign_attributes("#{key.to_s.downcase}": itg.id)
weekday_data.each do |item|
start_time = Time.zone.parse(item['first']['value'])
end_time = Time.zone.parse(item['last']['value'])
while start_time <= end_time
InventoryTemplate.create!(
start_time: start_time.strftime('%H:%M'),
end_time: (start_time += 15.minutes).strftime('%H:%M'),
quantity_available: item['seat']['value'],
inventory_template_group_id: itg.id,
)
end
end
end
rescue StandardError => e
APMErrorHandler.report(e)
message = 'Something went wrong with inventory'
raise ActiveRecord::Rollback
end
unless @restaurant.save
message = @restaurant.errors.full_messages.to_sentence
raise ActiveRecord::Rollback
end
unless create_staff
message = 'Failed to create staff'
raise ActiveRecord::Rollback
end
if cuisine_tag.present?
PrimaryTagCpt::Operations::UpdateRelation.call(id: cuisine_tag.id,
restaurant_id: @restaurant.id)
end
Restaurant.generate_schedule(@restaurant.id)
result = true
end
if result
ServiceResult.new data: { owner: @owner, restaurant: @restaurant }
else
ServiceResult.new message: message, errors: [message]
end
rescue StandardError => e
APMErrorHandler.report(e)
ServiceResult.new message: 'Something went wrong', errors: [e.message]
end
|