Class: Api::V5::SubscribeEmailsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/api/v5/subscribe_emails_controller.rb

Overview

typed: ignore frozen_string_literal: true

Constant Summary

Constants inherited from BaseController

BaseController::CACHE_NAMESPACE, BaseController::INTERNAL_SERVER_ERROR_MESSAGE, BaseController::ResponseSchema

Instance Method Summary collapse

Methods inherited from BaseController

#identity_cache_memoization

Methods included from LogrageCustomLogger

#append_info_to_payload

Methods included from ResponseCacheConcern

#my_response_cache

Instance Method Details

#createObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/controllers/api/v5/subscribe_emails_controller.rb', line 5

def create
  email = params.require(:email).to_s.downcase
  user = User.find_by(email: email)
  if user.blank?
    render json: { success: false, message: 'User not found', data: nil }, status: :not_found
    return
  end

  unless user.subscribe_marketing_email?
    user.update!(subscribe_marketing_email: true)
  end

  render json: {
    success: true,
    message: 'Email subscribed successfully',
    data: { email: email },
  }, status: :ok
rescue StandardError => e
  APMErrorHandler.report("Failed to sync user data for email subscription: #{email}", error: e)
  render json: {
    success: false,
    message: 'Sorry, something went wrong',
    data: nil,
  }, status: :internal_server_error
end