GhostManSec
Server: Apache
System: Linux instant0279.server.com 2.6.32-696.3.1.el6.x86_64 #1 SMP Tue May 30 19:52:55 UTC 2017 x86_64
User: ptgoldenvisa (520)
PHP: 7.4.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: /home/ptgoldenvisa/public_html/scheduleyourcall/script.gs
// open < File -> Project properties -> Script properties > to configure this script

function getSpreadsheet(){
  var properties = PropertiesService.getScriptProperties();

  var id = properties.getProperty('spreadsheet');

  return SpreadsheetApp.openById(id);
}

function getSettings(){
  var sheet = getSpreadsheet().getSheetByName('settings');
  var calendarId = sheet.getRange(2,2).getValue();
  var duration1 = sheet.getRange(3,2).getValue();
  var duration2 = sheet.getRange(4,2).getValue();
  var hour1 = sheet.getRange(5,2).getValue();
  var hour2 = sheet.getRange(6,2).getValue();
  var daysOff = sheet.getRange(7,2).getValue().split(',');

  var settings = {
    calendarId: calendarId,
    duration1: duration1,
    duration2: duration2,
    hour1: hour1,
    hour2: hour2,
    daysOff: daysOff
  };

  return settings;
}

// Get calendar events between 2 given timestamps
function getCalendarEvents(start, end){
  var cal = getSettings().calendarId;

  var events = CalendarApp.getCalendarById(cal).getEvents(new Date(start), new Date(end));

  return events;
}


function getSlots(d){
  var date = new Date(d);
  var settings = getSettings();

  var today = new Date().setHours(0,0,0);
  var tomorrow = new Date(today + 24 * 60 * 60 * 1000);
  tomorrow = Utilities.formatDate(tomorrow, CalendarApp.getTimeZone(), "yyyy-MM-dd");
  today = Utilities.formatDate(new Date(), CalendarApp.getTimeZone(), "yyyy-MM-dd");

  var duration1 = settings.duration1 * 60 * 1000;
  var duration2 = settings.duration2 * 60 * 1000;
  var duration = duration1 + duration2;

  var hour1 = settings.hour1;
  var hour2 = settings.hour2;
  var slots = [];


  var start = new Date(date).setHours(hour1);
  var end = new Date(date).setHours(hour2);
  var slotStart = new Date(start);
  var slotEnd = new Date(start + duration1);

  while (slotEnd.getTime() < end) {
    if (d == tomorrow && new Date().getHours() >= 19 && slotStart.getHours() < 10) {
      start = new Date(slotEnd.getTime() + duration2).getTime();
      slotStart = new Date(start);
      slotEnd = new Date(start + duration1);
      continue;
    }

    if (d == today && slotStart.getTime() < new Date().getTime() + 2*60*60*1000) {
      start = new Date(slotEnd.getTime() + duration2).getTime();
      slotStart = new Date(start);
      slotEnd = new Date(start + duration1);
      continue;
    }

    // other events/appointment in main calendar blocking current appoinment slot
    var calEvents  = getCalendarEvents(start - duration2, slotEnd.getTime() + duration2);
      // if no other appoinments and slot start is in future
      if (!calEvents.length && start >= new Date().getTime()) {
        slots.push({
          start: slotStart,
          end: slotEnd,
          // title: slotToStr(start, slotEnd)
        });

        // CalendarApp.getCalendarById(getSettings().calendarId).createEvent("-", slotStart, slotEnd);

      }

      // go to next slot
      start = new Date(slotEnd.getTime() + duration2).getTime();
      slotStart = new Date(start);
      slotEnd = new Date(start + duration1);
  }

  return slots;

}

function doGet(request) {
    // date format: '2017-07-28'
    // month: 2017-07
    var d = request.parameters.date;
    if (!d) {
        d = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd");
    }

    var feature = request.parameters.feature;

    slots = getSlots(d);

    slots = JSON.stringify(slots);

    return ContentService.createTextOutput(slots).setMimeType(ContentService.MimeType.JSON);
}

function doPost(request){
    var cal = getSettings().calendarId;
    var data = request.parameters.data;
    data = JSON.parse(data);
    var duration2 = getSettings().duration2;

    var startTime = new Date(data.start);
    var endTime = new Date(data.end);
    var name = data.name;
    var phone = data.phone;
    var email = data.email;
    // var subject = data.subject;

    var appDate = Utilities.formatDate(startTime, CalendarApp.getTimeZone(), "dd MMMM yyyy");
    var appTime = Utilities.formatDate(startTime, CalendarApp.getTimeZone(), "HH:mm") + " - " + Utilities.formatDate(endTime, CalendarApp.getTimeZone(), "HH:mm");

    var sheet = getSpreadsheet().getSheetByName('appointments');

    var check = !getCalendarEvents(new Date(startTime.getTime() - duration2), new Date(endTime) + duration2).length;

    if (check) {
        var options = {};
        if (email) {
            options = {
                guests: email,
                sendInvites: true
            };
        }
        var evt = CalendarApp.getCalendarById(cal).createEvent(name +  " " + phone, startTime, endTime, options);
        var evtId = evt.getId();

        var splitEventId = evtId.split('@');
        var eventURL = "https://www.google.com/calendar/event?eid=" + Utilities.base64Encode(splitEventId[0] + " " + cal);
        evtLink = '=HYPERLINK("'+eventURL+'","#")';

        var desc = ["", "Name:", name, "\n", "Phone: ", phone, "\n", /* 'Subject: ', subject, '\n',*/ "Email:", email, "\n"].join(" ");
        evt.setDescription(desc);
        evt.setTag('phone', phone);
        evt.setTag('name', name);
        sheet.appendRow([name,  "'"+phone, email, /* subject*/ '',  evtLink, appDate, appTime, new Date(), 'ok']);

        var notifyEmail = getSpreadsheet().getSheetByName('settings').getRange(7,2).getValue();


        if (notifyEmail) {
          var emailSubject = "New call scheduled: schedule your call";
          var emailBody = name +  "\n"
               /* + 'Subject: ' + subject + "\n" */
               + 'Phone: ' + phone + "\n"
               + 'Event link: ' + eventURL + "\n"
               + Utilities.formatDate(startTime, CalendarApp.getTimeZone(), "dd MMMM yyyy");
          GmailApp.sendEmail(notifyEmail, emailSubject, emailBody);
        }

        return ContentService.createTextOutput("ok");
    } else {
        sheet.appendRow([name, "'"+phone, email, /* subject*/ '', '', appDate, appTime, new Date(), 'fail']);
        return ContentService.createTextOutput("fail");
    }
}

function test(){

  var email = Session.getActiveUser().getEmail()
  Logger.log(email);

}