1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-29 18:52:22 +01:00

coupon edition + refactored coupon status

This commit is contained in:
Sylvain 2016-08-08 15:21:33 +02:00
parent a6ded12ee9
commit e2ea64cf9c
11 changed files with 42 additions and 36 deletions

View File

@ -63,11 +63,12 @@ Application.Controllers.controller "EditCouponController", ["$scope", "$state",
### PUBLIC SCOPE ###
## Used in the form to freeze unmodifiable fields
$scope.mode = 'EDIT'
## Coupon to edit
$scope.coupon = couponPromise
## Options for the validity per user
$scope.validities = userValidities

View File

@ -311,22 +311,6 @@ Application.Controllers.controller "EditPricingController", ["$scope", "$state",
##
# Return a textual status concerning the given coupon
# @param coupon {Object}
# @return {string}
##
$scope.getCouponStatus = (coupon) ->
unless coupon.active
return _t('disabled')
if coupon.valid_until and moment(coupon.valid_until).isBefore()
return _t('expired')
if coupon.max_usages and coupon.usages >= coupon.max_usages
return _t('sold_out')
return _t('active')
##
# Delete a coupon from the server's database and, in case of success, from the list in memory
# @param coupons {Array<Object>} should be called with $scope.coupons

View File

@ -15,6 +15,7 @@
class="form-control"
ng-model="coupon.code"
ng-pattern="/^[A-Z0-9]+$/"
ng-disabled="mode == 'EDIT'"
required="required"/>
<span class="help-block error" ng-show="couponForm['coupon[code]'].$dirty && couponForm['coupon[code]'].$error.required" translate>{{ 'code_is_required' }}</span>
<span class="help-block error" ng-show="couponForm['coupon[code]'].$dirty && couponForm['coupon[code]'].$error.pattern" translate>{{ 'code_must_be_composed_of_capital_letters_and_or_digits' }}</span>
@ -29,6 +30,7 @@
ng-model="coupon.percent_off"
min="0"
max="100"
ng-disabled="mode == 'EDIT'"
required="required"/>
<span class="input-group-addon"><i class="fa fa-percent"></i></span>
</div>
@ -43,9 +45,8 @@
class="form-control"
ng-model="coupon.validity_per_user"
required="required"
ng-disabled="method == 'PATCH'"
ng-options="( validity | translate ) for validity in validities"
required>
ng-disabled="mode == 'EDIT'"
ng-options="( validity | translate ) for validity in validities">
</select>
<span class="help-block error" ng-show="couponForm['coupon[validity_per_user]'].$dirty && couponForm['coupon[validity_per_user]'].$error.required" translate>{{ 'validity_per_user_is_required' }}</span>
</div>
@ -61,9 +62,10 @@
datepicker-options="datePicker.options"
is-open="datePicker.opened"
min-date="datePicker.minDate"
ng-disabled="mode == 'EDIT'"
ng-click="toggleDatePicker($event)"/>
<span class="input-group-btn">
<button type="button" class="btn btn-default" ng-click="toggleDatePicker($event)"><i class="fa fa-calendar"></i></button>
<button type="button" class="btn btn-default" ng-click="toggleDatePicker($event)" ng-disabled="mode == 'EDIT'"><i class="fa fa-calendar"></i></button>
</span>
</div>
</div>
@ -74,6 +76,7 @@
name="coupon[max_usages]"
class="form-control"
ng-model="coupon.max_usages"
ng-disabled="mode == 'EDIT'"
min="0"/>
<span class="help-block error" ng-show="couponForm['coupon[max_usages]'].$dirty && couponForm['coupon[max_usages]'].$error.min" translate>{{ 'max_usages_must_be_equal_or_greater_than_0' }}</span>
</div>

View File

@ -6,6 +6,7 @@
<tr>
<th translate>{{ 'name' }}</th>
<th translate>{{ 'percentage_off' }}</th>
<th translate>{{ 'nb_of_usages' }}</th>
<th translate>{{ 'status' }}</th>
<th></th>
</tr>
@ -14,7 +15,8 @@
<tr ng-repeat="coupon in coupons">
<td>{{coupon.name}}</td>
<td>{{coupon.percent_off}} %</td>
<td>{{getCouponStatus(coupon)}}</td>
<td>{{coupon.usages}}</td>
<td translate>{{coupon.status}}</td>
<td>
<button type="button" class="btn btn-default" ui-sref="app.admin.coupons_edit({id:coupon.id})"><i class="fa fa-pencil-square-o"></i></button>
<button type="button" class="btn btn-danger" ng-click="deleteCoupon(coupons, coupon.id)"><i class="fa fa-trash"></i></button>

View File

@ -23,20 +23,19 @@ class API::CouponsController < API::ApiController
@coupon = Coupon.find_by_code(params[:code])
if @coupon.nil?
render json: {status: 'rejected'}, status: :not_found
elsif not @coupon.active?
render json: {status: 'disabled'}, status: :unauthorized
elsif @coupon.valid_until.is < DateTime.now
render json: {status: 'expired'}, status: :unauthorized
elsif @coupon.max_usages >= @coupon.invoices.size
render json: {status: 'sold_out'}, status: :unauthorized
else
render :validate, status: :ok, location: @coupon
status = @coupon.status
if status != 'active'
render json: {status: status}, status: :unauthorized
else
render :validate, status: :ok, location: @coupon
end
end
end
def update
authorize Coupon
if @coupon.update(coupon_params)
if @coupon.update(coupon_editable_params)
render :show, status: :ok, location: @coupon
else
render json: @coupon.errors, status: :unprocessable_entity
@ -60,4 +59,8 @@ class API::CouponsController < API::ApiController
def coupon_params
params.require(:coupon).permit(:name, :code, :percent_off, :validity_per_user, :valid_until, :max_usages, :active)
end
def coupon_editable_params
params.require(:coupon).permit(:name, :active)
end
end

View File

@ -19,6 +19,18 @@ class Coupon < ActiveRecord::Base
end
end
def status
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'
else
'active'
end
end
def create_stripe_coupon
StripeWorker.perform_async(:create_stripe_coupon, id)
end

View File

@ -1,2 +1,3 @@
json.extract! coupon, :id, :name, :code, :percent_off, :valid_until, :validity_per_user, :max_usages, :active, :created_at
json.usages coupon.invoices.count
json.usages coupon.invoices.count
json.status coupon.status

View File

@ -1,5 +1,3 @@
json.cache! [@coupons] do
json.array!(@coupons) do |coupon|
json.partial! 'api/coupons/coupon', coupon: coupon
end
json.array!(@coupons) do |coupon|
json.partial! 'api/coupons/coupon', coupon: coupon
end

View File

@ -1 +1 @@
json.extract! @coupon, :id, :code, :percent_off, :stp_coupon_id
json.extract! @coupon, :id, :code, :percent_off

View File

@ -147,6 +147,7 @@ en:
coupons: "Coupons"
list_of_the_coupons: "List of the coupons"
percentage_off: "Percentage off"
nb_of_usages: "Number of usages"
status: "Status"
add_a_new_coupon: "Add a new coupon"
disabled: "Disabled"

View File

@ -147,6 +147,7 @@ fr:
coupons: "Codes promotionnels"
list_of_the_coupons: "Liste des codes promotionnels"
percentage_off: "Pourcentage de réduction"
nb_of_usages: "Nombre d'utilisations"
status: "Statut"
add_a_new_coupon: "Ajouter un code promotionnel"
disabled: "Désactivé"