<commit_before>var Joi = require('joi');
var _ = require('../libs/underscore.1.6.0.min');

var MOCK_DATA = [
  {
    id: 1,
    rsvpCode: "12345",
    isInvitedToRehearsalDinner: true,
    isAttendingBigDay: true,
    isAttendingRehearsalDinner: false,

    contacts: [
    {
      id: 1,
      firstName: "FirstName1",
      middleName: "MiddleName1",
      lastName: "LastName1",
      isChild: false,
      isAttendingBigDay: true,
      isAttendingRehearsalDinner: false,
    },
    {
      id: 4,
      firstName: "FirstName2",
      middleName: "MiddleName2",
      lastName: "LastName2",
      isChild: false,
      isAttendingBigDay: true,
      isAttendingRehearsalDinner: false,
    }]
  },
  {
    id: 2,
    rsvpCode: "abcde",
    isInvitedToRehearsalDinner: false,
    isAttendingBigDay: false,
    isAttendingRehearsalDinner: false,

    contacts: [
    {
      id: 6,
      firstName: "Some",
      middleName: "Middle",
      lastName: "Name",
      isChild: false,
      isAttendingBigDay: false,
      isAttendingRehearsalDinner: false,
    },
    {
      id: 13,
      firstName: "Fname",
      middleName: "Mname",
      lastName: "Lname",
      isChild: true,
      isAttendingBigDay: false,
      isAttendingRehearsalDinner: false,
    }]
  }
];

exports.retrieveReservation = {
  handler: function(request, reply) {

    var reservation = MOCK_DATA.filter(function(c) { return c.rsvpCode == request.params.rsvpCode });
    if (reservation && reservation.length == 1)
    {
      reply({ reservation: reservation[0] });
    }

    reply({ error: { code: 500, message: "Could not find reservation for given RSVP code" }});
  }
}

exports.updateReservation = {
  handler: function(request, reply) {

    console.log("New request to save reservation: " + request.payload.reservation);

    var requestReservation = request.payload.reservation;

    if (!requestReservation || !requestReservation.rsvpCode)
    {
      reply({ error: { code: 500, message: "Could not find reservation for given RSVP code" }});
    }

    var rsvpCode = requestReservation.rsvpCode;

    var reservation = MOCK_DATA.filter(function(c) { return c.rsvpCode == rsvpCode });
    if (reservation && reservation.length == 1)
    {
      reservation[0].isAttendingBigDay = requestReservation.isAttendingBigDay;
      reservation[0].isAttendingRehearsalDinner = requestReservation.isAttendingRehearsalDinner;

      _.each(requestReservation.contacts, function(contact)
      {
        var serverContact = reservation[0].contacts.filter(function(c) { return c.id == contact.id });

        serverContact.firstName = contact.firstName;
        serverContact.middleName = contact.middleName;
        serverContact.lastName = contact.lastName;
        serverContact.isAttendingBigDay = contact.isAttendingBigDay;
        serverContact.isAttendingRehearsalDinner = contact.isAttendingRehearsalDinner;
        serverContact.isChild = contact.isChild;
      });

      console.log("New Reservation Data: " + reservation[0]);
    }<commit_msg>Fix issue with saving reservations.<commit_after>var Joi = require('joi');
var util = require("util");
var _ = require('../libs/underscore.1.6.0.min');

var MOCK_DATA = [
  {
    id: 1,
    rsvpCode: "12345",
    isInvitedToRehearsalDinner: true,
    isAttendingBigDay: true,
    isAttendingRehearsalDinner: false,

    contacts: [
    {
      id: 1,
      firstName: "FirstName1",
      middleName: "MiddleName1",
      lastName: "LastName1",
      isChild: false,
      isAttendingBigDay: true,
      isAttendingRehearsalDinner: false,
    },
    {
      id: 4,
      firstName: "FirstName2",
      middleName: "MiddleName2",
      lastName: "LastName2",
      isChild: false,
      isAttendingBigDay: true,
      isAttendingRehearsalDinner: false,
    }]
  },
  {
    id: 2,
    rsvpCode: "abcde",
    isInvitedToRehearsalDinner: false,
    isAttendingBigDay: false,
    isAttendingRehearsalDinner: false,

    contacts: [
    {
      id: 6,
      firstName: "Some",
      middleName: "Middle",
      lastName: "Name",
      isChild: false,
      isAttendingBigDay: false,
      isAttendingRehearsalDinner: false,
    },
    {
      id: 13,
      firstName: "Fname",
      middleName: "Mname",
      lastName: "Lname",
      isChild: true,
      isAttendingBigDay: false,
      isAttendingRehearsalDinner: false,
    }]
  }
];

exports.retrieveReservation = {
  handler: function(request, reply) {

    var reservation = MOCK_DATA.filter(function(c) { return c.rsvpCode == request.params.rsvpCode });
    if (reservation && reservation.length == 1)
    {
      reply({ reservation: reservation[0] });
    }

    reply({ error: { code: 500, message: "Could not find reservation for given RSVP code" }});
  }
}

exports.updateReservation = {
  handler: function(request, reply) {

    console.log("New request to save reservation: " + request.payload.reservation);

    var requestReservation = request.payload.reservation;

    if (!requestReservation || !requestReservation.rsvpCode)
    {
      reply({ error: { code: 500, message: "Could not find reservation for given RSVP code" }});
    }

    var rsvpCode = requestReservation.rsvpCode;

    var reservation = MOCK_DATA.filter(function(c) { return c.rsvpCode == rsvpCode });
    if (reservation && reservation.length == 1)
    {
      reservation[0].isAttendingBigDay = requestReservation.isAttendingBigDay === "true" ? true : false;
      reservation[0].isAttendingRehearsalDinner = requestReservation.isAttendingRehearsalDinner === "true" ? true : false;

      var newContacts = [];

      _.each(requestReservation.contacts, function(contact)
      {

        var contactsList = reservation[0].contacts.filter(function(c) { return c.id == contact.id });
        if (!contactsList || contactsList.length == 0)
        {
          return;
        }

        var serverContact = _.first(contactsList);

        serverContact.firstName = contact.firstName;
        serverContact.middleName = contact.middleName;
        serverContact.lastName = contact.lastName;
        serverContact.isAttendingBigDay = contact.isAttendingBigDay === "true" ? true : false;
        serverContact.isAttendingRehearsalDinner = contact.isAttendingRehearsalDinner === "true" ? true : false;
        serverContact.isChild = contact.isChild === "true" ? true : false;

        newContacts.push(serverContact);
      });

      reservation[0].contacts = newContacts;

      console.log("New Reservation Data: " + reservation[0]);
    }
{'fraction_non_alphanumeric': 0.11742074484456756, 'fraction_numerical': 0.01077254539858418, 'mean_word_length': 3.0067817509247843, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5544897', 'n_tokens_mistral': 2165, 'n_tokens_neox': 2054, 'n_words': 405}