Class: VoucherService::Modify
- Inherits:
-
ApplicationService
- Object
- ApplicationService
- VoucherService::Modify
- Includes:
- DefaultErrorContainer
- Defined in:
- app/services/voucher_service/modify.rb
Overview
typed: ignore frozen_string_literal: true
Defined Under Namespace
Classes: InvalidVoucher
Instance Attribute Summary
Attributes inherited from ApplicationService
Instance Method Summary collapse
- #activate! ⇒ Object
- #inactive! ⇒ Object
-
#initialize(reservation, new_voucher_codes = [], action = 'modify') ⇒ Modify
constructor
A new instance of Modify.
- #override_vouchers! ⇒ Object
Methods included from DefaultErrorContainer
#error, #error_message_simple, #merge_errors
Methods inherited from ApplicationService
Constructor Details
#initialize(reservation, new_voucher_codes = [], action = 'modify') ⇒ Modify
Returns a new instance of Modify.
7 8 9 10 11 12 |
# File 'app/services/voucher_service/modify.rb', line 7 def initialize(reservation, new_voucher_codes = [], action = 'modify') @action = action.to_sym @reservation = reservation @new_voucher_codes = new_voucher_codes.reject(&:empty?) @vouchers = reservation.vouchers end |
Instance Method Details
#activate! ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'app/services/voucher_service/modify.rb', line 100 def activate! reservation_vouchers = reservation.reservation_vouchers return if reservation_vouchers.blank? reservation_vouchers.each do |rv| reduce_quota(rv.voucher) unless rv.active? rv.update!(active: true) end ServiceResult.new data: reservation rescue InvalidVoucher => e errors.add :base, e. ServiceResult.new errors: errors, message: end |
#inactive! ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'app/services/voucher_service/modify.rb', line 82 def inactive! reservation_vouchers = reservation.reservation_vouchers return if reservation_vouchers.blank? reservation_vouchers.each do |rv| rv.update!(active: false) restore_quota(rv.voucher) unless non_restorable_quota?(rv.voucher) restore_balance(rv.voucher) end ServiceResult.new data: reservation rescue InvalidVoucher => e errors.add :base, e. ServiceResult.new errors: errors, message: end |
#override_vouchers! ⇒ Object
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 |
# File 'app/services/voucher_service/modify.rb', line 14 def override_vouchers! errors.clear # voucher is not modified return ServiceResult.new data: reservation if new_voucher_codes == old_voucher_codes if deleted_voucher_codes.present? deleted_voucher_codes.each do |voucher_code| voucher = Voucher.find_by(voucher_code: voucher_code) remove_voucher(voucher) end end # new voucher added if new_added_voucher_codes.present? new_vouchers = [] user_id = reservation.user_id ActiveRecord::Base.transaction do new_added_voucher_codes.each do |voucher_code| voucher = Voucher.find_by(voucher_code: voucher_code) referral_voucher_form = VoucherForm::Referral.new(user_id: user_id, code: voucher_code, country_code: reservation.restaurant&.country&.alpha3) referral_voucher = referral_voucher_form.build_voucher if referral_voucher.present? user = reservation.user referrer = User.find_referrer_based_on_voucher_code(voucher_code) if referrer.blank? raise ActiveRecord::Rollback, 'Invalid referral code' else user.update!(r_code: referrer.short_my_r_code) end referral_voucher.save! voucher = referral_voucher end raise InvalidVoucher, "Promo code #{voucher_code} is not found" if voucher.blank? reservation_deductible = VoucherDeductible.where(voucher_id: voucher.id, reservation_id: reservation.id) next if reservation_deductible.present? new_vouchers.push(voucher) end end payment_type = reservation.payment_type_provider.presence || '' reservation.reload service = VoucherService::Apply.new(reservation, new_vouchers, :admin, { payment_type: payment_type }) result = service.execute raise InvalidVoucher, result. unless result.success? end reservation.reload reservation.assign_charged_data reservation.reservation_vouchers.each(&:assign_amount_voucher) ServiceResult.new data: reservation rescue InvalidVoucher => e errors.add :base, e. ServiceResult.new errors: errors, message: end |