/
create-discount-code.js
181 lines (174 loc) · 5.55 KB
/
create-discount-code.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import Component from '@ember/component';
import { computed } from '@ember/object';
import FormMixin from 'open-event-frontend/mixins/form';
import { later } from '@ember/runloop';
import { currencySymbol } from 'open-event-frontend/helpers/currency-symbol';
export default Component.extend(FormMixin, {
getValidationRules() {
window.$.fn.form.settings.rules.checkMaxMin = () => {
return this.data.minQuantity <= this.data.maxQuantity;
};
window.$.fn.form.settings.rules.checkMaxTotal = () => {
return this.data.maxQuantity <= this.data.ticketsNumber;
};
window.$.fn.form.settings.rules.checkTicketSelected = () => {
const tickets = this.eventTickets;
for (const ticket of tickets) {
if (ticket.isChecked) {
return true;
}
}
return false;
};
// TODO: Removing the Discount Code Time Validations due to the weird and buggy behaviour. Will be restored once a perfect solution is found. Please check issue: https://github.com/fossasia/open-event-frontend/issues/3667
return {
inline : true,
delay : false,
on : 'blur',
fields: {
discountCode: {
identifier : 'discount_code',
rules : [
{
type : 'empty',
prompt : this.l10n.t('Please enter a discount code')
},
{
type : 'regExp',
value : '^[a-zA-Z0-9_-]*$'
}
]
},
numberOfdiscountTickets: {
identifier : 'number_of_discount_tickets',
rules : [
{
type : 'empty',
prompt : this.l10n.t('Please enter the number of tickets')
}
]
},
discountAmount: {
identifier : 'discount_amount',
rules : [
{
type : 'empty',
prompt : this.l10n.t('Please enter the discount amount')
}
]
},
discountPercent: {
identifier : 'discount_percent',
rules : [
{
type : 'empty',
prompt : this.l10n.t('Please enter the discount percentage')
}
]
},
minOrder: {
identifier : 'min_order',
optional : true,
rules : [
{
type : 'integer',
prompt : this.l10n.t('Please enter a valid integer')
},
{
type : 'checkMaxMin',
prompt : this.l10n.t('Minimum value should not be greater than maximum')
}
]
},
maxOrder: {
identifier : 'max_order',
optional : true,
rules : [
{
type : 'integer',
prompt : this.l10n.t('Please enter a valid integer')
},
{
type : 'checkMaxMin',
prompt : this.l10n.t('Maximum value should not be less than minimum')
},
{
type : 'checkMaxTotal',
prompt : this.l10n.t('Maximum value should not be greater than number of tickets')
}
]
},
ticketName: {
identifier : 'ticket_name',
rules : [
{
type : 'checkTicketSelected',
prompt : this.l10n.t('Please select at least 1 ticket.')
}
]
}
}
};
},
discountLink: computed('data.code', function() {
const { params } = this.router._router.currentState.routerJsState;
const origin = this.fastboot.isFastBoot ? `${this.fastboot.request.protocol}//${this.fastboot.request.host}` : location.origin;
const link = origin + this.router.urlFor('public', params['events.view'].event_id, { queryParams: { code: this.data.code } });
this.set('data.discountUrl', link);
return link;
}),
amountLabel: computed('event.paymentCurrency', function() {
return `Amount (${currencySymbol([this.event.paymentCurrency])})`;
}),
eventTickets: computed('tickets', function() {
return this.tickets.filter(ticket => ticket.type === 'paidRegistration' || ticket.type === 'paid');
}),
allTicketTypesChecked: computed('tickets', function() {
if (this.eventTickets.length && this.data.tickets.length === this.eventTickets.length) {
return true;
}
return false;
}),
isLinkSuccess: false,
actions: {
toggleAllSelection(allTicketTypesChecked) {
this.toggleProperty('allTicketTypesChecked');
const tickets = this.eventTickets;
if (allTicketTypesChecked) {
this.set('data.tickets', tickets.slice());
} else {
this.data.tickets.clear();
}
tickets.forEach(ticket => {
ticket.set('isChecked', allTicketTypesChecked);
});
},
updateTicketsSelection(ticket) {
if (!ticket.get('isChecked')) {
this.data.tickets.pushObject(ticket);
ticket.set('isChecked', true);
if (this.data.tickets.length === this.eventTickets.length) {
this.set('allTicketTypesChecked', true);
}
} else {
this.data.tickets.removeObject(ticket);
ticket.set('isChecked', false);
this.set('allTicketTypesChecked', false);
}
},
submit(data) {
this.onValid(() => {
this.sendAction('save', data);
});
},
copiedText() {
this.set('isLinkSuccess', true);
later(this, () => {
this.set('isLinkSuccess', false);
}, 5000);
},
onChange() {
this.onValid(() => {});
}
}
});