Class: VoucherForm::Validation

Inherits:
Object
  • Object
show all
Includes:
DefaultErrorContainer, MoneyRails::ActionViewExtension
Defined in:
app/forms/voucher_form/validation.rb

Defined Under Namespace

Classes: InvalidVoucher

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DefaultErrorContainer

#error, #error_message_simple, #merge_errors

Constructor Details

#initialize(param, actor) ⇒ Validation

but what we need is admin checking

Parameters:

  • :param (Hash)

    time_zone: 'Asia/Bangkok', for validating expiry date
    voucher: @voucher,
    user_id: nil, # optional
    restaurant_id: nil, # optional
    restaurant_packages: [menu_sections] # required with menu_sections params if package is ala carte type,
    date: '2021-01-01', # required for validating expiry date and ChargeCalculator
    

  • :actor (Symbol)

    :restaurant_staff, :user, :owner or :admin,



22
23
24
25
26
27
28
# File 'app/forms/voucher_form/validation.rb', line 22

def initialize(param, actor)
  self.param = param.to_h.with_indifferent_access
  @voucher = self.param[:voucher]
  @calculate = {}
  self.actor = actor.to_sym
  raise 'Invalid actor' unless %i[admin restaurant_staff owner user].include?(actor.to_sym)
end

Instance Attribute Details

#actorObject

Returns the value of attribute actor.



9
10
11
# File 'app/forms/voucher_form/validation.rb', line 9

def actor
  @actor
end

#calculateObject (readonly)

Returns the value of attribute calculate.



9
10
11
# File 'app/forms/voucher_form/validation.rb', line 9

def calculate
  @calculate
end

#paramObject

Returns the value of attribute param.



9
10
11
# File 'app/forms/voucher_form/validation.rb', line 9

def param
  @param
end

#voucherObject (readonly)

Returns the value of attribute voucher.



9
10
11
# File 'app/forms/voucher_form/validation.rb', line 9

def voucher
  @voucher
end

Instance Method Details

#validateObject



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
# File 'app/forms/voucher_form/validation.rb', line 30

def validate
  errors.clear

  voucher_blank!
  inactive_voucher!

  voucher_type_present!

  if actor != :admin
    valid_quota_and_one_time!
    active_voucher!
    valid_on_specific_date!
    valid_currency_for_country!

    selected_day_is_active!
    valid_per_user_quota!
    valid_guest_quota!
    valid_device_type!
  end

  valid_package_params!
  valid_prefix_number!
  valid_city!

  valid_package_types!
  valid_restaurant_tag!
  valid_restaurant_voucher!

  if voucher_first_booking?
    email = user.persisted? ? user.email : param[:email]
    phone = user.persisted? ? user.phone : param[:phone]

    invalid_email_and_phone!(email, phone)
    invalid_for_new_customer!
    valid_first_booking_user!(email, phone)
  end

  voucher_delivery_and_invalid_distance!
  check_voucher_validity!
  valid_deductible_voucher!

  true
rescue InvalidVoucher => e
  errors.add :base, e.message
  false
end