Class: PendingTransactionService

Inherits:
ApplicationService show all
Includes:
DefaultErrorContainer
Defined in:
app/services/pending_transaction_service.rb

Overview

typed: ignore

Defined Under Namespace

Classes: InvalidPendingTransaction

Instance Attribute Summary collapse

Attributes inherited from ApplicationService

#object

Instance Method Summary collapse

Methods included from DefaultErrorContainer

#error, #error_message_simple, #merge_errors

Methods inherited from ApplicationService

#execute!

Constructor Details

#initialize(params) ⇒ PendingTransactionService

Returns a new instance of PendingTransactionService.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/services/pending_transaction_service.rb', line 7

def initialize(params)
  @reservation_params = params[:reservation].tap do |h|
    if h[:service_type] == 'pick_up'
      h[:service_type] = 'delivery'
    end
  end
  @package_params = params[:packages]
  @address_params = params[:address]
  service_type = reservation_params[:service_type]
  last_transaction = PendingTransaction.find_by(user_id: reservation_params[:user_id], service_type: service_type)
  @pending_transaction = if last_transaction.present?
                            last_transaction
                          else
                            PendingTransaction.new(reservation_params)
                          end
end

Instance Attribute Details

#address_paramsObject (readonly)

Returns the value of attribute address_params.



4
5
6
# File 'app/services/pending_transaction_service.rb', line 4

def address_params
  @address_params
end

#package_paramsObject (readonly)

Returns the value of attribute package_params.



4
5
6
# File 'app/services/pending_transaction_service.rb', line 4

def package_params
  @package_params
end

#pending_transactionObject

Returns the value of attribute pending_transaction.



5
6
7
# File 'app/services/pending_transaction_service.rb', line 5

def pending_transaction
  @pending_transaction
end

#reservation_paramsObject (readonly)

Returns the value of attribute reservation_params.



4
5
6
# File 'app/services/pending_transaction_service.rb', line 4

def reservation_params
  @reservation_params
end

Instance Method Details

#executeObject



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
# File 'app/services/pending_transaction_service.rb', line 24

def execute
  errors.clear

  validate

  return false if errors.present?

  ActiveRecord::Base.transaction do
    pending_transaction.attributes = reservation_params
    pending_transaction.build_property if pending_transaction.property.blank?
     = HhPackage::ReservationPackages::Metadata.from_reservation(pending_transaction)
    .package_params = package_params
    pending_transaction.property.assign_attributes(pending_transaction: pending_transaction, package: .generate)
    pending_transaction.package_data = package_params
    pending_transaction.address_attributes = address_params
    pending_transaction.save!
  end

  merge_errors(pending_transaction.errors) if pending_transaction.errors.present?

  if errors.present?
    ServiceResult.new errors: errors, message: error_message_simple
  else
    ServiceResult.new data: pending_transaction
  end
rescue InvalidPendingTransaction => error
  errors.add :base, error.message
  ServiceResult.new errors: errors, message: error_message_simple
end