Class: Cats::Core::DispatchAuthorizationsController

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

Instance Method Summary collapse

Methods included from Common

#show, #update

Methods inherited from ApplicationController

#authenticate, #current_user, #skip_bullet

Instance Method Details

#confirmObject



24
25
26
27
28
29
30
# File 'app/controllers/cats/core/dispatch_authorizations_controller.rb', line 24

def confirm
  authorization = DispatchAuthorization.find(params[:id])
  authorization = authorization.confirm
  render json: {success: true, data: serialize(authorization)}
rescue StandardError => e
  render json: {success: false, error: e.message}
end

#createObject



14
15
16
17
18
19
20
21
22
# File 'app/controllers/cats/core/dispatch_authorizations_controller.rb', line 14

def create
  authorization = DispatchAuthorization.new(model_params)
  if authorization.save
    AuthorizationService.new.send_dispatch_notification(authorization)
    render json: {success: true, data: serialize(authorization)}, status: :created
  else
    render json: {success: false, error: authorization.errors.full_messages[0]}, status: :unprocessable_entity
  end
end

#filterObject



42
43
44
45
46
47
48
49
50
# File 'app/controllers/cats/core/dispatch_authorizations_controller.rb', line 42

def filter
  query = DispatchAuthorization.includes(
    :dispatch,
    :store,
    :authorized_by,
    :unit
  ).ransack(params[:q])
  render json: {success: true, data: serialize(query.result.reverse_order)}
end

#indexObject



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

def index
  super do
    DispatchAuthorization.includes(:dispatch, :store, :authorized_by, :unit).where(
      dispatch_id: params[:id]
    ).reverse_order
  end
end

#storekeeper_authorizationsObject



32
33
34
35
36
37
38
39
40
# File 'app/controllers/cats/core/dispatch_authorizations_controller.rb', line 32

def storekeeper_authorizations
  storekeeper = User.find(params[:id])
  stores = storekeeper.stores
  authorizations = DispatchAuthorization.joins(:dispatch)
                                        .includes(:dispatch, :store, :authorized_by, :unit)
                                        .where(store: stores, dispatch: {dispatch_status: Dispatch::APPROVED})
                                        .reverse_order
  render json: {success: true, data: serialize(authorizations)}
end