File: /var/www/vhost/disk-apps/teamdemo.sports-crowd.com/public/js/smsNotifications.js
function convertTime12to24(time12h) {
const [time, modifier] = time12h.split(" ");
let [hours, minutes] = time.split(":");
if (hours === "12") {
hours = "00";
}
if (modifier === "PM") {
hours = parseInt(hours, 10) + 12;
}
return hours + ":" + minutes;
}
function removeAmAndPm(time12h2) {
const [time, modifier] = time12h2.split(" ");
let [hours, minutes, seconds] = time.split(":");
if (hours === "12") {
hours = "00";
}
if (modifier === "PM") {
hours = parseInt(hours, 10) + 12;
}
return hours + ":" + minutes + ":" + seconds;
}
function createNotification() {
$("#createNotification").validator("update");
$("#createNotification")
.validator("update")
.on("submit", function (e) {
if (e.isDefaultPrevented()) {
} else {
e.preventDefault();
$("#btn-create-notification").addClass("displayNone");
$("#spinnerButton").removeClass("displayNone");
var date1 = dateNow();
var date2 = $(" #createNotification input[name='date']").val();
var hour1 = hourNow();
var hour2 = convertTime12to24(
$(" #createNotification input[name='time']").val()
);
var fecha1 = new Date(date1 + " " + hour1);
var fecha2 = new Date(date2 + " " + hour2);
var tags = $("#createNotification select[name='generalMultiselect']").val();
if (new Date(fecha1).getTime() > new Date(fecha2).getTime()) {
swal(
"Advertencia",
Lang.get("messagesClient.notification_tag3"),
"warning"
);
$("#spinnerButton").addClass("displayNone");
$("#btn-create-notification").removeClass("displayNone");
return;
} else {
var email_user = $(
" #createNotification input[name='email_user']"
).val();
var user_id = $("#email_user").attr("user-id");
var link = $(
" #createNotification input[name='link']"
).val();
if (email_user.trim() != "" && user_id == "") {
swal(
"Advertencia",
"No se encontro el usuario con el correo electrónico ingresado",
"warning"
);
$("#spinnerButton").addClass("displayNone");
$("#btn-create-notification").removeClass(
"displayNone"
);
return;
}
if (!user_id || user_id == "") {
user_id = null;
}
var info = {
target_id: $("#createNotification select[name='target_id']").val(),
message: $(" #createNotification textarea[name='message']").val(),
when_send:
$(" #createNotification input[name='date']").val() +
" " +
removeAmAndPm($(
" #createNotification input[name='time']"
).val()),
user_id,
link: link != "" ? link : null,
tags: tags
};
swal(Lang.get("messagesClient.notification_tag1"), {
buttons: {
cancel: "No",
Ok: true,
},
}).then((val) => {
if (val == "Ok") {
goCreateNotification(info);
} else {
$("#btn-create-notification").removeClass(
"displayNone"
);
$("#spinnerButton").addClass("displayNone");
}
});
}
}
});
/**
* Esta función hace un llamado AJAX y envia un json creando un nuevo registro de notificaciones
* @param info el json con la informacion de la vista
* @version 2018/05/20
*/
function goCreateNotification(info) {
$.ajax({
url: "/smsnotifications/create",
type: "POST",
contentType: "application/json",
headers: {
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"),
"Content-Type": "application/json",
},
data: JSON.stringify(info),
success: function (r) {
if (r.r) {
swal(r.m, {
icon: "success",
buttons: {
Ok: true,
},
}).then((val) => {
if (val == "Ok") {
$(location).attr("href", "/smsnotifications");
}
});
} else {
swal(r.m, "error");
$("#btn-create-notification").removeClass("displayNone");
$("#spinnerButton").addClass("displayNone");
}
},
error: function (e) {
swal(
"Error",
Lang.get("messagesClient.notification_tag2"),
"error"
);
$("#btn-create-notification").removeClass("displayNone");
$("#spinnerButton").addClass("displayNone");
},
});
}
}
function dateNow() {
var hoy = new Date();
var dd = hoy.getDate();
var mm = hoy.getMonth() + 1;
var yyyy = hoy.getFullYear();
dd = addZero(dd);
mm = addZero(mm);
return yyyy + "-" + mm + "-" + dd;
}
function addZero(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
function hourNow() {
var hour = new Date();
hour = hour.getHours() + ":" + hour.getMinutes();
return hour;
}
function autocompleteEmail() {
$("#email_user").keyup(function (e) {
$("#email_user").attr("user-id", "");
$("#name_user").html("");
});
$("#name_user").html("");
$("#email_user").attr("user-id", "");
$("#email_user").autocomplete({
source: "/client/autocomplete/mail",
minLength: 3,
select: function (event, ui) {
console.log("ui", ui);
$("#email_user").attr("user-id", ui.item.id);
$("#email_user").val(ui.item.email);
$("#name_user").html(ui.item.first_name + ui.item.last_name);
},
});
}
function update() {
active = $("#chkActive").is(":checked");
ticket_buy_message = $("#edit input[name='sms_notification_ticket']").val();
shop_buy_message = $("#edit input[name='sms_notification_shop']").val();
api = $("#edit input[name='url_api']").val();
$("#edit").validator("update");
$("#edit")
.validator("update")
.on("submit", function (e) {
if (e.isDefaultPrevented()) {
} else {
e.preventDefault();
if(ticket_buy_message == "" && shop_buy_message == "") {
swal("Error", "Error al actualizar la configuración notificaciones sms, digite el mensaje de texto", "error");
return;
} else if (api == "") {
swal("Error", "Error al actualizar la configuración notificaciones sms, digite url api", "error");
return;
}
var info = {
id: $("#edit input[name='id']").val(),
api: $("#edit input[name='url_api']").val(),
user: $("#edit input[name='user_api']").val(),
pass: $("#edit input[name='pass_api']").val(),
ticket_buy_message: $("#edit input[name='sms_notification_ticket']").val(),
shop_buy_message: $("#edit input[name='sms_notification_shop']").val(),
active,
};
console.log(info);
swal((info.id ? "¿Modificar" : "¿Crear") + " configuración notificaciones sms?", {
buttons: {
cancel: "No",
Ok: true,
},
}).then((val) => {
if (val == "Ok") {
$.ajax({
url: "/smsnotifications/notification_settings",
type: "POST",
contentType: "application/json",
headers: {
"X-CSRF-TOKEN": $(
'meta[name="csrf-token"]'
).attr("content"),
"Content-Type": "application/json",
},
data: JSON.stringify(info),
success: function (r) {
if (r.r) {
swal("Notificación sms modificadas con éxito!", {
icon: "success",
buttons: {
Ok: true,
},
}).then((val) => {
if (val == "Ok") {
$(location).attr("href", "/smsnotifications/smsparameters");
}
});
}
},
error: function (e) {
swal("Error", "Error al actualizar la configuración notificaciones sms", "error");
},
});
}
});
}
});
}