Class: Cats::Core::DispatchTransactionsController

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

Instance Method Summary collapse

Methods included from Common

#create, #show, #update

Methods inherited from ApplicationController

#authenticate, #current_user, #skip_bullet

Instance Method Details

#create_allocationObject

This end-point is used to create upstream dispatch transactions. We need a specific method like this because for upstream the source stack is a dummy one and we need to use that without waiting for the user to give us a source.



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

def create_allocation
  authorization = DispatchAuthorization.find(model_params[:dispatch_authorization_id])
  commodity = authorization.dispatch.dispatch_plan_item.commodity
  transaction = DispatchTransaction.new(model_params)

  # Fetch supplier stack by commodity
  stack = Stack.joins(:store).find_by(commodity: commodity, store: {code: "SUP-STORE"})
  transaction.source = stack

  if transaction.save
    render json: {success: true, data: serialize(transaction)}, status: :created
  else
    render json: {success: false, error: transaction.errors.full_messages[0]}, status: :unprocessable_entity
  end
end

#filterObject



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

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

#indexObject



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

def index
  super do
    DispatchTransaction.includes(:source, :unit).where(dispatch_authorization_id: params[:id])
  end
end