Class: SupportCase::Create

Inherits:
Object
  • Object
show all
Defined in:
app/services/support_case/create.rb

Overview

TODO, examine this pattern for refactor

Defined Under Namespace

Classes: Result

Constant Summary collapse

SMART_SERVICE_ASSIGNEE_ID =
Employee::SCOTT
NO_SERVICE_AVAILABLE =
%q{The service you selected is not provided in the customer address.
SMARFIT is only provided within 50 miles of WY LZ office.}

Instance Method Summary collapse

Instance Method Details

#call(support_case_params) ⇒ Object



12
13
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
# File 'app/services/support_case/create.rb', line 12

def call(support_case_params)

  # Putting support case outside block so it does not go out of scope

  support_case = SupportCase.new(support_case_params)

  SupportCase.transaction do

    if support_case.service.present?
      support_case.description = %Q{#{support_case.description}
                                    <p>Customer prefers to be called in the
                                    #{support_case_params[:service_time]} }
      support_case.assigned_to_id = SMART_SERVICE_ASSIGNEE_ID #JL
      address = Address.where(id: support_case.service_address_id).first
      services = check_postal_code(address&.zip)
      if services.present?
        order = create_order(support_case.service, support_case.service_room_qty, support_case.customer)
        order.shipping_address = address
        unless order.save
          raise ActiveRecord::Rollback
        end
        order.reload
        order.pending_payment
      else
        return Result.new(support_case: support_case,
                          success: false,
                          order: order,
                          error: NO_SERVICE_AVAILABLE)
      end
    end
    unless support_case.save
      raise ActiveRecord::Rollback
    end

    if order.present?
      support_case.order_ids = [@order.id]
    end

    return Result.new(success: true, support_case: support_case)
  end # End transaction block

  Result.new(success: false, support_case: support_case)
end