<commit_before>import google from 'googleapis';
import moment from 'moment';
import uuidv1 from 'uuid/v1';
import generateJWT from '../utilities/jwt';
import { byPersonCount as getAppointmentsCountByPerson } from '../appointments/person';

const EDIT_URL = 'https://rarebeauty.soho.sg/appointment/edit';
const TEST_EMAIL = `<EMAIL>`;

function findExistingAppointments(calendar, options) {
  const { calendarId, startDT, endDT } = options;

  return new Promise(async (res, rej) => {
    const timeMin =
      moment(startDT).add(1, 'seconds').toISOString() ||
      moment().subtract(2, 'hours').toISOString();
    const timeMax = endDT || moment().add(2, 'hours').toISOString();
    const finalOptions = {
      calendarId,
      timeMin,
      timeMax,
      maxResults: 10,
      singleEvents: true,
      orderBy: 'startTime',
    };

    calendar.events.list(finalOptions, async (err, response) => {
      if (err) {
        rej(err);
      } else {
        res(response.items);
      }
    });
  });
}

function createAppointment(calendar, options) {
  const {
    name,
    mobile,
    startDT,
    endDT,
    services,
    calendarId,
    resourceName,
    reminded,
    informed,
  } = options;

  return new Promise(async (res, rej) => {
    const uuid = uuidv1();

    const {
      count: countOfExistingAppointments,
    } = await getAppointmentsCountByPerson({ id: resourceName });
    // const countOfExistingAppointments = 0;
    calendar.events.insert(
      {
        calendarId,
        resource: {
          start: { dateTime: startDT },
          end: { dateTime: endDT },
          summary: `${name} (${countOfExistingAppointments > 0
            ? countOfExistingAppointments
            : 'FIRST'})`,
          location: 'Home',
          status: 'tentative',
          extendedProperties: {
            shared: {
              mobile,
              reminded: reminded !== undefined ? reminded : false,
              informed: informed !== undefined ? informed : false,
              services: services.map(item => item.id).join(','),
              uuid,
              resourceName,
            },
          },
          attendees: [
            {
              displayName: name,
              comment: mobile,
              email: TEST_EMAIL,
            },
          ],
          description: `${services
            .map(item => item.service)
            .join(',')}\n\n${EDIT_URL}/${uuid}`,
        },
      },
      (err, event) => {
        if (err) {
          console.error(
            `There was an error contacting the Calendar service: ${err}`,
          );
          rej(err);
        }<commit_msg>adding amount<commit_after>import google from 'googleapis';
import moment from 'moment';
import uuidv1 from 'uuid/v1';
import generateJWT from '../utilities/jwt';
import { byPersonCount as getAppointmentsCountByPerson } from '../appointments/person';

const EDIT_URL = 'https://rarebeauty.soho.sg/appointment/edit';
const TEST_EMAIL = `<EMAIL>`;

function findExistingAppointments(calendar, options) {
  const { calendarId, startDT, endDT } = options;

  return new Promise(async (res, rej) => {
    const timeMin =
      moment(startDT).add(1, 'seconds').toISOString() ||
      moment().subtract(2, 'hours').toISOString();
    const timeMax = endDT || moment().add(2, 'hours').toISOString();
    const finalOptions = {
      calendarId,
      timeMin,
      timeMax,
      maxResults: 10,
      singleEvents: true,
      orderBy: 'startTime',
    };

    calendar.events.list(finalOptions, async (err, response) => {
      if (err) {
        rej(err);
      } else {
        res(response.items);
      }
    });
  });
}

function createAppointment(calendar, options) {
  const {
    name,
    mobile,
    startDT,
    endDT,
    services,
    calendarId,
    resourceName,
    reminded,
    informed,
  } = options;

  return new Promise(async (res, rej) => {
    const uuid = uuidv1();

    const {
      count: countOfExistingAppointments,
    } = await getAppointmentsCountByPerson({ id: resourceName });
    // const countOfExistingAppointments = 0;
    calendar.events.insert(
      {
        calendarId,
        resource: {
          start: { dateTime: startDT },
          end: { dateTime: endDT },
          summary: `${name} (${countOfExistingAppointments > 0
            ? countOfExistingAppointments
            : 'FIRST'})`,
          location: 'Home',
          status: 'tentative',
          extendedProperties: {
            shared: {
              mobile,
              reminded: reminded !== undefined ? reminded : false,
              informed: informed !== undefined ? informed : false,
              services: services.map(item => item.id).join(','),
              uuid,
              resourceName,
            },
          },
          attendees: [
            {
              displayName: name,
              comment: mobile,
              email: TEST_EMAIL,
            },
          ],
          description: `
            $${services.reduce((prevSum, item) => prevSum + item.price, 0)}

            ${services
              .map(item => item.service)
              .join(',')}\n\n${EDIT_URL}/${uuid}`,
        },
      },
      (err, event) => {
        if (err) {
          console.error(
            `There was an error contacting the Calendar service: ${err}`,
          );
          rej(err);
        }
{'fraction_non_alphanumeric': 0.13021609538002982, 'fraction_numerical': 0.003912071535022355, 'mean_word_length': 1.9646604086140254, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 20, 'https://': 2, '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': '4862261', 'n_tokens_mistral': 1570, 'n_tokens_neox': 1553, 'n_words': 342}