<commit_before>var pg = require('pg');
var Joi = require('joi');
var DB = require('../db');
var Hapi = require('hapi');
var Util = require('util');
var _ = require('../libs/underscore.1.6.0.min')

var MOCK_DATA = [
  {
    id: 1,

    reservationTitle: "Mr. & Mrs. <NAME>",

    addressLineOne: "12345 Street One",
    addressLineTwo: "Apt 111",
    addressCity: "Some City",
    addressState: "IN",
    addressZipCode: "554433",

    rsvpCode: "d18cb7",
    emailAddress: "<EMAIL>",

    reservationNotes: "",
    dietaryRestrictions: "",
    notesForBrideGroom: "",

    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,

    isInvitedToRehearsalDinner: false,
    isAttendingBigDay: false,
    isAttendingRehearsalDinner: false,

    reservationTitle: "Mr. & Mrs. <NAME>",

    addressLineOne: "1 This Way",
    addressLineTwo: "",
    addressCity: "Firstly",
    addressState: "IN",
    addressZipCode: "11223",

    reservationNotes: "",
    dietaryRestrictions: "",
    notesForBrideGroom: "",

    rsvpCode: "abcde",
    emailAddress: "<EMAIL>",

    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,
    }]
  }
];

var GET_RESERVATION_SQL = "SELECT * FROM reservation r WHERE r.rsvp_code = $1";
var GET_CONTACTS_FOR_RESERVATION_SQL = "SELECT * FROM contact c WHERE c.reservation_id = $1"

var INSERT_NEW_CONTACT_SQL = 'INSERT INTO contact (reservation_id, first_name, middle_name, last_name, '
    + ' is_child, is_attending_big_day, is_attending_rehearsal_dinner )'
    + ' VALUES '
    + ' ($1, $2, $3, $4, $5, $6, $7'
    + ' ) RETURNING id';

var INSERT_NEW_RESERVATION_SQL = 'INSERT INTO reservation (reservation_Title, rsvp_Code_Source, '
    + ' address_Line_One, address_Line_Two, '
    + 'address_City, address_State, address_Zip_Code, rsvp_Code, email_Address, reservation_Notes, '
    + 'dietary_Restrictions, notes_For_Bride_Groom, is_Invited_To_Rehearsal_Dinner, is_Attending_Big_Day, is_Attending_Rehearsal_Dinner '
    + ', create_date, modified_date) '
    + ' VALUES '
    + ' ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, '
    + ' CURRENT_TIMESTAMP, CURRENT_TIMESTAMP'
    + ' ) RETURNING id';

var UPDATE_RESERVATION_SQL = 'UPDATE reservation                        \
                              SET                                       \
                                  address_line_one = $1,                \
                                  address_line_two = $2,                \
                                  address_city = $3,                    \
                                  address_state = $4,                   \
                                  address_zip_code = $5,                \
                                  email_address = $6,                   \
                                  reservation_notes = $7,               \
                                  dietary_restrictions = $8,            \
                                  notes_for_bride_groom = $9,           \
                                  is_attending_big_day = $10,           \
                                  is_attending_rehearsal_dinner = $11   \
                              WHERE id = $12                            \
                            ';

var UPDATE_CONTACT_SQL = 'UPDATE contact                          \
                          SET                                     \
                              first_name = $1,                    \
                              middle_name = $2,                   \
                              last_name = $3,                     \
                              is_attending_big_day = $4,          \
                              is_attending_rehearsal_dinner = $5  \
                          WHERE id = $6                           \
                        ';


// the letter O ("Oh") should be a 0
function normalizeRSVPCode(rsvpCode) {
  return rsvpCode.toLowerCase().replace(/o/gi, "0");
}

var VALID_RSVP_CODE_REGEX_PATTERN = /^([a-fA-F0-9]{6})$/ig;

function isValidRSVPCode(rsvpCode) {
  if (rsvpCode === undefined) {
    return false;
  }

  return VALID_RSVP_CODE_REGEX_PATTERN.test(rsvpCode);
}

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

    var normalizedRSVPCode = normalizeRSVPCode(request.params.rsvpCode);

    if (!isValidRSVPCode(normalizedRSVPCode)) {
      console.log("Code Entered (errored): " + normalizedRSVPCode);
      reply({ error: { code: 500, message: "The RSVP Code entered is not valid. It must be 6 characters and only contains letters A through F, 0 (zero) through 9 (nine)." }});
    }
<commit_msg>Cleanup check for bad rsvpCode value (empty/null).<commit_after>var pg = require('pg');
var Joi = require('joi');
var DB = require('../db');
var Hapi = require('hapi');
var Util = require('util');
var _ = require('../libs/underscore.1.6.0.min')

