Class: Cats::Core::DispatchPlansController

Inherits:
ApplicationController show all
Includes:
Common
Defined in:
app/controllers/cats/core/dispatch_plans_controller.rb

Instance Method Summary collapse

Methods included from Common

#show, #update

Methods inherited from ApplicationController

#authenticate, #current_user, #skip_bullet

Instance Method Details

#approveObject



57
58
59
60
61
62
63
64
# File 'app/controllers/cats/core/dispatch_plans_controller.rb', line 57

def approve
  service = DispatchPlanService.new
  plan = DispatchPlan.find(params[:id])
  plan = service.approve(plan)
  render json: {success: true, data: serialize(plan)}
rescue StandardError => e
  render json: {success: false, error: e.message}
end

#bulk_createObject



51
52
53
54
55
# File 'app/controllers/cats/core/dispatch_plans_controller.rb', line 51

def bulk_create
  service = DispatchPlanService.new
  plan = service.bulk_create(bulk_create_params, current_user)
  render json: {success: true, data: serialize(plan)}
end

#createObject



37
38
39
40
41
42
43
# File 'app/controllers/cats/core/dispatch_plans_controller.rb', line 37

def create
  service = DispatchPlanService.new
  plan = service.create(model_params, current_user)
  render json: {success: true, data: serialize(plan)}, status: :created
rescue StandardError => e
  render json: {success: false, error: e.message}, status: :unprocessable_entity
end

#filterObject



32
33
34
35
# File 'app/controllers/cats/core/dispatch_plans_controller.rb', line 32

def filter
  query = DispatchPlan.ransack(params[:q])
  render json: {success: true, data: serialize(query.result)}
end

#generateObject



45
46
47
48
49
# File 'app/controllers/cats/core/dispatch_plans_controller.rb', line 45

def generate
  service = DispatchPlanService.new
  items = service.generate(params[:id])
  render json: {success: true, data: serialize(items)}
end

#indexObject



6
7
8
9
10
# File 'app/controllers/cats/core/dispatch_plans_controller.rb', line 6

def index
  super do
    DispatchPlan.all.includes(:dispatchable)
  end
end

#plans_for_locationObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/controllers/cats/core/dispatch_plans_controller.rb', line 17

def plans_for_location
  if params[:location_type] == "source"
    plans = DispatchPlan.includes([:dispatchable]).joins(:dispatch_plan_items)
                        .where(status: Cats::Core::DispatchPlan::APPROVED,
                               upstream: false,
                               dispatch_plan_items: {source_id: params[:location_id]}).uniq
  elsif params[:location_type] == "destination"
    plans = DispatchPlan.includes([:dispatchable]).joins(:dispatch_plan_items)
                        .where(status: Cats::Core::DispatchPlan::APPROVED,
                               upstream: false,
                               dispatch_plan_items: {destination_id: params[:location_id]}).uniq
  end
  render json: {success: true, data: serialize(plans)}
end

#plans_for_userObject



12
13
14
15
# File 'app/controllers/cats/core/dispatch_plans_controller.rb', line 12

def plans_for_user
  plans = DispatchPlan.where(prepared_by: current_user).includes(:dispatchable)
  render json: {success: true, data: serialize(plans)}
end