Class: Api::V5::PointsController

Inherits:
BaseController
  • Object
show all
Includes:
Concerns::Authorization, Netcore::Payload
Defined in:
app/controllers/api/v5/points_controller.rb

Overview

This class should be renamed to RewardsController, so it will have same name with the existing model /api/v5/users/points

Constant Summary

Constants inherited from BaseController

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

Instance Method Summary collapse

Methods included from Netcore::Payload

#fav_payload, #reservation_data_payload, #reward_data_payload, #user_loyalty_payload, #user_properties, #user_properties_for_client

Methods included from CountryIdMemoization

#malaysia_id, #singapore_id, #thailand_id

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

#adjusted_limitsObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/controllers/api/v5/points_controller.rb', line 59

def adjusted_limits
  total_amount = params[:total_amount]

  if total_amount.blank?
    error_message = 'total_amount is required.'
  end

  country_code = params[:country_code]
  if country_code.present? && ApiV5::Constants::COUNTRY_CODES.exclude?(country_code.to_s.upcase)
    error_message = "country_code must be one of: #{ApiV5::Constants::COUNTRY_CODES.join(', ')}"
  end

  user_adjusted_points = ReservationService::UserAdjustedPoints.new(
    current_user, total_amount, country_code
  ).calculate

  if error_message
    render json: { success: !error_message, message: error_message, data: nil }, status: :unprocessable_entity
  else
    render json: {
      data: {
        user_adjusted_points: user_adjusted_points,
      },
      message: nil,
      success: true,
    }
  end
end

#expiryObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/controllers/api/v5/points_controller.rb', line 88

def expiry
  set_status_header(true)
  usr_loyalty = user_loyalty(current_user).loyalty_level

  data = {
    total_points: total_points,
    user_loyalty: usr_loyalty.as_json(only: [:id, :name]),
    expiry_date: Reward.points_expiry_date(current_user),
  }

  render json: {
    data: data,
    success: true,
    message: nil,
  }
end

#historyObject

GET /history get redeemed history



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/api/v5/points_controller.rb', line 17

def history
  set_status_header(true)

  options = {
    meta: {
      total_points: total_points,
    },
  }

  page = params.fetch(:page, {})

  filter = Api::V5::RewardsFilter.new current_user.rewards.order('id DESC')
  filter.page_number(page.fetch(:number, 1)).
    per_page(page.fetch(:size, 10))
  render json: filter.as_json(serialization_context, minor_version_param, options).merge(success: true, message: nil)
end

#redeem_as_voucherObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/controllers/api/v5/points_controller.rb', line 34

def redeem_as_voucher
  payload = {
    user: current_user,
    redeemed_points: params.require(:redeem_amount).to_i,
    points_country_code: params[:points_country_code],
  }
  redeem_operation = Redemption::RedeemAsVoucher.new(payload)

  if redeem_operation.execute
    render json: { success: true, message: I18n.t('actions.redemption.redeemed') }
  else
    render json: { message: redeem_operation.error_message_simple, success: false }
  end
end

#totalObject

GET /total



50
51
52
53
54
55
56
57
# File 'app/controllers/api/v5/points_controller.rb', line 50

def total
  set_status_header(true)
  render json: {
    data: total_points,
    success: true,
    message: nil,
  }
end