var MOCK_DATA = [
  {
    id: 1,

    reservationTitle: "Mr. & Mrs. <NAME>",

    addressLineOne: "12345 Street One",
    addressLineTwo: "Apt 111",
    addressCity: "Some City",
    addressState: "IN",
    addressZipCode: "554433",

    rsvpCode: "d18cb7",
    emailAddress: "<EMAIL>",

    reservationNotes: "",
    dietaryRestrictions: "",
    notesForBrideGroom: "",

    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,

    isInvitedToRehearsalDinner: false,
    isAttendingBigDay: false,
    isAttendingRehearsalDinner: false,

    reservationTitle: "Mr. & Mrs. <NAME>",

    addressLineOne: "1 This Way",
    addressLineTwo: "",
    addressCity: "Firstly",
    addressState: "IN",
    addressZipCode: "11223",

    reservationNotes: "",
    dietaryRestrictions: "",
    notesForBrideGroom: "",

    rsvpCode: "abcde",
    emailAddress: "<EMAIL>",

    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,
    }]
  }
];

var GET_RESERVATION_SQL = "SELECT * FROM reservation r WHERE r.rsvp_code = $1";
var GET_CONTACTS_FOR_RESERVATION_SQL = "SELECT * FROM contact c WHERE c.reservation_id = $1"

var INSERT_NEW_CONTACT_SQL = 'INSERT INTO contact (reservation_id, first_name, middle_name, last_name, '
    + ' is_child, is_attending_big_day, is_attending_rehearsal_dinner )'
    + ' VALUES '
    + ' ($1, $2, $3, $4, $5, $6, $7'
    + ' ) RETURNING id';

var INSERT_NEW_RESERVATION_SQL = 'INSERT INTO reservation (reservation_Title, rsvp_Code_Source, '
    + ' address_Line_One, address_Line_Two, '
    + 'address_City, address_State, address_Zip_Code, rsvp_Code, email_Address, reservation_Notes, '
    + 'dietary_Restrictions, notes_For_Bride_Groom, is_Invited_To_Rehearsal_Dinner, is_Attending_Big_Day, is_Attending_Rehearsal_Dinner '
    + ', create_date, modified_date) '
    + ' VALUES '
    + ' ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, '
    + ' CURRENT_TIMESTAMP, CURRENT_TIMESTAMP'
    + ' ) RETURNING id';

var UPDATE_RESERVATION_SQL = 'UPDATE reservation                        \
                              SET                                       \
                                  address_line_one = $1,                \
                                  address_line_two = $2,                \
                                  address_city = $3,                    \
                                  address_state = $4,                   \
                                  address_zip_code = $5,                \
                                  email_address = $6,                   \
                                  reservation_notes = $7,               \
                                  dietary_restrictions = $8,            \
                                  notes_for_bride_groom = $9,           \
                                  is_attending_big_day = $10,           \
                                  is_attending_rehearsal_dinner = $11   \
                              WHERE id = $12                            \
                            ';

var UPDATE_CONTACT_SQL = 'UPDATE contact                          \
                          SET                                     \
                              first_name = $1,                    \
                              middle_name = $2,                   \
                              last_name = $3,                     \
                              is_attending_big_day = $4,          \
                              is_attending_rehearsal_dinner = $5  \
                          WHERE id = $6                           \
                        ';

var VALID_RSVP_CODE_REGEX_PATTERN = /^([a-fA-F0-9]{6})$/ig;

// the letter O ("Oh") should be a 0
function normalizeRSVPCode(rsvpCode) {
  if (rsvpCode) {
    return rsvpCode.toLowerCase().replace(/o/gi, "0");
  }
  return false;
}

function isValidRSVPCode(rsvpCode) {
  if (rsvpCode) {
    return VALID_RSVP_CODE_REGEX_PATTERN.test(rsvpCode);
  }

  return false;
}

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

    var normalizedRSVPCode = normalizeRSVPCode(request.params.rsvpCode);

    if (!isValidRSVPCode(normalizedRSVPCode)) {
      console.log("Code Entered (errored): " + normalizedRSVPCode);
      reply({ error: { code: 500, message: "The RSVP Code entered is not valid. It must be 6 characters and only contains letters A through F, 0 (zero) through 9 (nine)." }});
    }

{'fraction_non_alphanumeric': 0.10802919708029197, 'fraction_numerical': 0.01843065693430657, 'mean_word_length': 1.6514271891630383, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 38, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5083262', 'n_tokens_mistral': 3639, 'n_tokens_neox': 3419, 'n_words': 683}