<commit_before>const helper = require('./GoogleCalendarShareHelper');
const leaveEventTransformer = require('./leaveEventTransformer');
const filters = require('./filters');
const EventFilter = require('./EventFilter');
const eventFilter = new EventFilter([filters.allDayEventFilter, filters.leaveEventFilter]);
const Promise = require('bluebird');
const LeaveEvent = Promise.promisifyAll(require('../persistence').models.LeaveEvent);
const updateLeaveEvent = Promise.method((leaveEvent, leaveEventObj) => {
  leaveEventObj.status = leaveEvent.status;
  leaveEventObj.summary = leaveEvent.summary;
  leaveEventObj.startDate = leaveEvent.startDate;
  leaveEventObj.endDate = leaveEvent.endDate;

  return Promise.method(leaveEventObj.save)();
});
const removeLeaveEvent = Promise.method((leaveEventObj) => {
  // Wrapping in a promise instead of using Promise.method due possible scoping bug in bluebird/mongoose.
  return new Promise((resolve) => {
    leaveEventObj.remove(() => {
      return resolve();
    });
  });
});
const saveLeaveEvent = Promise.method((leaveEvent) => {
  return Promise.method(leaveEvent.save)();
});

function checkSaveUpdateOrRemove(leaveEvent) {
  return LeaveEvent.findAsync({'id': leaveEvent.id}).then((leaveEventObj) => {
    if (leaveEventObj.length && leaveEvent.status !== 'cancelled') {
      // If leave event exists and isn't canceled, update it.
      return updateLeaveEvent(leaveEvent, leaveEventObj[0]);
    } else if (leaveEventObj.length && leaveEvent.status === 'cancelled') {
      // If leave event exists and it is canceled, remove it.
      return removeLeaveEvent(leaveEventObj[0]);
    } else if (leaveEvent.status !== 'cancelled') {
      // If leave event doesn't exist and it isn't canceled, save it.
      return saveLeaveEvent(leaveEvent);
    }
  });
}

function calendarEventHandler(event) {
  return eventFilter.apply([event]).map((calLeaveEvent) => {
    return leaveEventTransformer(calLeaveEvent).then((leaveEvent) => {
      return leaveEvent;
    });
  }).map(checkSaveUpdateOrRemove);
}

function scan(users, startDate, endDate, auth) {
  return Promise.map(users, (user) => {
    return helper.processEventsForUser(user.email, startDate, endDate, calendarEventHandler, auth);
  });
}

module.exports = {scan};<commit_msg>adding comments to event scanner<commit_after>const helper = require('./GoogleCalendarShareHelper');
const leaveEventTransformer = require('./leaveEventTransformer');
const filters = require('./filters');
const EventFilter = require('./EventFilter');
const eventFilter = new EventFilter([filters.allDayEventFilter, filters.leaveEventFilter]);
const Promise = require('bluebird');
const LeaveEvent = Promise.promisifyAll(require('../persistence').models.LeaveEvent);
const updateLeaveEvent = Promise.method((leaveEvent, leaveEventObj) => {
  // Handles updating of an event that already exists.
  leaveEventObj.status = leaveEvent.status;
  leaveEventObj.summary = leaveEvent.summary;
  leaveEventObj.startDate = leaveEvent.startDate;
  leaveEventObj.endDate = leaveEvent.endDate;

  return Promise.method(leaveEventObj.save)();
});
const removeLeaveEvent = Promise.method((leaveEventObj) => {
  // Handles removal of an event that exists but was cancelled/deleted.
  // Wrapping in a promise instead of using Promise.method due possible scoping bug in bluebird/mongoose.
  return new Promise((resolve) => {
    leaveEventObj.remove(() => {
      return resolve();
    });
  });
});
const saveLeaveEvent = Promise.method((leaveEvent) => {
  // Handles creation of a new event
  return Promise.method(leaveEvent.save)();
});

/**
 * Determines what operation to perform on the passed in event.
 *
 * @param leaveEvent Leave event to dispatch to correct handler
 * @returns {Promise.<T>|*}
 */
function checkSaveUpdateOrRemove(leaveEvent) {
  return LeaveEvent.findAsync({'id': leaveEvent.id}).then((leaveEventObj) => {
    if (leaveEventObj.length && leaveEvent.status !== 'cancelled') {
      // If leave event exists and isn't canceled, update it.
      return updateLeaveEvent(leaveEvent, leaveEventObj[0]);
    } else if (leaveEventObj.length && leaveEvent.status === 'cancelled') {
      // If leave event exists and it is canceled, remove it.
      return removeLeaveEvent(leaveEventObj[0]);
    } else if (leaveEvent.status !== 'cancelled') {
      // If leave event doesn't exist and it isn't canceled, save it.
      return saveLeaveEvent(leaveEvent);
    }
  });
}

/**
 * Handler to process each leave event.
 *
 * @param event Event to process
 * @returns {*}
 */
function calendarEventHandler(event) {
  return eventFilter.apply([event]).map((calLeaveEvent) => {
    return leaveEventTransformer(calLeaveEvent).then((leaveEvent) => {
      return leaveEvent;
    });
  }).map(checkSaveUpdateOrRemove);
}

/**
 * Iterates through a list of users and processes events in the specified date range.
 *
 * @param users List of users to process events on
 * @param startDate Starting date of events to search for
 * @param endDate Ending date of events to search for
 * @param auth Google OAuth client
 * @returns {Array}
 */
function scan(users, startDate, endDate, auth) {
  return Promise.map(users, (user) => {
    return helper.processEventsForUser(user.email, startDate, endDate, calendarEventHandler, auth);
  });
}

module.exports = {scan};
{'fraction_non_alphanumeric': 0.12787867440554204, 'fraction_numerical': 0.0007489234225800412, 'mean_word_length': 4.611344537815126, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4867050', 'n_tokens_mistral': 1474, 'n_tokens_neox': 1467, 'n_words': 398}