370 lines
14 KiB
JavaScript
370 lines
14 KiB
JavaScript
supersamApp.controller('SmsSubscribesController', [
|
|
'$scope',
|
|
'$http',
|
|
'$timeout',
|
|
'config',
|
|
'$filter',
|
|
'Upload',
|
|
function($scope,
|
|
$http,
|
|
$timeout,
|
|
config,
|
|
$filter,
|
|
Upload) {
|
|
var SmsSubscribes;
|
|
SmsSubscribes = (function() {
|
|
class SmsSubscribes {
|
|
constructor() {
|
|
this.getSubscribes = this.getSubscribes.bind(this);
|
|
this.getSubscribeStatusLabel = this.getSubscribeStatusLabel.bind(this);
|
|
this.changeSubscribeStatus = this.changeSubscribeStatus.bind(this);
|
|
this.subscribeModal = this.subscribeModal.bind(this);
|
|
this.addSubscribe = this.addSubscribe.bind(this);
|
|
this.editSubscribe = this.editSubscribe.bind(this);
|
|
this.confirmSubscribeEdit = this.confirmSubscribeEdit.bind(this);
|
|
this.uploadSubscribeRecipientsList = this.uploadSubscribeRecipientsList.bind(this);
|
|
this.deleteSubscribeRecipient = this.deleteSubscribeRecipient.bind(this);
|
|
$scope.utilities.showContentOnLoad();
|
|
}
|
|
|
|
getSubscribes(subscribeId = null) {
|
|
var dataToSend;
|
|
if (!this.isSubscribesLoading) {
|
|
this.isSubscribesLoading = true;
|
|
dataToSend = {
|
|
action: "getSubscribes",
|
|
month: config.subscribes.month,
|
|
year: config.subscribes.year,
|
|
subscribeId: subscribeId
|
|
};
|
|
return $http.post(config.urls.subscribes, dataToSend).then((response) => {
|
|
var data;
|
|
data = response.data;
|
|
this.isSubscribesLoading = false;
|
|
if (data) {
|
|
if (data.status && (data.status === "success")) {
|
|
this.smsErrors = [];
|
|
if (subscribeId) {
|
|
return false;
|
|
} else {
|
|
return config.subscribes = data.subscribes;
|
|
}
|
|
} else {
|
|
if (data.errors && data.errors.length) {
|
|
return this.errors = data.errors;
|
|
} else {
|
|
return toastr.error(config.global_labels.defaultErrorMessage.value);
|
|
}
|
|
}
|
|
}
|
|
}).catch((response) => {
|
|
if (response.data && response.data.errors) {
|
|
this.smsErrors = response.data.errors;
|
|
} else {
|
|
toastr.error(config.global_labels.defaultErrorMessage.value);
|
|
}
|
|
return this.isSubscribesLoading = false;
|
|
});
|
|
}
|
|
}
|
|
|
|
getSubscribeStatusLabel(statusId) {
|
|
var i, label, len, ref, status, v;
|
|
label = "-";
|
|
ref = config.subscribeStatuses;
|
|
for (i = 0, len = ref.length; i < len; i++) {
|
|
v = ref[i];
|
|
if (v.id === statusId) {
|
|
status = v;
|
|
}
|
|
}
|
|
if (status) {
|
|
label = status.label;
|
|
}
|
|
return label;
|
|
}
|
|
|
|
changeSubscribeStatus(subscribe, newStatus) {
|
|
var confirmMessage, dataToSend;
|
|
if (!this.isSubmitting) {
|
|
switch (newStatus) {
|
|
case "deleted":
|
|
if (!config.isAdmin && (subscribe.createdBy !== config.loggedInUserId)) {
|
|
return false;
|
|
}
|
|
confirmMessage = 'удалить';
|
|
break;
|
|
case "queued":
|
|
confirmMessage = 'поставить в очередь';
|
|
break;
|
|
case "stopped":
|
|
confirmMessage = 'остановить';
|
|
}
|
|
if (confirm("Вы уверены, что хотите " + confirmMessage + " данную рассылку?")) {
|
|
this.isSubmitting = true;
|
|
dataToSend = {
|
|
action: "setSubscribeStatus",
|
|
newStatus: newStatus,
|
|
subscribeId: subscribe.id
|
|
};
|
|
return $http.post(config.urls.subscribes, dataToSend).then((response) => {
|
|
var data, error, i, key, len, ref, ref1, results, results1, tmpSubscribe;
|
|
data = response.data;
|
|
this.isSubmitting = false;
|
|
if (data) {
|
|
if (data.status && (data.status === "success")) {
|
|
this.smsErrors = [];
|
|
if (newStatus !== 'deleted') {
|
|
subscribe.status = angular.copy(newStatus);
|
|
return toastr.success("Статус рассылки был успешно изменен");
|
|
} else {
|
|
toastr.success("Рассылка была успешно удалена");
|
|
ref = config.subscribes.list;
|
|
results = [];
|
|
for (key in ref) {
|
|
tmpSubscribe = ref[key];
|
|
if (subscribe.id === tmpSubscribe.id) {
|
|
results.push(config.subscribes.list.splice(key, 1));
|
|
} else {
|
|
results.push(void 0);
|
|
}
|
|
}
|
|
return results;
|
|
}
|
|
} else {
|
|
if (data.errors && data.errors.length) {
|
|
ref1 = data.errors;
|
|
results1 = [];
|
|
for (i = 0, len = ref1.length; i < len; i++) {
|
|
error = ref1[i];
|
|
results1.push(toastr.error(error));
|
|
}
|
|
return results1;
|
|
} else {
|
|
return toastr.error(config.global_labels.defaultErrorMessage.value);
|
|
}
|
|
}
|
|
}
|
|
}).catch((response) => {
|
|
var error, i, len, ref;
|
|
if (response.data && response.data.errors) {
|
|
ref = data.errors;
|
|
for (i = 0, len = ref.length; i < len; i++) {
|
|
error = ref[i];
|
|
toastr.error(error);
|
|
}
|
|
} else {
|
|
toastr.error(config.global_labels.defaultErrorMessage.value);
|
|
}
|
|
return this.isSubmitting = false;
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
subscribeModal(action) {
|
|
this.subscribeModalIncorrectRecipients = [];
|
|
this.subscribeModalErrors = [];
|
|
$timeout(() => {
|
|
return angular.element("#subscribeModal").modal(action);
|
|
});
|
|
return $scope.subscribeForm.$setPristine();
|
|
}
|
|
|
|
addSubscribe() {
|
|
this.subscribeModalData = {
|
|
message: "",
|
|
action: "addSubscribe",
|
|
reportEmail: angular.copy(config.loggedInUserEmail)
|
|
};
|
|
this.isSubscribeDataLoading = false;
|
|
return this.subscribeModal("show");
|
|
}
|
|
|
|
editSubscribe(subscribe) {
|
|
var dataToSend;
|
|
this.subscribeModalData = angular.copy(subscribe);
|
|
this.subscribeModal("show");
|
|
if (!this.isSubscribeDataLoading) {
|
|
this.isSubscribeDataLoading = true;
|
|
this.subscribeModalErrors = [];
|
|
dataToSend = {
|
|
action: "getSubscribeData",
|
|
subscribeId: subscribe.id
|
|
};
|
|
return $http({
|
|
method: 'POST',
|
|
url: config.urls.subscribes,
|
|
data: dataToSend,
|
|
timeout: 30000
|
|
}).then((response) => {
|
|
var data;
|
|
this.isSubscribeDataLoading = false;
|
|
if (response.data) {
|
|
data = response.data;
|
|
if (data.status && (data.status === "success")) {
|
|
this.subscribeModalData = angular.copy(data.subscribe);
|
|
this.subscribeModalData.action = "updateSubscribe";
|
|
return this.subscribeModalData.recipientsToDelete = [];
|
|
} else {
|
|
if (data.errors) {
|
|
return this.subscribeModalErrors = data.errors;
|
|
}
|
|
}
|
|
}
|
|
}).catch(() => {
|
|
this.isSubscribeDataLoading = false;
|
|
return toastr.error(config.global_labels.defaultErrorMessage.value);
|
|
});
|
|
}
|
|
}
|
|
|
|
confirmSubscribeEdit() {
|
|
if (!this.isSubmitting) {
|
|
this.isSubmitting = true;
|
|
this.subscribeModalErrors = [];
|
|
return $http({
|
|
method: 'POST',
|
|
url: config.urls.subscribes,
|
|
data: this.subscribeModalData,
|
|
timeout: 30000
|
|
}).then((response) => {
|
|
var data, key, ref, results, subscribe;
|
|
this.isSubmitting = false;
|
|
if (response.data) {
|
|
data = response.data;
|
|
if (data.status && (data.status === "success")) {
|
|
if (this.subscribeModalData.id) {
|
|
toastr.success("Данные SMS рассылки были успешно обновлены");
|
|
this.subscribeModal("hide");
|
|
ref = config.subscribes.list;
|
|
results = [];
|
|
for (key in ref) {
|
|
subscribe = ref[key];
|
|
if (subscribe.id === data.subscribe.id) {
|
|
results.push(config.subscribes.list[key] = angular.copy(data.subscribe));
|
|
} else {
|
|
results.push(void 0);
|
|
}
|
|
}
|
|
return results;
|
|
} else {
|
|
toastr.success("SMS рассылка была успешно добавлена");
|
|
$scope.subscribeForm.$setPristine();
|
|
this.subscribeModalData = angular.copy(data.subscribe);
|
|
this.subscribeModalData.action = "updateSubscribe";
|
|
this.subscribeModalData.recipientsToDelete = [];
|
|
return config.subscribes.list.unshift(angular.copy(data.subscribe));
|
|
}
|
|
} else {
|
|
if (data.errors && data.errors.length) {
|
|
return this.subscribeModalErrors = data.errors;
|
|
} else {
|
|
return toastr.error(config.global_labels.defaultErrorMessage.value);
|
|
}
|
|
}
|
|
}
|
|
}).catch(() => {
|
|
this.isSubmitting = false;
|
|
return toastr.error(config.global_labels.defaultErrorMessage.value);
|
|
});
|
|
}
|
|
}
|
|
|
|
uploadSubscribeRecipientsList() {
|
|
if (!this.isSubmitting && this.importSubscribeRecipientsFile) {
|
|
this.isSubmitting = true;
|
|
this.subscribeModalErrors = [];
|
|
this.subscribeModalIncorrectRecipients = [];
|
|
return Upload.upload({
|
|
url: config.urls.subscribes,
|
|
data: {
|
|
file: this.importSubscribeRecipientsFile,
|
|
action: 'importSubscribeRecipients',
|
|
subscribeId: this.subscribeModalData.id,
|
|
sms_subscribe_recipients: this.subscribeModalData.sms_subscribe_recipients
|
|
}
|
|
}).then((response) => {
|
|
var data;
|
|
data = response.data;
|
|
this.isSubmitting = false;
|
|
this.importSubscribeRecipientsFile = null;
|
|
if (data.status && (data.status === "success")) {
|
|
toastr.success('Импорт списка получателей выполнен успешно, найдено ' + data.importNewSubscribersCountTotal + ' получателя, импортировано новых - ' + data.importNewSubscribersCount + (data.incorrectList.length > 0 ? ", ошибок импорта - " + data.incorrectList.length : ""));
|
|
this.subscribeModalData.sms_subscribe_recipients = angular.copy(data.importSubscribers);
|
|
this.subscribeModalIncorrectRecipients = data.incorrectList;
|
|
return $scope.subscribeForm.$setDirty();
|
|
} else {
|
|
if (data.errors && data.errors.length) {
|
|
return this.subscribeModalErrors = data.errors;
|
|
} else {
|
|
return toastr.error(config.global_labels.defaultErrorMessage.value);
|
|
}
|
|
}
|
|
}).catch((response) => {
|
|
if (response.data && response.data.errors) {
|
|
this.subscribeModalErrors = response.data.errors;
|
|
} else {
|
|
this.subscribeModalErrors = ['Ошибка импорта списка получателей, повторите позднее!'];
|
|
}
|
|
this.isSubmitting = false;
|
|
return this.importSubscribeRecipientsFile = null;
|
|
});
|
|
}
|
|
}
|
|
|
|
deleteSubscribeRecipient(recipientToDelete) {
|
|
var recipient, recipientKey, ref, results;
|
|
if (confirm("Вы уверены, что хотите удалить данного получателя?")) {
|
|
ref = this.subscribeModalData.sms_subscribe_recipients;
|
|
results = [];
|
|
for (recipientKey in ref) {
|
|
recipient = ref[recipientKey];
|
|
if (recipient === recipientToDelete) {
|
|
this.subscribeModalData.sms_subscribe_recipients.splice(recipientKey, 1);
|
|
$scope.subscribeForm.$setDirty();
|
|
if (recipientToDelete.id) {
|
|
if (!this.subscribeModalData.recipientsToDelete) {
|
|
this.subscribeModalData.recipientsToDelete = [];
|
|
}
|
|
this.subscribeModalData.recipientsToDelete.push(recipientToDelete.id);
|
|
}
|
|
break;
|
|
} else {
|
|
results.push(void 0);
|
|
}
|
|
}
|
|
return results;
|
|
}
|
|
}
|
|
|
|
};
|
|
|
|
SmsSubscribes.prototype.isSubmitting = false;
|
|
|
|
SmsSubscribes.prototype.isSubscribesLoading = false;
|
|
|
|
SmsSubscribes.prototype.errors = [];
|
|
|
|
SmsSubscribes.prototype.smsErrors = [];
|
|
|
|
SmsSubscribes.prototype.subscribeModalErrors = [];
|
|
|
|
SmsSubscribes.subscribeModalIncorrectRecipients = [];
|
|
|
|
SmsSubscribes.prototype.isSubscribeDataLoading = false;
|
|
|
|
SmsSubscribes.prototype.importSubscribeRecipientsFile = null;
|
|
|
|
return SmsSubscribes;
|
|
|
|
}).call(this);
|
|
$scope.config = config;
|
|
$scope.utilities = new UtilitiesClass(config,
|
|
$filter,
|
|
$timeout,
|
|
$http);
|
|
return $scope.subscribesList = new SmsSubscribes();
|
|
}
|
|
]);
|