Class: Spree::Api::V3::Store::NewsletterSubscribersController

Inherits:
BaseController show all
Defined in:
app/controllers/spree/api/v3/store/newsletter_subscribers_controller.rb

Constant Summary

Constants included from ChannelResolution

ChannelResolution::CHANNEL_HEADER

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 included from ApiKeyAuthentication

#authenticate_api_key!, #authenticate_secret_key!

Methods included from JwtAuthentication

#authenticate_user, #require_authentication!

Instance Method Details

#createObject

POST /api/v3/store/newsletter_subscribers



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/controllers/spree/api/v3/store/newsletter_subscribers_controller.rb', line 13

def create
  subscriber = Spree::NewsletterSubscriber.subscribe(
    email: params[:email],
    user: current_user,
    store: current_store,
    redirect_url: validated_redirect_url
  )

  if subscriber.errors.any?
    render_errors(subscriber.errors)
  else
    render json: serialize_resource(subscriber), status: :created
  end
end

#verifyObject

POST /api/v3/store/newsletter_subscribers/verify



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/controllers/spree/api/v3/store/newsletter_subscribers_controller.rb', line 29

def verify
  token = params[:token]

  if token.blank?
    return render_error(
      code: ERROR_CODES[:parameter_missing],
      message: 'token is required',
      status: :unprocessable_content
    )
  end

  subscriber = Spree::NewsletterSubscriber.for_store(current_store).unverified.find_by(verification_token: token)

  unless subscriber
    return render_error(
      code: ERROR_CODES[:invalid_token],
      message: Spree.t(:newsletter_verification_token_invalid, scope: :api),
      status: :unprocessable_content
    )
  end

  Spree::Newsletter::Verify.new(subscriber: subscriber).call

  render json: serialize_resource(subscriber)
end