Class: Launchpad::API::V2::Admin::IEO::SalesController

Inherits:
BaseController show all
Defined in:
app/controllers/launchpad/api/v2/admin/ieo/sales_controller.rb

Constant Summary

Constants inherited from BaseController

BaseController::ADMIN_ROLES

Instance Method Summary collapse

Methods inherited from BaseController

#authorize_admin!

Methods included from JWTPayload

#email, #jwt_payload, #role, #uid

Methods included from ExceptionHandlers

included

Methods included from Response

#controller_namespace, #error_response, #errors_response, #json_response, #not_found

Instance Method Details

#createObject

POST /admin/sales



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/controllers/launchpad/api/v2/admin/ieo/sales_controller.rb', line 26

def create
  if sale_params[:pairs].blank?
    error_response("sale_pair.missing_or_empty_pairs", 422)
    return
  end

  sale = nil
  ActiveRecord::Base.transaction do
    sale = Launchpad::IEO::Sale.create!(sale_params.except(:pairs))

    sale_params[:pairs].each do |pair|
      sale.pairs.create!(pair)
    end
  end

  json_response(sale, 201)
end

#indexObject

GET /admin/sales



10
11
12
13
14
15
16
17
18
19
# File 'app/controllers/launchpad/api/v2/admin/ieo/sales_controller.rb', line 10

def index
  ransack_params = {
    state_in:       params[:state],
    currency_id_eq: params[:currency_id],
    name_cont_all:  params[:name]
  }

  sales = Launchpad::IEO::Sale.ransack(ransack_params)
  json_response(paginate(sales.result), 200)
end

#release_fundObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/launchpad/api/v2/admin/ieo/sales_controller.rb', line 60

def release_fund
  sale = Launchpad::IEO::Sale.find(params[:id])
  unless sale.finished?
    error_response("launchpad_ieo_sale.state_mismatch", 422)
    return
  end
  if sale_lockup_params[:release_fund].to_d.negative? || sale.lockup_percentage.to_d - sale_lockup_params[:release_fund].to_d < 0
    error_response("launchpad_ieo_sale.invalid_lockup_percentage_or_negative", 422)
    return
  end
  params.merge!(lockup_percentage: sale.lockup_percentage.to_d - sale_lockup_params[:release_fund].to_d)
  sale.update!(sale_lockup_params.except(:release_fund))
  sale.send(:enqueue_release_funds_job, sale_lockup_params[:release_fund].to_d)
  json_response(sale, 200)
end

#showObject



21
22
23
# File 'app/controllers/launchpad/api/v2/admin/ieo/sales_controller.rb', line 21

def show
  json_response(Launchpad::IEO::Sale.find(params[:id]), 200)
end

#trigger_eventObject

PUT /admin/sales/:id/:event



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/controllers/launchpad/api/v2/admin/ieo/sales_controller.rb', line 77

def trigger_event
  events = Launchpad::IEO::Sale.aasm.events.map { |e| e.name.to_s }
  sale = Launchpad::IEO::Sale.find(params[:id])

  begin
    if params[:event].in?(events)
      sale.send("#{params[:event]}!")
    else
      error_response("sale.event_not_found", 400)
      return
    end
  rescue AASM::InvalidTransition
    error_response("sale.transition_failed", 422)
    return
  end

  json_response(sale, 200)
end

#updateObject

PUT /admin/sales/:id



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/controllers/launchpad/api/v2/admin/ieo/sales_controller.rb', line 45

def update
  sale = Launchpad::IEO::Sale.find(params[:id])
  states = Launchpad::IEO::Sale::EDITABLE_STATES

  if sale.state.in?(states.keys.map(&:to_s))
    sale.update!(sale_params.slice(*states[sale.state.to_sym]))
    sale.send(:enqueue_distribute_job) if sale.ongoing? && sale_params["finishes_at"].present?
  else
    error_response("sale.non_editable", 422)
    return
  end

  json_response(sale, 200)
end