Class: Cats::Core::DispatchService

Inherits:
Object
  • Object
show all
Defined in:
app/services/cats/core/dispatch_service.rb

Instance Method Summary collapse

Instance Method Details

#search(user, status, authorized: false) ⇒ Object

Raises:

  • (StandardError)


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/services/cats/core/dispatch_service.rb', line 4

def search(user, status, authorized: false)
  details = user.details

  raise(StandardError, "User does not have associated location.") unless details["stores"] || details["warehouse"] || details["hub"]

  # Get user's hub
  hub = if details["stores"]
          Store.find(details["stores"][0]).warehouse.parent
        elsif details["warehouse"]
          Location.find(details["warehouse"]).parent
        else
          Location.find(details["hub"])
        end

  if authorized
    return Dispatch.joins(:dispatch_plan_item)
                   .includes({dispatch_plan_item: :destination}, :transporter, :prepared_by, :unit)
                   .where(
                     dispatch_plan_item: {destination: hub, status: DispatchPlanItem::AUTHORIZED},
                     dispatch_status: status
                   )
  end
  Dispatch.joins(:dispatch_plan_item)
          .includes({dispatch_plan_item: :destination}, :transporter, :prepared_by, :unit)
          .where(
            dispatch_plan_item: {destination: hub},
            dispatch_status: status
          )
end

#send_notification(dispatch) ⇒ Object

Raises:

  • (StandardError)


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/services/cats/core/dispatch_service.rb', line 67

def send_notification(dispatch)
  notification_rule = NotificationRule.find_by(code: "dispatch")
  raise(StandardError, "Notification rule not found for dispatch notification.") unless notification_rule

  users = User.joins(:roles).where(cats_core_roles: {name: notification_rule.roles})
  location_id = dispatch.dispatch_plan_item.destination_id
  hub = Location.find(location_id)
  recipients = users.map do |user|
    details = user.details

    if (details.key?("warehouse") && hub.child_ids.include?(details["warehouse"])) ||
       (details.key?("hub") && details["hub"] == location_id)
      user
    end
  end.compact
  return if recipients.empty?

  notification = DispatchNotification.with(dispatch: dispatch)
  notification.deliver(recipients)
end

#start(dispatch) ⇒ Object



34
35
36
37
38
# File 'app/services/cats/core/dispatch_service.rb', line 34

def start(dispatch)
  dispatch.start
  send_notification(dispatch)
  dispatch
end

#start_with_pin(dispatch_id, pin) ⇒ Object

Raises:

  • (StandardError)


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
# File 'app/services/cats/core/dispatch_service.rb', line 40

def start_with_pin(dispatch_id, pin)
  dispatch = Dispatch.includes(
    {dispatch_plan_item: :destination},
    :transporter,
    :prepared_by,
    :unit
  ).find(dispatch_id)
  raise(StandardError, "Dispatch is already started.") if dispatch.dispatch_status == Dispatch::STARTED

  raise(StandardError, "No pin has been generated for dispatch.") if dispatch.auth_details.nil?

  raise(StandardError, "Incorrect pin.") unless pin == dispatch.auth_details["pin"]

  raise(StandardError, "Pin is not active.") unless dispatch.auth_details["active"]

  if DateTime.now > dispatch.auth_details["expires_at"]
    dispatch.auth_details[:active] = false
    dispatch.save!
    raise(StandardError, "Pin has expired.")
  end
  dispatch.start
  send_notification(dispatch)
  dispatch.auth_details = nil
  dispatch.save!
  dispatch
end