2018-10-25 16:51:20 +02:00
|
|
|
/* eslint-disable
|
|
|
|
no-return-assign,
|
|
|
|
no-undef,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2018-10-25 16:50:16 +02:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
|
|
|
/* COMMON CODE */
|
|
|
|
|
|
|
|
// The validity per user defines how many time a user may ba able to use the same coupon
|
|
|
|
// Here are the various options for this parameter
|
2018-10-25 16:51:20 +02:00
|
|
|
const userValidities = ['once', 'forever']
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-21 09:42:26 +01:00
|
|
|
/**
|
|
|
|
* Controller used in the coupon creation page
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
Application.Controllers.controller('NewCouponController', ['$scope', '$state', 'Coupon', 'growl', '_t',
|
|
|
|
function ($scope, $state, Coupon, growl, _t) {
|
2018-11-21 09:42:26 +01:00
|
|
|
// Values for the coupon currently created
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.coupon = {
|
|
|
|
active: true,
|
|
|
|
type: 'percent_off'
|
2018-10-25 16:50:16 +02:00
|
|
|
}
|
2016-08-08 12:08:09 +02:00
|
|
|
|
2018-11-21 09:42:26 +01:00
|
|
|
// Options for the validity per user
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.validities = userValidities
|
|
|
|
|
2018-11-21 09:42:26 +01:00
|
|
|
// Default parameters for AngularUI-Bootstrap datepicker (used for coupon validity limit selection)
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.datePicker = {
|
|
|
|
format: Fablab.uibDateFormat,
|
|
|
|
opened: false, // default: datePicker is not shown
|
|
|
|
minDate: moment().toDate(),
|
|
|
|
options: {
|
|
|
|
startingDay: Fablab.weekStartingDay
|
|
|
|
}
|
|
|
|
}
|
2016-08-08 12:08:09 +02:00
|
|
|
|
2018-11-21 09:42:26 +01:00
|
|
|
/**
|
|
|
|
* Shows/hides the validity limit datepicker
|
|
|
|
* @param $event {Object} jQuery event object
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.toggleDatePicker = function ($event) {
|
|
|
|
$event.preventDefault()
|
|
|
|
$event.stopPropagation()
|
|
|
|
return $scope.datePicker.opened = !$scope.datePicker.opened
|
|
|
|
}
|
2016-08-08 12:08:09 +02:00
|
|
|
|
2018-11-21 09:42:26 +01:00
|
|
|
/**
|
|
|
|
* Callback to save the new coupon in $scope.coupon and redirect the user to the listing page
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
return $scope.saveCoupon = () =>
|
|
|
|
Coupon.save({ coupon: $scope.coupon }, coupon => $state.go('app.admin.pricing')
|
|
|
|
, function (err) {
|
|
|
|
growl.error(_t('unable_to_create_the_coupon_check_code_already_used'))
|
|
|
|
return console.error(err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
])
|
|
|
|
|
2018-11-21 09:42:26 +01:00
|
|
|
/**
|
|
|
|
* Controller used in the coupon edition page
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
Application.Controllers.controller('EditCouponController', ['$scope', '$state', 'Coupon', 'couponPromise', '_t', 'growl',
|
|
|
|
function ($scope, $state, Coupon, couponPromise, _t, growl) {
|
2018-10-25 16:50:16 +02:00
|
|
|
/* PUBLIC SCOPE */
|
2016-08-04 18:13:19 +02:00
|
|
|
|
2018-11-21 09:42:26 +01:00
|
|
|
// Used in the form to freeze unmodifiable fields
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.mode = 'EDIT'
|
2016-08-04 18:13:19 +02:00
|
|
|
|
2018-11-21 09:42:26 +01:00
|
|
|
// Coupon to edit
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.coupon = couponPromise
|
2016-08-08 12:08:09 +02:00
|
|
|
|
2018-11-21 09:42:26 +01:00
|
|
|
// Options for the validity per user
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.validities = userValidities
|
2016-08-08 12:08:09 +02:00
|
|
|
|
2018-11-21 09:42:26 +01:00
|
|
|
// Mapping for validation errors
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.errors = {}
|
2016-08-08 14:42:17 +02:00
|
|
|
|
2018-11-21 09:42:26 +01:00
|
|
|
// Default parameters for AngularUI-Bootstrap datepicker (used for coupon validity limit selection)
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.datePicker = {
|
|
|
|
format: Fablab.uibDateFormat,
|
|
|
|
opened: false, // default: datePicker is not shown
|
|
|
|
minDate: moment().toDate(),
|
|
|
|
options: {
|
|
|
|
startingDay: Fablab.weekStartingDay
|
|
|
|
}
|
2018-10-25 16:50:16 +02:00
|
|
|
}
|
2016-08-08 12:08:09 +02:00
|
|
|
|
2018-11-21 09:42:26 +01:00
|
|
|
/**
|
|
|
|
* Shows/hides the validity limit datepicker
|
|
|
|
* @param $event {Object} jQuery event object
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.toggleDatePicker = function ($event) {
|
|
|
|
$event.preventDefault()
|
|
|
|
$event.stopPropagation()
|
|
|
|
return $scope.datePicker.opened = !$scope.datePicker.opened
|
|
|
|
}
|
2016-08-08 12:08:09 +02:00
|
|
|
|
2018-11-21 09:42:26 +01:00
|
|
|
/**
|
|
|
|
* Callback to save the coupon's changes to the API
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.updateCoupon = function () {
|
|
|
|
$scope.errors = {}
|
|
|
|
return Coupon.update({ id: $scope.coupon.id }, { coupon: $scope.coupon }, coupon => $state.go('app.admin.pricing')
|
|
|
|
, function (err) {
|
|
|
|
growl.error(_t('unable_to_update_the_coupon_an_error_occurred'))
|
|
|
|
return $scope.errors = err.data
|
|
|
|
})
|
|
|
|
}
|
2016-08-08 12:08:09 +02:00
|
|
|
|
2018-10-25 16:51:20 +02:00
|
|
|
/* PRIVATE SCOPE */
|
2016-08-08 12:08:09 +02:00
|
|
|
|
2018-11-21 09:42:26 +01:00
|
|
|
/**
|
|
|
|
* Kind of constructor: these actions will be realized first when the controller is loaded
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
const initialize = function () {
|
2018-10-25 16:50:16 +02:00
|
|
|
// parse the date if any
|
2018-10-25 16:51:20 +02:00
|
|
|
if (couponPromise.valid_until) {
|
|
|
|
return $scope.coupon.valid_until = moment(couponPromise.valid_until).toDate()
|
|
|
|
}
|
2018-10-25 16:50:16 +02:00
|
|
|
}
|
2016-08-08 12:08:09 +02:00
|
|
|
|
2018-11-21 09:42:26 +01:00
|
|
|
// !!! MUST BE CALLED AT THE END of the controller
|
2018-10-25 16:51:20 +02:00
|
|
|
return initialize()
|
|
|
|
}
|
|
|
|
])
|