Class: Api::V5::VouchersController

Inherits:
BaseController
  • Object
show all
Includes:
Concerns::Authorization, Concerns::PaginationParam
Defined in:
app/controllers/api/v5/vouchers_controller.rb

Constant Summary

Constants inherited from BaseController

BaseController::CACHE_NAMESPACE, BaseController::INTERNAL_SERVER_ERROR_MESSAGE, BaseController::ResponseSchema

Instance Method Summary collapse

Methods inherited from BaseController

#identity_cache_memoization

Methods included from LogrageCustomLogger

#append_info_to_payload

Methods included from ResponseCacheConcern

#my_response_cache

Instance Method Details

#checkObject



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'app/controllers/api/v5/vouchers_controller.rb', line 210

def check
  voucher = Voucher.find_by(voucher_code: params[:voucher_code])
   = params[:category]

  if voucher.blank? || !['giftcard', 'marketing'].include?()
    error_message = 'Voucher code not found or section name is invalid'
  elsif voucher.gift? &&  == 'marketing'
    error_message = 'Please use Gift Card codes in Gift Card section'
  elsif !voucher.gift? &&  == 'giftcard'
    error_message = 'Please use Promo Code in Offers section'
  end

  render json: {
    success: error_message.blank?,
    message: error_message,
  }
end

#indexObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/controllers/api/v5/vouchers_controller.rb', line 31

def index
  section_type = params.require(:section_type)
  page = params.fetch(:page, {})
  filter = Api::V5::VouchersFilter.new(current_user)
  filter.by_category(params[:category])
  filter.by_currency_code(params[:currency])

  if section_type.present?
    case section_type
    when 'active'
      filter.active
    when 'inactive'
      filter.inactive
    else
      return render json: { success: false, data: nil, message: 'Invalid section_type value' }
    end
  end
  filter.page_number(page[:number].presence || 1).per_page(page[:size].presence || 10)
  render json: filter.as_json(serialization_context).merge(success: true, message: nil)
end

#introsObject



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'app/controllers/api/v5/vouchers_controller.rb', line 191

def intros
  intros = Vouchers::Intro.all
  options = {
    serializer: ActiveModel::Serializer::CollectionSerializer,
    each_serializer: Api::V5::Vouchers::IntroSerializer,
  }

  json_data = ActiveModelSerializers::SerializableResource.new(intros, options).serializable_hash
  render json: {
    success: true, data: json_data, message: '',
    intro_description_en: AdminSetting.voucher_intro_description_en,
    intro_description_th: AdminSetting.voucher_intro_description_th,
    hh_custom_voucher_detail: Voucher.static_template('hh_custom_voucher_detail', I18n.locale),
    hh_custom_voucher_tnc: Voucher.static_template('hh_custom_voucher_tnc', I18n.locale),
    restaurant_custom_voucher_detail: Voucher.static_template('restaurant_custom_voucher_detail', I18n.locale),
    restaurant_custom_voucher_tnc: Voucher.static_template('restaurant_custom_voucher_tnc', I18n.locale)
  }
end

#serialized_voucher(voucher_param, user) ⇒ Object



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
# File 'app/controllers/api/v5/vouchers_controller.rb', line 123

def serialized_voucher(voucher_param, user)
  form = VoucherForm::Apply.new(voucher_param, :user, user)
  vouchers = form.vouchers
  vouchers_amount = form.vouchers_amount
  vouchers_amount_v2 = form.vouchers_amount_v2
  array_voucher = voucher_param[:voucher_code].is_a?(Array)
  options = {}
  options_info = {}

  if form.validate
    options_info[:success] = true
    options_info[:message] = ''
    options[:meta] = {
      calculate: form.calculate,
      total_voucher_amount: vouchers_amount,
      total_voucher_amount_v2: vouchers_amount_v2,
    }
  elsif vouchers.present?
    options_info[:success] = false
    options_info[:message] = form.error_message_simple

    options[:meta] = {
      calculate: form.calculate,
      total_voucher_amount: vouchers_amount,
      total_voucher_amount_v2: vouchers_amount_v2,
    }
  else
    options_info[:success] = false
    options_info[:message] = form.error_message_simple
    options_info[:data] = array_voucher ? [] : {}
  end

  options[:serialization_context] = serialization_context
  options[:adapter] = :json_api

  package_payment_types = []
  form.calculate[:selected_packages]&.each do |selected_package|
    restaurant_package_id = selected_package[:restaurant_package_id]
    next if restaurant_package_id.blank?

    rp = HhPackage::RestaurantPackage.fetch(restaurant_package_id)
    next unless rp&.package&.package_attr

    types = rp.package&.package_attr&.payment_types&.valid_payment_types&.map(&:name)
    package_payment_types.concat(types) if types.present?
  end
  package_payment_types.uniq!

  if array_voucher
    options[:serializer] = ActiveModel::Serializer::CollectionSerializer
    options[:each_serializer] = Api::V5::VoucherSerializer
    options[:params] = { package_payment_types: package_payment_types }
  elsif !array_voucher && vouchers.present?
    options[:serializer] = Api::V5::VoucherSerializer
    options[:params] = { package_payment_types: package_payment_types }
    vouchers = vouchers.first
  else
    options[:serializer] = ActiveModel::Serializer::CollectionSerializer
    vouchers = {}
  end

  json_data = ActiveModelSerializers::SerializableResource.new(vouchers, options).
    serializable_hash.
    merge(options_info)

  render(json: json_data)
