Class: Spree::Api::V3::Admin::Customers::StoreCreditsController

Inherits:
ResourceController show all
Defined in:
app/controllers/spree/api/v3/admin/customers/store_credits_controller.rb

Constant Summary

Constants included from ScopedAuthorization

ScopedAuthorization::READ_ACTIONS

Constants inherited from BaseController

BaseController::RATE_LIMIT_RESPONSE

Constants included from Idempotent

Idempotent::IDEMPOTENCY_HEADER, Idempotent::IDEMPOTENCY_TTL, Idempotent::MAX_KEY_LENGTH, Idempotent::MUTATING_METHODS

Constants included from ErrorHandler

ErrorHandler::ERROR_CODES

Constants included from JwtAuthentication

JwtAuthentication::JWT_AUDIENCE_ADMIN, JwtAuthentication::JWT_AUDIENCE_STORE, JwtAuthentication::JWT_ISSUER, JwtAuthentication::USER_TYPE_ADMIN, JwtAuthentication::USER_TYPE_CUSTOMER

Instance Method Summary collapse

Methods inherited from ResourceController

#index, #show

Methods included from Spree::Api::V3::ApiKeyAuthentication

#authenticate_api_key!, #authenticate_secret_key!

Methods included from JwtAuthentication

#authenticate_user, #require_authentication!

Instance Method Details

#createObject

POST /api/v3/admin/customers/:customer_id/store_credits



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/controllers/spree/api/v3/admin/customers/store_credits_controller.rb', line 10

def create
  @resource = @parent.store_credits.new(create_attrs)
  @resource.created_by = try_spree_current_user
  @resource.store ||= current_store
  @resource.category ||= Spree::StoreCreditCategory.first
  authorize_resource!(@resource, :create)

  if @resource.save
    render json: serialize_resource(@resource), status: :created
  else
    render_validation_error(@resource.errors)
  end
end

#destroyObject

DELETE /api/v3/admin/customers/:customer_id/store_credits/:id



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/controllers/spree/api/v3/admin/customers/store_credits_controller.rb', line 45

def destroy
  authorize_resource!(@resource)

  if @resource.amount_used.positive?
    render_error(
      code: 'store_credit_in_use',
      message: 'Cannot delete a store credit that has already been used',
      status: :unprocessable_content
    )
    return
  end

  @resource.destroy
  head :no_content
end

#updateObject

PATCH /api/v3/admin/customers/:customer_id/store_credits/:id



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/controllers/spree/api/v3/admin/customers/store_credits_controller.rb', line 25

def update
  authorize_resource!(@resource)

  if @resource.amount_used.positive? && update_attrs.key?(:amount)
    render_error(
      code: 'store_credit_in_use',
      message: 'Cannot change amount on a store credit that has already been used',
      status: :unprocessable_content
    )
    return
  end

  if @resource.update(update_attrs)
    render json: serialize_resource(@resource.reload)
  else
    render_validation_error(@resource.errors)
  end
end