diff --git a/app/assets/javascripts/directives/coupon.coffee.erb b/app/assets/javascripts/directives/coupon.coffee.erb index 3caf5d36d..531116100 100644 --- a/app/assets/javascripts/directives/coupon.coffee.erb +++ b/app/assets/javascripts/directives/coupon.coffee.erb @@ -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})) diff --git a/app/assets/templates/machines/reserve.html.erb b/app/assets/templates/machines/reserve.html.erb index 7c0bc4c6e..f80ffb700 100644 --- a/app/assets/templates/machines/reserve.html.erb +++ b/app/assets/templates/machines/reserve.html.erb @@ -78,7 +78,7 @@
{{ 'remove_this_slot' }}
- +
diff --git a/app/assets/templates/shared/_coupon.html.erb b/app/assets/templates/shared/_coupon.html.erb index 6512730fa..d6661868b 100644 --- a/app/assets/templates/shared/_coupon.html.erb +++ b/app/assets/templates/shared/_coupon.html.erb @@ -1,5 +1,5 @@
- {{ ' i_have_a_coupon' }} + {{ 'i_have_a_coupon' }}
diff --git a/app/controllers/api/coupons_controller.rb b/app/controllers/api/coupons_controller.rb index fbdbef9ae..902d100dc 100644 --- a/app/controllers/api/coupons_controller.rb +++ b/app/controllers/api/coupons_controller.rb @@ -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 diff --git a/app/models/coupon.rb b/app/models/coupon.rb index 7ccf64f63..74d2efba8 100644 --- a/app/models/coupon.rb +++ b/app/models/coupon.rb @@ -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 diff --git a/config/locales/app.shared.en.yml b/config/locales/app.shared.en.yml index df648243f..4cc878958 100644 --- a/config/locales/app.shared.en.yml +++ b/config/locales/app.shared.en.yml @@ -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." \ No newline at end of file diff --git a/config/locales/app.shared.fr.yml b/config/locales/app.shared.fr.yml index 1ac4f2af4..c5fd7f0ac 100644 --- a/config/locales/app.shared.fr.yml +++ b/config/locales/app.shared.fr.yml @@ -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." \ No newline at end of file