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 =

Smart service assignee id.

Employee::SCOTT
NO_SERVICE_AVAILABLE =

No service available.

'The service you selected is not provided in the customer address.
SMARFIT is only provided within 50 miles of WY LZ office.'.freeze

Instance Method Summary collapse

Instance Method Details

#call(support_case_params) ⇒ Object



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

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 = "#{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
        raise ActiveRecord::Rollback unless order.save

        order.reload
        order.pending_payment
      else
        return Result.new(support_case: support_case,
                          success: false,
                          order: order,
                          error: NO_SERVICE_AVAILABLE)
      end
    end
    raise ActiveRecord::Rollback unless support_case.save

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

    return Result.new(success: true, support_case: support_case)
  end

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