2019-01-16 16:28:25 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# API Controller for resources of type Coupon
|
|
|
|
# Coupons are used in payments
|
2016-08-03 17:56:36 +02:00
|
|
|
class API::CouponsController < API::ApiController
|
2022-12-01 15:43:16 +01:00
|
|
|
include ApplicationHelper
|
|
|
|
|
2022-09-07 17:52:23 +02:00
|
|
|
before_action :authenticate_user!, except: %i[validate]
|
2019-01-16 16:28:25 +01:00
|
|
|
before_action :set_coupon, only: %i[show update destroy]
|
2016-08-03 17:56:36 +02:00
|
|
|
|
2019-04-04 17:28:29 +02:00
|
|
|
# Number of notifications added to the page when the user clicks on 'load next notifications'
|
|
|
|
COUPONS_PER_PAGE = 10
|
|
|
|
|
2016-08-03 17:56:36 +02:00
|
|
|
def index
|
2019-04-08 11:00:00 +02:00
|
|
|
@coupons = Coupon.method(params[:filter]).call.page(params[:page]).per(COUPONS_PER_PAGE).order('created_at DESC')
|
|
|
|
@total = Coupon.method(params[:filter]).call.length
|
2016-08-03 17:56:36 +02:00
|
|
|
end
|
|
|
|
|
2019-01-16 16:28:25 +01:00
|
|
|
def show; end
|
2016-08-03 17:56:36 +02:00
|
|
|
|
|
|
|
def create
|
|
|
|
authorize Coupon
|
|
|
|
@coupon = Coupon.new(coupon_params)
|
|
|
|
if @coupon.save
|
|
|
|
render :show, status: :created, location: @coupon
|
|
|
|
else
|
|
|
|
render json: @coupon.errors, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-08 14:42:17 +02:00
|
|
|
def validate
|
2016-11-23 16:30:19 +01:00
|
|
|
@coupon = Coupon.find_by(code: params[:code])
|
2016-08-08 14:42:17 +02:00
|
|
|
if @coupon.nil?
|
2019-01-16 16:28:25 +01:00
|
|
|
render json: { status: 'rejected' }, status: :not_found
|
2016-08-08 14:42:17 +02:00
|
|
|
else
|
2022-12-01 15:43:16 +01:00
|
|
|
user_id = if current_user&.admin?
|
|
|
|
params[:user_id]
|
|
|
|
else
|
|
|
|
current_user&.id
|
|
|
|
end
|
|
|
|
|
|
|
|
status = @coupon.status(user_id, to_centimes(params[:amount]))
|
2022-09-07 17:52:23 +02:00
|
|
|
if status == 'active'
|
2016-08-08 15:21:33 +02:00
|
|
|
render :validate, status: :ok, location: @coupon
|
2022-09-07 17:52:23 +02:00
|
|
|
else
|
|
|
|
render json: { status: status }, status: :unprocessable_entity
|
2016-08-08 15:21:33 +02:00
|
|
|
end
|
2016-08-08 14:42:17 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-03 17:56:36 +02:00
|
|
|
def update
|
|
|
|
authorize Coupon
|
2016-08-08 15:21:33 +02:00
|
|
|
if @coupon.update(coupon_editable_params)
|
2016-08-03 17:56:36 +02:00
|
|
|
render :show, status: :ok, location: @coupon
|
|
|
|
else
|
|
|
|
render json: @coupon.errors, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
authorize Coupon
|
2016-08-08 12:25:27 +02:00
|
|
|
if @coupon.safe_destroy
|
|
|
|
head :no_content
|
|
|
|
else
|
|
|
|
head :unprocessable_entity
|
|
|
|
end
|
2016-08-03 17:56:36 +02:00
|
|
|
end
|
|
|
|
|
2016-08-16 18:12:13 +02:00
|
|
|
def send_to
|
|
|
|
authorize Coupon
|
|
|
|
|
2016-11-23 16:30:19 +01:00
|
|
|
@coupon = Coupon.find_by(code: params[:coupon_code])
|
2019-01-16 16:28:25 +01:00
|
|
|
if @coupon.nil?
|
|
|
|
render json: { error: "no coupon with code #{params[:coupon_code]}" }, status: :not_found
|
|
|
|
elsif @coupon.send_to(params[:user_id])
|
|
|
|
render :show, status: :ok, location: @coupon
|
|
|
|
else
|
|
|
|
render json: @coupon.errors, status: :unprocessable_entity
|
|
|
|
end
|
2016-08-16 18:12:13 +02:00
|
|
|
end
|
|
|
|
|
2016-08-03 17:56:36 +02:00
|
|
|
private
|
2019-01-16 16:28:25 +01:00
|
|
|
|
2016-08-03 17:56:36 +02:00
|
|
|
def set_coupon
|
|
|
|
@coupon = Coupon.find(params[:id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def coupon_params
|
2016-11-23 15:44:59 +01:00
|
|
|
if @parameters
|
|
|
|
@parameters
|
|
|
|
else
|
|
|
|
@parameters = params
|
2022-12-01 15:43:16 +01:00
|
|
|
@parameters[:coupon][:amount_off] = to_centimes(@parameters[:coupon][:amount_off]) if @parameters[:coupon][:amount_off]
|
2016-11-23 15:44:59 +01:00
|
|
|
|
2019-01-16 16:28:25 +01:00
|
|
|
@parameters = @parameters.require(:coupon).permit(:name, :code, :percent_off, :amount_off, :validity_per_user, :valid_until,
|
|
|
|
:max_usages, :active)
|
2016-11-23 15:44:59 +01:00
|
|
|
end
|
2016-08-03 17:56:36 +02:00
|
|
|
end
|
2016-08-08 15:21:33 +02:00
|
|
|
|
|
|
|
def coupon_editable_params
|
2016-12-13 12:01:34 +01:00
|
|
|
params.require(:coupon).permit(:name, :active, :valid_until)
|
2016-08-08 15:21:33 +02:00
|
|
|
end
|
2016-08-03 17:56:36 +02:00
|
|
|
end
|