6
7
8
9
10
11
12
13
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
# File 'app/controllers/api/v5/concerns/tmp_reservation.rb', line 6
def create_v2
vendor_name = params[:vendor_name]
if vendor_name.present?
invalid_vendor_name = !vendor_is_valid?(vendor_name)
if invalid_vendor_name
render json: { success: false, message: 'vendor_name is invalid', data: nil }
return
end
end
reservation_id = params[:tmp_reservation_id]
reservation = Reservation.find(reservation_id)
is_group_booking = params.fetch(:big_group, false)
restaurant_packages_params = if params[:packages].present?
params.permit(
packages: [:id, :quantity, {
menu_sections: [:id, :quantity,
{ menus: [:id, :quantity, { subsections: [box: [:id, { menus: %i[id quantity] }]] }] }],
selected_special_menus: [:id, :quantity],
group_sections: [:id, :quantity],
}],
)[:packages]
else
[]
end
restaurant_add_ons_params = if params[:add_ons].present?
params.permit(add_ons: [:id, :quantity])[:add_ons]
else
[]
end
we_travel_together_params = if params[:we_travel_together].present?
params.require(:we_travel_together).permit(:wtt_id_card, :wtt_phone)
else
{}
end
nested_package_params = restaurant_packages_params.map(&:to_h)
nested_add_on_params = restaurant_add_ons_params.map(&:to_h)
nested_package_params.map do |package|
next if package[:menu_sections].blank?
package[:menu_sections].map do |ms|
= ms.delete :menus
ms[:menus] = []
.map do ||
if [:subsections].present?
temp_subsections = .delete :subsections
temp_subsections.map do |subsection|
[:quantity] = 1
= .merge(subsections: subsection[:box])
ms[:menus] <<
end
else
ms[:menus] <<
end
end
end
end
reservation_param = params.require(:reservation).permit(
:date,
:start_time,
:adult,
:kids,
:service_type,
:delivery_address,
:distance_to_restaurant,
:special_request,
:dining_occasion_id,
:restaurant_id,
:promo_code,
:is_order_now,
:click_id,
:adv_partner,
:redeemed_points,
:redeemed_amount,
:currency_code,
:adaptive_points_ratio_id,
:pay_now,
:accept_we_travel_together,
:accept_refund_guarantee,
).tap do |param|
param[:restaurant_id] = if restaurant_packages_params.present?
package = HhPackage::RestaurantPackage.fetch(restaurant_packages_params.first[:id])
package.restaurant_id
else
param[:restaurant_id]
end
param[:date] = Date.parse(param[:date]).to_s if param[:date].present?
end
fb_conversion_params = if params.key?(:fb_conversion)
params.require(:fb_conversion).permit(:fbp, :fbc).merge({
ip_address: request.remote_ip,
user_agent: browser.ua,
event_source_url: request.referrer,
action_source: parse_medium_source,
event_time: Time.zone.now.to_i,
})
else
{}
end
if params[:reservation][:pay_now].present?
pay_now = params[:reservation][:pay_now]
reservation_param[:pay_now] = pay_now.to_s == 'true'
else
reservation_param[:pay_now] = false
end
= if params.key?(:selected_special_menus)
params.permit(selected_special_menus: %i[id quantity])[:selected_special_menus]
elsif params[:packages].present? && params[:packages].select do |p|
p.key?(:selected_special_menus)
end.present?
= []
params[:packages].map(&:permit!).each do |p|
p[:selected_special_menus] = (p[:selected_special_menus] || []).map do |h|
h[:restaurant_package_id] = p[:id]
h
end
+= p[:selected_special_menus] if p[:selected_special_menus].present?
end
else
[]
end
= ( + (nested_package_params, reservation_param[:adult])).uniq
create_reservation_params = {
use_3d_secure: params.fetch(:use_3d_secure, false),
is_load_test: params.fetch(:is_load_test, false),
line_id: (params.key?(:guest_user) && params[:guest_user].key?(:line_id) && params[:guest_user][:line_id]) || nil,
reservation: reservation_param,
address: params.fetch(:address, {})&.permit(:detail, :lat, :lon, :name, :note_for_driver, :phone) || {},
user_id: params.key?(:access_token) && params.key?(:provider) && authorize! && user_signed_in? ? current_user.id : nil,
address_id: params.fetch(:address_id, nil),
guest_user: params.key?(:guest_user) ? params.require(:guest_user).permit(:name, :email, :phone) : nil,
channel: parse_channel_source,
medium: parse_medium_source,
created_by: :user,
group_booking: is_group_booking,
business_booking: false,
omise_token: params.fetch(:omise_token, ''),
omise_payment_type: params.fetch(:omise_payment_type, nil),
payment_type: params.fetch(:payment_type, nil),
gb_primepay_card: params.fetch(:gb_primepay_card, {}),
restaurant_packages: nested_package_params,
restaurant_add_ons: nested_add_on_params,
reservation_we_travel_together_attributes: we_travel_together_params,
voucher_code: params.require(:reservation).fetch(:voucher_code, nil),
guests_attributes: params.key?(:guests_attributes) && params.permit(guests_attributes: %i[name
phone])[:guests_attributes],
fb_conversion_params: fb_conversion_params,
corporate_event_id: params[:corporate_event_id],
selected_special_menus: ,
aoa_token: params[:aoa_token],
vendor_name: vendor_name,
web_v2_host: params[:web_v2_host],
card_id: params.fetch(:gb_primepay_card, {}).fetch(:card_id, nil)&.to_i,
ip_address: request.remote_ip.presence || request.ip,
rwg_token: params[:rwg_token],
accept_refund_guarantee: params.fetch(:accept_refund_guarantee, false),
tracking: tracking_params,
}.tap do |param|
param.each do |key, value|
param[key] = value.permit!.to_h if value.is_a?(ActionController::Parameters)
end
end
async_booking = params.fetch(:async_booking, false)
if async_booking
form = ReservationService::FormFactory.new(create_reservation_params, reservation).create_form_service
success = false
if form.validate
date = Date.today
time = Time.zone.now.strftime('%H:%M')
code = SecureRandom.hex(5).downcase
reservation_tracking_key = "reservation_tracking/date-#{date}/time-#{time}/random-#{code}"
firebase = MyFirebase.new
response = firebase.update(reservation_tracking_key, { status: :loading })
if response.success?
create_reservation_params.tap do |h|
h[:guest_user] = h[:guest_user].present? && h[:guest_user].to_h
h[:reservation] = h[:reservation].to_h
h[:address] = h[:address].to_h
h[:gb_primepay_card] = h[:gb_primepay_card].to_h
h[:guests_attributes] = h[:guests_attributes].is_a?(Array) && h[:guests_attributes].map(&:to_h)
h[:selected_special_menus] =
h[:selected_special_menus].is_a?(Array) && h[:selected_special_menus].map(&:to_h)
end
Workers::Reservations::CompleteWorker.perform_async(create_reservation_params, reservation_tracking_key,
reservation_id)
success = true
else
form.errors.add(:base, 'failed to create reservation')
success = false
end
end
if success
(true)
render json: {
success: true,
firebase_key: reservation_tracking_key,
}
else
(false)
render json: { success: false, message: form.error_message_simple, data: nil }
end
else
service = ReservationService::Create.new(create_reservation_params, reservation)
if service.execute
(true)
options = {
include: DEFAULT_INCLUDE,
}
render json: resource_as_json(service.outcome, Api::V5::ReservationSerializer, options).merge(
success: true,
message: I18n.t('actions.reservation.created'),
meta: {
misc: misc(service.outcome),
},
)
else
cancel_supplier_reservation(reservation)
(false)
render json: { success: false, message: service.error_message_simple, data: nil }
end
end
rescue StandardError => e
if Rails.env.production?
APMErrorHandler.report(e)
render json: { success: false, message: 'Sorry, something went wrong', data: nil }
else
raise e
end
end
|