MaketRandevu/assets/js/http/calendar_http_client.js
2022-01-17 21:19:46 +01:00

242 lines
7.5 KiB
JavaScript
Executable file

/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @copyright Copyright (c) Alex Tselegidis
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
* @link https://easyappointments.org
* @since v1.2.0
* ---------------------------------------------------------------------------- */
/**
* Calendar HTTP client.
*
* This module implements the calendar related HTTP requests.
*
* Old Name: BackendCalendarApi
*/
App.Http.Calendar = (function () {
/**
* Save Appointment
*
* This method stores the changes of an already registered appointment into the database, via an ajax call.
*
* @param {Object} appointment Contain the new appointment data. The ID of the appointment must be already included.
* The rest values must follow the database structure.
* @param {Object} [customer] Optional, contains the customer data.
* @param {Function} [successCallback] Optional, if defined, this function is going to be executed on post success.
* @param {Function} [errorCallback] Optional, if defined, this function is going to be executed on post failure.
*
* @return {*|jQuery.jqXHR}
*/
function saveAppointment(appointment, customer, successCallback, errorCallback) {
const url = App.Utils.Url.siteUrl('calendar/save_appointment');
const data = {
csrf_token: App.Vars.csrf_token,
appointment_data: appointment
};
if (customer) {
data.customer_data = customer;
}
return $.post(url, data)
.done((response) => {
if (successCallback) {
successCallback(response);
}
})
.fail(() => {
if (errorCallback) {
errorCallback();
}
});
}
/**
* Remove an appointment.
*
* @param {Number} appointmentId
* @param {String} deleteReason
*
* @return {jQuery.jqXHR}
*/
function deleteAppointment(appointmentId, deleteReason) {
const url = App.Utils.Url.siteUrl('calendar/delete_appointment');
const data = {
csrf_token: App.Vars.csrf_token,
appointment_id: appointmentId,
delete_reason: deleteReason
};
return $.post(url, data);
}
/**
* Save unavailable period to database.
*
* @param {Object} unavailable Contains the unavailable period data.
* @param {Function} [successCallback] The ajax success callback function.
* @param {Function} [errorCallback] The ajax failure callback function.
*/
function saveUnavailable(unavailable, successCallback, errorCallback) {
const url = App.Utils.Url.siteUrl('calendar/save_unavailable');
const data = {
csrf_token: App.Vars.csrf_token,
unavailable: unavailable
};
return $.post(url, data)
.done((response) => {
if (successCallback) {
successCallback(response);
}
})
.fail(() => {
if (errorCallback) {
errorCallback();
}
});
}
/**
* Remove an unavailable.
*
* @param {Number} unavailableId
*
* @return {jQuery.jqXHR}
*/
function deleteUnavailable(unavailableId) {
const url = App.Utils.Url.siteUrl('calendar/delete_unavailable');
const data = {
csrf_token: App.Vars.csrf_token,
unavailable_id: unavailableId
};
return $.post(url, data);
}
/**
* Save working plan exception of work to database.
*
* @param {Date} date Contains the working plan exceptions data.
* @param {Object} workingPlanException Contains the working plan exceptions data.
* @param {Number} providerId Contains the working plan exceptions data.
* @param {Function} successCallback The ajax success callback function.
* @param {Function} errorCallback The ajax failure callback function.
*/
function saveWorkingPlanException(date, workingPlanException, providerId, successCallback, errorCallback) {
const url = App.Utils.Url.siteUrl('calendar/save_working_plan_exception');
const data = {
csrf_token: App.Vars.csrf_token,
date: date,
working_plan_exception: workingPlanException,
provider_id: providerId
};
return $.post(url, data)
.done((response) => {
if (successCallback) {
successCallback(response);
}
})
.fail(() => {
if (errorCallback) {
errorCallback();
}
});
}
/**
* Delete working plan exception
*
* @param {String} date
* @param {Number} providerId
* @param {Function} [successCallback]
* @param {Function} [errorCallback]
*
* @return {*|jQuery.jqXHR}
*/
function deleteWorkingPlanException(date, providerId, successCallback, errorCallback) {
const url = App.Utils.Url.siteUrl('calendar/delete_working_plan_exception');
const data = {
csrf_token: App.Vars.csrf_token,
date: date,
provider_id: providerId
};
return $.post(url, data)
.done((response) => {
if (successCallback) {
successCallback(response);
}
})
.fail(() => {
if (errorCallback) {
errorCallback();
}
});
}
/**
* Get the appointments for the displayed calendar period.
*
* @param {Number} recordId Record ID (provider or service).
* @param {String} filterType The filter type, could be either "provider" or "service".
* @param {String} startDate Visible start date of the calendar.
* @param {String} endDate Visible end date of the calendar.
*
* @returns {jQuery.jqXHR}
*/
function getCalendarAppointments(recordId, startDate, endDate, filterType) {
const url = App.Utils.Url.siteUrl('calendar/get_calendar_appointments');
const data = {
csrf_token: App.Vars.csrf_token,
record_id: recordId,
start_date: moment(startDate).format('YYYY-MM-DD'),
end_date: moment(endDate).format('YYYY-MM-DD'),
filter_type: filterType
};
return $.post(url, data);
}
/**
* Get the calendar appointments for the table view (different data structure).
*
* @param {Date} startDate
* @param {Date} endDate
*
* @return {*|jQuery.jqXHR}
*/
function getCalendarAppointmentsForTableView(startDate, endDate) {
const url = App.Utils.Url.siteUrl('calendar/get_calendar_appointments_for_table_view');
const data = {
csrf_token: App.Vars.csrf_token,
start_date: moment(startDate).format('YYYY-MM-DD'),
end_date: moment(endDate).format('YYYY-MM-DD')
};
return $.post(url, data);
}
return {
saveAppointment,
deleteAppointment,
saveUnavailable,
deleteUnavailable,
saveWorkingPlanException,
deleteWorkingPlanException,
getCalendarAppointments,
getCalendarAppointmentsForTableView
};
})();