Class: VoucherService::Validation

Inherits:
ApplicationService show all
Includes:
DefaultErrorContainer, MoneyRails::ActionViewExtension
Defined in:
app/services/voucher_service/validation.rb

Defined Under Namespace

Classes: InvalidVoucher

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, #execute!

Constructor Details

#initialize(reservation, voucher, actor, payment_params = {}, skip_prepayment: false) ⇒ Validation

Returns a new instance of Validation.



7
8
9
10
11
12
13
14
# File 'app/services/voucher_service/validation.rb', line 7

def initialize(reservation, voucher, actor, payment_params = {}, skip_prepayment: false)
  @reservation = reservation
  @voucher = voucher
  @multiple = Array.wrap(voucher).count > 1
  @actor = actor
  @payment_params = payment_params
  @skip_prepayment = skip_prepayment
end

Instance Method Details

#validateObject



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
# File 'app/services/voucher_service/validation.rb', line 16

def validate
  errors.clear

  unless form.validate
    merge_errors(form.errors)
    return false
  end

  errors.add(:base, I18n.t('views.voucher.valid_voucher')) if applied?

  # TODO: need to remove this, this is temporary fix payment_type
  payment_type_param = case payment_params[:payment_type]
                       when 'creditcard', 'card', 'credit_card'
                         'credit_card'
                       when 'qrcode'
                         'promptpay'
                       else
                         payment_params[:payment_type]
                       end

  if voucher.payment_types.present? && !skip_prepayment
    unless voucher.payment_types.map(&:name).include?(payment_type_param)
      errors.add(:base,
                 I18n.t('views.voucher.invalid_payment', voucher_code: voucher.voucher_code,
                                                         payment_types: voucher.payment_types.map(&:name).map(&:humanize).to_sentence))
    end

    if voucher.payment_types.map(&:name).include?('credit_card') && !valid_prefix_number?
      errors.add(:base, I18n.t('views.voucher.valid_cc_perfix',
                               prefix: voucher.invalid_prefix_number_message,
                               voucher_code: voucher.voucher_code))
    end
  end

  if actor != :admin
    if per_user_quota.positive? && voucher.one_time?
      if reservation.user.blank?
        errors.add(:base, I18n.t('views.voucher.valid_quota_user_login',
                                 voucher_code: voucher.voucher_code))
      end

      if per_user_quota <= total_user_usage
        errors.add(:base, I18n.t('views.voucher.valid_user_quota',
                                 voucher_code: voucher.voucher_code))
      end
    end

    unless valid_on_specific_date?
      errors.add(:base, I18n.t('views.voucher.valid_range_date',
                               voucher_code: voucher.voucher_code))
    end

    unless selected_day_is_active?
      errors.add(:base, I18n.t('views.voucher.invalid_on_current_date',
                               voucher_code: voucher.voucher_code))
    end

    errors.add(:base, I18n.t('views.voucher.invalid_wtt_with_gift_card_voucher')) if invalid_wtt_usage?
  end

  if voucher.city_id.present? && reservation.restaurant.city_id != voucher.city_id
    errors.add(:base, I18n.t('views.voucher.valid_city',
                             city_name: voucher.city.name,
                             voucher_code: voucher.voucher_code))
  end

  unless valid_minimum_price?
    errors.add(:base, I18n.t('views.voucher.valid_min_price',
                             total_price: voucher.min_total_price.to_i,
                             voucher_code: voucher.voucher_code))
  end

  unless valid_restaurant_tag?
    errors.add(:base, I18n.t('views.voucher.valid_restaurant_tag',
                             title: voucher.restaurant_tag.title,
                             voucher_code: voucher.voucher_code))
  end

  unless valid_combination_vouchers?
    errors.add(:base, I18n.t('views.voucher.valid_combine', voucher_code: voucher.voucher_code))
  end

  errors.blank?
end