end

#showObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/controllers/api/v5/vouchers_controller.rb', line 13

def show
  voucher = Voucher.find_by(id: Voucher.decrypt_id(params[:id]))

  if voucher.blank? || voucher&.token.to_s != params[:token].to_s
    return render json: { success: false, data: nil, message: 'Invalid voucher' }
  end

  my_response_cache voucher.cache_key, :json, public: true do
    JSON.parse(
      render_to_string(
        json: voucher,
        serializer: Api::V5::VoucherSerializer,
        adapter: :json_api,
      ),
    ).merge(success: true)
  end
end

#validateObject



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
# File 'app/controllers/api/v5/vouchers_controller.rb', line 52

def validate
  user_id = params.fetch(:user_id, nil)
  user = user_signed_in? ? current_user : User.find_by(id: user_id)
  user_id = user.try(:id) if user_id.blank?
  restaurant = Restaurant.fetch(params.require(:restaurant_id))
  voucher_code = params.require(:voucher_code)
  delivery_fee = params.fetch(:delivery_fee, 0)
  distance_to_restaurant = params.fetch(:distance_to_restaurant, 0)
  adult = params.fetch(:adult, 0)
  kids = params.fetch(:kids, 0)
  client_type = params.fetch(:client_type, ApiV5::Constants::CLIENT_TYPE_WEB)
  accept_refund_guarantee = params.fetch(:accept_refund_guarantee, false)

  # Parameter permitting for restaurant packages
  # - type_code: Required for DIY packages to identify package type
  # - group_sections: Required for DIY packages to handle spending tiers and pricing
  restaurant_packages_param = if params[:packages].present?
                                params.permit(packages: [
                                                :quantity, :id, :type_code,
                                                { menu_sections: [
                                                  :id, { menus: %i[
                                                    quantity id
                                                  ] }
                                                ] },
                                                { group_sections: [
                                                  :id, :quantity
                                                ] }
                                              ])[:packages]
                              else
                                []
                              end

  restaurant_add_ons_param = if params[:add_ons].present?
                               params.permit(add_ons: [:id, :quantity])[:add_ons]
                             else
                               []
                             end

  date_param = params.fetch(:reservation_date) do
    HH_LOGGER.debug('No date param found, using tomorrow as default', params: params)
    Time.now_in_tz(restaurant.time_zone).to_date.tomorrow
  end.to_date

  voucher_param = {
    time_zone: restaurant.time_zone,
    voucher_code: voucher_code,
    user_id: user_id,
    restaurant_id: restaurant.id,
    restaurant_packages: restaurant_packages_param,
    restaurant_add_ons: restaurant_add_ons_param,
    delivery_fee: delivery_fee,
    distance_to_restaurant: distance_to_restaurant,
    cc_number: params[:cc_number],
    date: date_param,
    adult: adult,
    kids: kids,
    email: params[:email],
    phone: params[:phone],
    service_type: params[:service_type],
    client_type: client_type,
    for_validation: true,
    redeemed_points: params[:redeemed_points],
    redeemed_amount: params[:redeemed_amount],
    adaptive_points_ratio_id: params[:adaptive_points_ratio_id],
    country_code: params[:country_code],
    accept_refund_guarantee: accept_refund_guarantee,
  }

  serialized_voucher(voucher_param, user)
end