mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-01-17 06:52:27 +01:00
validation of validity_per_user on coupon usage
This commit is contained in:
parent
b4e28b94f3
commit
def5384a38
@ -4,6 +4,7 @@ Application.Directives.directive 'coupon', [ 'Coupon', 'growl', '_t', (Coupon, g
|
||||
scope:
|
||||
show: '='
|
||||
coupon: '='
|
||||
userId: '@'
|
||||
templateUrl: '<%= asset_path "shared/_coupon.html" %>'
|
||||
link: ($scope, element, attributes) ->
|
||||
|
||||
@ -27,7 +28,7 @@ Application.Directives.directive 'coupon', [ 'Coupon', 'growl', '_t', (Coupon, g
|
||||
$scope.status = 'pending'
|
||||
$scope.coupon = null
|
||||
else
|
||||
Coupon.validate {code: $scope.couponCode}, (res) ->
|
||||
Coupon.validate {code: $scope.couponCode, user_id: $scope.userId}, (res) ->
|
||||
$scope.status = 'valid'
|
||||
$scope.coupon = res
|
||||
growl.success(_t('the_coupon_has_been_applied_you_get_PERCENT_discount', {PERCENT: res.percent_off}))
|
||||
|
@ -78,7 +78,7 @@
|
||||
<div class="clear"><a class="pull-right m-b-sm text-u-l ng-scope m-r-sm" href="#" ng-click="removeMachineSlot(machineSlot, $event)" ng-if="machineSlot.isValid" translate>{{ 'remove_this_slot' }}</a></div>
|
||||
</div>
|
||||
|
||||
<coupon show="machineSlotsValid() && (!plansAreShown || selectedPlan)" coupon="coupon.applied" callback="setCouponCb()"></coupon>
|
||||
<coupon show="machineSlotsValid() && (!plansAreShown || selectedPlan)" coupon="coupon.applied" user-id="{{ctrl.member.id}}"></coupon>
|
||||
|
||||
<span ng-hide="fablabWithoutPlans">
|
||||
<div ng-if="machineSlotsValid() && !ctrl.member.subscribed_plan" ng-show="!plansAreShown">
|
||||
|
@ -1,5 +1,5 @@
|
||||
<div class="form-group m-b-lg" ng-show="show">
|
||||
<a ng-click="code. input = true" ng-hide="code.input" class="b-b" translate>{{ ' i_have_a_coupon' }}</a>
|
||||
<a ng-click="code. input = true" ng-hide="code.input" class="b-b" translate>{{ 'i_have_a_coupon' }}</a>
|
||||
|
||||
<div ng-show="code.input">
|
||||
<label for="coupon_code" translate>{{ 'code_' }}</label>
|
||||
|
@ -24,7 +24,13 @@ class API::CouponsController < API::ApiController
|
||||
if @coupon.nil?
|
||||
render json: {status: 'rejected'}, status: :not_found
|
||||
else
|
||||
status = @coupon.status
|
||||
if !current_user.is_admin?
|
||||
_user_id = current_user.id
|
||||
else
|
||||
_user_id = params[:user_id]
|
||||
end
|
||||
|
||||
status = @coupon.status(_user_id)
|
||||
if status != 'active'
|
||||
render json: {status: status}, status: :unprocessable_entity
|
||||
else
|
||||
|
@ -21,18 +21,43 @@ class Coupon < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
|
||||
def status
|
||||
##
|
||||
# Check the status of the current coupon. The coupon:
|
||||
# - may have been disabled by an admin,
|
||||
# - may has expired because the validity date has been reached,
|
||||
# - may have been used the maximum number of times it was allowed
|
||||
# - may have already been used by the provided user, if the coupon is configured to allow only one use per user,
|
||||
# - may be available for use
|
||||
# @param [user_id] {Number} if provided and if the current coupon's validity_per_user == 'once', check that the coupon
|
||||
# was already used by the provided user
|
||||
# @return {String} status identifier
|
||||
##
|
||||
def status(user_id = nil)
|
||||
if not active?
|
||||
'disabled'
|
||||
elsif (!valid_until.nil?) and valid_until.at_end_of_day < DateTime.now
|
||||
'expired'
|
||||
elsif (!max_usages.nil?) and invoices.count >= max_usages
|
||||
'sold_out'
|
||||
elsif (!user_id.nil?) and validity_per_user == 'once' and users_ids.include?(user_id.to_i)
|
||||
'already_used'
|
||||
else
|
||||
'active'
|
||||
end
|
||||
end
|
||||
|
||||
def users
|
||||
self.invoices.map do |i|
|
||||
i.user
|
||||
end
|
||||
end
|
||||
|
||||
def users_ids
|
||||
users.map do |u|
|
||||
u.id
|
||||
end
|
||||
end
|
||||
|
||||
def create_stripe_coupon
|
||||
StripeWorker.perform_async(:create_stripe_coupon, id)
|
||||
end
|
||||
|
@ -358,4 +358,5 @@ en:
|
||||
unable_to_apply_the_coupon_because_disabled: "Unable to apply the coupon: this code was disabled."
|
||||
unable_to_apply_the_coupon_because_expired: "Unable to apply the coupon: this code has expired."
|
||||
unable_to_apply_the_coupon_because_sold_out: "Unable to apply the coupon: this code reached its quota."
|
||||
unable_to_apply_the_coupon_because_already_used: "Unable to apply the coupon: you have already used this code once before."
|
||||
unable_to_apply_the_coupon_because_rejected: "This code does not exists."
|
@ -358,4 +358,5 @@ fr:
|
||||
unable_to_apply_the_coupon_because_disabled: "Impossible d'appliquer la réduction : ce code promo a été désactivé."
|
||||
unable_to_apply_the_coupon_because_expired: "Impossible d'appliquer la réduction : ce code promo a expiré."
|
||||
unable_to_apply_the_coupon_because_sold_out: "Impossible d'appliquer la réduction : ce code promo a atteint son quota."
|
||||
unable_to_apply_the_coupon_because_already_used: "Impossible d'appliquer la réduction : vous avez déjà utilisé ce code promo par le passé."
|
||||
unable_to_apply_the_coupon_because_rejected: "Ce code promo n'existe pas."
|
Loading…
x
Reference in New Issue
Block a user